[IMP] account: defined new fields (amount_residual and amount_residual_currency) as function to compute the residual amount (resp. in company currency and in the currency of teh move line) direclty on the account.move.line object (to refactor the code). Changed the residual amount of the account.invoice in order to reuse the code depicted above. +few changes to improve the translations

bzr revid: qdp-launchpad@tinyerp.com-20110105134654-bils0yz2w1vwjtpy
This commit is contained in:
qdp-launchpad@tinyerp.com 2011-01-05 14:46:54 +01:00
parent 359d6d5c92
commit b0b8fe2aaf
3 changed files with 66 additions and 31 deletions

View File

@ -100,6 +100,57 @@ class account_move_line(osv.osv):
return query return query
def _amount_residual(self, cr, uid, ids, field_names, args, context=None):
"""
This function returns the residual amount on a receivable or payable account.move.line.
By default, it returns an amount in the currency of this journal entry (maybe different
of the company currency), but if you pass 'residual_in_company_currency' = True in the
context then the returned amount will be in company currency.
"""
res = {}
if context is None:
context = {}
cur_obj = self.pool.get('res.currency')
for move_line in self.browse(cr, uid, ids, context=context):
res[move_line.id] = {
'amount_residual': 0.0,
'amount_residual_currency': 0.0,
}
if move_line.reconcile_id:
continue
if not move_line.account_id.type in ('payable', 'receivable'):
#this function does not suport to be used on move lines not related to payable or receivable accounts
continue
if move_line.currency_id:
move_line_total = move_line.amount_currency
sign = move_line.amount_currency < 0 and -1 or 1
else:
move_line_total = move_line.debit - move_line.credit
sign = (move_line.debit - move_line.credit) < 0 and -1 or 1
line_total_in_company_currency = move_line.debit - move_line.credit
context_unreconciled = context.copy()
if move_line.reconcile_partial_id:
for payment_line in move_line.reconcile_partial_id.line_partial_ids:
if payment_line.id == move_line.id:
continue
if payment_line.currency_id and move_line.currency_id and payment_line.currency_id.id == move_line.currency_id.id:
move_line_total += payment_line.amount_currency
else:
if move_line.currency_id:
context_unreconciled.update({'date': payment_line.date})
amount_in_foreign_currency = cur_obj.compute(cr, uid, move_line.company_id.currency_id.id, move_line.currency_id.id, (payment_line.debit - payment_line.credit), round=False, context=context_unreconciled)
move_line_total += amount_in_foreign_currency
else:
move_line_total += (payment_line.debit - payment_line.credit)
line_total_in_company_currency += (payment_line.debit - payment_line.credit)
result = move_line_total
res[move_line.id]['amount_residual_currency'] = sign * (move_line.currency_id and self.pool.get('res.currency').round(cr, uid, move_line.currency_id, result) or result)
res[move_line.id]['amount_residual'] = sign * line_total_in_company_currency
return res
def default_get(self, cr, uid, fields, context=None): def default_get(self, cr, uid, fields, context=None):
data = self._default_get(cr, uid, fields, context=context) data = self._default_get(cr, uid, fields, context=context)
for f in data.keys(): for f in data.keys():
@ -433,6 +484,8 @@ class account_move_line(osv.osv):
'reconcile_id': fields.many2one('account.move.reconcile', 'Reconcile', readonly=True, ondelete='set null', select=2), 'reconcile_id': fields.many2one('account.move.reconcile', 'Reconcile', readonly=True, ondelete='set null', select=2),
'reconcile_partial_id': fields.many2one('account.move.reconcile', 'Partial Reconcile', readonly=True, ondelete='set null', select=2), 'reconcile_partial_id': fields.many2one('account.move.reconcile', 'Partial Reconcile', readonly=True, ondelete='set null', select=2),
'amount_currency': fields.float('Amount Currency', help="The amount expressed in an optional other currency if it is a multi-currency entry.", digits_compute=dp.get_precision('Account')), 'amount_currency': fields.float('Amount Currency', help="The amount expressed in an optional other currency if it is a multi-currency entry.", digits_compute=dp.get_precision('Account')),
'amount_residual_currency': fields.function(_amount_residual, method=True, string='Residual Amount', multi="residual", help="The residual amount on a receivable or payable of a journal entry expressed in its currency (maybe different of the company currency)."),
'amount_residual': fields.function(_amount_residual, method=True, string='Residual Amount', multi="residual", help="The residual amount on a receivable or payable of a journal entry expressed in the company currency."),
'currency_id': fields.many2one('res.currency', 'Currency', help="The optional other currency if it is a multi-currency entry."), 'currency_id': fields.many2one('res.currency', 'Currency', help="The optional other currency if it is a multi-currency entry."),
'period_id': fields.many2one('account.period', 'Period', required=True, select=2), 'period_id': fields.many2one('account.period', 'Period', required=True, select=2),
'journal_id': fields.many2one('account.journal', 'Journal', required=True, select=1), 'journal_id': fields.many2one('account.journal', 'Journal', required=True, select=1),

