Merge lp:~openbig/bigconsulting/changes_account_invoice_cash_discount into lp:bigconsulting

Proposed by gpa(OpenERP)
Status: Merged
Merged at revision: 9
Proposed branch: lp:~openbig/bigconsulting/changes_account_invoice_cash_discount
Merge into: lp:bigconsulting
Diff against target: 619 lines (+327/-213)
8 files modified
account_invoice_cash_discount/__terp__.py (+1/-1)
account_invoice_cash_discount/account_invoice_cash_discount.py (+36/-2)
account_invoice_cash_discount/account_invoice_cash_discount_view.xml (+2/-0)
account_invoice_cash_discount/account_wizard.xml (+0/-6)
account_invoice_cash_discount/wizard/__init__.py (+1/-1)
account_invoice_cash_discount/wizard/account_pay_invoice.py (+224/-0)
account_invoice_cash_discount/wizard/account_pay_invoice_view.xml (+63/-0)
account_invoice_cash_discount/wizard/wizard_pay_invoice.py (+0/-203)
To merge this branch: bzr merge lp:~openbig/bigconsulting/changes_account_invoice_cash_discount
Reviewer Review Type Date Requested Status
openbig Pending
Review via email: mp+26461@code.launchpad.net

Description of the change

convert wizard pay invoice to osv memroy wizard make onchange on Amount Paid field

To post a comment you must log in.
10. By gpa(OpenERP)

