[FIX] Avoid display write-off in pay invoice wizard. Put the right date and currency for conversion

lp bug: https://launchpad.net/bugs/453030 fixed

bzr revid: joel.grandguillaume@camptocamp.com-20091102085213-d3w0kwk10sj6gyb8
This commit is contained in:
Joel Grand-Guillaume 2009-11-02 09:52:13 +01:00
parent 2d09f1d639
commit 4ee6427c0b
2 changed files with 39 additions and 8 deletions

View File

@ -878,6 +878,16 @@ class account_invoice(osv.osv):
date=context['date_p'] date=context['date_p']
else: else:
date=time.strftime('%Y-%m-%d') date=time.strftime('%Y-%m-%d')
# Take the amount in currency and the currency of the payment
if 'amount_currency' in context and context['amount_currency'] and 'currency_id' in context and context['currency_id']:
amount_currency = context['amount_currency']
currency_id = context['currency_id']
else:
amount_currency = False
currency_id = False
# Pay attention to the sign for both debit/credit AND amount_currency
l1 = { l1 = {
'debit': direction * pay_amount>0 and direction * pay_amount, 'debit': direction * pay_amount>0 and direction * pay_amount,
'credit': direction * pay_amount<0 and - direction * pay_amount, 'credit': direction * pay_amount<0 and - direction * pay_amount,
@ -885,6 +895,8 @@ class account_invoice(osv.osv):
'partner_id': invoice.partner_id.id, 'partner_id': invoice.partner_id.id,
'ref':invoice.number, 'ref':invoice.number,
'date': date, 'date': date,
'currency_id':currency_id,
'amount_currency':amount_currency and direction * amount_currency or 0.0,
} }
l2 = { l2 = {
'debit': direction * pay_amount<0 and - direction * pay_amount, 'debit': direction * pay_amount<0 and - direction * pay_amount,
@ -893,6 +905,8 @@ class account_invoice(osv.osv):
'partner_id': invoice.partner_id.id, 'partner_id': invoice.partner_id.id,
'ref':invoice.number, 'ref':invoice.number,
'date': date, 'date': date,
'currency_id':currency_id,
'amount_currency':amount_currency and - direction * amount_currency or 0.0,
} }
if not name: if not name:

View File

@ -25,6 +25,7 @@ import netsvc
import pooler import pooler
import time import time
from tools.translate import _ from tools.translate import _
import tools
pay_form = '''<?xml version="1.0"?> pay_form = '''<?xml version="1.0"?>
<form string="Pay invoice"> <form string="Pay invoice">
@ -37,7 +38,7 @@ pay_form = '''<?xml version="1.0"?>
</form>''' </form>'''
pay_fields = { pay_fields = {
'amount': {'string': 'Amount paid', 'type':'float', 'required':True}, 'amount': {'string': 'Amount paid', 'type':'float', 'required':True, 'digits': (16,int(tools.config['price_accuracy']))},
'name': {'string': 'Entry Name', 'type':'char', 'size': 64, 'required':True}, 'name': {'string': 'Entry Name', 'type':'char', 'size': 64, 'required':True},
'date': {'string': 'Payment date', 'type':'date', 'required':True, 'default':lambda *args: time.strftime('%Y-%m-%d')}, 'date': {'string': 'Payment date', 'type':'date', 'required':True, 'default':lambda *args: time.strftime('%Y-%m-%d')},
'journal_id': {'string': 'Journal/Payment Mode', 'type': 'many2one', 'relation':'account.journal', 'required':True, 'domain':[('type','=','cash')]}, 'journal_id': {'string': 'Journal/Payment Mode', 'type': 'many2one', 'relation':'account.journal', 'required':True, 'domain':[('type','=','cash')]},
@ -57,10 +58,15 @@ def _pay_and_reconcile(self, cr, uid, data, context):
invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context) invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
journal = pool.get('account.journal').browse(cr, uid, data['form']['journal_id'], context) journal = pool.get('account.journal').browse(cr, uid, data['form']['journal_id'], context)
# Compute the amount in company's currency, with the journal currency (which is equal to payment currency)
# when it is needed : If payment currency (according to selected journal.currency) is <> from company currency
if journal.currency and invoice.company_id.currency_id.id<>journal.currency.id: if journal.currency and invoice.company_id.currency_id.id<>journal.currency.id:
ctx = {'date':data['form']['date']} ctx = {'date':data['form']['date']}
amount = cur_obj.compute(cr, uid, journal.currency.id, invoice.company_id.currency_id.id, amount, context=ctx) amount = cur_obj.compute(cr, uid, journal.currency.id, invoice.company_id.currency_id.id, amount, context=ctx)
currency_id = journal.currency.id
# Put the paid amount in currency, and the currency, in the context if currency is different from company's currency
context.update({'amount_currency':form['amount'],'currency_id':currency_id})
# Take the choosen date # Take the choosen date
if form.has_key('comment'): if form.has_key('comment'):
context.update({'date_p':form['date'],'comment':form['comment']}) context.update({'date_p':form['date'],'comment':form['comment']})
@ -79,12 +85,23 @@ def _wo_check(self, cr, uid, data, context):
pool = pooler.get_pool(cr.dbname) pool = pooler.get_pool(cr.dbname)
invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context) invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
journal = pool.get('account.journal').browse(cr, uid, data['form']['journal_id'], context) journal = pool.get('account.journal').browse(cr, uid, data['form']['journal_id'], context)
if invoice.company_id.currency_id.id <> invoice.currency_id.id: cur_obj = pool.get('res.currency')
return 'addendum' # Here we need that:
if journal.currency and (journal.currency.id <> invoice.currency_id.id): # The invoice total amount in company's currency <> paid amount in company currency
return 'addendum' # (according to the correct day rate, invoicing rate and payment rate are may be different)
if pool.get('res.currency').is_zero(cr, uid, invoice.currency_id, # => Ask to a write-off of the difference. This could happen even if both amount are equal,
(data['form']['amount'] - invoice.amount_total)): # because if the currency rate
# Get the amount in company currency for the invoice (according to move lines)
inv_amount_company_currency=invoice.move_id.amount
# Get the amount paid in company currency
if journal.currency and invoice.company_id.currency_id.id<>journal.currency.id:
ctx = {'date':data['form']['date']}
amount_paid = cur_obj.compute(cr, uid, journal.currency.id, invoice.company_id.currency_id.id, data['form']['amount'], round=True, context=ctx)
else:
amount_paid = data['form']['amount']
# Test if there is a difference according to currency rouding setting
if pool.get('res.currency').is_zero(cr, uid, invoice.company_id.currency_id,
(amount_paid - inv_amount_company_currency)):
return 'reconcile' return 'reconcile'
return 'addendum' return 'addendum'