From 7555b99e19f373b79875cdcfb22c90773836bc49 Mon Sep 17 00:00:00 2001 From: Nhomar Hernandez Date: Sat, 8 Oct 2011 18:46:41 -0400 Subject: [PATCH 01/17] [REF] Deleted unused method _get_payment_term_lines on account_voucher, avoiding noise and so much lines in this code. bzr revid: openerp@cl-t076-283cl-20111008224641-omo5xikllr1xryg7 --- addons/account_voucher/account_voucher.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index 005e5d9ab46..772c988c18a 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -652,12 +652,10 @@ class account_voucher(osv.osv): return {'value':res} def action_move_line_create(self, cr, uid, ids, context=None): - def _get_payment_term_lines(term_id, amount): - term_pool = self.pool.get('account.payment.term') - if term_id and amount: - terms = term_pool.compute(cr, uid, term_id, amount) - return terms - return False + ''' + This method create account move from voucher. + Method refactored by Vauxoo. + ''' if context is None: context = {} move_pool = self.pool.get('account.move') From 95384a0c97c06371d9d5fe16bb17801088ca8eb6 Mon Sep 17 00:00:00 2001 From: "Javier Duran javier@vauxoo.com" <> Date: Sat, 8 Oct 2011 18:58:14 -0400 Subject: [PATCH 02/17] [REF] Moved variables to be initialized in the correct place bzr revid: info@vauxoo.com-20111008225814-0wzdey6gnb36tmyz --- addons/account_voucher/account_voucher.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index 772c988c18a..5458469220d 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -664,11 +664,14 @@ class account_voucher(osv.osv): tax_obj = self.pool.get('account.tax') seq_obj = self.pool.get('ir.sequence') for inv in self.browse(cr, uid, ids, context=context): + if inv.move_id: continue context_multi_currency = context.copy() context_multi_currency.update({'date': inv.date}) - + company_currency = inv.journal_id.company_id.currency_id.id + current_currency = inv.currency_id.id or company_currency + current_currency_obj = inv.currency_id or inv.journal_id.company_id.currency_id if inv.number: name = inv.number elif inv.journal_id.sequence_id: @@ -691,9 +694,6 @@ class account_voucher(osv.osv): move_id = move_pool.create(cr, uid, move) #create the first line manually - company_currency = inv.journal_id.company_id.currency_id.id - current_currency = inv.currency_id.id or company_currency - current_currency_obj = inv.currency_id or inv.journal_id.company_id.currency_id debit = 0.0 credit = 0.0 # TODO: is there any other alternative then the voucher type ?? From 560bab4062c1a95e87c76df8e40370b91b6545ca Mon Sep 17 00:00:00 2001 From: "Javier Duran javier@vauxoo.com" <> Date: Sat, 8 Oct 2011 19:06:26 -0400 Subject: [PATCH 03/17] [REF] The method call the voucher as 'inv' it is confusing whe you are reading a lot of lines and you are refering to something called diferently bzr revid: info@vauxoo.com-20111008230626-g196xminfyudvnk6 --- addons/account_voucher/account_voucher.py | 122 +++++++++++----------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index 5458469220d..4b022c1af18 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -663,33 +663,33 @@ class account_voucher(osv.osv): currency_pool = self.pool.get('res.currency') tax_obj = self.pool.get('account.tax') seq_obj = self.pool.get('ir.sequence') - for inv in self.browse(cr, uid, ids, context=context): + for voucher in self.browse(cr, uid, ids, context=context): - if inv.move_id: + if voucher.move_id: continue context_multi_currency = context.copy() - context_multi_currency.update({'date': inv.date}) - company_currency = inv.journal_id.company_id.currency_id.id - current_currency = inv.currency_id.id or company_currency - current_currency_obj = inv.currency_id or inv.journal_id.company_id.currency_id - if inv.number: - name = inv.number - elif inv.journal_id.sequence_id: - name = seq_obj.next_by_id(cr, uid, inv.journal_id.sequence_id.id) + context_multi_currency.update({'date': voucher.date}) + company_currency = voucher.journal_id.company_id.currency_id.id + current_currency = voucher.currency_id.id or company_currency + current_currency_obj = voucher.currency_id or voucher.journal_id.company_id.currency_id + if voucher.number: + name = voucher.number + elif voucher.journal_id.sequence_id: + name = seq_obj.next_by_id(cr, uid, voucher.journal_id.sequence_id.id) else: raise osv.except_osv(_('Error !'), _('Please define a sequence on the journal !')) - if not inv.reference: + if not voucher.reference: ref = name.replace('/','') else: - ref = inv.reference + ref = voucher.reference move = { 'name': name, - 'journal_id': inv.journal_id.id, - 'narration': inv.narration, - 'date': inv.date, + 'journal_id': voucher.journal_id.id, + 'narration': voucher.narration, + 'date': voucher.date, 'ref': ref, - 'period_id': inv.period_id and inv.period_id.id or False + 'period_id': voucher.period_id and voucher.period_id.id or False } move_id = move_pool.create(cr, uid, move) @@ -698,10 +698,10 @@ class account_voucher(osv.osv): credit = 0.0 # TODO: is there any other alternative then the voucher type ?? # -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 inv.type in ('purchase', 'payment'): - credit = currency_pool.compute(cr, uid, current_currency, company_currency, inv.amount, context=context_multi_currency) - elif inv.type in ('sale', 'receipt'): - debit = currency_pool.compute(cr, uid, current_currency, company_currency, inv.amount, context=context_multi_currency) + if voucher.type in ('purchase', 'payment'): + credit = currency_pool.compute(cr, uid, current_currency, company_currency, voucher.amount, context=context_multi_currency) + elif voucher.type in ('sale', 'receipt'): + debit = currency_pool.compute(cr, uid, current_currency, company_currency, voucher.amount, context=context_multi_currency) if debit < 0: credit = -debit debit = 0.0 @@ -711,32 +711,32 @@ class account_voucher(osv.osv): sign = debit - credit < 0 and -1 or 1 #create the first line of the voucher, the payment made move_line = { - 'name': inv.name or '/', + 'name': voucher.name or '/', 'debit': debit, 'credit': credit, - 'account_id': inv.account_id.id, + 'account_id': voucher.account_id.id, 'move_id': move_id, - 'journal_id': inv.journal_id.id, - 'period_id': inv.period_id.id, - 'partner_id': inv.partner_id.id, + 'journal_id': voucher.journal_id.id, + 'period_id': voucher.period_id.id, + 'partner_id': voucher.partner_id.id, 'currency_id': company_currency <> current_currency and current_currency or False, - 'amount_currency': company_currency <> current_currency and sign * inv.amount or 0.0, - 'date': inv.date, - 'date_maturity': inv.date_due + 'amount_currency': company_currency <> current_currency and sign * voucher.amount or 0.0, + 'date': voucher.date, + 'date_maturity': voucher.date_due } move_line_pool.create(cr, uid, move_line) rec_list_ids = [] line_total = debit - credit - if inv.type == 'sale': - line_total = line_total - currency_pool.compute(cr, uid, current_currency, company_currency, inv.tax_amount, context=context_multi_currency) - elif inv.type == 'purchase': - line_total = line_total + currency_pool.compute(cr, uid, current_currency, company_currency, inv.tax_amount, context=context_multi_currency) + if voucher.type == 'sale': + line_total = line_total - currency_pool.compute(cr, uid, current_currency, company_currency, voucher.tax_amount, context=context_multi_currency) + elif voucher.type == 'purchase': + line_total = line_total + currency_pool.compute(cr, uid, current_currency, company_currency, voucher.tax_amount, context=context_multi_currency) - for line in inv.line_ids: + for line in voucher.line_ids: #create one move line per voucher line where amount is not 0.0 if not line.amount: 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 + #we check if the voucher line is fully paid or not and create a move line to balance the payment and initial voucheroice if needed if line.amount == line.amount_unreconciled: amount = currency_pool.compute(cr, uid, current_currency, company_currency, line.untax_amount or line.amount, context=context_multi_currency) amount_residual = line.move_line_id.amount_residual - amount #residual amount in company currency @@ -744,18 +744,18 @@ class account_voucher(osv.osv): amount = currency_pool.compute(cr, uid, current_currency, company_currency, line.untax_amount or line.amount, context=context_multi_currency) amount_residual = 0.0 move_line = { - 'journal_id': inv.journal_id.id, - 'period_id': inv.period_id.id, + 'journal_id': voucher.journal_id.id, + 'period_id': voucher.period_id.id, 'name': line.name or '/', 'account_id': line.account_id.id, 'move_id': move_id, - 'partner_id': inv.partner_id.id, + 'partner_id': voucher.partner_id.id, 'currency_id': company_currency <> current_currency and current_currency or False, 'analytic_account_id': line.account_analytic_id and line.account_analytic_id.id or False, 'quantity': 1, 'credit': 0.0, 'debit': 0.0, - 'date': inv.date + 'date': voucher.date } if amount < 0: amount = -amount @@ -770,9 +770,9 @@ class account_voucher(osv.osv): line_total -= amount move_line['credit'] = amount - if inv.tax_id and inv.type in ('sale', 'purchase'): + if voucher.tax_id and voucher.type in ('sale', 'purchase'): move_line.update({ - 'account_tax_id': inv.tax_id.id, + 'account_tax_id': voucher.tax_id.id, }) if move_line.get('account_tax_id', False): tax_data = tax_obj.browse(cr, uid, [move_line['account_tax_id']], context=context)[0] @@ -785,36 +785,36 @@ class account_voucher(osv.osv): # Change difference entry if amount_residual: - if not inv.exchange_acc_id.id: + if not voucher.exchange_acc_id.id: raise osv.except_osv(_('Error!'), _('You must provide an account for the exchange difference.')) move_line = { - 'journal_id': inv.journal_id.id, - 'period_id': inv.period_id.id, + 'journal_id': voucher.journal_id.id, + 'period_id': voucher.period_id.id, 'name': _('change')+': '+(line.name or '/'), 'account_id': line.account_id.id, 'move_id': move_id, - 'partner_id': inv.partner_id.id, + 'partner_id': voucher.partner_id.id, 'currency_id': company_currency <> current_currency and current_currency or False, 'amount_currency': 0.0, 'quantity': 1, 'credit': amount_residual > 0 and amount_residual or 0.0, 'debit': amount_residual < 0 and -amount_residual or 0.0, - 'date': inv.date + 'date': voucher.date } new_id = move_line_pool.create(cr, uid, move_line) move_line = { - 'journal_id': inv.journal_id.id, - 'period_id': inv.period_id.id, + 'journal_id': voucher.journal_id.id, + 'period_id': voucher.period_id.id, 'name': _('change')+': '+(line.name or '/'), - 'account_id': inv.exchange_acc_id.id, + 'account_id': voucher.exchange_acc_id.id, 'move_id': move_id, 'amount_currency': 0.0, - 'partner_id': inv.partner_id.id, + 'partner_id': voucher.partner_id.id, 'currency_id': company_currency <> current_currency and current_currency or False, 'quantity': 1, 'debit': amount_residual > 0 and amount_residual or 0.0, 'credit': amount_residual < 0 and -amount_residual or 0.0, - 'date': inv.date + 'date': voucher.date } move_line_pool.create(cr, uid, move_line) rec_ids.append(new_id) @@ -826,35 +826,35 @@ class account_voucher(osv.osv): diff = line_total account_id = False write_off_name = '' - if inv.payment_option == 'with_writeoff': - account_id = inv.writeoff_acc_id.id - write_off_name = inv.comment - elif inv.type in ('sale', 'receipt'): - account_id = inv.partner_id.property_account_receivable.id + if voucher.payment_option == 'with_writeoff': + account_id = voucher.writeoff_acc_id.id + write_off_name = voucher.comment + elif voucher.type in ('sale', 'receipt'): + account_id = voucher.partner_id.property_account_receivable.id else: - account_id = inv.partner_id.property_account_payable.id + account_id = voucher.partner_id.property_account_payable.id move_line = { 'name': write_off_name or name, 'account_id': account_id, 'move_id': move_id, - 'partner_id': inv.partner_id.id, - 'date': inv.date, + 'partner_id': voucher.partner_id.id, + 'date': voucher.date, 'credit': diff > 0 and diff or 0.0, 'debit': diff < 0 and -diff or 0.0, #'amount_currency': company_currency <> current_currency and currency_pool.compute(cr, uid, company_currency, current_currency, diff * -1, context=context_multi_currency) or 0.0, #'currency_id': company_currency <> current_currency and current_currency or False, } move_line_pool.create(cr, uid, move_line) - self.write(cr, uid, [inv.id], { + self.write(cr, uid, [voucher.id], { 'move_id': move_id, 'state': 'posted', 'number': name, }) - if inv.journal_id.entry_posted: + if voucher.journal_id.entry_posted: move_pool.post(cr, uid, [move_id], context={}) for rec_ids in rec_list_ids: if len(rec_ids) >= 2: - move_line_pool.reconcile_partial(cr, uid, rec_ids, writeoff_acc_id=inv.exchange_acc_id.id, writeoff_period_id=inv.period_id.id, writeoff_journal_id=inv.journal_id.id) + move_line_pool.reconcile_partial(cr, uid, rec_ids, writeoff_acc_id=voucher.exchange_acc_id.id, writeoff_period_id=voucher.period_id.id, writeoff_journal_id=voucher.journal_id.id) return True def copy(self, cr, uid, id, default={}, context=None): From 81b73777fad5b719513f3698e8d39e71174a3a74 Mon Sep 17 00:00:00 2001 From: "Javier Duran javier@vauxoo.com" <> Date: Sat, 8 Oct 2011 20:31:19 -0400 Subject: [PATCH 04/17] [REF] Send outside action_move_line_create the process to create the account_move record bzr revid: info@vauxoo.com-20111009003119-q3ab9jg72m4tpjvc --- addons/account_voucher/account_voucher.py | 55 ++++++++++++++--------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index 4b022c1af18..ed4f9d221e9 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -651,6 +651,37 @@ class account_voucher(osv.osv): res['account_id'] = account_id return {'value':res} + def action_move_create(self, cr, uid, voucher_id, context=None): + ''' + This method create the account move related to voucher. + @voucher_id: voucher_id what we are creating account_move. + ''' + move_obj = self.pool.get('account.move') + seq_obj = self.pool.get('ir.sequence') + voucher_brw = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) + if voucher_brw.number: + name = voucher_brw.number + elif voucher_brw.journal_id.sequence_id: + name = seq_obj.next_by_id(cr, uid, voucher_brw.journal_id.sequence_id.id) + else: + raise osv.except_osv(_('Error !'), + _('Please define a sequence on the journal !')) + if not voucher_brw.reference: + ref = name.replace('/','') + else: + ref = voucher_brw.reference + + move = { + 'name': name, + 'journal_id': voucher_brw.journal_id.id, + 'narration': voucher_brw.narration, + 'date': voucher_brw.date, + 'ref': ref, + 'period_id': voucher_brw.period_id and voucher_brw.period_id.id or False + } + move_id = move_obj.create(cr, uid, move) + return move_id + def action_move_line_create(self, cr, uid, ids, context=None): ''' This method create account move from voucher. @@ -672,27 +703,9 @@ class account_voucher(osv.osv): company_currency = voucher.journal_id.company_id.currency_id.id current_currency = voucher.currency_id.id or company_currency current_currency_obj = voucher.currency_id or voucher.journal_id.company_id.currency_id - if voucher.number: - name = voucher.number - elif voucher.journal_id.sequence_id: - name = seq_obj.next_by_id(cr, uid, voucher.journal_id.sequence_id.id) - else: - raise osv.except_osv(_('Error !'), _('Please define a sequence on the journal !')) - if not voucher.reference: - ref = name.replace('/','') - else: - ref = voucher.reference - - move = { - 'name': name, - 'journal_id': voucher.journal_id.id, - 'narration': voucher.narration, - 'date': voucher.date, - 'ref': ref, - 'period_id': voucher.period_id and voucher.period_id.id or False - } - move_id = move_pool.create(cr, uid, move) - + #Create the account move record. + move_id = self.action_move_create(cr,uid,voucher.id) + name = move_pool.browse(cr, uid, move_id, context=context).name #create the first line manually debit = 0.0 credit = 0.0 From af970c6babd3ed4b9493e6dfc60e5eea7efbf48d Mon Sep 17 00:00:00 2001 From: Javier Duran Date: Sat, 8 Oct 2011 22:43:46 -0400 Subject: [PATCH 05/17] [REF] Creation of the first line in an external method call first_move_line_create bzr revid: info@vauxoo.com-20111009024346-ertt0atg17d8t827 --- addons/account_voucher/account_voucher.py | 87 +++++++++++++---------- 1 file changed, 51 insertions(+), 36 deletions(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index ed4f9d221e9..d6a7e4f78d1 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -651,12 +651,56 @@ class account_voucher(osv.osv): res['account_id'] = account_id return {'value':res} + def first_move_line_create(self, cr, uid, voucher_id, move_id, context=None): + ''' + Creation of first line in a voucher with fixed partner. + @voucher_id: id of the voucher. + @move_id: account move where this line will be added. + ''' + move_line_obj = self.pool.get('account.move.line') + currency_obj = self.pool.get('res.currency') + voucher_brw = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) + company_currency = voucher_brw.journal_id.company_id.currency_id.id + current_currency = voucher_brw.currency_id.id + context_multi_currency = context.copy() + context_multi_currency.update({'date': voucher.date}) + if current_currency <> company_currency: context = context_multi_currency + debit = 0.0 + credit = 0.0 + # TODO: is there any other alternative then the voucher type ?? + # ANSWER: We can have payment and receipt "In Advance". + # 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) + elif voucher_brw.type in ('sale', 'receipt'): + debit = currency_obj.compute(cr, uid, current_currency, company_currency, voucher_brw.amount, context) + if debit < 0: credit = -debit; debit = 0.0 + if credit < 0: debit = -credit; credit = 0.0 + sign = debit - credit < 0 and -1 or 1 + #create the first line of the voucher + move_line = { + 'name': voucher_brw.name or '/', + 'debit': debit, + 'credit': credit, + 'account_id': voucher_brw.account_id.id, + 'move_id': move_id, + 'journal_id': voucher_brw.journal_id.id, + 'period_id': voucher_brw.period_id.id, + 'partner_id': voucher_brw.partner_id.id, + 'currency_id': company_currency <> current_currency and current_currency or False, + 'amount_currency': company_currency <> current_currency and sign * voucher_brw.amount or 0.0, + 'date': voucher_brw.date, + 'date_maturity': voucher_brw.date_due + } + return move_line_obj.create(cr, uid, move_line, context) + def action_move_create(self, cr, uid, voucher_id, context=None): ''' This method create the account move related to voucher. @voucher_id: voucher_id what we are creating account_move. ''' - move_obj = self.pool.get('account.move') + move_obj = self.pool.get('account.move') seq_obj = self.pool.get('ir.sequence') voucher_brw = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) if voucher_brw.number: @@ -698,48 +742,19 @@ class account_voucher(osv.osv): if voucher.move_id: continue - context_multi_currency = context.copy() - context_multi_currency.update({'date': voucher.date}) company_currency = voucher.journal_id.company_id.currency_id.id current_currency = voucher.currency_id.id or company_currency current_currency_obj = voucher.currency_id or voucher.journal_id.company_id.currency_id #Create the account move record. move_id = self.action_move_create(cr,uid,voucher.id) + # Get the name of the acc_move just created name = move_pool.browse(cr, uid, move_id, context=context).name - #create the first line manually - debit = 0.0 - credit = 0.0 - # TODO: is there any other alternative then the voucher type ?? - # -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.type in ('purchase', 'payment'): - credit = currency_pool.compute(cr, uid, current_currency, company_currency, voucher.amount, context=context_multi_currency) - elif voucher.type in ('sale', 'receipt'): - debit = currency_pool.compute(cr, uid, current_currency, company_currency, voucher.amount, context=context_multi_currency) - if debit < 0: - credit = -debit - debit = 0.0 - if credit < 0: - debit = -credit - credit = 0.0 - sign = debit - credit < 0 and -1 or 1 - #create the first line of the voucher, the payment made - move_line = { - 'name': voucher.name or '/', - 'debit': debit, - 'credit': credit, - 'account_id': voucher.account_id.id, - 'move_id': move_id, - 'journal_id': voucher.journal_id.id, - 'period_id': voucher.period_id.id, - 'partner_id': voucher.partner_id.id, - 'currency_id': company_currency <> current_currency and current_currency or False, - 'amount_currency': company_currency <> current_currency and sign * voucher.amount or 0.0, - 'date': voucher.date, - 'date_maturity': voucher.date_due - } - move_line_pool.create(cr, uid, move_line) + #Create the first line of the voucher, the payment made + c + move_line_id = self.first_move_line_create(cr,uid,voucher.id, move_id, context) + move_line_brw = move_line_pool.browse(cr,uid,move_line_id, context) + line_total = move_line_brw.debit - move_line_brw.credit rec_list_ids = [] - line_total = debit - credit if voucher.type == 'sale': line_total = line_total - currency_pool.compute(cr, uid, current_currency, company_currency, voucher.tax_amount, context=context_multi_currency) elif voucher.type == 'purchase': From 451de7a5ca38eaa1997711bc366ef834afba7230 Mon Sep 17 00:00:00 2001 From: Javier Duran Date: Sat, 8 Oct 2011 23:41:02 -0400 Subject: [PATCH 06/17] [REF] voucher_move_line_create was created to generate all lines of acc move in an external method in this way in localization envoroment we can override just this method. bzr revid: info@vauxoo.com-20111009034102-oqss5lzmh2vcbx0k --- addons/account_voucher/account_voucher.py | 208 ++++++++++++---------- 1 file changed, 114 insertions(+), 94 deletions(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index d6a7e4f78d1..30166503466 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -663,10 +663,9 @@ class account_voucher(osv.osv): company_currency = voucher_brw.journal_id.company_id.currency_id.id current_currency = voucher_brw.currency_id.id context_multi_currency = context.copy() - context_multi_currency.update({'date': voucher.date}) + context_multi_currency.update({'date': voucher_brw.date}) if current_currency <> company_currency: context = context_multi_currency - debit = 0.0 - credit = 0.0 + debit = credit = 0.0 # TODO: is there any other alternative then the voucher type ?? # ANSWER: We can have payment and receipt "In Advance". # TODO: Make this logic available. @@ -726,6 +725,115 @@ class account_voucher(osv.osv): move_id = move_obj.create(cr, uid, move) return move_id + def voucher_move_line_create(self, cr, uid, voucher_id, line_total, move_id, context=None): + ''' + Create all ther rest of account move lines on accout move object. + @voucher_id: Voucher id what we are working with + @line_total: Total of the first line. + @move_id: Account move wher this lines will be joined. + ''' + move_line_obj = self.pool.get('account.move.line') + currency_obj = self.pool.get('res.currency') + tot_line = line_total + rec_lst_ids = [] + + voucher_brw = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) + company_currency = voucher_brw.journal_id.company_id.currency_id.id + current_currency = voucher_brw.currency_id.id + context_multi_currency = context.copy() + context_multi_currency.update({'date': voucher_brw.date}) + if current_currency <> company_currency: context = context_multi_currency + for line in voucher_brw.line_ids: + #create one move line per voucher line where amount is not 0.0 + if not line.amount: + 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) + 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) + amount_residual = 0.0 + move_line = { + 'journal_id': voucher_brw.journal_id.id, + 'period_id': voucher_brw.period_id.id, + 'name': line.name or '/', + 'account_id': line.account_id.id, + 'move_id': move_id, + 'partner_id': voucher_brw.partner_id.id, + 'currency_id': company_currency <> current_currency and current_currency or False, + 'analytic_account_id': line.account_analytic_id and line.account_analytic_id.id or False, + 'quantity': 1, + 'credit': 0.0, + 'debit': 0.0, + 'date': voucher_brw.date + } + if amount < 0: + amount = -amount + if line.type == 'dr': line.type = 'cr' else: line.type = 'dr' + + if (line.type=='dr'): + tot_line += amount + move_line['debit'] = amount + else: + tot_line -= amount + move_line['credit'] = amount + + if voucher_brw.tax_id and voucher_brw.type in ('sale', 'purchase'): + move_line.update({ + 'account_tax_id': voucher_brw.tax_id.id, + }) + + if move_line.get('account_tax_id', False): + tax_data = tax_obj.browse(cr, uid, [move_line['account_tax_id']], context=context)[0] + if not (tax_data.base_code_id and tax_data.tax_code_id): + raise osv.except_osv(_('No Account Base Code and Account Tax Code!'),_("You have to configure account base code and account tax code on the '%s' tax!") % (tax_data.name)) + + sign = (move_line['debit'] - move_line['credit']) < 0 and -1 or 1 + move_line['amount_currency'] = company_currency <> current_currency and sign * line.amount or 0.0 + voucher_line = move_line_obj.create(cr, uid, move_line) + rec_ids = [voucher_line, line.move_line_id.id] + + if amount_residual: # Change difference entry + if not inv.exchange_acc_id.id: + raise osv.except_osv(_('Error!'), _('You must provide an account for the exchange difference.')) + move_line = { + 'journal_id': inv.journal_id.id, + 'period_id': inv.period_id.id, + 'name': _('change')+': '+(line.name or '/'), + 'account_id': line.account_id.id, + 'move_id': move_id, + 'partner_id': inv.partner_id.id, + 'currency_id': company_currency <> current_currency and current_currency or False, + 'amount_currency': 0.0, + 'quantity': 1, + 'credit': amount_residual > 0 and amount_residual or 0.0, + 'debit': amount_residual < 0 and -amount_residual or 0.0, + 'date': inv.date + } + new_id = move_line_pool.create(cr, uid, move_line) + move_line = { + 'journal_id': inv.journal_id.id, + 'period_id': inv.period_id.id, + 'name': _('change')+': '+(line.name or '/'), + 'account_id': inv.exchange_acc_id.id, + 'move_id': move_id, + 'amount_currency': 0.0, + 'partner_id': inv.partner_id.id, + 'currency_id': company_currency <> current_currency and current_currency or False, + 'quantity': 1, + 'debit': amount_residual > 0 and amount_residual or 0.0, + 'credit': amount_residual < 0 and -amount_residual or 0.0, + 'date': inv.date + } + move_line_pool.create(cr, uid, move_line) + rec_ids.append(new_id) + + if line.move_line_id.id: + rec_lst_ids.append(rec_ids) + + return (tot_line, rec_lst_ids) + def action_move_line_create(self, cr, uid, ids, context=None): ''' This method create account move from voucher. @@ -750,7 +858,6 @@ class account_voucher(osv.osv): # Get the name of the acc_move just created name = move_pool.browse(cr, uid, move_id, context=context).name #Create the first line of the voucher, the payment made - c move_line_id = self.first_move_line_create(cr,uid,voucher.id, move_id, context) move_line_brw = move_line_pool.browse(cr,uid,move_line_id, context) line_total = move_line_brw.debit - move_line_brw.credit @@ -759,96 +866,9 @@ class account_voucher(osv.osv): line_total = line_total - currency_pool.compute(cr, uid, current_currency, company_currency, voucher.tax_amount, context=context_multi_currency) elif voucher.type == 'purchase': line_total = line_total + currency_pool.compute(cr, uid, current_currency, company_currency, voucher.tax_amount, context=context_multi_currency) - - for line in voucher.line_ids: - #create one move line per voucher line where amount is not 0.0 - if not line.amount: - continue - #we check if the voucher line is fully paid or not and create a move line to balance the payment and initial voucheroice if needed - if line.amount == line.amount_unreconciled: - amount = currency_pool.compute(cr, uid, current_currency, company_currency, line.untax_amount or line.amount, context=context_multi_currency) - amount_residual = line.move_line_id.amount_residual - amount #residual amount in company currency - else: - amount = currency_pool.compute(cr, uid, current_currency, company_currency, line.untax_amount or line.amount, context=context_multi_currency) - amount_residual = 0.0 - move_line = { - 'journal_id': voucher.journal_id.id, - 'period_id': voucher.period_id.id, - 'name': line.name or '/', - 'account_id': line.account_id.id, - 'move_id': move_id, - 'partner_id': voucher.partner_id.id, - 'currency_id': company_currency <> current_currency and current_currency or False, - 'analytic_account_id': line.account_analytic_id and line.account_analytic_id.id or False, - 'quantity': 1, - 'credit': 0.0, - 'debit': 0.0, - 'date': voucher.date - } - if amount < 0: - amount = -amount - if line.type == 'dr': - line.type = 'cr' - else: - line.type = 'dr' - if (line.type=='dr'): - line_total += amount - move_line['debit'] = amount - else: - line_total -= amount - move_line['credit'] = amount - - if voucher.tax_id and voucher.type in ('sale', 'purchase'): - move_line.update({ - 'account_tax_id': voucher.tax_id.id, - }) - if move_line.get('account_tax_id', False): - tax_data = tax_obj.browse(cr, uid, [move_line['account_tax_id']], context=context)[0] - if not (tax_data.base_code_id and tax_data.tax_code_id): - raise osv.except_osv(_('No Account Base Code and Account Tax Code!'),_("You have to configure account base code and account tax code on the '%s' tax!") % (tax_data.name)) - sign = (move_line['debit'] - move_line['credit']) < 0 and -1 or 1 - move_line['amount_currency'] = company_currency <> current_currency and sign * line.amount or 0.0 - voucher_line = move_line_pool.create(cr, uid, move_line) - rec_ids = [voucher_line, line.move_line_id.id] - - # Change difference entry - if amount_residual: - if not voucher.exchange_acc_id.id: - raise osv.except_osv(_('Error!'), _('You must provide an account for the exchange difference.')) - move_line = { - 'journal_id': voucher.journal_id.id, - 'period_id': voucher.period_id.id, - 'name': _('change')+': '+(line.name or '/'), - 'account_id': line.account_id.id, - 'move_id': move_id, - 'partner_id': voucher.partner_id.id, - 'currency_id': company_currency <> current_currency and current_currency or False, - 'amount_currency': 0.0, - 'quantity': 1, - 'credit': amount_residual > 0 and amount_residual or 0.0, - 'debit': amount_residual < 0 and -amount_residual or 0.0, - 'date': voucher.date - } - new_id = move_line_pool.create(cr, uid, move_line) - move_line = { - 'journal_id': voucher.journal_id.id, - 'period_id': voucher.period_id.id, - 'name': _('change')+': '+(line.name or '/'), - 'account_id': voucher.exchange_acc_id.id, - 'move_id': move_id, - 'amount_currency': 0.0, - 'partner_id': voucher.partner_id.id, - 'currency_id': company_currency <> current_currency and current_currency or False, - 'quantity': 1, - 'debit': amount_residual > 0 and amount_residual or 0.0, - 'credit': amount_residual < 0 and -amount_residual or 0.0, - 'date': voucher.date - } - move_line_pool.create(cr, uid, move_line) - rec_ids.append(new_id) - - if line.move_line_id.id: - rec_list_ids.append(rec_ids) + ############################################################ + #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, context) if not currency_pool.is_zero(cr, uid, current_currency_obj, line_total): diff = line_total From 14b8b8d52b918974f22d0ad66762a09088b365b5 Mon Sep 17 00:00:00 2001 From: Javier Duran Date: Sun, 9 Oct 2011 00:08:36 -0400 Subject: [PATCH 07/17] [REF] writeoff_move_line_create We put in an external methos the writeoff process bzr revid: info@vauxoo.com-20111009040836-tqy6erz2qu6ih3uw --- addons/account_voucher/account_voucher.py | 78 ++++++++++++++++------- 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index 30166503466..4187337a103 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -770,7 +770,10 @@ class account_voucher(osv.osv): } if amount < 0: amount = -amount - if line.type == 'dr': line.type = 'cr' else: line.type = 'dr' + if line.type == 'dr': + line.type = 'cr' + else: + line.type = 'dr' if (line.type=='dr'): tot_line += amount @@ -834,6 +837,54 @@ class account_voucher(osv.osv): return (tot_line, rec_lst_ids) + + def writeoff_move_line_create(self, cr, uid, voucher_id, line_total, move_id, name, context= None): + ''' + Create the writeoff move line + @voucher_id: Voucher id what we are working with + @line_total: Total of the first line. + @move_id: Account move wher this lines will be joined. + ''' + move_line_obj = self.pool.get('account.move.line') + currency_obj = self.pool.get('res.currency') + move_line_id = False + + voucher_brw = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) + company_currency = voucher_brw.journal_id.company_id.currency_id.id + current_currency = voucher_brw.currency_id.id + context_multi_currency = context.copy() + context_multi_currency.update({'date': voucher_brw.date}) + if current_currency <> company_currency: context = context_multi_currency + + current_currency_obj = voucher_brw.currency_id or voucher_brw.journal_id.company_id.currency_id + + if not currency_obj.is_zero(cr, uid, current_currency_obj, line_total): + diff = line_total + account_id = False + write_off_name = '' + if voucher_brw.payment_option == 'with_writeoff': + account_id = voucher_brw.writeoff_acc_id.id + write_off_name = voucher_brw.comment + elif voucher_brw.type in ('sale', 'receipt'): + account_id = voucher_brw.partner_id.property_account_receivable.id + else: + account_id = voucher_brw.partner_id.property_account_payable.id + move_line = { + 'name': write_off_name or name, + 'account_id': account_id, + 'move_id': move_id, + 'partner_id': voucher_brw.partner_id.id, + 'date': voucher_brw.date, + 'credit': diff > 0 and diff or 0.0, + 'debit': diff < 0 and -diff or 0.0, + #'amount_currency': company_currency <> current_currency and currency_obj.compute(cr, uid, company_currency, current_currency, diff * -1, context=context_multi_currency) or 0.0, + #'currency_id': company_currency <> current_currency and current_currency or False, + } + move_line_id = move_line_obj.create(cr, uid, move_line) + + return move_line_id + + def action_move_line_create(self, cr, uid, ids, context=None): ''' This method create account move from voucher. @@ -870,29 +921,8 @@ class account_voucher(osv.osv): #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, context) - if not currency_pool.is_zero(cr, uid, current_currency_obj, line_total): - diff = line_total - account_id = False - write_off_name = '' - if voucher.payment_option == 'with_writeoff': - account_id = voucher.writeoff_acc_id.id - write_off_name = voucher.comment - elif voucher.type in ('sale', 'receipt'): - account_id = voucher.partner_id.property_account_receivable.id - else: - account_id = voucher.partner_id.property_account_payable.id - move_line = { - 'name': write_off_name or name, - 'account_id': account_id, - 'move_id': move_id, - 'partner_id': voucher.partner_id.id, - 'date': voucher.date, - 'credit': diff > 0 and diff or 0.0, - 'debit': diff < 0 and -diff or 0.0, - #'amount_currency': company_currency <> current_currency and currency_pool.compute(cr, uid, company_currency, current_currency, diff * -1, context=context_multi_currency) or 0.0, - #'currency_id': company_currency <> current_currency and current_currency or False, - } - move_line_pool.create(cr, uid, move_line) + #create the writeoff line if needed + ml_writeoff_id = self.writeoff_move_line_create(cr, uid, voucher.id, line_total, move_id, name, context) self.write(cr, uid, [voucher.id], { 'move_id': move_id, 'state': 'posted', From dbc307b540957392f827bcdfb8847fbe4493558a Mon Sep 17 00:00:00 2001 From: Javier Duran Date: Sun, 9 Oct 2011 00:15:19 -0400 Subject: [PATCH 08/17] [REF] Just added a final comment bzr revid: info@vauxoo.com-20111009041519-10ab4qb46w8sc65k --- addons/account_voucher/account_voucher.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index 4187337a103..998b5291d2b 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -923,6 +923,7 @@ class account_voucher(osv.osv): #create the writeoff line if needed ml_writeoff_id = self.writeoff_move_line_create(cr, uid, voucher.id, line_total, move_id, name, context) + #We put posted the account move. self.write(cr, uid, [voucher.id], { 'move_id': move_id, 'state': 'posted', @@ -930,6 +931,7 @@ class account_voucher(osv.osv): }) if voucher.journal_id.entry_posted: move_pool.post(cr, uid, [move_id], context={}) + #We automatically reconcile the account move lines. for rec_ids in rec_list_ids: if len(rec_ids) >= 2: move_line_pool.reconcile_partial(cr, uid, rec_ids, writeoff_acc_id=voucher.exchange_acc_id.id, writeoff_period_id=voucher.period_id.id, writeoff_journal_id=voucher.journal_id.id) From 4a58b3e8d363c2441fe0e3499ffa2e651dcad15e Mon Sep 17 00:00:00 2001 From: "javier@vauxoo.com" <> Date: Mon, 10 Oct 2011 15:39:34 -0400 Subject: [PATCH 09/17] [REF] Modifying action_move_create method to return a dict, rename action_move_create method to account_move_get, added docstring of account_move_get, methods follows the guideline. bzr revid: javier@vauxoo.com-20111010193934-cnwxv0j63tln8gro --- addons/account_voucher/account_voucher.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index 998b5291d2b..bfb0e5c29b2 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -694,10 +694,15 @@ class account_voucher(osv.osv): } return move_line_obj.create(cr, uid, move_line, context) - def action_move_create(self, cr, uid, voucher_id, context=None): + def account_move_get(self, cr, uid, voucher_id, context=None): ''' This method create the account move related to voucher. - @voucher_id: voucher_id what we are creating account_move. + + @param cr: A database cursor + @param uid: ID of the user currently logged in + @param voucher_id: Id of voucher what we are creating account_move. + @param context: optional context dictionary + @return: dictionary which contains information regarding account move ''' move_obj = self.pool.get('account.move') seq_obj = self.pool.get('ir.sequence') @@ -722,8 +727,7 @@ class account_voucher(osv.osv): 'ref': ref, 'period_id': voucher_brw.period_id and voucher_brw.period_id.id or False } - move_id = move_obj.create(cr, uid, move) - return move_id + return move def voucher_move_line_create(self, cr, uid, voucher_id, line_total, move_id, context=None): ''' @@ -905,7 +909,7 @@ class account_voucher(osv.osv): current_currency = voucher.currency_id.id or company_currency current_currency_obj = voucher.currency_id or voucher.journal_id.company_id.currency_id #Create the account move record. - move_id = self.action_move_create(cr,uid,voucher.id) + move_id = move_pool.create(cr, uid, self.account_move_get(cr,uid,voucher.id)) # Get the name of the acc_move just created name = move_pool.browse(cr, uid, move_id, context=context).name #Create the first line of the voucher, the payment made From 5141bf3d8821942b4653f0931366e5d280bb2386 Mon Sep 17 00:00:00 2001 From: "javier@vauxoo.com" <> Date: Mon, 10 Oct 2011 17:08:06 -0400 Subject: [PATCH 10/17] [REF] Modifying first_move_line_create method to return a dict, rename first_move_line_create method to first_move_line_get added docstring of first_move_line_get method follows the guideline. bzr revid: javier@vauxoo.com-20111010210806-5b4zh7gb3hdabq6k --- addons/account_voucher/account_voucher.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index bfb0e5c29b2..ae216c3a8ed 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -651,11 +651,16 @@ class account_voucher(osv.osv): res['account_id'] = account_id return {'value':res} - def first_move_line_create(self, cr, uid, voucher_id, move_id, context=None): + def first_move_line_get(self, cr, uid, voucher_id, move_id, context=None): ''' - Creation of first line in a voucher with fixed partner. - @voucher_id: id of the voucher. - @move_id: account move where this line will be added. + Set a dict to be use to create the first account move line of voucher. + + @param cr: A database cursor + @param uid: ID of the user currently logged in + @param voucher_id: Id of voucher what we are creating account_move. + @param move_id: Id of account move where this line will be added. + @param context: optional context dictionary + @return: dictionary which contains information regarding account move line ''' move_line_obj = self.pool.get('account.move.line') currency_obj = self.pool.get('res.currency') @@ -677,7 +682,7 @@ class account_voucher(osv.osv): if debit < 0: credit = -debit; debit = 0.0 if credit < 0: debit = -credit; credit = 0.0 sign = debit - credit < 0 and -1 or 1 - #create the first line of the voucher + #set the first line of the voucher move_line = { 'name': voucher_brw.name or '/', 'debit': debit, @@ -692,7 +697,7 @@ class account_voucher(osv.osv): 'date': voucher_brw.date, 'date_maturity': voucher_brw.date_due } - return move_line_obj.create(cr, uid, move_line, context) + return move_line def account_move_get(self, cr, uid, voucher_id, context=None): ''' @@ -913,7 +918,7 @@ class account_voucher(osv.osv): # Get the name of the acc_move just created name = move_pool.browse(cr, uid, move_id, context=context).name #Create the first line of the voucher, the payment made - move_line_id = self.first_move_line_create(cr,uid,voucher.id, move_id, context) + move_line_id = move_line_pool.create(cr, uid, self.first_move_line_get(cr,uid,voucher.id, move_id, context), context) move_line_brw = move_line_pool.browse(cr,uid,move_line_id, context) line_total = move_line_brw.debit - move_line_brw.credit rec_list_ids = [] From 8a8f4923e0668d03849ad95ba6c6ac74ce1d0ae7 Mon Sep 17 00:00:00 2001 From: "javier@vauxoo.com" <> Date: Mon, 10 Oct 2011 18:19:15 -0400 Subject: [PATCH 11/17] [REF] Modifying writeoff_move_line_create method to return a dict, rename writeoff_move_line_create method to writeoff_move_line_get, added docstring of writeoff_move_line_get methods follows the guideline. bzr revid: javier@vauxoo.com-20111010221915-crmjncln12cnjpwv --- addons/account_voucher/account_voucher.py | 29 ++++++++++++++--------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index ae216c3a8ed..8f9ed49529c 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -847,16 +847,22 @@ class account_voucher(osv.osv): return (tot_line, rec_lst_ids) - def writeoff_move_line_create(self, cr, uid, voucher_id, line_total, move_id, name, context= None): + def writeoff_move_line_get(self, cr, uid, voucher_id, line_total, move_id, name, context= None): ''' - Create the writeoff move line - @voucher_id: Voucher id what we are working with - @line_total: Total of the first line. - @move_id: Account move wher this lines will be joined. + Set a dict to be use to create the writeoff move line. + + @param cr: A database cursor + @param uid: ID of the user currently logged in + @param voucher_id: Id of voucher what we are creating account_move. + @param line_total: Amount total of the first account move line of the voucher. + @param move_id: Id of account move where this line will be added. + @param name: Description of account move line. + @param context: optional context dictionary + @return: dictionary which contains information regarding account move line ''' move_line_obj = self.pool.get('account.move.line') currency_obj = self.pool.get('res.currency') - move_line_id = False + move_line = {} voucher_brw = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) company_currency = voucher_brw.journal_id.company_id.currency_id.id @@ -889,9 +895,8 @@ class account_voucher(osv.osv): #'amount_currency': company_currency <> current_currency and currency_obj.compute(cr, uid, company_currency, current_currency, diff * -1, context=context_multi_currency) or 0.0, #'currency_id': company_currency <> current_currency and current_currency or False, } - move_line_id = move_line_obj.create(cr, uid, move_line) - - return move_line_id + + return move_line def action_move_line_create(self, cr, uid, ids, context=None): @@ -931,8 +936,10 @@ class account_voucher(osv.osv): line_total, rec_list_ids = self.voucher_move_line_create(cr, uid, voucher.id, line_total, move_id, context) #create the writeoff line if needed - ml_writeoff_id = self.writeoff_move_line_create(cr, uid, voucher.id, line_total, move_id, name, context) - #We put posted the account move. + ml_writeoff = self.writeoff_move_line_get(cr, uid, voucher.id, line_total, move_id, name, context) + if ml_writeoff: + ml_writeoff_id = move_line_pool.create(cr, uid, ml_writeoff, context) + #We put posted the voucher. self.write(cr, uid, [voucher.id], { 'move_id': move_id, 'state': 'posted', From a864b4715674cd9909d7c24c4b3b8fa9ec9426b0 Mon Sep 17 00:00:00 2001 From: "Javier Duran javier@vauxoo.com" <> Date: Mon, 10 Oct 2011 19:10:46 -0400 Subject: [PATCH 12/17] [REF] Changing to an external method curr currency and comp currency renaming a method from create to get bzr revid: javier@vauxoo.com-20111010231046-f6j4us30lscj65lz --- addons/account_voucher/account_voucher.py | 33 +++++++++++++++-------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index 8f9ed49529c..1a34103fdc4 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -665,8 +665,8 @@ class account_voucher(osv.osv): move_line_obj = self.pool.get('account.move.line') currency_obj = self.pool.get('res.currency') voucher_brw = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) - company_currency = voucher_brw.journal_id.company_id.currency_id.id - current_currency = voucher_brw.currency_id.id + company_currency = _get_company_currency(cr, uid, voucher_brw.id, context) + current_currency = _get_current_currency(cr, uid, voucher_brw.id, context) context_multi_currency = context.copy() context_multi_currency.update({'date': voucher_brw.date}) if current_currency <> company_currency: context = context_multi_currency @@ -747,8 +747,8 @@ class account_voucher(osv.osv): rec_lst_ids = [] voucher_brw = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) - company_currency = voucher_brw.journal_id.company_id.currency_id.id - current_currency = voucher_brw.currency_id.id + company_currency = _get_company_currency(cr, uid, voucher_brw.id, context) + current_currency = _get_current_currency(cr, uid, voucher_brw.id, context) context_multi_currency = context.copy() context_multi_currency.update({'date': voucher_brw.date}) if current_currency <> company_currency: context = context_multi_currency @@ -865,8 +865,8 @@ class account_voucher(osv.osv): move_line = {} voucher_brw = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) - company_currency = voucher_brw.journal_id.company_id.currency_id.id - current_currency = voucher_brw.currency_id.id + company_currency = _get_company_currency(cr, uid, voucher_brw.id, context) + current_currency = _get_current_currency(cr, uid, voucher_brw.id, context) context_multi_currency = context.copy() context_multi_currency.update({'date': voucher_brw.date}) if current_currency <> company_currency: context = context_multi_currency @@ -898,11 +898,22 @@ class account_voucher(osv.osv): return move_line - + def _get_company_currency(self, cr, uid, voucher_id, context=None): + ''' + @voucher_id: Id of the voucher what i want to obtain company currency. + ''' + return self.pool.get('account.voucher').browse(cr,uid,voucher_id,context).journal_id.company_id.currency_id.id + + def _get_current_currency(self, cr, uid, voucher_id, context=None): + ''' + @voucher_id: Id of the voucher what i want to obtain current currency. + ''' + voucher = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) + return voucher.currency_id.id or _get_company_currency(voucher.id) + def action_move_line_create(self, cr, uid, ids, context=None): ''' - This method create account move from voucher. - Method refactored by Vauxoo. + Create account move for account voucher. ''' if context is None: context = {} @@ -915,8 +926,8 @@ class account_voucher(osv.osv): if voucher.move_id: continue - company_currency = voucher.journal_id.company_id.currency_id.id - current_currency = voucher.currency_id.id or company_currency + company_currency = _get_company_currency(cr, uid, voucher.id, context) + current_currency = _get_current_currency(cr, uid, voucher.id, context) current_currency_obj = voucher.currency_id or voucher.journal_id.company_id.currency_id #Create the account move record. move_id = move_pool.create(cr, uid, self.account_move_get(cr,uid,voucher.id)) From fd04f1bb9bf147c2291520b77d5c7687087dae71 Mon Sep 17 00:00:00 2001 From: "Javier Duran javier@vauxoo.com" <> Date: Mon, 10 Oct 2011 22:44:14 -0400 Subject: [PATCH 13/17] [REF] All the context_multicurrency logic pushed in an external method it is called 3 times bzr revid: javier@vauxoo.com-20111011024414-l78qdz3d3lv3eygg --- addons/account_voucher/account_voucher.py | 74 +++++++++++++---------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index 1a34103fdc4..f678f5f2f43 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -651,6 +651,19 @@ class account_voucher(osv.osv): res['account_id'] = account_id return {'value':res} + def _sel_context(self, cr, uid, voucher_id,context=None): + """ + Select if context will be multicurrency or not. + + :param voucher_id: Id of the actual voucher + :return Dict with new context + """ + context_multi_currency = context.copy() + voucher_brw = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) + context_multi_currency.update({'date': voucher_brw.date}) + if current_currency <> company_currency: context = context_multi_currency + return context + def first_move_line_get(self, cr, uid, voucher_id, move_id, context=None): ''' Set a dict to be use to create the first account move line of voucher. @@ -665,11 +678,9 @@ class account_voucher(osv.osv): move_line_obj = self.pool.get('account.move.line') currency_obj = self.pool.get('res.currency') voucher_brw = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) - company_currency = _get_company_currency(cr, uid, voucher_brw.id, context) - current_currency = _get_current_currency(cr, uid, voucher_brw.id, context) - context_multi_currency = context.copy() - context_multi_currency.update({'date': voucher_brw.date}) - if current_currency <> company_currency: context = context_multi_currency + company_currency = self._get_company_currency(cr, uid, voucher_brw.id, context) + current_currency = self._get_current_currency(cr, uid, voucher_brw.id, context) + context = self._sel_context(cr,uid,voucher_brw.id,context) debit = credit = 0.0 # TODO: is there any other alternative then the voucher type ?? # ANSWER: We can have payment and receipt "In Advance". @@ -737,9 +748,13 @@ class account_voucher(osv.osv): def voucher_move_line_create(self, cr, uid, voucher_id, line_total, move_id, context=None): ''' Create all ther rest of account move lines on accout move object. - @voucher_id: Voucher id what we are working with - @line_total: Total of the first line. - @move_id: Account move wher this lines will be joined. + It returns Tuple with tot_line what is total of difference between debit and credit and + a list of lists with ids to be reconciled with this format (total_deb_cred,list_of_lists). + + @param voucher_id: Voucher id what we are working with + @param line_total: Total of the first line. + @param move_id: Account move wher this lines will be joined. + @return Tuple with data to evaluate reconcilation process. ''' move_line_obj = self.pool.get('account.move.line') currency_obj = self.pool.get('res.currency') @@ -747,11 +762,9 @@ class account_voucher(osv.osv): rec_lst_ids = [] voucher_brw = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) - company_currency = _get_company_currency(cr, uid, voucher_brw.id, context) - current_currency = _get_current_currency(cr, uid, voucher_brw.id, context) - context_multi_currency = context.copy() - context_multi_currency.update({'date': voucher_brw.date}) - if current_currency <> company_currency: context = context_multi_currency + company_currency = self._get_company_currency(cr, uid, voucher_brw.id, context) + current_currency = self._get_current_currency(cr, uid, voucher_brw.id, context) + context = self._sel_context(cr,uid,voucher_brw.id,context) for line in voucher_brw.line_ids: #create one move line per voucher line where amount is not 0.0 if not line.amount: @@ -807,36 +820,36 @@ class account_voucher(osv.osv): rec_ids = [voucher_line, line.move_line_id.id] if amount_residual: # Change difference entry - if not inv.exchange_acc_id.id: + if not voucher_brw.exchange_acc_id.id: raise osv.except_osv(_('Error!'), _('You must provide an account for the exchange difference.')) move_line = { - 'journal_id': inv.journal_id.id, - 'period_id': inv.period_id.id, + 'journal_id': voucher_brw.journal_id.id, + 'period_id': voucher_brw.period_id.id, 'name': _('change')+': '+(line.name or '/'), 'account_id': line.account_id.id, 'move_id': move_id, - 'partner_id': inv.partner_id.id, + 'partner_id': voucher_brw.partner_id.id, 'currency_id': company_currency <> current_currency and current_currency or False, 'amount_currency': 0.0, 'quantity': 1, 'credit': amount_residual > 0 and amount_residual or 0.0, 'debit': amount_residual < 0 and -amount_residual or 0.0, - 'date': inv.date + 'date': voucher_brw.date, } new_id = move_line_pool.create(cr, uid, move_line) move_line = { - 'journal_id': inv.journal_id.id, - 'period_id': inv.period_id.id, + 'journal_id': voucher_brw.journal_id.id, + 'period_id': voucher_brw.period_id.id, 'name': _('change')+': '+(line.name or '/'), - 'account_id': inv.exchange_acc_id.id, + 'account_id': voucher_brw.exchange_acc_id.id, 'move_id': move_id, 'amount_currency': 0.0, - 'partner_id': inv.partner_id.id, + 'partner_id': voucher_brw.partner_id.id, 'currency_id': company_currency <> current_currency and current_currency or False, 'quantity': 1, 'debit': amount_residual > 0 and amount_residual or 0.0, 'credit': amount_residual < 0 and -amount_residual or 0.0, - 'date': inv.date + 'date': voucher_brw.date, } move_line_pool.create(cr, uid, move_line) rec_ids.append(new_id) @@ -865,12 +878,9 @@ class account_voucher(osv.osv): move_line = {} voucher_brw = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) - company_currency = _get_company_currency(cr, uid, voucher_brw.id, context) - current_currency = _get_current_currency(cr, uid, voucher_brw.id, context) - context_multi_currency = context.copy() - context_multi_currency.update({'date': voucher_brw.date}) - if current_currency <> company_currency: context = context_multi_currency - + company_currency = self._get_company_currency(cr, uid, voucher_brw.id, context) + current_currency = self._get_current_currency(cr, uid, voucher_brw.id, context) + context = self._sel_context(cr,uid,voucher_brw.id,context) current_currency_obj = voucher_brw.currency_id or voucher_brw.journal_id.company_id.currency_id if not currency_obj.is_zero(cr, uid, current_currency_obj, line_total): @@ -909,7 +919,7 @@ class account_voucher(osv.osv): @voucher_id: Id of the voucher what i want to obtain current currency. ''' voucher = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) - return voucher.currency_id.id or _get_company_currency(voucher.id) + return voucher.currency_id.id or self._get_company_currency(cr,uid,voucher.id,context) def action_move_line_create(self, cr, uid, ids, context=None): ''' @@ -926,8 +936,8 @@ class account_voucher(osv.osv): if voucher.move_id: continue - company_currency = _get_company_currency(cr, uid, voucher.id, context) - current_currency = _get_current_currency(cr, uid, voucher.id, context) + company_currency = self._get_company_currency(cr, uid, voucher.id, context) + current_currency = self._get_current_currency(cr, uid, voucher.id, context) current_currency_obj = voucher.currency_id or voucher.journal_id.company_id.currency_id #Create the account move record. move_id = move_pool.create(cr, uid, self.account_move_get(cr,uid,voucher.id)) From cb6892b20bcd9702616b1a1de10f33ace702e96d Mon Sep 17 00:00:00 2001 From: "Javier Duran javier@vauxoo.com" <> Date: Mon, 10 Oct 2011 23:32:33 -0400 Subject: [PATCH 14/17] [REF] Exchange difference move lines was pushed to an external method. In this way calculation of this type of rounding method can be managed diferently from a law to another. bzr revid: javier@vauxoo.com-20111011033233-ijbfcnp7f0tlcw2x --- addons/account_voucher/account_voucher.py | 96 ++++++++++++++--------- 1 file changed, 60 insertions(+), 36 deletions(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index f678f5f2f43..3e782b88f26 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -658,6 +658,8 @@ class account_voucher(osv.osv): :param voucher_id: Id of the actual voucher :return Dict with new context """ + company_currency = self._get_company_currency(cr, uid, voucher_id, context) + current_currency = self._get_current_currency(cr, uid, voucher_id, context) context_multi_currency = context.copy() voucher_brw = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) context_multi_currency.update({'date': voucher_brw.date}) @@ -745,6 +747,50 @@ class account_voucher(osv.osv): } return move + def _get_exchange_lines(self, cr, uid, line, move_id, amount_residual, context = None): + ''' + Generate two lines when the amount residual is due to difference on exchange. + + @param line: browse object of the voucher.line + @param move_id: Account move wher the move lines will be. + @param amount_residual: Amount to be posted. + @param context: Context wher we are working + @return: Tuple with 2 dicts account move line in pos [0] and counterpart in pos [1]. + ''' + company_currency = self._get_company_currency(cr, uid, line.voucher_id.id, context) + current_currency = self._get_current_currency(cr, uid, line.voucher_id.id, context) + if not line.voucher_id.exchange_acc_id.id: + raise osv.except_osv(_('Error!'), _('You must provide an account for the exchange difference.')) + move_line = { + 'journal_id': line.voucher_id.journal_id.id, + 'period_id': line.voucher_id.period_id.id, + 'name': _('change')+': '+(line.name or '/'), + 'account_id': line.account_id.id, + 'move_id': move_id, + 'partner_id': line.voucher_id.partner_id.id, + 'currency_id': company_currency <> current_currency and current_currency or False, + 'amount_currency': 0.0, + 'quantity': 1, + 'credit': amount_residual > 0 and amount_residual or 0.0, + 'debit': amount_residual < 0 and -amount_residual or 0.0, + 'date': line.voucher_id.date, + } + move_line_counterpart = { + 'journal_id': line.voucher_id.journal_id.id, + 'period_id': line.voucher_id.period_id.id, + 'name': _('change')+': '+(line.name or '/'), + 'account_id': line.voucher_id.exchange_acc_id.id, + 'move_id': move_id, + 'amount_currency': 0.0, + 'partner_id': line.voucher_id.partner_id.id, + 'currency_id': company_currency <> current_currency and current_currency or False, + 'quantity': 1, + 'debit': amount_residual > 0 and amount_residual or 0.0, + 'credit': amount_residual < 0 and -amount_residual or 0.0, + 'date': line.voucher_id.date, + } + return (move_line,move_line_counterpart) + def voucher_move_line_create(self, cr, uid, voucher_id, line_total, move_id, context=None): ''' Create all ther rest of account move lines on accout move object. @@ -820,38 +866,11 @@ class account_voucher(osv.osv): rec_ids = [voucher_line, line.move_line_id.id] if amount_residual: # Change difference entry - if not voucher_brw.exchange_acc_id.id: - raise osv.except_osv(_('Error!'), _('You must provide an account for the exchange difference.')) - move_line = { - 'journal_id': voucher_brw.journal_id.id, - 'period_id': voucher_brw.period_id.id, - 'name': _('change')+': '+(line.name or '/'), - 'account_id': line.account_id.id, - 'move_id': move_id, - 'partner_id': voucher_brw.partner_id.id, - 'currency_id': company_currency <> current_currency and current_currency or False, - 'amount_currency': 0.0, - 'quantity': 1, - 'credit': amount_residual > 0 and amount_residual or 0.0, - 'debit': amount_residual < 0 and -amount_residual or 0.0, - 'date': voucher_brw.date, - } - new_id = move_line_pool.create(cr, uid, move_line) - move_line = { - 'journal_id': voucher_brw.journal_id.id, - 'period_id': voucher_brw.period_id.id, - 'name': _('change')+': '+(line.name or '/'), - 'account_id': voucher_brw.exchange_acc_id.id, - 'move_id': move_id, - 'amount_currency': 0.0, - 'partner_id': voucher_brw.partner_id.id, - 'currency_id': company_currency <> current_currency and current_currency or False, - 'quantity': 1, - 'debit': amount_residual > 0 and amount_residual or 0.0, - 'credit': amount_residual < 0 and -amount_residual or 0.0, - 'date': voucher_brw.date, - } - move_line_pool.create(cr, uid, move_line) + print "Amount Residual %s " % str(amount_residual) + exch_lines = self._get_exchange_lines(cr, uid, line, move_id, + amount_residual ,context) + new_id = move_line_obj.create(cr, uid, exch_lines[0],context) + move_line_obj.create(cr, uid, exch_lines[1], context) rec_ids.append(new_id) if line.move_line_id.id: @@ -864,8 +883,6 @@ class account_voucher(osv.osv): ''' Set a dict to be use to create the writeoff move line. - @param cr: A database cursor - @param uid: ID of the user currently logged in @param voucher_id: Id of voucher what we are creating account_move. @param line_total: Amount total of the first account move line of the voucher. @param move_id: Id of account move where this line will be added. @@ -910,13 +927,19 @@ class account_voucher(osv.osv): def _get_company_currency(self, cr, uid, voucher_id, context=None): ''' - @voucher_id: Id of the voucher what i want to obtain company currency. + Get the courrency of the actual company. + + @param voucher_id: Id of the voucher what i want to obtain company currency. + @return id of currency ''' return self.pool.get('account.voucher').browse(cr,uid,voucher_id,context).journal_id.company_id.currency_id.id def _get_current_currency(self, cr, uid, voucher_id, context=None): ''' - @voucher_id: Id of the voucher what i want to obtain current currency. + Get currency we are working with. + + @param voucher_id: Id of the voucher what i want to obtain current currency. + @return id of currency ''' voucher = self.pool.get('account.voucher').browse(cr,uid,voucher_id,context) return voucher.currency_id.id or self._get_company_currency(cr,uid,voucher.id,context) @@ -924,6 +947,7 @@ class account_voucher(osv.osv): def action_move_line_create(self, cr, uid, ids, context=None): ''' Create account move for account voucher. + ''' if context is None: context = {} From 737fccbbc2f2928c9c4b360f1991f196430e6759 Mon Sep 17 00:00:00 2001 From: "Javier Duran javier@vauxoo.com" <> Date: Mon, 10 Oct 2011 23:56:46 -0400 Subject: [PATCH 15/17] [REF] Including decimal precision on amount_residual calculated to avoid account move lines when we have little very little rounding problems bzr revid: javier@vauxoo.com-20111011035646-7jn8z1p8dw6l3f40 --- addons/account_voucher/account_voucher.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index 3e782b88f26..1cd7be48bff 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -864,8 +864,8 @@ class account_voucher(osv.osv): move_line['amount_currency'] = company_currency <> current_currency and sign * line.amount or 0.0 voucher_line = move_line_obj.create(cr, uid, move_line) rec_ids = [voucher_line, line.move_line_id.id] - - if amount_residual: # Change difference entry + print "What the fuck %s"% (self.pool.get('decimal.precision').precision_get(cr,uid,'Account')) + if round(amount_residual,self.pool.get('decimal.precision').precision_get(cr,uid,'Account')): # Change difference entry print "Amount Residual %s " % str(amount_residual) exch_lines = self._get_exchange_lines(cr, uid, line, move_id, amount_residual ,context) From 410ee16b088595b7b10b43e781666c4bd6d5a0e8 Mon Sep 17 00:00:00 2001 From: Javier Duran Date: Tue, 11 Oct 2011 00:01:40 -0400 Subject: [PATCH 16/17] [REF] Just taking off some useless strings bzr revid: javier@vauxoo.com-20111011040140-dkpt9hc76n029mwo --- addons/account_voucher/account_voucher.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index 1cd7be48bff..b383cbe7f2b 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -864,9 +864,7 @@ class account_voucher(osv.osv): move_line['amount_currency'] = company_currency <> current_currency and sign * line.amount or 0.0 voucher_line = move_line_obj.create(cr, uid, move_line) rec_ids = [voucher_line, line.move_line_id.id] - print "What the fuck %s"% (self.pool.get('decimal.precision').precision_get(cr,uid,'Account')) if round(amount_residual,self.pool.get('decimal.precision').precision_get(cr,uid,'Account')): # Change difference entry - print "Amount Residual %s " % str(amount_residual) exch_lines = self._get_exchange_lines(cr, uid, line, move_id, amount_residual ,context) new_id = move_line_obj.create(cr, uid, exch_lines[0],context) @@ -947,7 +945,6 @@ class account_voucher(osv.osv): def action_move_line_create(self, cr, uid, ids, context=None): ''' Create account move for account voucher. - ''' if context is None: context = {} @@ -957,7 +954,6 @@ class account_voucher(osv.osv): tax_obj = self.pool.get('account.tax') seq_obj = self.pool.get('ir.sequence') for voucher in self.browse(cr, uid, ids, context=context): - if voucher.move_id: continue company_currency = self._get_company_currency(cr, uid, voucher.id, context) @@ -976,7 +972,6 @@ class account_voucher(osv.osv): line_total = line_total - currency_pool.compute(cr, uid, current_currency, company_currency, voucher.tax_amount, context=context_multi_currency) elif voucher.type == 'purchase': line_total = line_total + currency_pool.compute(cr, uid, current_currency, company_currency, voucher.tax_amount, context=context_multi_currency) - ############################################################ #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, context) From 98fd90fcd846627b066fd5a720f2797941869aaf Mon Sep 17 00:00:00 2001 From: Javier Duran Date: Tue, 11 Oct 2011 00:14:15 -0400 Subject: [PATCH 17/17] [REF] Correcting bad comment bzr revid: javier@vauxoo.com-20111011041415-ql0k37mouhu8a9fg --- addons/account_voucher/account_voucher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index b383cbe7f2b..6a113fa8e56 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -979,7 +979,7 @@ class account_voucher(osv.osv): ml_writeoff = self.writeoff_move_line_get(cr, uid, voucher.id, line_total, move_id, name, context) if ml_writeoff: ml_writeoff_id = move_line_pool.create(cr, uid, ml_writeoff, context) - #We put posted the voucher. + #We post the voucher. self.write(cr, uid, [voucher.id], { 'move_id': move_id, 'state': 'posted',