changes on account_invoice_cash_discount

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'account_invoice_cash_discount/__terp__.py'
--- account_invoice_cash_discount/__terp__.py 2010-05-28 10:52:25 +0000
+++ account_invoice_cash_discount/__terp__.py 2010-06-01 08:53:22 +0000
@@ -35,7 +35,7 @@
35 ],35 ],
36 "update_xml" : [36 "update_xml" : [
37 "account_invoice_cash_discount_view.xml",37 "account_invoice_cash_discount_view.xml",
38 "account_wizard.xml"38 'wizard/account_pay_invoice_view.xml',
39 ],39 ],
40 "active": False,40 "active": False,
41 "installable": True,41 "installable": True,
4242
=== modified file 'account_invoice_cash_discount/account_invoice_cash_discount.py'
--- account_invoice_cash_discount/account_invoice_cash_discount.py 2010-05-28 10:52:25 +0000
+++ account_invoice_cash_discount/account_invoice_cash_discount.py 2010-06-01 08:53:22 +0000
@@ -49,8 +49,10 @@
49 _inherit = "account.invoice"49 _inherit = "account.invoice"
50 50
51 def _get_payment(self, cr, uid, ids, resudial_amonut, payment_term, context=None):51 def _get_payment(self, cr, uid, ids, resudial_amonut, payment_term, context=None):
5252 """
53 #This function used to count the discount payment term53 This function return the Discount according to the payment term cash discount term
54 """
55
54 if context is None:56 if context is None:
55 context = {}57 context = {}
56 58
@@ -88,6 +90,38 @@
88 ted +=rr90 ted +=rr
89 discunt = res + ted91 discunt = res + ted
90 return discunt92 return discunt
93
94 def _get_account(self, cr, uid, ids, resudial_amonut, payment_term, context=None):
95 """
96 This function return the Account according to the payment term cash discount term
97 """
98 if context is None:
99 context = {}
100
101 tax_obj = self.pool.get('account.tax')
102 invoice = self.browse(cr, uid, ids[0], context=context)
103
104 if invoice.date_due:
105 date1 = invoice.date_due
106 else:
107 date1 = time.strftime('%Y-%m-%d')
108
109 if 'date_p' in context and context['date_p']:
110 date2 = context['date_p']
111 else:
112 date2 = time.strftime('%Y-%m-%d')
113
114 from_dt = time.mktime(time.strptime(date1,'%Y-%m-%d'))
115 to_dt = time.mktime(time.strptime(date2,'%Y-%m-%d'))
116 diff_day = (from_dt-to_dt)/(3600*24)
117 if payment_term:
118 pt = self.pool.get('account.payment.term').browse(cr, uid, payment_term, context=context)
119 if pt.cash_discount_ids:
120 res = 0.0
121 for d in pt.cash_discount_ids:
122 if diff_day == d.delay or diff_day < d.delay:
123 account_id = d.discount_account_id.id
124 return account_id
91 125
92 def pay_and_reconcile(self, cr, uid, ids, pay_amount, pay_account_id, period_id, pay_journal_id, writeoff_acc_id, writeoff_period_id, writeoff_journal_id, context=None, name=''):126 def pay_and_reconcile(self, cr, uid, ids, pay_amount, pay_account_id, period_id, pay_journal_id, writeoff_acc_id, writeoff_period_id, writeoff_journal_id, context=None, name=''):
93 127
94128
=== modified file 'account_invoice_cash_discount/account_invoice_cash_discount_view.xml'
--- account_invoice_cash_discount/account_invoice_cash_discount_view.xml 2010-05-26 13:32:15 +0000
+++ account_invoice_cash_discount/account_invoice_cash_discount_view.xml 2010-06-01 08:53:22 +0000
@@ -14,6 +14,7 @@
14 <field name="name" select="1"/>14 <field name="name" select="1"/>
15 <field name="delay" select="1"/>15 <field name="delay" select="1"/>
16 <field name="discount" select="1"/>16 <field name="discount" select="1"/>
17 <field name="discount_account_id" />
17 </form>18 </form>
18 </field>19 </field>
19 </record>20 </record>
@@ -27,6 +28,7 @@
27 <field name="name" select="1"/>28 <field name="name" select="1"/>
28 <field name="delay" select="1"/>29 <field name="delay" select="1"/>
29 <field name="discount" select="1"/>30 <field name="discount" select="1"/>
31 <field name="discount_account_id" />
30 </tree>32 </tree>
31 </field>33 </field>
32 </record>34 </record>
3335
=== removed file 'account_invoice_cash_discount/account_wizard.xml'
--- account_invoice_cash_discount/account_wizard.xml 2010-05-26 13:32:15 +0000
+++ account_invoice_cash_discount/account_wizard.xml 1970-01-01 00:00:00 +0000
@@ -1,6 +0,0 @@
1<?xml version="1.0" encoding="utf-8"?>
2<openerp>
3 <data>
4 <wizard id="wizard_invoice_pay" model="account.invoice" name="account.invoice.pay1" string="Pay invoice" groups="base.group_user"/>
5 </data>
6</openerp>
70
=== modified file 'account_invoice_cash_discount/wizard/__init__.py'
--- account_invoice_cash_discount/wizard/__init__.py 2010-05-26 13:32:15 +0000
+++ account_invoice_cash_discount/wizard/__init__.py 2010-06-01 08:53:22 +0000
@@ -20,7 +20,7 @@
20#20#
21##############################################################################21##############################################################################
2222
23import wizard_pay_invoice23import account_pay_invoice
2424
25# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:25# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
2626
2727
=== added file 'account_invoice_cash_discount/wizard/account_pay_invoice.py'
--- account_invoice_cash_discount/wizard/account_pay_invoice.py 1970-01-01 00:00:00 +0000
+++ account_invoice_cash_discount/wizard/account_pay_invoice.py 2010-06-01 08:53:22 +0000
@@ -0,0 +1,224 @@
1# -*- coding: utf-8 -*-
2##############################################################################
3#
4# OpenERP, Open Source Management Solution
5# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU Affero General Public License as
9# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Affero General Public License for more details.
16#
17# You should have received a copy of the GNU Affero General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20##############################################################################
21import time
22
23from osv import fields, osv
24from tools.translate import _
25#import decimal_precision as dp
26
27class account_invoice_pay_writeoff(osv.osv_memory):
28 """
29 Opens the write off amount pay form.
30 """
31 _name = "account.invoice.pay.writeoff"
32 _description = "Pay Invoice "
33 _columns = {
34 'writeoff_acc_id': fields.many2one('account.account', 'Write-Off account', required=True),
35 'writeoff_journal_id': fields.many2one('account.journal', 'Write-Off journal', required=True),
36 'comment': fields.char('Comment', size=64, required=True),
37 'analytic_id': fields.many2one('account.analytic.account','Analytic Account'),
38 }
39 _defaults = {
40 'comment': 'Write-Off',
41 }
42
43 def pay_and_reconcile_writeoff(self, cr, uid, ids, context=None):
44 data = self.read(cr, uid, ids,context=context)[0]
45 context.update({'write_off':data})
46 self.pool.get('account.invoice.pay').pay_and_reconcile(cr, uid, ids, context=context)
47 return {}
48
49account_invoice_pay_writeoff()
50
51class account_invoice_pay(osv.osv_memory):
52 """
53 Generate pay invoice wizard, user can make partial or full payment for invoice.
54 """
55 _name = "account.invoice.pay"
56 _description = "Pay Invoice "
57 _columns = {
58 'amount': fields.float('Amount paid', required=True),
59 'name': fields.char('Entry Name', size=64, required=True),
60 'date': fields.date('Date payment', required=True),
61 'journal_id': fields.many2one('account.journal', 'Journal/Payment Mode', required=True),
62 'period_id': fields.many2one('account.period', 'Period', required=True),
63 'cash_amount':fields.float('Cash Discount Amount', required=True),
64 'account_id': fields.many2one('account.account', 'Account For Discount', required=True),
65 }
66
67 def view_init(self, cr, uid, ids, context=None):
68 invoice = self.pool.get('account.invoice').browse(cr, uid, context['active_id'], context=context)
69 if invoice.state in ['draft', 'proforma2', 'cancel']:
70 raise osv.except_osv(_('Error !'), _('Can not pay draft/proforma/cancel invoice.'))
71 pass
72
73 def _get_period(self, cr, uid, context=None):
74 ids = self.pool.get('account.period').find(cr, uid, context=context)
75 period_id = False
76 if len(ids):
77 period_id = ids[0]
78 return period_id
79
80 def _get_amount(self, cr, uid, context=None):
81 return self.pool.get('account.invoice').browse(cr, uid, context['id'], context=context).residual
82
83 def on_change_ammount(self, cr, uid, ids, amount, context=None):
84 """
85 Function return the Discount according to the Amount paid and Payment Term Cash Discount
86 """
87 res = {}
88 obj_inv = self.pool.get('account.invoice')
89 invoice = obj_inv.browse(cr, uid, context['id'], context=context)
90 discount = obj_inv._get_payment(cr, uid, [context['id']] , amount, invoice.payment_term.id, context=context)
91 return {'value' : {'cash_amount':discount}}
92
93 def _get_discount(self, cr, uid, context=None):
94 obj_inv = self.pool.get('account.invoice')
95 invoice = obj_inv.browse(cr, uid, context['id'], context=context)
96 discount = obj_inv._get_payment(cr, uid, [context['id']] , invoice.residual, invoice.payment_term.id, context=context)
97 return discount
98
99 def _get_account(self, cr, uid, context=None):
100 """
101 This function return the Account according to the payment term cash term and map the account with fiscal position
102 """
103 obj_inv = self.pool.get('account.invoice')
104 invoice = obj_inv.browse(cr, uid, context['id'], context=context)
105 account = obj_inv._get_account(cr, uid, [context['id']] , invoice.residual, invoice.payment_term.id, context=context)
106 fpos = invoice.fiscal_position or False
107 account = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, account)
108 return account
109
110 _defaults = {
111 'date': lambda *a: time.strftime('%Y-%m-%d'),
112 'period_id': _get_period,
113 'amount': _get_amount,
114 'cash_amount':_get_discount,
115 'account_id':_get_account
116 }
117
118 def wo_check(self, cr, uid, ids, context=None):
119 cur_obj = self.pool.get('res.currency')
120 mod_obj = self.pool.get('ir.model.data')
121 if context is None:
122 context = {}
123 data = self.read(cr, uid, ids,context=context)[0]
124 invoice = self.pool.get('account.invoice').browse(cr, uid, context['id'], context)
125 journal = self.pool.get('account.journal').browse(cr, uid, data['journal_id'], context)
126
127 # Here we need that:
128 # The invoice total amount in company's currency <> paid amount in company currency
129 # (according to the correct day rate, invoicing rate and payment rate are may be different)
130 # => Ask to a write-off of the difference. This could happen even if both amount are equal,
131 # because if the currency rate
132 # Get the amount in company currency for the invoice (according to move lines)
133 inv_amount_company_currency = 0
134 for aml in invoice.move_id.line_id:
135 if aml.account_id.id == invoice.account_id.id or aml.account_id.type in ('receivable', 'payable'):
136 inv_amount_company_currency += aml.debit
137 inv_amount_company_currency -= aml.credit
138 inv_amount_company_currency = abs(inv_amount_company_currency)
139
140 # Get the current amount paid in company currency
141 if journal.currency and invoice.company_id.currency_id.id<>journal.currency.id:
142 ctx = {'date':data['date']}
143 amount_paid = cur_obj.compute(cr, uid, journal.currency.id, invoice.company_id.currency_id.id, data['amount'], round=True, context=ctx)
144 else:
145 amount_paid = data['amount']
146 # Get the old payment if there are some
147 if invoice.payment_ids:
148 debit=credit=0.0
149 for payment in invoice.payment_ids:
150 debit+=payment.debit
151 credit+=payment.credit
152 amount_paid+=abs(debit-credit)
153
154 # Test if there is a difference according to currency rouding setting
155 if self.pool.get('res.currency').is_zero(cr, uid, invoice.company_id.currency_id,
156 (amount_paid - inv_amount_company_currency)):
157 return self.pay_and_reconcile(cr, uid, ids, context=context)
158 else:
159 model_data_ids = mod_obj.search(cr, uid,[('model','=','ir.ui.view'),('name','=','view_account_invoice_pay_writeoff')], context=context)
160 resource_id = mod_obj.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
161 return {
162 'name': _('Information addendum'),
163 'context': context,
164 'view_type': 'form',
165 'view_mode': 'form',
166 'res_model': 'account.invoice.pay.writeoff',
167 'views': [(resource_id,'form')],
168 'type': 'ir.actions.act_window',
169 'target': 'new',
170 }
171
172 def pay_and_reconcile(self, cr, uid, ids, context=None):
173 cur_obj = self.pool.get('res.currency')
174 if context is None:
175 context = {}
176 data = self.read(cr, uid, ids,context=context)[0]
177 writeoff_account_id = False
178 writeoff_journal_id = False
179 comment = False
180
181 if 'write_off' in context and context['write_off'] :
182 writeoff_account_id = context['write_off']['writeoff_acc_id']
183 writeoff_journal_id = context['write_off']['writeoff_journal_id']
184 comment = context['write_off']['comment']
185
186 amount = data['amount']
187
188 invoice = self.pool.get('account.invoice').browse(cr, uid, context['id'], context=context)
189 journal = self.pool.get('account.journal').browse(cr, uid, data['journal_id'], context=context)
190 # Compute the amount in company's currency, with the journal currency (which is equal to payment currency)
191 # when it is needed : If payment currency (according to selected journal.currency) is <> from company currency
192 if journal.currency and invoice.company_id.currency_id.id<>journal.currency.id:
193 ctx = {'date':data['date']}
194 amount = cur_obj.compute(cr, uid, journal.currency.id, invoice.company_id.currency_id.id, amount, context=ctx)
195 currency_id = journal.currency.id
196 # Put the paid amount in currency, and the currency, in the context if currency is different from company's currency
197 context.update({'amount_currency':data['amount'],'currency_id':currency_id})
198
199 if invoice.company_id.currency_id.id<>invoice.currency_id.id:
200 ctx = {'date':data['date']}
201 amount = cur_obj.compute(cr, uid, invoice.currency_id.id, invoice.company_id.currency_id.id, amount, context=ctx)
202 currency_id = invoice.currency_id.id
203 # Put the paid amount in currency, and the currency, in the context if currency is different from company's currency
204 context.update({'amount_currency':data['amount'],'currency_id':currency_id})
205
206 # Take the choosen date
207 if comment:
208 context.update({'date_p':data['date'],'comment':comment})
209 else:
210 context.update({'date_p':data['date'],'comment':False})
211
212 context.update({'account_id':data['account_id'],'cash_amount':data['cash_amount'],'amount_currency':data['amount']})
213
214 acc_id = journal.default_credit_account_id and journal.default_credit_account_id.id
215 if not acc_id:
216 raise osv.except_osv(_('Error !'), _('Your journal must have a default credit and debit account.'))
217 self.pool.get('account.invoice').pay_and_reconcile(cr, uid, [context['id']],
218 amount, acc_id, data['period_id'], data['journal_id'], writeoff_account_id,
219 data['period_id'], writeoff_journal_id, context, data['name'])
220 return {}
221
222account_invoice_pay()
223
224# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
0\ No newline at end of file225\ No newline at end of file
1226
=== added file 'account_invoice_cash_discount/wizard/account_pay_invoice_view.xml'
--- account_invoice_cash_discount/wizard/account_pay_invoice_view.xml 1970-01-01 00:00:00 +0000
+++ account_invoice_cash_discount/wizard/account_pay_invoice_view.xml 2010-06-01 08:53:22 +0000
@@ -0,0 +1,63 @@
1<?xml version="1.0" encoding="utf-8"?>
2<openerp>
3 <data>
4 <record id="view_account_invoice_pay" model="ir.ui.view">
5 <field name="name">account.invoice.pay.form</field>
6 <field name="model">account.invoice.pay</field>
7 <field name="type">form</field>
8 <field name="arch" type="xml">
9 <form string="Pay invoice">
10 <group colspan="4" >
11 <field name="amount" on_change="on_change_ammount(amount,context)"/>
12 <newline/>
13 <field name="name"/>
14 <field name="date"/>
15 <field name="journal_id"/>
16 <field name="period_id"/>
17 <field name="cash_amount"/>
18 <field name="account_id"/>
19 </group>
20 <group colspan="4" col="6">
21 <label string ="" colspan="2"/>
22 <button icon="gtk-cancel" special="cancel" string="Cancel"/>
23 <button icon="gtk-execute" string="Partial Payment" name="pay_and_reconcile" type="object"/>
24 <button icon="gtk-execute" string="Full Payment" name="wo_check" type="object"/>
25 </group>
26 </form>
27 </field>
28 </record>
29
30 <act_window name="Pay Invoice"
31 res_model="account.invoice.pay"
32 src_model="account.invoice"
33 view_mode="form"
34 target="new"
35 context="{'id': active_id}"
36 id="action_view_account_invoice_pay"/>
37
38 <record id="view_account_invoice_pay_writeoff" model="ir.ui.view">
39 <field name="name">account.invoice.pay.writeoff.form</field>
40 <field name="model">account.invoice.pay.writeoff</field>
41 <field name="type">form</field>
42 <field name="arch" type="xml">
43 <form string="Information addendum">
44 <group colspan="4" >
45 <separator string="Write-Off Move" colspan="4"/>
46 <field name="writeoff_journal_id"/>
47 <field name="writeoff_acc_id" domain="[('type','&lt;&gt;','view'),('type','&lt;&gt;','consolidation')]"/>
48 <field name="comment"/>
49 <separator string="Analytic" colspan="4"/>
50 <field name="analytic_id"/>
51 </group>
52 <group colspan="4" col="6">
53 <label string ="" colspan="2"/>
54 <button icon="gtk-cancel" special="cancel" string="Cancel"/>
55 <button icon="gtk-execute" string="Pay and reconcile" name="pay_and_reconcile_writeoff" type="object"/>
56 </group>
57 </form>
58 </field>
59 </record>
60
61
62 </data>
63</openerp>
0\ No newline at end of file64\ No newline at end of file
165
=== removed file 'account_invoice_cash_discount/wizard/wizard_pay_invoice.py'
--- account_invoice_cash_discount/wizard/wizard_pay_invoice.py 2010-05-27 07:24:45 +0000
+++ account_invoice_cash_discount/wizard/wizard_pay_invoice.py 1970-01-01 00:00:00 +0000
@@ -1,203 +0,0 @@
1# -*- encoding: utf-8 -*-
2##############################################################################
3#
4# OpenERP, Open Source Management Solution
5# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6# $Id$
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20#
21##############################################################################
22
23import wizard
24import pooler
25import time
26from tools.translate import _
27import tools
28
29pay_form = '''<?xml version="1.0"?>
30<form string="Pay invoice">
31 <field name="amount"/>
32 <newline/>
33 <field name="name"/>
34 <field name="date"/>
35 <field name="journal_id"/>
36 <field name="period_id"/>
37 <field name="cash_amount"/>
38 <field name="account_id"/>
39</form>'''
40
41pay_fields = {
42 'amount': {'string': 'Amount paid', 'type':'float', 'required':True, 'digits': (16,int(tools.config['price_accuracy']))},
43 'name': {'string': 'Entry Name', 'type':'char', 'size': 64, 'required':True},
44 'cash_amount': {'string': 'Cash Discount Amount', 'type':'float', 'required':True, 'digits': (16,int(tools.config['price_accuracy']))},
45 'account_id': {'string': 'Account For Discount', 'type': 'many2one', 'relation':'account.account', 'required':True},
46 'date': {'string': 'Payment date', 'type':'date', 'required':True, 'default':lambda *args: time.strftime('%Y-%m-%d')},
47 'journal_id': {'string': 'Journal/Payment Mode', 'type': 'many2one', 'relation':'account.journal', 'required':True, 'domain':[('type','=','cash')]},
48 'period_id': {'string': 'Period', 'type': 'many2one', 'relation':'account.period', 'required':True},
49}
50
51def _pay_and_reconcile(self, cr, uid, data, context):
52 form = data['form']
53 period_id = form.get('period_id', False)
54 journal_id = form.get('journal_id', False)
55 writeoff_account_id = form.get('writeoff_acc_id', False)
56 writeoff_journal_id = form.get('writeoff_journal_id', False)
57 pool = pooler.get_pool(cr.dbname)
58 cur_obj = pool.get('res.currency')
59 amount = form['amount']
60 context['analytic_id'] = form.get('analytic_id', False)
61
62 invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
63 journal = pool.get('account.journal').browse(cr, uid, data['form']['journal_id'], context)
64 # Compute the amount in company's currency, with the journal currency (which is equal to payment currency)
65 # when it is needed : If payment currency (according to selected journal.currency) is <> from company currency
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 currency_id = journal.currency.id
70 # Put the paid amount in currency, and the currency, in the context if currency is different from company's currency
71 context.update({'amount_currency':form['amount'],'currency_id':currency_id})
72
73 if invoice.company_id.currency_id.id<>invoice.currency_id.id:
74 ctx = {'date':data['form']['date']}
75 amount = cur_obj.compute(cr, uid, invoice.currency_id.id, invoice.company_id.currency_id.id, amount, context=ctx)
76 currency_id = invoice.currency_id.id
77 # Put the paid amount in currency, and the currency, in the context if currency is different from company's currency
78 context.update({'amount_currency':form['amount'],'currency_id':currency_id})
79
80 # Take the choosen date
81 if form.has_key('comment'):
82 context.update({'date_p':form['date'],'comment':form['comment']})
83 else:
84 context.update({'date_p':form['date'],'comment':False})
85
86 context.update({'account_id':form['account_id'],'cash_amount':form['cash_amount'],'amount_currency':form['amount']})
87
88 acc_id = journal.default_credit_account_id and journal.default_credit_account_id.id
89 if not acc_id:
90 raise wizard.except_wizard(_('Error !'), _('Your journal must have a default credit and debit account.'))
91 pool.get('account.invoice').pay_and_reconcile(cr, uid, [data['id']],
92 amount, acc_id, period_id, journal_id, writeoff_account_id,
93 period_id, writeoff_journal_id, context, data['form']['name'])
94 return {}
95
96def _wo_check(self, cr, uid, data, context):
97 pool = pooler.get_pool(cr.dbname)
98 invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
99 journal = pool.get('account.journal').browse(cr, uid, data['form']['journal_id'], context)
100 cur_obj = pool.get('res.currency')
101 # Here we need that:
102 # The invoice total amount in company's currency <> paid amount in company currency
103 # (according to the correct day rate, invoicing rate and payment rate are may be different)
104 # => Ask to a write-off of the difference. This could happen even if both amount are equal,
105 # because if the currency rate
106 # Get the amount in company currency for the invoice (according to move lines)
107 inv_amount_company_currency = 0
108 for aml in invoice.move_id.line_id:
109 if aml.account_id.id == invoice.account_id.id or aml.account_id.type in ('receivable', 'payable'):
110 inv_amount_company_currency += aml.debit
111 inv_amount_company_currency -= aml.credit
112 inv_amount_company_currency = abs(inv_amount_company_currency)
113
114 # Get the current amount paid in company currency
115 if journal.currency and invoice.company_id.currency_id.id<>journal.currency.id:
116 ctx = {'date':data['form']['date']}
117 amount_paid = cur_obj.compute(cr, uid, journal.currency.id, invoice.company_id.currency_id.id, data['form']['amount'], round=True, context=ctx)
118 else:
119 amount_paid = data['form']['amount']
120 # Get the old payment if there are some
121 if invoice.payment_ids:
122 debit=credit=0.0
123 for payment in invoice.payment_ids:
124 debit+=payment.debit
125 credit+=payment.credit
126 amount_paid+=abs(debit-credit)
127
128 # Test if there is a difference according to currency rouding setting
129 if pool.get('res.currency').is_zero(cr, uid, invoice.company_id.currency_id,
130 (amount_paid - inv_amount_company_currency)):
131 return 'reconcile'
132 return 'addendum'
133
134_transaction_add_form = '''<?xml version="1.0"?>
135<form string="Information addendum">
136 <separator string="Write-Off Move" colspan="4"/>
137 <field name="writeoff_journal_id"/>
138 <field name="writeoff_acc_id" domain="[('type','&lt;&gt;','view'),('type','&lt;&gt;','consolidation')]"/>
139 <field name="comment"/>
140 <separator string="Analytic" colspan="4"/>
141 <field name="analytic_id"/>
142</form>'''
143
144_transaction_add_fields = {
145 'writeoff_acc_id': {'string':'Write-Off account', 'type':'many2one', 'relation':'account.account', 'required':True},
146 'writeoff_journal_id': {'string': 'Write-Off journal', 'type': 'many2one', 'relation':'account.journal', 'required':True},
147 'comment': {'string': 'Comment', 'type':'char', 'size': 64 , 'required':True},
148 'analytic_id': {'string':'Analytic Account', 'type': 'many2one', 'relation':'account.analytic.account'},
149}
150
151def _get_value_addendum(self, cr, uid, data, context={}):
152 return {'comment': _('Write-Off')}
153
154def _compute(self, cr, uid, data, context={}):
155 pool = pooler.get_pool(cr.dbname)
156 invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
157
158
159def _get_period(self, cr, uid, data, context={}):
160 pool = pooler.get_pool(cr.dbname)
161 ids = pool.get('account.period').find(cr, uid, context=context)
162 period_id = False
163 if len(ids):
164 period_id = ids[0]
165 invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
166 cash_amount = pool.get('account.invoice')._get_payment(cr, uid, [data['id']], invoice.residual, invoice.payment_term.id, context=context)
167 if invoice.state in ['draft', 'proforma2', 'cancel']:
168 raise wizard.except_wizard(_('Error !'), _('Can not pay draft/proforma/cancel invoice.'))
169 return {
170 'period_id': period_id,
171 'amount': invoice.residual,
172 'date': time.strftime('%Y-%m-%d'),
173 'cash_amount': cash_amount,
174 }
175
176class wizard_pay_invoice(wizard.interface):
177 states = {
178 'init': {
179 'actions': [_get_period],
180 'result': {'type':'form', 'arch':pay_form, 'fields':pay_fields, 'state':[('end','Cancel'),('reconcile','Partial Payment'),('writeoff_check','Full Payment')]}
181 },
182 'writeoff_check': {
183 'actions': [],
184 'result' : {'type': 'choice', 'next_state': _wo_check }
185 },
186 'addendum': {
187 'actions': [_get_value_addendum],
188 'result': {'type': 'form', 'arch':_transaction_add_form, 'fields':_transaction_add_fields, 'state':[('end','Cancel'),('reconcile','Pay and reconcile')]}
189 },
190 'reconcile': {
191 'actions': [_pay_and_reconcile],
192 'result': {'type':'state', 'state':'end'}
193 },
194 'compute_dis': {
195 'actions': [_compute],
196 'result': {'type':'state', 'state':'end'}
197 }
198 }
199wizard_pay_invoice('account.invoice.pay1')
200
201
202# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
203

Subscribers

People subscribed via source and target branches