product.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # -*- coding: utf-8 -*-
  2. from odoo import api, fields, models, _
  3. from odoo.exceptions import ValidationError
  4. from odoo.tools import format_amount
  5. ACCOUNT_DOMAIN = "['&', '&', '&', ('deprecated', '=', False), ('account_type', 'not in', ('asset_receivable','liability_payable','asset_cash','liability_credit_card')), ('company_id', '=', current_company_id), ('is_off_balance', '=', False)]"
  6. class ProductCategory(models.Model):
  7. _inherit = "product.category"
  8. property_account_income_categ_id = fields.Many2one('account.account', company_dependent=True,
  9. string="Income Account",
  10. domain=ACCOUNT_DOMAIN,
  11. help="This account will be used when validating a customer invoice.")
  12. property_account_expense_categ_id = fields.Many2one('account.account', company_dependent=True,
  13. string="Expense Account",
  14. domain=ACCOUNT_DOMAIN,
  15. help="The expense is accounted for when a vendor bill is validated, except in anglo-saxon accounting with perpetual inventory valuation in which case the expense (Cost of Goods Sold account) is recognized at the customer invoice validation.")
  16. #----------------------------------------------------------
  17. # Products
  18. #----------------------------------------------------------
  19. class ProductTemplate(models.Model):
  20. _inherit = "product.template"
  21. taxes_id = fields.Many2many('account.tax', 'product_taxes_rel', 'prod_id', 'tax_id', help="Default taxes used when selling the product.", string='Customer Taxes',
  22. domain=[('type_tax_use', '=', 'sale')], default=lambda self: self.env.company.account_sale_tax_id)
  23. tax_string = fields.Char(compute='_compute_tax_string')
  24. supplier_taxes_id = fields.Many2many('account.tax', 'product_supplier_taxes_rel', 'prod_id', 'tax_id', string='Vendor Taxes', help='Default taxes used when buying the product.',
  25. domain=[('type_tax_use', '=', 'purchase')], default=lambda self: self.env.company.account_purchase_tax_id)
  26. property_account_income_id = fields.Many2one('account.account', company_dependent=True,
  27. string="Income Account",
  28. domain=ACCOUNT_DOMAIN,
  29. help="Keep this field empty to use the default value from the product category.")
  30. property_account_expense_id = fields.Many2one('account.account', company_dependent=True,
  31. string="Expense Account",
  32. domain=ACCOUNT_DOMAIN,
  33. help="Keep this field empty to use the default value from the product category. If anglo-saxon accounting with automated valuation method is configured, the expense account on the product category will be used.")
  34. account_tag_ids = fields.Many2many(
  35. string="Account Tags",
  36. comodel_name='account.account.tag',
  37. domain="[('applicability', '=', 'products')]",
  38. help="Tags to be set on the base and tax journal items created for this product.")
  39. def _get_product_accounts(self):
  40. return {
  41. 'income': self.property_account_income_id or self.categ_id.property_account_income_categ_id,
  42. 'expense': self.property_account_expense_id or self.categ_id.property_account_expense_categ_id
  43. }
  44. def _get_asset_accounts(self):
  45. res = {}
  46. res['stock_input'] = False
  47. res['stock_output'] = False
  48. return res
  49. def get_product_accounts(self, fiscal_pos=None):
  50. accounts = self._get_product_accounts()
  51. if not fiscal_pos:
  52. fiscal_pos = self.env['account.fiscal.position']
  53. return fiscal_pos.map_accounts(accounts)
  54. @api.depends('taxes_id', 'list_price')
  55. def _compute_tax_string(self):
  56. for record in self:
  57. record.tax_string = record._construct_tax_string(record.list_price)
  58. def _construct_tax_string(self, price):
  59. currency = self.currency_id
  60. res = self.taxes_id.compute_all(price, product=self, partner=self.env['res.partner'])
  61. joined = []
  62. included = res['total_included']
  63. if currency.compare_amounts(included, price):
  64. joined.append(_('%s Incl. Taxes', format_amount(self.env, included, currency)))
  65. excluded = res['total_excluded']
  66. if currency.compare_amounts(excluded, price):
  67. joined.append(_('%s Excl. Taxes', format_amount(self.env, excluded, currency)))
  68. if joined:
  69. tax_string = f"(= {', '.join(joined)})"
  70. else:
  71. tax_string = " "
  72. return tax_string
  73. @api.constrains('uom_id')
  74. def _check_uom_not_in_invoice(self):
  75. self.env['product.template'].flush_model(['uom_id'])
  76. self._cr.execute("""
  77. SELECT prod_template.id
  78. FROM account_move_line line
  79. JOIN product_product prod_variant ON line.product_id = prod_variant.id
  80. JOIN product_template prod_template ON prod_variant.product_tmpl_id = prod_template.id
  81. JOIN uom_uom template_uom ON prod_template.uom_id = template_uom.id
  82. JOIN uom_category template_uom_cat ON template_uom.category_id = template_uom_cat.id
  83. JOIN uom_uom line_uom ON line.product_uom_id = line_uom.id
  84. JOIN uom_category line_uom_cat ON line_uom.category_id = line_uom_cat.id
  85. WHERE prod_template.id IN %s
  86. AND line.parent_state = 'posted'
  87. AND template_uom_cat.id != line_uom_cat.id
  88. LIMIT 1
  89. """, [tuple(self.ids)])
  90. if self._cr.fetchall():
  91. raise ValidationError(_(
  92. "This product is already being used in posted Journal Entries.\n"
  93. "If you want to change its Unit of Measure, please archive this product and create a new one."
  94. ))
  95. class ProductProduct(models.Model):
  96. _inherit = "product.product"
  97. tax_string = fields.Char(compute='_compute_tax_string')
  98. def _get_product_accounts(self):
  99. return self.product_tmpl_id._get_product_accounts()
  100. @api.model
  101. def _get_tax_included_unit_price(self, company, currency, document_date, document_type,
  102. is_refund_document=False, product_uom=None, product_currency=None,
  103. product_price_unit=None, product_taxes=None, fiscal_position=None
  104. ):
  105. """ Helper to get the price unit from different models.
  106. This is needed to compute the same unit price in different models (sale order, account move, etc.) with same parameters.
  107. """
  108. product = self
  109. assert document_type
  110. if product_uom is None:
  111. product_uom = product.uom_id
  112. if not product_currency:
  113. if document_type == 'sale':
  114. product_currency = product.currency_id
  115. elif document_type == 'purchase':
  116. product_currency = company.currency_id
  117. if product_price_unit is None:
  118. if document_type == 'sale':
  119. product_price_unit = product.with_company(company).lst_price
  120. elif document_type == 'purchase':
  121. product_price_unit = product.with_company(company).standard_price
  122. else:
  123. return 0.0
  124. if product_taxes is None:
  125. if document_type == 'sale':
  126. product_taxes = product.taxes_id.filtered(lambda x: x.company_id == company)
  127. elif document_type == 'purchase':
  128. product_taxes = product.supplier_taxes_id.filtered(lambda x: x.company_id == company)
  129. # Apply unit of measure.
  130. if product_uom and product.uom_id != product_uom:
  131. product_price_unit = product.uom_id._compute_price(product_price_unit, product_uom)
  132. # Apply fiscal position.
  133. if product_taxes and fiscal_position:
  134. product_taxes_after_fp = fiscal_position.map_tax(product_taxes)
  135. flattened_taxes_after_fp = product_taxes_after_fp._origin.flatten_taxes_hierarchy()
  136. flattened_taxes_before_fp = product_taxes._origin.flatten_taxes_hierarchy()
  137. taxes_before_included = all(tax.price_include for tax in flattened_taxes_before_fp)
  138. if set(product_taxes.ids) != set(product_taxes_after_fp.ids) and taxes_before_included:
  139. taxes_res = flattened_taxes_before_fp.compute_all(
  140. product_price_unit,
  141. quantity=1.0,
  142. currency=currency,
  143. product=product,
  144. is_refund=is_refund_document,
  145. )
  146. product_price_unit = taxes_res['total_excluded']
  147. if any(tax.price_include for tax in flattened_taxes_after_fp):
  148. taxes_res = flattened_taxes_after_fp.compute_all(
  149. product_price_unit,
  150. quantity=1.0,
  151. currency=currency,
  152. product=product,
  153. is_refund=is_refund_document,
  154. handle_price_include=False,
  155. )
  156. for tax_res in taxes_res['taxes']:
  157. tax = self.env['account.tax'].browse(tax_res['id'])
  158. if tax.price_include:
  159. product_price_unit += tax_res['amount']
  160. # Apply currency rate.
  161. if currency != product_currency:
  162. product_price_unit = product_currency._convert(product_price_unit, currency, company, document_date)
  163. return product_price_unit
  164. @api.depends('lst_price', 'product_tmpl_id', 'taxes_id')
  165. def _compute_tax_string(self):
  166. for record in self:
  167. record.tax_string = record.product_tmpl_id._construct_tax_string(record.lst_price)