New trunk
[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 osv
31 import time
32 import pooler
33
34 pay_form = '''<?xml version="1.0"?>
35 <form string="Pay invoice">
36         <field name="amount"/>
37         <field name="dest_account_id"/>
38         <field name="journal_id"/>
39         <field name="period_id"/>
40 </form>'''
41
42 pay_fields = {
43         'amount': {'string': 'Amount paid', 'type':'float', 'required':True},
44         'dest_account_id': {'string':'Payment to Account', 'type':'many2one', 'required':True, 'relation':'account.account', 'domain':[('type','=','cash')]},
45         'journal_id': {'string': 'Journal', 'type': 'many2one', 'relation':'account.journal', 'required':True},
46         'period_id': {'string': 'Period', 'type': 'many2one', 'relation':'account.period', 'required':True},
47 }
48
49 def _pay_and_reconcile(self, cr, uid, data, context):
50         service = netsvc.LocalService("object_proxy")
51         form = data['form']
52         account_id = form.get('writeoff_acc_id', False)
53         period_id = form.get('period_id', False)
54         journal_id = form.get('journal_id', False)
55         service.execute(cr.dbname, uid, 'account.invoice', 'pay_and_reconcile', [data['id']], form['amount'], form['dest_account_id'], journal_id, account_id, period_id, journal_id, context)
56         return {}
57
58 def _trans_reconcile(self, cr, uid, data, context):
59         service = netsvc.LocalService("object_proxy")
60         return {}
61
62 def _wo_check(self, cr, uid, data, context):
63         pool = pooler.get_pool(cr.dbname)
64         invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
65         if data['form']['amount'] == invoice.amount_total:
66                 return 'reconcile'
67         return 'addendum'
68
69 _transaction_add_form = '''<?xml version="1.0"?>
70 <form string="Information addendum">
71         <separator string="Write-Off Move" colspan="4"/>
72         <field name="writeoff_acc_id"/>
73 </form>'''
74
75 _transaction_add_fields = {
76         'writeoff_acc_id': {'string':'Write-Off account', 'type':'many2one', 'relation':'account.account', 'required':True},
77 }
78
79 def _get_value_addendum(self, cr, uid, data, context={}):
80         return {}
81
82 def _get_period(self, cr, uid, data, context={}):
83         pool = pooler.get_pool(cr.dbname)
84         ids = pool.get('account.period').find(cr, uid, context=context)
85         period_id = False
86         if len(ids):
87                 period_id = ids[0]
88         invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
89         return {'period_id': period_id, 'amount': invoice.amount_total}
90
91 class wizard_pay_invoice(wizard.interface):
92         states = {
93                 'init': {
94                         'actions': [_get_period],
95                         'result': {'type':'form', 'arch':pay_form, 'fields':pay_fields, 'state':[('end','Cancel'),('writeoff_check','Pay Invoice')]}
96                 },
97                 'writeoff_check': {
98                         'actions': [],
99                         'result' : {'type': 'choice', 'next_state': _wo_check }
100                 },
101                 'addendum': {
102                         'actions': [_get_value_addendum],
103                         'result': {'type': 'form', 'arch':_transaction_add_form, 'fields':_transaction_add_fields, 'state':[('end','Cancel'),('reconcile','Pay and reconcile')]}
104                 },
105                 'reconcile': {
106                         'actions': [_pay_and_reconcile],
107                         'result': {'type':'state', 'state':'end'}
108                 }
109         }
110 wizard_pay_invoice('account.invoice.pay')
111