Skip to content

Commit

Permalink
[MIG] product_logistics_uom: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bizzappdev committed May 6, 2024
1 parent 23be033 commit 8cb7c03
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 43 deletions.
2 changes: 1 addition & 1 deletion product_logistics_uom/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Product logistics UoM",
"summary": "Configure product weights and volume UoM",
"version": "16.0.3.0.0",
"version": "17.0.1.0.0",
"development_status": "Beta",
"category": "Product",
"website": "https://github.com/OCA/product-attribute",
Expand Down
38 changes: 19 additions & 19 deletions product_logistics_uom/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@
_logger = logging.getLogger(__name__)


def pre_init_hook(cr): # pragma: nocover
def pre_init_hook(env): # pragma: nocover
"""Recompute the volume and weight column on product and template
by converting the value from the uom defined on the product to the default uom
"""
if sql.column_exists(cr, "product_template", "volume_uom_id"):
if sql.column_exists(env.cr, "product_template", "volume_uom_id"):
_logger.info("Recompute volume on product.product")
# get default m3 uom
cr.execute(
env.cr.execute(
"""
SELECT res_id
FROM ir_model_data
WHERE module = 'uom' AND name = 'product_uom_cubic_meter'
"""
)
m3_uom_id = cr.fetchone()[0]
m3_uom_id = env.cr.fetchone()[0]
# get uom factor
cr.execute(
env.cr.execute(
"""
SELECT factor
FROM uom_uom
WHERE id = %s
""",
(m3_uom_id,),
)
m3_uom_factor = cr.fetchone()[0]
m3_uom_factor = env.cr.fetchone()[0]
# update volume where volume_uom_id is not null and not m3
cr.execute(
env.cr.execute(
"""
UPDATE product_product
SET volume = product_product.volume / product_uom.factor * %s
Expand All @@ -46,9 +46,9 @@ def pre_init_hook(cr): # pragma: nocover
""",
(m3_uom_factor, m3_uom_id),
)
_logger.info(f"{cr.rowcount} product_product rows updated")
_logger.info(f"{env.cr.rowcount} product_product rows updated")
# update product_template with 1 product_product
cr.execute(
env.cr.execute(
"""
UPDATE product_template
SET Volume = unique_product.volume
Expand All @@ -64,30 +64,30 @@ def pre_init_hook(cr): # pragma: nocover
""",
(m3_uom_id,),
)
_logger.info(f"{cr.rowcount} product_template rows updated")
if sql.column_exists(cr, "product_template", "weight_uom_id"):
_logger.info(f"{env.cr.rowcount} product_template rows updated")
if sql.column_exists(env.cr, "product_template", "weight_uom_id"):
_logger.info("Recompute weight on product.product")
# get default kg uom
cr.execute(
env.cr.execute(
"""
SELECT res_id
FROM ir_model_data
WHERE module = 'uom' AND name = 'product_uom_kgm'
"""
)
kg_uom_id = cr.fetchone()[0]
kg_uom_id = env.cr.fetchone()[0]
# get uom factor
cr.execute(
env.cr.execute(
"""
SELECT factor
FROM uom_uom
WHERE id = %s
""",
(kg_uom_id,),
)
kg_uom_factor = cr.fetchone()[0]
kg_uom_factor = env.cr.fetchone()[0]
# update weight where weight_uom_id is not null and not kg
cr.execute(
env.cr.execute(
"""
UPDATE product_product
SET weight = product_product.weight / product_uom.factor * %s
Expand All @@ -98,9 +98,9 @@ def pre_init_hook(cr): # pragma: nocover
""",
(kg_uom_factor, kg_uom_id),
)
_logger.info(f"{cr.rowcount} product_product rows updated")
_logger.info(f"{env.cr.rowcount} product_product rows updated")
# update product_template with 1 product_product
cr.execute(
env.cr.execute(
"""
UPDATE product_template
SET weight = unique_product.weight
Expand All @@ -116,4 +116,4 @@ def pre_init_hook(cr): # pragma: nocover
""",
(kg_uom_id,),
)
_logger.info(f"{cr.rowcount} product_template rows updated")
_logger.info(f"{env.cr.rowcount} product_template rows updated")
10 changes: 0 additions & 10 deletions product_logistics_uom/migrations/16.0.2.0.0/post-migrate.py

This file was deleted.

2 changes: 1 addition & 1 deletion product_logistics_uom/tests/test_product_logistics_uom.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class TestProductLogisticsUom(TransactionCase):
@classmethod
def setUpClass(cls):
super(TestProductLogisticsUom, cls).setUpClass()
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.product = cls.env["product.product"].create(
{
Expand Down
18 changes: 6 additions & 12 deletions product_logistics_uom/views/product.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
<label for="weight" position="after">
<label
for="product_weight"
attrs="{'invisible':[('product_variant_count', '>', 1), ('is_product_variant', '=', False)]}"
invisible="product_variant_count &gt; 1 and not is_product_variant"
/>
</label>
<field name="weight" position="after">
<field name="show_weight_uom_warning" invisible="1" />
<field name="product_weight" string="Weight" class="oe_inline" />
<span attrs="{'invisible':[('show_weight_uom_warning', '=', False)]}">
<span invisible="not show_weight_uom_warning">
<i
class="fa fa-fw fa-warning text-warning"
title="Value too small for the selected uom. Select another uom to display the value."
Expand All @@ -34,10 +34,7 @@
/>
</field>
<field name="weight_uom_name" position="before">
<span
attrs="{'invisible':[('show_weight_uom_warning', '=', False)]}"
groups="!uom.group_uom"
>
<span invisible="not show_weight_uom_warning" groups="!uom.group_uom">
<i
class="fa fa-fw fa-warning text-warning"
title="Value too small for the selected uom. Select another uom to display the value."
Expand All @@ -59,13 +56,13 @@
<label
for="product_volume"
string="Volume"
attrs="{'invisible':[('product_variant_count', '>', 1), ('is_product_variant', '=', False)]}"
invisible="product_variant_count &gt; 1 and not is_product_variant"
/>
</label>
<field name="volume" position="after">
<field name="show_volume_uom_warning" invisible="1" />
<field name="product_volume" class="oe_inline" />
<span attrs="{'invisible':[('show_volume_uom_warning', '=', False)]}">
<span invisible="not show_volume_uom_warning">
<i
class="fa fa-fw fa-warning text-warning"
title="Value too small for the selected uom. Select another uom to display the value."
Expand All @@ -78,10 +75,7 @@
/>
</field>
<field name="volume_uom_name" position="before">
<span
attrs="{'invisible':[('show_volume_uom_warning', '=', False)]}"
groups="!uom.group_uom"
>
<span invisible="not show_volume_uom_warning" groups="!uom.group_uom">
<i
class="fa fa-fw fa-warning text-warning"
title="Value too small for the selected uom. Select another uom to display the value."
Expand Down

0 comments on commit 8cb7c03

Please sign in to comment.