Browse Source

[IMP] account_analytic_analysis_recurring: Hook method for preparing invoice lines

pull/26/head
Pedro M. Baeza 10 years ago
parent
commit
1afffca5f2
  1. 72
      contract/account_analytic_analysis_recurring.py

72
contract/account_analytic_analysis_recurring.py

@ -146,13 +146,45 @@ class AccountAnalyticAccount(orm.Model):
value = {'value': {'recurring_next_date': date_start}} value = {'value': {'recurring_next_date': date_start}}
return value 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): def _prepare_invoice(self, cr, uid, contract, context=None):
if context is None: if context is None:
context = {} 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: if not contract.partner_id:
raise orm.except_orm( raise orm.except_orm(
_('No Customer Defined!'), _('No Customer Defined!'),
@ -170,7 +202,7 @@ class AccountAnalyticAccount(orm.Model):
_('Error!'), _('Error!'),
_('Please define a sale journal for the company "%s".') % _('Please define a sale journal for the company "%s".') %
(contract.company_id.name or '',)) (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 = { inv_data = {
'reference': contract.code or False, 'reference': contract.code or False,
'account_id': partner.property_account_receivable.id, '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) invoice_id = inv_obj.create(cr, uid, inv_data, context=context)
for line in contract.recurring_invoice_line_ids: 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( self.pool['account.invoice.line'].create(
cr, uid, invoice_line_vals, context=context) cr, uid, invoice_line_vals, context=context)
inv_obj.button_compute(cr, uid, [invoice_id], context=context) inv_obj.button_compute(cr, uid, [invoice_id], context=context)

Loading…
Cancel
Save