[FIX] account: extract fpos recomputation logic from onchange

We extract the `price_unit` and `tax_ids` recomputation logic from the
`_onchange_product_id` method. This way, it can be called
programmatically.

opw-2371934

closes odoo/odoo#61477

X-original-commit: ca6f25bef45137c032bc5cafd336c3f16a97a929
Signed-off-by: Nicolas Martinelli (nim) <nim@odoo.com>
This commit is contained in:
Nicolas Martinelli
2020-11-06 13:01:54 +00:00
parent 53a1821d3d
commit 57d76c8539
+24 -19
View File
@@ -3118,6 +3118,28 @@ class AccountMoveLine(models.Model):
return self.product_id.uom_id
return False
def _set_price_and_tax_after_fpos(self):
self.ensure_one()
# Manage the fiscal position after that and adapt the price_unit.
# E.g. mapping a price-included-tax to a price-excluded-tax must
# remove the tax amount from the price_unit.
# However, mapping a price-included tax to another price-included tax must preserve the balance but
# adapt the price_unit to the new tax.
# E.g. mapping a 10% price-included tax to a 20% price-included tax for a price_unit of 110 should preserve
# 100 as balance but set 120 as price_unit.
if self.tax_ids and self.move_id.fiscal_position_id:
price_subtotal = self._get_price_total_and_subtotal()['price_subtotal']
self.tax_ids = self.move_id.fiscal_position_id.map_tax(
self.tax_ids._origin,
partner=self.move_id.partner_id)
accounting_vals = self._get_fields_onchange_subtotal(
price_subtotal=price_subtotal,
currency=self.move_id.company_currency_id)
amount_currency = accounting_vals['amount_currency']
business_vals = self._get_fields_onchange_balance(amount_currency=amount_currency)
if 'price_unit' in business_vals:
self.price_unit = business_vals['price_unit']
@api.depends('product_id', 'account_id', 'partner_id', 'date')
def _compute_analytic_account(self):
for record in self:
@@ -3343,25 +3365,8 @@ class AccountMoveLine(models.Model):
line.product_uom_id = line._get_computed_uom()
line.price_unit = line._get_computed_price_unit()
# Manage the fiscal position after that and adapt the price_unit.
# E.g. mapping a price-included-tax to a price-excluded-tax must
# remove the tax amount from the price_unit.
# However, mapping a price-included tax to another price-included tax must preserve the balance but
# adapt the price_unit to the new tax.
# E.g. mapping a 10% price-included tax to a 20% price-included tax for a price_unit of 110 should preserve
# 100 as balance but set 120 as price_unit.
if line.tax_ids and line.move_id.fiscal_position_id:
price_subtotal = line._get_price_total_and_subtotal()['price_subtotal']
line.tax_ids = line.move_id.fiscal_position_id.map_tax(
line.tax_ids._origin,
partner=line.move_id.partner_id)
accounting_vals = line._get_fields_onchange_subtotal(
price_subtotal=price_subtotal,
currency=line.move_id.company_currency_id)
amount_currency = accounting_vals['amount_currency']
business_vals = line._get_fields_onchange_balance(amount_currency=amount_currency)
if 'price_unit' in business_vals:
line.price_unit = business_vals['price_unit']
# price_unit and taxes may need to be adapted following Fiscal Position
line._set_price_and_tax_after_fpos()
# Convert the unit price to the invoice's currency.
company = line.move_id.company_id