View File

@ -88,32 +88,14 @@ class account_invoice(osv.osv):
return [('none', _('Free Reference'))] return [('none', _('Free Reference'))]
def _amount_residual(self, cr, uid, ids, name, args, context=None): def _amount_residual(self, cr, uid, ids, name, args, context=None):
res = {} result = {}
if context is None: for invoice in self.browse(cr, uid, ids, context=context):
context = {} result[invoice.id] = 0.0
if invoice.move_id:
cur_obj = self.pool.get('res.currency') for m in invoice.move_id.line_id:
data_inv = self.browse(cr, uid, ids, context=context) if m.account_id.type in ('receivable','payable'):
for inv in data_inv: result[invoice.id] = m.amount_residual_currency
if inv.reconciled: return result
res[inv.id] = 0.0
continue
inv_total = inv.amount_total
context_unreconciled = context.copy()
for lines in inv.move_lines:
if lines.currency_id and lines.currency_id.id == inv.currency_id.id:
if inv.type in ('out_invoice','in_refund'):
inv_total += lines.amount_currency
else:
inv_total -= lines.amount_currency
else:
context_unreconciled.update({'date': lines.date})
amount_in_invoice_currency = cur_obj.compute(cr, uid, inv.company_id.currency_id.id, inv.currency_id.id,abs(lines.debit-lines.credit),round=False,context=context_unreconciled)
inv_total -= amount_in_invoice_currency
result = inv_total
res[inv.id] = self.pool.get('res.currency').round(cr, uid, inv.currency_id, result)
return res
# Give Journal Items related to the payment reconciled to this invoice # Give Journal Items related to the payment reconciled to this invoice
# Return ids of partial and total payments related to the selected invoices # Return ids of partial and total payments related to the selected invoices

View File

@ -64,8 +64,8 @@ class common_report_header(object):
def _get_target_move(self, data): def _get_target_move(self, data):
if data.get('form', False) and data['form'].get('target_move', False): if data.get('form', False) and data['form'].get('target_move', False):
if data['form']['target_move'] == 'all': if data['form']['target_move'] == 'all':
return 'All Entries' return _('All Entries')
return 'All Posted Entries' return _('All Posted Entries')
return '' return ''
def _get_end_date(self, data): def _get_end_date(self, data):
@ -94,10 +94,10 @@ class common_report_header(object):
def _get_filter(self, data): def _get_filter(self, data):
if data.get('form', False) and data['form'].get('filter', False): if data.get('form', False) and data['form'].get('filter', False):
if data['form']['filter'] == 'filter_date': if data['form']['filter'] == 'filter_date':
return 'Date' return _('Date')
elif data['form']['filter'] == 'filter_period': elif data['form']['filter'] == 'filter_period':
return 'Periods' return _('Periods')
return 'No Filter' return _('No Filter')
def _sum_debit_period(self, period_id, journal_id=None): def _sum_debit_period(self, period_id, journal_id=None):
journals = journal_id or self.journal_ids journals = journal_id or self.journal_ids