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