account_analytic_analysis.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. # For copyright and license notices, see __openerp__.py file in module root
  4. # directory
  5. ##############################################################################
  6. from dateutil.relativedelta import relativedelta
  7. import datetime
  8. import logging
  9. import time
  10. from openerp.osv import osv, fields
  11. import openerp.tools
  12. from openerp.tools.translate import _
  13. from openerp import models, api
  14. from openerp.addons.decimal_precision import decimal_precision as dp
  15. class account_analytic_account(osv.osv):
  16. _inherit = "account.analytic.account"
  17. def _prepare_invoice_data(self, cr, uid, contract, context=None):
  18. context = context or {}
  19. journal_obj = self.pool.get('account.journal')
  20. if contract.type == 'purchase_contract':
  21. invoice = {}
  22. if not contract.partner_id:
  23. raise osv.except_osv(_('No Supplier Defined!'), _(
  24. "You must first select a Supplier for Contract %s!") % contract.name)
  25. fpos = contract.partner_id.property_account_position or False
  26. journal_ids = journal_obj.search(cr, uid, [(
  27. 'type', '=', 'purchase'), ('company_id', '=', contract.company_id.id or False)], limit=1)
  28. if not journal_ids:
  29. raise osv.except_osv(_('Error!'),
  30. _('Please define a pruchase journal for the company "%s".') % (contract.company_id.name or '', ))
  31. currency_id = False
  32. if contract.pricelist_id:
  33. currency_id = contract.pricelist_id.currency_id.id
  34. elif contract.partner_id.property_product_pricelist:
  35. currency_id = contract.partner_id.property_product_pricelist.currency_id.id
  36. elif contract.company_id:
  37. currency_id = contract.company_id.currency_id.id
  38. invoice = {
  39. 'account_id': contract.partner_id.property_account_payable.id,
  40. 'type': 'in_invoice',
  41. 'reference': contract.name,
  42. 'partner_id': contract.partner_id.id,
  43. 'currency_id': currency_id,
  44. 'journal_id': len(journal_ids) and journal_ids[0] or False,
  45. 'date_invoice': contract.recurring_next_date,
  46. 'origin': contract.code,
  47. 'fiscal_position': fpos and fpos.id,
  48. 'company_id': contract.company_id.id or False,
  49. }
  50. return invoice
  51. else:
  52. return super(account_analytic_account, self)._prepare_invoice_data(cr, uid, contract, context=context)
  53. def _prepare_invoice_lines(self, cr, uid, contract, fiscal_position_id, context=None):
  54. if not context:
  55. context = {}
  56. if contract.type == 'purchase_contract':
  57. fpos_obj = self.pool.get('account.fiscal.position')
  58. fiscal_position = None
  59. if fiscal_position_id:
  60. fiscal_position = fpos_obj.browse(
  61. cr, uid, fiscal_position_id, context=context)
  62. invoice_lines = []
  63. for line in contract.recurring_invoice_line_ids:
  64. res = line.product_id
  65. account_id = res.property_account_expense.id
  66. if not account_id:
  67. account_id = res.categ_id.property_account_expense_categ.id
  68. account_id = fpos_obj.map_account(
  69. cr, uid, fiscal_position, account_id)
  70. taxes = res.supplier_taxes_id or False
  71. if taxes:
  72. tax_ids = [x.id for x in taxes]
  73. tax_ids = self.pool['account.tax'].search(
  74. cr, uid,
  75. [('id', 'in', tax_ids),
  76. ('company_id', '=', contract.company_id.id)],
  77. context=context)
  78. taxes = self.pool['account.tax'].browse(
  79. cr, uid, tax_ids, context=context)
  80. tax_id = fpos_obj.map_tax(cr, uid, fiscal_position, taxes)
  81. invoice_lines.append((0, 0, {
  82. 'name': line.name,
  83. 'account_id': account_id,
  84. 'account_analytic_id': contract.id,
  85. 'price_unit': line.price_unit or 0.0,
  86. 'quantity': line.quantity,
  87. 'uos_id': line.uom_id.id or False,
  88. 'product_id': line.product_id.id or False,
  89. 'invoice_line_tax_id': [(6, 0, tax_id)],
  90. }))
  91. return invoice_lines
  92. else:
  93. return super(account_analytic_account, self)._prepare_invoice_lines(cr, uid, contract, fiscal_position_id, context=None)
  94. def _cron_recurring_create_invoice_purchase(self, cr, uid, context=None):
  95. current_date = time.strftime('%Y-%m-%d')
  96. contract_ids = self.search(cr, uid, [('recurring_next_date', '<=', current_date), (
  97. 'state', '=', 'open'), ('recurring_invoices', '=', True), ('type', '=', 'purchase_contract')])
  98. return self._recurring_create_invoice(cr, uid, contract_ids, context=context)
  99. class AccountAnalyticInvoiceLine(models.Model):
  100. _inherit = "account.analytic.invoice.line"
  101. @api.multi
  102. def product_id_change(
  103. self, product, uom_id, qty=0, name='', partner_id=False,
  104. price_unit=False, pricelist_id=False, company_id=None):
  105. context = self._context or {}
  106. company_id = company_id or False
  107. local_context = dict(context, company_id=company_id,
  108. force_company=company_id, pricelist=pricelist_id)
  109. result = super(AccountAnalyticInvoiceLine, self).product_id_change(
  110. product=product, uom_id=uom_id, qty=0, name='',
  111. partner_id=False, price_unit=False, pricelist_id=False,
  112. company_id=None)
  113. if not product or not context.get('purchase', False):
  114. return result
  115. res = self.env['product.product'].with_context(local_context).browse(
  116. product)
  117. if pricelist_id:
  118. price = res.price
  119. else:
  120. price = res.standard_price
  121. result['value']['price_unit'] = price
  122. if result['value']['uom_id'] != res.uom_id.id:
  123. new_price = self.env['product.uom']._compute_price(
  124. res.uom_id.id,
  125. result['value']['price_unit'],
  126. result['value']['uom_id'])
  127. result['value']['price_unit'] = new_price
  128. return result