*improved the pay invoice wizard: the default value of the amount is now the residual...
[odoo/odoo.git] / addons / account / wizard / wizard_pay_invoice.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 #
6 # $Id$
7 #
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
13 # Service Company
14 #
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28 #
29 ##############################################################################
30
31 import wizard
32 import netsvc
33 import pooler
34 import time
35 from tools.translate import _
36
37 pay_form = '''<?xml version="1.0"?>
38 <form string="Pay invoice">
39     <field name="amount"/>
40     <newline/>
41     <field name="name"/>
42     <field name="date"/>
43     <field name="journal_id"/>
44     <field name="period_id"/>
45 </form>'''
46
47 pay_fields = {
48     'amount': {'string': 'Amount paid', 'type':'float', 'required':True},
49     'name': {'string': 'Entry Name', 'type':'char', 'size': 64, 'required':True},
50     'date': {'string': 'Payment date', 'type':'date', 'required':True, 'default':lambda *args: time.strftime('%Y-%m-%d')},
51     'journal_id': {'string': 'Journal', 'type': 'many2one', 'relation':'account.journal', 'required':True, 'domain':[('type','=','cash')]},
52     'period_id': {'string': 'Period', 'type': 'many2one', 'relation':'account.period', 'required':True},
53 }
54
55 def _pay_and_reconcile(self, cr, uid, data, context):
56     form = data['form']
57     period_id = form.get('period_id', False)
58     journal_id = form.get('journal_id', False)
59     writeoff_account_id = form.get('writeoff_acc_id', False)
60     writeoff_journal_id = form.get('writeoff_journal_id', False)
61     pool = pooler.get_pool(cr.dbname)
62     cur_obj = pool.get('res.currency')
63     amount = form['amount']
64
65     invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
66     journal = pool.get('account.journal').browse(cr, uid, data['form']['journal_id'], context)
67     if journal.currency and invoice.company_id.currency_id.id<>journal.currency.id:
68         ctx = {'date':data['form']['date']}
69         amount = cur_obj.compute(cr, uid, journal.currency.id, invoice.company_id.currency_id.id, amount, context=ctx)
70
71     
72     # Take the choosen date
73     if form.has_key('comment'):
74         context={'date_p':form['date'],'comment':form['comment']}
75     else:
76         context={'date_p':form['date'],'comment':False}
77
78     acc_id = journal.default_credit_account_id and journal.default_credit_account_id.id
79     if not acc_id:
80         raise wizard.except_wizard(_('Error !'), _('Your journal must have a default credit and debit account.'))
81     pool.get('account.invoice').pay_and_reconcile(cr, uid, [data['id']],
82             amount, acc_id, period_id, journal_id, writeoff_account_id,
83             period_id, writeoff_journal_id, context, data['form']['name'])
84     return {}
85
86 def _wo_check(self, cr, uid, data, context):
87     pool = pooler.get_pool(cr.dbname)
88     invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
89     journal = pool.get('account.journal').browse(cr, uid, data['form']['journal_id'], context)
90     if invoice.company_id.currency_id.id <> invoice.currency_id.id:
91         return 'addendum'
92     if journal.currency and (journal.currency.id <> invoice.currency_id.id):
93         return 'addendum'
94     if pool.get('res.currency').is_zero(cr, uid, invoice.currency_id,
95             (data['form']['amount'] - invoice.amount_total)):
96         return 'reconcile'
97     return 'addendum'
98
99 _transaction_add_form = '''<?xml version="1.0"?>
100 <form string="Information addendum">
101     <separator string="Write-Off Move" colspan="4"/>
102     <field name="writeoff_acc_id"/>
103     <field name="writeoff_journal_id"/>
104     <field name="comment"/>
105 </form>'''
106
107 _transaction_add_fields = {
108     'writeoff_acc_id': {'string':'Write-Off account', 'type':'many2one', 'relation':'account.account', 'required':True},
109     'writeoff_journal_id': {'string': 'Write-Off journal', 'type': 'many2one', 'relation':'account.journal', 'required':True},
110     'comment': {'string': 'Entry Name', 'type':'char', 'size': 64, 'required':True},
111 }
112
113 def _get_value_addendum(self, cr, uid, data, context={}):
114     return {}
115
116 def _get_period(self, cr, uid, data, context={}):
117     pool = pooler.get_pool(cr.dbname)
118     ids = pool.get('account.period').find(cr, uid, context=context)
119     period_id = False
120     if len(ids):
121         period_id = ids[0]
122     invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
123     if invoice.state == 'draft':
124         raise wizard.except_wizard(_('Error !'), _('Can not pay draft invoice.'))
125     return {
126         'period_id': period_id,
127         'amount': invoice.residual,
128         'date': time.strftime('%Y-%m-%d')
129     }
130
131 class wizard_pay_invoice(wizard.interface):
132     states = {
133         'init': {
134             'actions': [_get_period],
135             'result': {'type':'form', 'arch':pay_form, 'fields':pay_fields, 'state':[('end','Cancel'),('reconcile','Partial Payment'),('writeoff_check','Full Payment')]}
136         },
137         'writeoff_check': {
138             'actions': [],
139             'result' : {'type': 'choice', 'next_state': _wo_check }
140         },
141         'addendum': {
142             'actions': [_get_value_addendum],
143             'result': {'type': 'form', 'arch':_transaction_add_form, 'fields':_transaction_add_fields, 'state':[('end','Cancel'),('reconcile','Pay and reconcile')]}
144         },
145         'reconcile': {
146             'actions': [_pay_and_reconcile],
147             'result': {'type':'state', 'state':'end'}
148         }
149     }
150 wizard_pay_invoice('account.invoice.pay')
151
152
153 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
154