Skip to content

Commit

Permalink
[MIG] product_variant_sale_price: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ecino committed Apr 14, 2023
1 parent eb90f60 commit 296eff6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 39 deletions.
21 changes: 9 additions & 12 deletions product_variant_sale_price/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,25 @@ def _compute_lst_price(self):
uom_model = self.env["uom.uom"]
for product in self:
price = product.fix_price or product.list_price
if "uom" in self.env.context:
price = product.uom_id._compute_price(
price, uom_model.browse(self.env.context["uom"])
)
if self.env.context.get("uom"):
context_uom = uom_model.browse(self.env.context["uom"])
price = product.uom_id._compute_price(price, context_uom)
product.lst_price = price

def _compute_list_price(self):
uom_model = self.env["uom.uom"]
for product in self:
price = product.fix_price or product.product_tmpl_id.list_price
if "uom" in self.env.context:
price = product.uom_id._compute_price(
price, uom_model.browse(self.env.context["uom"])
)
if self.env.context.get("uom"):
context_uom = uom_model.browse(self.env.context["uom"])
price = product.uom_id._compute_price(price, context_uom)
product.list_price = price

def _inverse_product_lst_price(self):
uom_model = self.env["uom.uom"]
for product in self:
vals = {}
if "uom" in self.env.context:
if self.env.context.get("uom"):
vals["fix_price"] = product.uom_id._compute_price(
product.lst_price, uom_model.browse(self.env.context["uom"])
)
Expand All @@ -63,9 +61,8 @@ def _inverse_product_lst_price(self):
if product.product_variant_count == 1:
product.product_tmpl_id.list_price = vals["fix_price"]
else:
fix_prices = product.product_tmpl_id.mapped(
"product_variant_ids.fix_price"
)
other_products = product.product_tmpl_id.product_variant_ids - product
fix_prices = other_products.mapped("fix_price") + [product.lst_price]
# for consistency with price shown in the shop
product.product_tmpl_id.with_context(
skip_update_fix_price=True
Expand Down
6 changes: 3 additions & 3 deletions product_variant_sale_price/tests/test_product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ def test_post_init_hook(self):
)

# Flush the records to DB before direct SQL
self.product_blue.flush()
self.product_red.flush()
self.product_template.product_variant_ids.flush_model()
self.product_blue.product_template_attribute_value_ids.flush_model()

set_sale_price_on_variant(self.cr, None, self.product_template.id)
self.product_template.product_variant_ids.invalidate_cache()
self.product_template.product_variant_ids.invalidate_recordset()
self.assertEqual(
self.product_template.list_price + 100.00, self.product_blue.lst_price
)
Expand Down
64 changes: 40 additions & 24 deletions product_variant_sale_price/views/product_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,53 @@
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view" />
<field name="arch" type="xml">
<xpath expr="//field[@name='list_price']" position="attributes">
<attribute name="attrs">{'invisible': [
('product_variant_count', '>', 1)
]}</attribute>
</xpath>
<field name="list_price" position="after">
<div
name="fix_price_tooltip"
attrs="{'invisible': ['|', ('product_variant_count', '=', 1), ('is_product_variant', '=', True)]}"
>
<span
class="text-muted fst-italic"
>Setting the price here will update all variants.</span>
</div>
</field>
</field>
</record>
<record id="product_normal_form_view_default_check" model="ir.ui.view">
<record id="product_variant_easy_edit_price_view" model="ir.ui.view">
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_normal_form_view" />
<field name="inherit_id" ref="product.product_variant_easy_edit_view" />
<field name="arch" type="xml">
<xpath expr="//field[@name='uom_id']" position="after">
<field
name="fix_price"
attrs="{'invisible': [('product_variant_count', '=', 1)]}"
/>
</xpath>
<!-- Make the field editable -->
<field name="lst_price" position="attributes">
<attribute name="attrs" />
</field>
</field>
</record>
<record id="product_variant_easy_edit_price_view" model="ir.ui.view">
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_variant_easy_edit_view" />
<!-- Hide price extra fields, as they are no longer useful -->
<record id="product_template_attribute_value_view_tree" model="ir.ui.view">
<field name="name">product.template.attribute.value.tree.hide.extra</field>
<field name="model">product.template.attribute.value</field>
<field
name="inherit_id"
ref="product.product_template_attribute_value_view_tree"
/>
<field name="arch" type="xml">
<field name="price_extra" position="attributes">
<attribute name="invisible">1</attribute>
</field>
</field>
</record>
<record id="product_template_attribute_value_view_form" model="ir.ui.view">
<field name="name">product.template.attribute.value.form.hide.extra</field>
<field name="model">product.template.attribute.value</field>
<field
name="inherit_id"
ref="product.product_template_attribute_value_view_form"
/>
<field name="arch" type="xml">
<xpath expr="//field[@name='standard_price']" position="before">
<field
name="fix_price"
attrs="{'invisible': [('product_variant_count', '=', 1)]}"
widget='monetary'
options="{'currency_field': 'currency_id', 'field_digits': True}"
/>
</xpath>
<field name="price_extra" position="attributes">
<attribute name="invisible">1</attribute>
</field>
</field>
</record>
</odoo>

0 comments on commit 296eff6

Please sign in to comment.