[REF] website_sale: factorize tax computation in combination info

This commit factorizes price computation in combination info to avoid
code duplication.

task-2382027
This commit is contained in:
Thibault Libioulle
2022-04-14 14:33:23 +02:00
parent 1518cd6ef9
commit efb0fca0c8
+24 -17
View File
@@ -224,33 +224,29 @@ class ProductTemplate(models.Model):
parent_combination=parent_combination, only_template=only_template)
if self.env.context.get('website_id'):
context = dict(self.env.context, ** {
'quantity': self.env.context.get('quantity', add_qty),
'pricelist': pricelist and pricelist.id
})
product = (self.env['product.product'].browse(combination_info['product_id']) or self).with_context(context)
product = self.env['product.product'].browse(combination_info['product_id']) or self
partner = self.env.user.partner_id
company_id = current_website.company_id
tax_display = self.user_has_groups('account.group_show_line_subtotals_tax_excluded') and 'total_excluded' or 'total_included'
fpos = self.env['account.fiscal.position'].sudo()._get_fiscal_position(partner)
product_taxes = product.sudo().taxes_id.filtered(lambda x: x.company_id == company_id)
taxes = fpos.map_tax(product_taxes)
# The list_price is always the price of one.
quantity_1 = 1
combination_info['price'] = self.env['account.tax']._fix_tax_included_price_company(
combination_info['price'], product_taxes, taxes, company_id)
price = taxes.compute_all(combination_info['price'], pricelist.currency_id, quantity_1, product, partner)[tax_display]
price = self._price_with_tax_computed(
combination_info['price'], product_taxes, taxes, company_id, pricelist, product,
partner
)
if pricelist.discount_policy == 'without_discount':
combination_info['list_price'] = self.env['account.tax']._fix_tax_included_price_company(
combination_info['list_price'], product_taxes, taxes, company_id)
list_price = taxes.compute_all(combination_info['list_price'], pricelist.currency_id, quantity_1, product, partner)[tax_display]
list_price = self._price_with_tax_computed(
combination_info['list_price'], product_taxes, taxes, company_id, pricelist,
product, partner
)
else:
list_price = price
combination_info['price_extra'] = self.env['account.tax']._fix_tax_included_price_company(combination_info['price_extra'], product_taxes, taxes, company_id)
price_extra = taxes.compute_all(combination_info['price_extra'], pricelist.currency_id, quantity_1, product, partner)[tax_display]
price_extra = self._price_with_tax_computed(
combination_info['price_extra'], product_taxes, taxes, company_id, pricelist,
product, partner
)
has_discounted_price = pricelist.currency_id.compare_amounts(list_price, price) == 1
combination_info.update(
@@ -264,6 +260,17 @@ class ProductTemplate(models.Model):
return combination_info
def _price_with_tax_computed(
self, price, product_taxes, taxes, company_id, pricelist, product, partner
):
price = self.env['account.tax']._fix_tax_included_price_company(
price, product_taxes, taxes, company_id
)
show_tax_excluded = self.user_has_groups('account.group_show_line_subtotals_tax_excluded')
tax_display = 'total_excluded' if show_tax_excluded else 'total_included'
# The list_price is always the price of one.
return taxes.compute_all(price, pricelist.currency_id, 1, product, partner)[tax_display]
def _create_first_product_variant(self, log_warning=False):
"""Create if necessary and possible and return the first product
variant for this template.