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