diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index 38af59009d8..d2f9d230fbd 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -427,36 +427,31 @@ class account_voucher(osv.osv): if date: context_multi_currency.update({'date': date}) - line_pool = self.pool.get('account.voucher.line') - line_ids = ids and line_pool.search(cr, uid, [('voucher_id', '=', ids[0])]) or False - if line_ids: - line_pool.unlink(cr, uid, line_ids) - currency_pool = self.pool.get('res.currency') move_line_pool = self.pool.get('account.move.line') partner_pool = self.pool.get('res.partner') 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') - - journal = journal_pool.browse(cr, uid, journal_id, context=context) currency_id = vals.get('currency_id', currency_id) default = { 'value':{'line_ids':[], 'line_dr_ids':[], 'line_cr_ids':[], 'pre_line': False, 'currency_id':currency_id}, } - currency_id = currency_id or journal.company_id.currency_id.id + + #drop existing lines + line_ids = ids and line_pool.search(cr, uid, [('voucher_id', '=', ids[0])]) or False + if line_ids: + line_pool.unlink(cr, uid, line_ids) if not partner_id: return default - if not partner_id and ids: - line_ids = line_pool.search(cr, uid, [('voucher_id', '=', ids[0])]) - if line_ids: - line_pool.unlink(cr, uid, line_ids) - return default - + journal = journal_pool.browse(cr, uid, journal_id, context=context) partner = partner_pool.browse(cr, uid, partner_id, context=context) + currency_id = currency_id or journal.company_id.currency_id.id account_id = False if journal.type in ('sale','sale_refund'): account_id = partner.property_account_receivable.id @@ -484,64 +479,78 @@ class account_voucher(osv.osv): ids = move_line_pool.search(cr, uid, [('state','=','valid'), ('account_id.type', '=', account_type), ('reconcile_id', '=', False), ('partner_id', '=', partner_id)], context=context) else: ids = context['move_line_ids'] + invoice_id = context.get('invoice_id', False) + company_currency = journal.company_id.currency_id.id + move_line_found = False + + #order the lines by most old first ids.reverse() moves = move_line_pool.browse(cr, uid, ids, context=context) - #company_currency = journal.company_id.currency_id.id - #if company_currency != currency_id and ttype == 'payment': - # total_debit = currency_pool.compute(cr, uid, currency_id, company_currency, total_debit, context=context_multi_currency) - #elif company_currency != currency_id and ttype == 'receipt': - # total_credit = currency_pool.compute(cr, uid, currency_id, company_currency, total_credit, context=context_multi_currency) - - company_currency = journal.company_id.currency_id.id for line in moves: if line.credit and line.reconcile_partial_id and ttype == 'receipt': continue if line.debit and line.reconcile_partial_id and ttype == 'payment': continue + if invoice_id: + if line.invoice.id == invoice_id: + #if the invoice linked to the voucher line is equal to the invoice_id in context + #then we assign the amount on that line, whatever the other voucher lines + move_line_found = line.id + break + elif currency_id == company_currency: + #otherwise treatments is the same but with other field names + if line.amount_residual == price: + #if the amount residual is equal the amount voucher, we assign it to that voucher + #line, whatever the other voucher lines + move_line_found = line.id + break + #otherwise we will split the voucher amount on each line (by most old first) + total_credit += line.credit or 0.0 + total_debit += line.debit or 0.0 + elif currency_id == line.currency_id.id: + if line.amount_residual_currency == price: + move_line_found = line.id + break + total_credit += line.credit and line.amount_currency or 0.0 + total_debit += line.debit and line.amount_currency or 0.0 - if line.currency_id and currency_id==line.currency_id.id: - total_credit += line.amount_currency <0 and -line.amount_currency or 0.0 - total_debit += line.amount_currency >0 and line.amount_currency or 0.0 - else: - total_credit += currency_pool.compute(cr, uid, company_currency, currency_id, line.credit or 0.0) - total_debit += currency_pool.compute(cr, uid, company_currency, currency_id, line.debit or 0.0) - + #voucher line creation for line in moves: if line.credit and line.reconcile_partial_id and ttype == 'receipt': continue if line.debit and line.reconcile_partial_id and ttype == 'payment': continue - if line.currency_id and currency_id==line.currency_id.id: amount_original = abs(line.amount_currency) amount_unreconciled = abs(line.amount_residual_currency) else: amount_original = currency_pool.compute(cr, uid, company_currency, currency_id, line.credit or line.debit or 0.0) amount_unreconciled = currency_pool.compute(cr, uid, company_currency, currency_id, abs(line.amount_residual)) - - #original_amount = line.credit or line.debit or 0.0 - #amount_unreconciled = currency_pool.compute(cr, uid, line.currency_id and line.currency_id.id or company_currency, currency_id, abs(line.amount_residual_currency), context=context_multi_currency) rs = { 'name':line.move_id.name, 'type': line.credit and 'dr' or 'cr', 'move_line_id':line.id, 'account_id':line.account_id.id, 'amount_original': amount_original, + 'amount': (move_line_found == line.id) and min(price, amount_unreconciled) or 0.0, 'date_original':line.date, 'date_due':line.date_maturity, 'amount_unreconciled': amount_unreconciled, - } - if line.credit: - amount = min(amount_unreconciled, total_debit) - rs['amount'] = amount - total_debit -= amount - else: - amount = min(amount_unreconciled, total_credit) - rs['amount'] = amount - total_credit -= amount + #split voucher amount by most old first, but only for lines in the same currency + if not move_line_found: + line_currency_id = line.currency_id and line.currency_id.id or company_currency + if currency_id == line_currency_id: + if line.credit: + amount = min(amount_unreconciled, abs(total_debit)) + rs['amount'] = amount + total_debit -= amount + else: + amount = min(amount_unreconciled, abs(total_credit)) + rs['amount'] = amount + total_credit -= amount default['value']['line_ids'].append(rs) if rs['type'] == 'cr': diff --git a/addons/account_voucher/invoice.py b/addons/account_voucher/invoice.py index 0230cac1009..b108bc14fe3 100644 --- a/addons/account_voucher/invoice.py +++ b/addons/account_voucher/invoice.py @@ -45,10 +45,11 @@ class invoice(osv.osv): 'close_after_process': True, 'invoice_type':inv.type, 'invoice_id':inv.id, - 'default_type': inv.type in ('out_invoice','out_refund') and 'receipt' or 'payment' + 'default_type': inv.type in ('out_invoice','out_refund') and 'receipt' or 'payment', + 'type': inv.type in ('out_invoice','out_refund') and 'receipt' or 'payment' } } invoice() -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: \ No newline at end of file +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/account_voucher/voucher_payment_receipt_view.xml b/addons/account_voucher/voucher_payment_receipt_view.xml index 9c8bdffb35d..0ddc868cb45 100644 --- a/addons/account_voucher/voucher_payment_receipt_view.xml +++ b/addons/account_voucher/voucher_payment_receipt_view.xml @@ -74,14 +74,14 @@
- - + + - + - + - + @@ -137,15 +137,15 @@ - + - + @@ -279,19 +279,19 @@ - + + on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)"/> - + @@ -303,7 +303,7 @@ - + - +