From 6ed22c3ba93ecd88e834e65722ddbe299da127f2 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Thu, 12 Feb 2015 16:30:41 +0100 Subject: [PATCH] [IMP] account_analytic_analysis_recurring: Hook method for preparing invoice lines --- .../account_analytic_analysis_recurring.py | 72 ++++++++++--------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/contract/account_analytic_analysis_recurring.py b/contract/account_analytic_analysis_recurring.py index d2e39633..59800642 100644 --- a/contract/account_analytic_analysis_recurring.py +++ b/contract/account_analytic_analysis_recurring.py @@ -146,13 +146,45 @@ class AccountAnalyticAccount(orm.Model): value = {'value': {'recurring_next_date': date_start}} return value + def _prepare_invoice_line(self, cr, uid, line, invoice_id, context=None): + fpos_obj = self.pool['account.fiscal.position'] + lang_obj = self.pool['res.lang'] + product = line.product_id + account_id = product.property_account_income.id + if not account_id: + account_id = product.categ_id.property_account_income_categ.id + contract = line.analytic_account_id + fpos = contract.partner_id.property_account_position or False + account_id = fpos_obj.map_account(cr, uid, fpos, account_id) + taxes = product.taxes_id or False + tax_id = fpos_obj.map_tax(cr, uid, fpos, taxes) + if 'old_date' in context: + lang_ids = lang_obj.search( + cr, uid, [('code', '=', contract.partner_id.lang)], + context=context) + format = lang_obj.browse( + cr, uid, lang_ids, context=context)[0].date_format + line.name = line.name.replace( + '#START#', context['old_date'].strftime(format)) + line.name = line.name.replace( + '#END#', context['next_date'].strftime(format)) + return { + 'name': line.name, + 'account_id': account_id, + 'account_analytic_id': contract.id, + 'price_unit': line.price_unit or 0.0, + 'quantity': line.quantity, + 'uos_id': line.uom_id.id or False, + 'product_id': line.product_id.id or False, + 'invoice_id': invoice_id, + 'invoice_line_tax_id': [(6, 0, tax_id)], + } + def _prepare_invoice(self, cr, uid, contract, context=None): if context is None: context = {} - inv_obj = self.pool.get('account.invoice') - journal_obj = self.pool.get('account.journal') - fpos_obj = self.pool.get('account.fiscal.position') - lang_obj = self.pool.get('res.lang') + inv_obj = self.pool['account.invoice'] + journal_obj = self.pool['account.journal'] if not contract.partner_id: raise orm.except_orm( _('No Customer Defined!'), @@ -170,7 +202,7 @@ class AccountAnalyticAccount(orm.Model): _('Error!'), _('Please define a sale journal for the company "%s".') % (contract.company_id.name or '',)) - partner_payment_term = contract.partner_id.property_payment_term.id + partner_payment_term = partner.property_payment_term.id inv_data = { 'reference': contract.code or False, 'account_id': partner.property_account_receivable.id, @@ -186,34 +218,8 @@ class AccountAnalyticAccount(orm.Model): } invoice_id = inv_obj.create(cr, uid, inv_data, context=context) for line in contract.recurring_invoice_line_ids: - res = line.product_id - account_id = res.property_account_income.id - if not account_id: - account_id = res.categ_id.property_account_income_categ.id - account_id = fpos_obj.map_account(cr, uid, fpos, account_id) - taxes = res.taxes_id or False - tax_id = fpos_obj.map_tax(cr, uid, fpos, taxes) - if 'old_date' in context: - lang_ids = lang_obj.search( - cr, uid, [('code', '=', contract.partner_id.lang)], - context=context) - format = lang_obj.browse( - cr, uid, lang_ids, context=context)[0].date_format - line.name = line.name.replace( - '#START#', context['old_date'].strftime(format)) - line.name = line.name.replace( - '#END#', context['next_date'].strftime(format)) - invoice_line_vals = { - 'name': line.name, - 'account_id': account_id, - 'account_analytic_id': contract.id, - 'price_unit': line.price_unit or 0.0, - 'quantity': line.quantity, - 'uos_id': line.uom_id.id or False, - 'product_id': line.product_id.id or False, - 'invoice_id': invoice_id, - 'invoice_line_tax_id': [(6, 0, tax_id)], - } + invoice_line_vals = self._prepare_invoice_line( + cr, uid, line, invoice_id, context=context) self.pool['account.invoice.line'].create( cr, uid, invoice_line_vals, context=context) inv_obj.button_compute(cr, uid, [invoice_id], context=context)