Merge branch 'master' of https://github.com/odoo/odoo
[odoo/odoo.git] / addons / account / wizard / pos_box.py
1 from openerp.osv import fields, osv
2 import openerp.addons.decimal_precision as dp
3 from openerp.tools.translate import _
4
5 class CashBox(osv.osv_memory):
6     _register = False
7     _columns = {
8         'name' : fields.char('Reason', required=True),
9         # Attention, we don't set a domain, because there is a journal_type key 
10         # in the context of the action
11         'amount' : fields.float('Amount',
12                                 digits_compute = dp.get_precision('Account'),
13                                 required=True),
14     }
15
16     def run(self, cr, uid, ids, context=None):
17         if not context:
18             context = dict()
19
20         active_model = context.get('active_model', False) or False
21         active_ids = context.get('active_ids', []) or []
22
23         records = self.pool[active_model].browse(cr, uid, active_ids, context=context)
24
25         return self._run(cr, uid, ids, records, context=context)
26
27     def _run(self, cr, uid, ids, records, context=None):
28         for box in self.browse(cr, uid, ids, context=context):
29             for record in records:
30                 if not record.journal_id:
31                     raise osv.except_osv(_('Error!'),
32                                          _("Please check that the field 'Journal' is set on the Bank Statement"))
33                     
34                 if not record.journal_id.internal_account_id:
35                     raise osv.except_osv(_('Error!'),
36                                          _("Please check that the field 'Internal Transfers Account' is set on the payment method '%s'.") % (record.journal_id.name,))
37
38                 self._create_bank_statement_line(cr, uid, box, record, context=context)
39
40         return {}
41
42     def _create_bank_statement_line(self, cr, uid, box, record, context=None):
43         values = self._compute_values_for_statement_line(cr, uid, box, record, context=context)
44         return self.pool.get('account.bank.statement.line').create(cr, uid, values, context=context)
45
46
47 class CashBoxIn(CashBox):
48     _name = 'cash.box.in'
49
50     _columns = CashBox._columns.copy()
51     _columns.update({
52         'ref': fields.char('Reference'),
53     })
54
55     def _compute_values_for_statement_line(self, cr, uid, box, record, context=None):
56         if not record.journal_id.internal_account_id.id:
57             raise osv.except_osv(_('Configuration Error'), _("You should have defined an 'Internal Transfer Account' in your cash register's journal!"))
58         return {
59             'statement_id': record.id,
60             'journal_id': record.journal_id.id,
61             'amount': box.amount or 0.0,
62             'account_id': record.journal_id.internal_account_id.id,
63             'ref': '%s' % (box.ref or ''),
64             'name': box.name,
65         }
66
67
68 class CashBoxOut(CashBox):
69     _name = 'cash.box.out'
70
71     _columns = CashBox._columns.copy()
72
73     def _compute_values_for_statement_line(self, cr, uid, box, record, context=None):
74         if not record.journal_id.internal_account_id.id:
75             raise osv.except_osv(_('Configuration Error'), _("You should have defined an 'Internal Transfer Account' in your cash register's journal!"))
76         amount = box.amount or 0.0
77         return {
78             'statement_id': record.id,
79             'journal_id': record.journal_id.id,
80             'amount': -amount if amount > 0.0 else amount,
81             'account_id': record.journal_id.internal_account_id.id,
82             'name': box.name,
83         }