Launchpad automatic translations update.
[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', size=64, 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.get(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', size=32),
53     })
54
55     def _compute_values_for_statement_line(self, cr, uid, box, record, context=None):
56         return {
57             'statement_id' : record.id,
58             'journal_id' : record.journal_id.id,
59             'account_id' : record.journal_id.internal_account_id.id,
60             'amount' : box.amount or 0.0,
61             'ref' : '%s' % (box.ref or ''),
62             'name' : box.name,
63         }
64
65 CashBoxIn()
66
67 class CashBoxOut(CashBox):
68     _name = 'cash.box.out'
69
70     def _compute_values_for_statement_line(self, cr, uid, box, record, context=None):
71         amount = box.amount or 0.0
72         return {
73             'statement_id' : record.id,
74             'journal_id' : record.journal_id.id,
75             'account_id' : record.journal_id.internal_account_id.id,
76             'amount' : -amount if amount > 0.0 else amount,
77             'name' : box.name,
78         }
79
80 CashBoxOut()