account, base: fix rounding for reconcile
[odoo/odoo.git] / addons / account / wizard / wizard_pay_invoice.py
1 ##############################################################################
2 #
3 # Copyright (c) 2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 # WARNING: This program as such is intended to be used by professional
6 # programmers who take the whole responsability of assessing all potential
7 # consequences resulting from its eventual inadequacies and bugs
8 # End users who are looking for a ready-to-use solution with commercial
9 # garantees and support are strongly adviced to contract a Free Software
10 # Service Company
11 #
12 # This program is Free Software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 2
15 # of the License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25 #
26 ##############################################################################
27
28 import wizard
29 import netsvc
30 import pooler
31
32 pay_form = '''<?xml version="1.0"?>
33 <form string="Pay invoice">
34         <field name="amount"/>
35         <field name="journal_id"/>
36         <field name="period_id"/>
37 </form>'''
38
39 pay_fields = {
40         'amount': {'string': 'Amount paid', 'type':'float', 'required':True},
41         'journal_id': {'string': 'Journal', 'type': 'many2one', 'relation':'account.journal', 'required':True, 'domain':[('type','=','cash')]},
42         'period_id': {'string': 'Period', 'type': 'many2one', 'relation':'account.period', 'required':True},
43 }
44
45 def _pay_and_reconcile(self, cr, uid, data, context):
46         service = netsvc.LocalService("object_proxy")
47         form = data['form']
48         period_id = form.get('period_id', False)
49         journal_id = form.get('journal_id', False)
50         writeoff_account_id = form.get('writeoff_acc_id', False)
51         writeoff_journal_id = form.get('writeoff_journal_id', False)
52         pool = pooler.get_pool(cr.dbname)
53         acc_id = pool.get('account.journal').browse(cr, uid, journal_id, context).default_credit_account_id.id
54         if not acc_id:
55                 raise wizard.except_wizard('Error !', 'Your journal must have a default credit and debit account.')
56         service.execute(cr.dbname, uid, 'account.invoice', 'pay_and_reconcile', [data['id']], form['amount'], acc_id, period_id, journal_id, writeoff_account_id, period_id, writeoff_journal_id, context)
57         return {}
58
59 def _trans_reconcile(self, cr, uid, data, context):
60         service = netsvc.LocalService("object_proxy")
61         return {}
62
63 def _wo_check(self, cr, uid, data, context):
64         pool = pooler.get_pool(cr.dbname)
65         invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
66         if pool.get('res.currency').is_zero(cr, uid, invoice.currency_id,
67                         (data['form']['amount'] - invoice.amount_total)):
68                 return 'reconcile'
69         return 'addendum'
70
71 _transaction_add_form = '''<?xml version="1.0"?>
72 <form string="Information addendum">
73         <separator string="Write-Off Move" colspan="4"/>
74         <field name="writeoff_acc_id"/>
75         <field name="writeoff_journal_id"/>
76 </form>'''
77
78 _transaction_add_fields = {
79         'writeoff_acc_id': {'string':'Write-Off account', 'type':'many2one', 'relation':'account.account', 'required':True},
80         'writeoff_journal_id': {'string': 'Write-Off journal', 'type': 'many2one', 'relation':'account.journal', 'required':True},
81 }
82
83 def _get_value_addendum(self, cr, uid, data, context={}):
84         return {}
85
86 def _get_period(self, cr, uid, data, context={}):
87         pool = pooler.get_pool(cr.dbname)
88         ids = pool.get('account.period').find(cr, uid, context=context)
89         period_id = False
90         if len(ids):
91                 period_id = ids[0]
92         invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
93         if invoice.state == 'draft':
94                 raise wizard.except_wizard('Error !', 'Can not pay draft invoice.')
95         return {'period_id': period_id, 'amount': invoice.amount_total}
96
97 class wizard_pay_invoice(wizard.interface):
98         states = {
99                 'init': {
100                         'actions': [_get_period],
101                         'result': {'type':'form', 'arch':pay_form, 'fields':pay_fields, 'state':[('end','Cancel'),('writeoff_check','Pay Invoice')]}
102                 },
103                 'writeoff_check': {
104                         'actions': [],
105                         'result' : {'type': 'choice', 'next_state': _wo_check }
106                 },
107                 'addendum': {
108                         'actions': [_get_value_addendum],
109                         'result': {'type': 'form', 'arch':_transaction_add_form, 'fields':_transaction_add_fields, 'state':[('end','Cancel'),('reconcile','Pay and reconcile')]}
110                 },
111                 'reconcile': {
112                         'actions': [_pay_and_reconcile],
113                         'result': {'type':'state', 'state':'end'}
114                 }
115         }
116 wizard_pay_invoice('account.invoice.pay')
117