[IMP] account_voucher: added payment_rate field on the voucher, if you don't want to use the rate encoded in openerp for this payment + made some order in the views

bzr revid: qdp-launchpad@openerp.com-20111021135240-0qjjs9p7y92ed1nr
This commit is contained in:
Quentin (OpenERP) 2011-10-21 15:52:40 +02:00
parent 9ecff1245a
commit 7f8fb0804a
2 changed files with 88 additions and 59 deletions

View File

@ -39,8 +39,6 @@ class account_voucher(osv.osv):
res[voucher.id] = ok
return res
def _get_type(self, cr, uid, context=None):
if context is None:
context = {}
@ -98,7 +96,6 @@ class account_voucher(osv.osv):
journal_id = context.get('journal_id', False)
if journal_id:
journal = journal_pool.browse(cr, uid, journal_id, context=context)
# currency_id = journal.company_id.currency_id.id
if journal.currency:
return journal.currency.id
return False
@ -182,6 +179,14 @@ class account_voucher(osv.osv):
res[voucher.id] = abs(voucher.amount - abs(credit - debit))
return res
def _paid_amount_in_company_currency(self, cr, uid, ids, name, args, context=None):
if not ids: return {}
res = {}
debit = credit = 0.0
for voucher in self.browse(cr, uid, ids, context=context):
res[voucher.id] = voucher.amount / voucher.payment_rate
return res
_name = 'account.voucher'
_description = 'Accounting Voucher'
_order = "date desc, id desc"
@ -242,6 +247,9 @@ class account_voucher(osv.osv):
'comment': fields.char('Counterpart Comment', size=64, required=True, readonly=True, states={'draft': [('readonly', False)]}),
'analytic_id': fields.many2one('account.analytic.account','Write-Off Analytic Account', readonly=True, states={'draft': [('readonly', False)]}),
'writeoff_amount': fields.function(_get_writeoff_amount, string='Reconcile Amount', type='float', readonly=True),
'payment_rate': fields.float('Payment Rate', digits=(12,6), required=True,
help='The rate between the journal currency and the company currency for this particular payment.'),
'paid_amount_in_company_currency': fields.function(_paid_amount_in_company_currency, string='Paid Amount in Company Currency', type='float', readonly=True),
}
_defaults = {
'period_id': _get_period,
@ -409,6 +417,19 @@ class account_voucher(osv.osv):
return default
def onchange_rate(self, cr, uid, ids, rate, amount, context=None):
res = {'value': {'paid_amount_in_company_currency': 0.0}}
if rate and amount:
res['value']['paid_amount_in_company_currency'] = amount / rate
return res
def onchange_amount(self, cr, uid, ids, amount, rate, partner_id, journal_id, currency_id, ttype, date, context=None):
res = self.onchange_partner_id(cr, uid, ids, partner_id, journal_id, amount, currency_id, ttype, date, context=context)
vals = self.onchange_rate(cr, uid, ids, rate, amount, context=context)
for key in vals.keys():
res[key].update(vals[key])
return res
def onchange_partner_id(self, cr, uid, ids, partner_id, journal_id, price, currency_id, ttype, date, context=None):
"""
Returns a dict that contains new values and context
@ -421,8 +442,6 @@ class account_voucher(osv.osv):
"""
if context is None:
context = {}
if not journal_id:
return {}
context_multi_currency = context.copy()
if date:
context_multi_currency.update({'date': date})
@ -433,12 +452,9 @@ class account_voucher(osv.osv):
journal_pool = self.pool.get('account.journal')
line_pool = self.pool.get('account.voucher.line')
#get default values
vals = self.onchange_journal(cr, uid, ids, journal_id, [], False, partner_id, context)
vals = vals.get('value')
currency_id = vals.get('currency_id', currency_id)
#set default values
default = {
'value':{'line_ids':[], 'line_dr_ids':[], 'line_cr_ids':[], 'pre_line': False, 'currency_id':currency_id},
'value': {'line_ids': [] ,'line_dr_ids': [] ,'line_cr_ids': [] ,'pre_line': False,},
}
#drop existing lines
@ -446,7 +462,7 @@ class account_voucher(osv.osv):
if line_ids:
line_pool.unlink(cr, uid, line_ids)
if not partner_id:
if not partner_id or not journal_id:
return default
journal = journal_pool.browse(cr, uid, journal_id, context=context)
@ -565,23 +581,30 @@ class account_voucher(osv.osv):
default['value']['writeoff_amount'] = self._compute_writeoff_amount(cr, uid, default['value']['line_dr_ids'], default['value']['line_cr_ids'], price)
return default
def onchange_date(self, cr, uid, ids, partner_id, journal_id, price, currency_id, ttype, date, context=None):
def onchange_date(self, cr, uid, ids, date, currency_id, amount, context=None):
"""
@param date: latest value from user input for field date
@param args: other arguments
@param context: context arguments, like lang, time zone
@return: Returns a dict which contains new values, and context
"""
res = {'value': {}}
#set the period of the voucher
period_pool = self.pool.get('account.period')
res = self.onchange_partner_id(cr, uid, ids, partner_id, journal_id, price, currency_id, ttype, date, context=context)
pids = period_pool.search(cr, uid, [('date_start', '<=', date), ('date_stop', '>=', date)])
if pids:
if not 'value' in res:
res['value'] = {}
res['value'].update({'period_id':pids[0]})
#set the default payment rate of the voucher and compute the paid amount in company currency
payment_rate = 1.0
if currency_id:
payment_rate = self.pool.get('res.currency').browse(cr, uid, currency_id, context={'date': date}).rate
res['value'].update({'payment_rate': payment_rate})
vals = self.onchange_rate(cr, uid, ids, payment_rate, amount, context=context)
for key in vals.keys():
res[key].update(vals[key])
return res
def onchange_journal(self, cr, uid, ids, journal_id, line_ids, tax_id, partner_id, context=None):
def onchange_journal(self, cr, uid, ids, journal_id, line_ids, tax_id, partner_id, date, amount, ttype, context=None):
if not journal_id:
return False
journal_pool = self.pool.get('account.journal')
@ -593,10 +616,18 @@ class account_voucher(osv.osv):
vals = self.onchange_price(cr, uid, ids, line_ids, tax_id, partner_id, context)
vals['value'].update({'tax_id':tax_id})
currency_id = False #journal.company_id.currency_id.id
currency_id = False
if journal.currency:
currency_id = journal.currency.id
vals['value'].update({'currency_id':currency_id})
payment_rate = self.pool.get('res.currency').browse(cr, uid, currency_id, context={'date': date}).rate
vals['value'].update({'payment_rate': payment_rate})
res = self.onchange_rate(cr, uid, ids, payment_rate, amount, context=context)
for key in res.keys():
vals[key].update(res[key])
vals['value'].update({'currency_id': currency_id})
res = self.onchange_partner_id(cr, uid, ids, partner_id, journal_id, amount, currency_id, ttype, date, context)
for key in res.keys():
vals[key].update(res[key])
return vals
def proforma_voucher(self, cr, uid, ids, context=None):
@ -640,7 +671,6 @@ class account_voucher(osv.osv):
raise osv.except_osv(_('Invalid action !'), _('Cannot delete Voucher(s) which are already opened or paid !'))
return super(account_voucher, self).unlink(cr, uid, ids, context=context)
# TODO: may be we can remove this method if not used anyware
def onchange_payment(self, cr, uid, ids, pay_now, journal_id, partner_id, ttype='sale'):
res = {}
if not partner_id:
@ -699,9 +729,9 @@ class account_voucher(osv.osv):
# TODO: Make this logic available.
# -for sale, purchase we have but for the payment and receipt we do not have as based on the bank/cash journal we can not know its payment or receipt
if voucher_brw.type in ('purchase', 'payment'):
credit = currency_obj.compute(cr, uid, current_currency, company_currency, voucher_brw.amount, context=context)
credit = voucher_brw.amount / voucher_brw.payment_rate
elif voucher_brw.type in ('sale', 'receipt'):
debit = currency_obj.compute(cr, uid, current_currency, company_currency, voucher_brw.amount, context=context)
debit = voucher_brw.amount / voucher_brw.payment_rate
if debit < 0: credit = -debit; debit = 0.0
if credit < 0: debit = -credit; credit = 0.0
sign = debit - credit < 0 and -1 or 1
@ -826,10 +856,10 @@ class account_voucher(osv.osv):
continue
#we check if the voucher line is fully paid or not and create a move line to balance the payment and initial invoice if needed
if line.amount == line.amount_unreconciled:
amount = currency_obj.compute(cr, uid, current_currency, company_currency, line.untax_amount or line.amount, context=context)
amount = (line.untax_amount or line.amount) / voucher_brw.payment_rate
amount_residual = line.move_line_id.amount_residual - amount #residual amount in company currency
else:
amount = currency_obj.compute(cr, uid, current_currency, company_currency, line.untax_amount or line.amount, context=context)
amount = (line.untax_amount or line.amount) / voucher_brw.payment_rate
amount_residual = 0.0
move_line = {
'journal_id': voucher_brw.journal_id.id,
@ -979,9 +1009,9 @@ class account_voucher(osv.osv):
line_total = move_line_brw.debit - move_line_brw.credit
rec_list_ids = []
if voucher.type == 'sale':
line_total = line_total - currency_pool.compute(cr, uid, current_currency, company_currency, voucher.tax_amount, context=context)
line_total = line_total - (voucher.tax_amount / voucher.payment_rate)
elif voucher.type == 'purchase':
line_total = line_total + currency_pool.compute(cr, uid, current_currency, company_currency, voucher.tax_amount, context=context)
line_total = line_total + (voucher.tax_amount / voucher.payment_rate)
#create one move line per voucher line where amount is not 0.0
line_total, rec_list_ids = self.voucher_move_line_create(cr, uid, voucher.id, line_total, move_id, company_currency, current_currency, context)
@ -1181,17 +1211,10 @@ class account_bank_statement_line(osv.osv):
def _amount_reconciled(self, cursor, user, ids, name, args, context=None):
if not ids:
return {}
res = {}
# company_currency_id = False
for line in self.browse(cursor, user, ids, context=context):
# if not company_currency_id:
# company_currency_id = line.company_id.id
if line.voucher_id:
res[line.id] = line.voucher_id.amount#
# res_currency_obj.compute(cursor, user,
# company_currency_id, line.statement_id.currency.id,
# line.voucher_id.amount, context=context)
else:
res[line.id] = 0.0
return res
@ -1212,7 +1235,6 @@ class account_bank_statement_line(osv.osv):
'amount_reconciled': fields.function(_amount_reconciled,
string='Amount reconciled', type='float'),
'voucher_id': fields.many2one('account.voucher', 'Payment'),
}
def unlink(self, cr, uid, ids, context=None):

View File

@ -75,13 +75,13 @@
<form string="Bill Payment">
<group col="6" colspan="4">
<field name="partner_id" required="1" on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)" context="{'invoice_currency':currency_id}" string="Supplier"/>
<field name="amount" on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)"/>
<field name="amount" on_change="onchange_amount(amount, payment_rate, partner_id, journal_id, currency_id, type, date, context)"/>
<field name="journal_id"
domain="[('type','in',['bank', 'cash'])]"
widget="selection" select="1"
on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)"
on_change="onchange_journal(journal_id, line_dr_ids, False, partner_id, date, amount, type, context)"
string="Payment Method"/>
<field name="date" select="1" on_change="onchange_date(partner_id, journal_id, amount, currency_id, type, date, context)"/>
<field name="date" select="1" on_change="onchange_date(date, currency_id, amount, context)"/>
<field name="reference" select="1" string="Payment Ref"/>
<field name="name" colspan="2"/>
<field name="account_id"
@ -138,14 +138,14 @@
<form string="Bill Payment">
<group col="6" colspan="4">
<field name="partner_id" domain="[('supplier','=',True)]" required="1" invisible="context.get('line_type', False)" on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)" context="{'invoice_currency':currency_id}" string="Supplier"/>
<field name="amount" invisible="context.get('line_type', False)" on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date)"/>
<field name="amount" invisible="context.get('line_type', False)" on_change="onchange_amount(amount, payment_rate, partner_id, journal_id, currency_id, type, date, context)"/>
<field name="journal_id"
domain="[('type','in',['bank', 'cash'])]"
invisible="context.get('line_type', False)"
widget="selection" select="1"
on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)"
on_change="onchange_journal(journal_id, line_dr_ids, False, partner_id, date, amount, type, context)"
string="Payment Method"/>
<field name="date" select="1" invisible="context.get('line_type', False)" on_change="onchange_date(partner_id, journal_id, amount, currency_id, type, date, context)"/>
<field name="date" select="1" invisible="context.get('line_type', False)" on_change="onchange_date(date, currency_id, amount, context)"/>
<field name="reference" select="1" invisible="context.get('line_type', False)" string="Payment Ref"/>
<field name="name" colspan="2" invisible="context.get('line_type', False)"/>
<field name="company_id" widget="selection" groups="base.group_multi_company"/>
@ -186,12 +186,14 @@
<field name="narration" colspan="2" nolabel="1"/>
</group>
<group col="2" colspan="1">
<group col="2" colspan="1">
<group col="2" colspan="1" attrs="{'invisible':[('currency_id','=',False)]}">
<separator string="Currency Options" colspan="2"/>
<field name="exchange_acc_id"
attrs="{'invisible':[('currency_id','=',False)]}"
domain="[('type','=','other')]"/>
<field name="analytic_id"
groups="analytic.group_analytic_accounting"/>
<field name="payment_rate" required="1" on_change="onchange_rate(payment_rate, amount, context)"/>
<field name="paid_amount_in_company_currency"/>
</group>
<group col="2" colspan="1">
<separator string="Payment Options" colspan="2"/>
<field name="payment_option" required="1"/>
<field name="writeoff_amount"
@ -201,12 +203,13 @@
domain="[('type','=','other')]"/>
<field name="comment"
attrs="{'invisible':[('payment_option','!=','with_writeoff')]}"/>
<field name="analytic_id"
groups="analytic.group_analytic_accounting"/>
</group>
<separator string="Other Information" colspan="2"/>
<group col="2" colspan="1">
<field name="number"/>
<field name="currency_id" invisible="1"/>
<group col="4" colspan="2">
<separator string="Other Information" colspan="4"/>
<field name="number" colspan="4"/>
<field name="currency_id" invisible="1" colspan="4"/>
</group>
</group>
</page>
@ -284,14 +287,14 @@
<field name="amount"
invisible="context.get('line_type', False)"
string="Paid Amount"
on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)"/>
on_change="onchange_amount(amount, payment_rate, partner_id, journal_id, currency_id, type, date, context)"/>
<field name="journal_id"
domain="[('type','in',['bank', 'cash'])]"
invisible="context.get('line_type', False)"
widget="selection" select="1"
on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)"
on_change="onchange_journal(journal_id, line_cr_ids, False, partner_id, date, amount, type, context)"
string="Payment Method"/>
<field name="date" select="1" invisible="context.get('line_type', False)" on_change="onchange_date(partner_id, journal_id, amount, currency_id, type, date, context)"/>
<field name="date" select="1" invisible="context.get('line_type', False)" on_change="onchange_date(date, currency_id, amount, context)"/>
<field name="reference" select="1" invisible="context.get('line_type', False)" string="Payment Ref"/>
<field name="name" colspan="2" invisible="context.get('line_type', False)"/>
<field name="company_id" widget="selection" groups="base.group_multi_company"/>
@ -332,13 +335,15 @@
<field name="narration" colspan="2" nolabel="1"/>
</group>
<group col="2" colspan="1">
<group col="2" colspan="1" attrs="{'invisible':[('currency_id','=',False)]}">
<separator string="Currency Options" colspan="2"/>
<field name="exchange_acc_id"
domain="[('type','=','other')]"/>
<field name="payment_rate" required="1" on_change="onchange_rate(payment_rate, amount, context)"/>
<field name="paid_amount_in_company_currency"/>
</group>
<group col="2" colspan="1">
<separator string="Payment Options" colspan="2"/>
<field name="analytic_id"
groups="analytic.group_analytic_accounting"/>
<field name="exchange_acc_id"
attrs="{'invisible':[('currency_id','=',False)]}"
domain="[('type','=','other')]"/>
<field name="payment_option" required="1"/>
<field name="writeoff_amount"
attrs="{'invisible':[('payment_option','!=','with_writeoff')]}"/>
@ -347,10 +352,12 @@
domain="[('type','=','other')]"/>
<field name="comment"
attrs="{'invisible':[('payment_option','!=','with_writeoff')]}"/>
<field name="analytic_id"
groups="analytic.group_analytic_accounting"/>
</group>
<separator string="Other Information" colspan="2"/>
<group col="4" colspan="1">
<field name="number"/>
<group col="4" colspan="2">
<separator string="Other Information" colspan="4"/>
<field name="number" colspan="4"/>
</group>
</group>
</page>