Skip to content

Commit

Permalink
[FIX] product_margin_classification : do not loose margin classificat…
Browse files Browse the repository at this point in the history
…ion when creating new product.template. For that purpose, use same algorithm as 'weight' field
  • Loading branch information
legalsylvain committed Aug 21, 2023
1 parent 50ab0fd commit 8ad9b10
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
21 changes: 20 additions & 1 deletion product_margin_classification/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ProductTemplate(models.Model):
margin_classification_id = fields.Many2one(
string="Margin Classification",
compute="_compute_theoretical_multi_template",
readonly=False,
inverse="_inverse_margin_classification_id",
comodel_name="product.margin.classification",
)

Expand All @@ -34,6 +34,11 @@ class ProductTemplate(models.Model):
selection=MARGIN_STATE_SELECTION,
)

def _get_related_fields_variant_template(self):
res = super()._get_related_fields_variant_template()
res.append("margin_classification_id")
return res

@api.onchange(
"standard_price", "taxes_id", "margin_classification_id", "list_price"
)
Expand All @@ -50,6 +55,13 @@ def _onchange_standard_price(self):
self.list_price,
)

@api.depends(
"product_variant_ids",
"product_variant_ids.margin_classification_id",
"product_variant_ids.theoretical_price",
"product_variant_ids.theoretical_difference",
"product_variant_ids.margin_state",
)
def _compute_theoretical_multi_template(self):
unique_variants = self.filtered(
lambda template: len(template.product_variant_ids) == 1
Expand All @@ -66,6 +78,13 @@ def _compute_theoretical_multi_template(self):
template.theoretical_difference = 0.0
template.margin_state = 0.0

def _inverse_margin_classification_id(self):
for template in self:
if len(template.product_variant_ids) == 1:
template.product_variant_ids.margin_classification_id = (
template.margin_classification_id
)

# Custom Section
def use_theoretical_price(self):
self.mapped("product_variant_ids").use_theoretical_price()
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def setUp(self):
self.classification_big_margin = self.env.ref(
"product_margin_classification.classification_big_margin"
)
self.ProductTemplate = self.env["product.template"]

def test_01_product_use_theoretical_price(self):
"""Apply a 100% Markup (with rounding method) for a product with
Expand Down Expand Up @@ -86,3 +87,14 @@ def test_05_margin_classification_change_value(self):
self.too_expensive_product.theoretical_price,
"Change price_surcharge should change theoritical Price",
)

def test_06_create_product_template(self):
template = self.ProductTemplate.create(
{
"name": "Template Name",
"margin_classification_id": self.classification_big_margin,
}
)
self.assertEqual(
template.margin_classification_id, self.classification_big_margin
)

0 comments on commit 8ad9b10

Please sign in to comment.