[FIX] webclient returns to database manager after 1st database creation
[odoo/odoo.git] / addons / point_of_sale / wizard / pos_box_entries.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import time
23
24 from openerp.osv import osv, fields
25 from openerp.tools.translate import _
26
27
28 def get_journal(self, cr, uid, context=None):
29     """
30          Make the selection list of Cash Journal  .
31          @param self: The object pointer.
32          @param cr: A database cursor
33          @param uid: ID of the user currently logged in
34          @param context: A standard dictionary
35          @return :Return the list of journal
36     """
37     
38     journal_obj = self.pool.get('account.journal')
39     statement_obj = self.pool.get('account.bank.statement')
40
41     j_ids = journal_obj.search(cr, uid, [('journal_user','=',1)], context=context)
42     obj_ids = statement_obj.search(cr, uid, [('state', '=', 'open'), ('user_id', '=', uid), ('journal_id', 'in', j_ids)], context=context)
43     res = statement_obj.read(cr, uid, obj_ids, ['journal_id'], context=context)
44     res = [(r['journal_id']) for r in res]
45     if not len(res) and context:
46         raise osv.except_osv(_('Error!'), _('You do not have any open cash register. You must create a payment method or open a cash register.'))
47     return res
48
49 class pos_box_entries(osv.osv_memory):
50     _name = 'pos.box.entries'
51     _description = 'Pos Box Entries'
52
53     def _get_income_product(self, cr, uid, context=None):
54         """
55              Make the selection list of purchasing  products.
56              @param self: The object pointer.
57              @param cr: A database cursor
58              @param uid: ID of the user currently logged in
59              @param context: A standard dictionary
60              @return :Return of operation of product
61         """
62         product_obj = self.pool.get('product.product')
63         ids = product_obj.search(cr, uid, [('income_pdt', '=', True)], context=context)
64         res = product_obj.read(cr, uid, ids, ['id', 'name'], context=context)
65         res = [(r['id'], r['name']) for r in res]
66         res.insert(0, ('', ''))
67
68         return res
69
70     _columns = {
71         'name': fields.char('Reason', size=32, required=True),
72         'journal_id': fields.many2one('account.journal', 'Cash Register', required=True, domain="[('journal_id.type', '=', 'cash')]"),
73         'product_id': fields.selection(_get_income_product, "Operation", required=True, size=-1),
74         'amount': fields.float('Amount', digits=(16, 2), required=True),
75         'ref': fields.char('Ref', size=32),
76         'session_id' : fields.many2one('pos.session', 'Session'),
77         'user_id' : fields.many2one('res.users', 'User'),
78     }
79
80     def _default_session_id(self, cr, uid, context=None):
81         return context and context.get('active_id', False) or False
82
83     def _default_cash_register(self, cr, uid, context=None):
84
85         #import pdb
86         #pdb.set_trace()
87         if not context:
88             context = {}
89         result = context.get('active_id', False) or False
90
91         return result
92
93     _defaults = {
94         #'session_id' : _default_session_id,
95         #'journal_id': _default_cash_register,
96         #'product_id': 1,
97         'user_id' : lambda obj, cr, uid, context: uid,
98     }
99
100     def get_in(self, cr, uid, ids, context=None):
101         """
102              Create the entry of statement in journal.
103              @param self: The object pointer.
104              @param cr: A database cursor
105              @param uid: ID of the user currently logged in
106              @param context: A standard dictionary
107              @return :Return of operation of product
108         """
109         statement_obj = self.pool.get('account.bank.statement')
110         res_obj = self.pool.get('res.users')
111         product_obj = self.pool.get('product.product')
112         bank_statement = self.pool.get('account.bank.statement.line')
113         for data in  self.read(cr, uid, ids, context=context):
114             vals = {}
115             curr_company = res_obj.browse(cr, uid, uid, context=context).company_id.id
116             statement_id = statement_obj.search(cr, uid, [('journal_id', '=', int(data['journal_id'])), ('company_id', '=', curr_company), ('user_id', '=', uid), ('state', '=', 'open')], context=context)
117             if not statement_id:
118                 raise osv.except_osv(_('Error!'), _('You have to open at least one cashbox.'))
119
120             product = product_obj.browse(cr, uid, int(data['product_id']))
121             acc_id = product.property_account_income or product.categ_id.property_account_income_categ
122             if not acc_id:
123                 raise osv.except_osv(_('Error!'), _('Please check that income account is set to %s.')%(product_obj.browse(cr, uid, data['product_id']).name))
124             if statement_id:
125                 statement_id = statement_id[0]
126             if not statement_id:
127                 statement_id = statement_obj.create(cr, uid, {
128                                     'date': time.strftime('%Y-%m-%d 00:00:00'),
129                                     'journal_id': data['journal_id'],
130                                     'company_id': curr_company,
131                                     'user_id': uid,
132                                 }, context=context)
133
134             vals['statement_id'] = statement_id
135             vals['journal_id'] = data['journal_id']
136             if acc_id:
137                 vals['account_id'] = acc_id.id
138             vals['amount'] = data['amount'] or 0.0
139             vals['ref'] = "%s" % (data['ref'] or '')
140             vals['name'] = "%s: %s " % (product_obj.browse(cr, uid, data['product_id'], context=context).name, data['name'].decode('utf8'))
141             bank_statement.create(cr, uid, vals, context=context)
142         return {}
143
144 pos_box_entries()
145
146
147 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: