Skip to content

Commit fe16619

Browse files
committed
[IMP] util/inconsistencies.py: Allow auto-fix for UoM inconsistencies
- Improve UoM inconsistencies warning message. The `category_id` is no longer displayed. The UoM and product names are now displayed to help the customer to better understand the changes that need to be done. - Add ODOO_MIG_FIX_ALL_UOM_INCONSISTENCIES environment variable to allow the upgrade to fix the uom inconsistencies using the product template UoM. The value can set to `1` to activate the uom fix. A message is added in the migration reports to inform the customer about the updated rows. - Use ODOO_MIG_DO_NOT_IGNORE_ARCHIVED_PRODUCTS_FOR_UOM_INCONSISTENCIES environment variable to allow the upgrade to list or fix the archived uom inconsistencies. By default, archived product are not used. The value can set to `1` to add archived products. The variable is also used in the `verify_product` function. tbg-71 Improved message to list uom inconsistencies --- ``` There is a UoM mismatch in some Stock Move Lines. The category of the UoM defined on the Stock Move Line is different from that defined on the Product Template and must be the same to avoid errors. We allowed the upgrade to continue, but these inconsistencies may cause error during the upgrade or issues on the upgraded database. To avoid any issue, here are the options to consider: * fix these inconsistencies manually (below, the details of the affected records) * let this script automatically fix the affected records by setting the environment variable ODOO_MIG_FIX_ALL_UOM_INCONSISTENCIES to 1. It will take the UoM from the Product Template and set it on the faulty Stock Move Lines. You can also take the archived products into account for listing or fixing faulty lines by setting the environment variable ODOO_MIG_DO_NOT_IGNORE_ARCHIVED_PRODUCTS_FOR_UOM_INCONSISTENCIES to 1 These Stock Move Lines have UoM inconsistencies: * Stock Move Line(id=46) has UoM `kg`(id=12, category: `Weight`), Product Template `Drawer`(id=24) has UoM `Units`(id=1, category: `Unit`) * Stock Move Line(id=45) has UoM `kg`(id=12, category: `Weight`), Product Template `Drawer`(id=24) has UoM `Units`(id=1, category: `Unit`) ``` New message in the logs to list fixed UoMs --- ``` Upon your request, we have automatically fixed the faulty UoMs by picking it from the Product Template and setting it on the Stock Move Lines. Please, take the time to check that the following Stock Move Lines inconsistencies have been updated to the right UoM: * Stock Move Line(id=46): Updated UoM from `kg`(id=12, category: `Weight`) to `Units`(id=1, category: `Unit`) for Product Template `Drawer`(id=24) * Stock Move Line(id=45): Updated UoM from `kg`(id=12, category: `Weight`) to `Units`(id=1, category: `Unit`) for Product Template `Drawer`(id=24) ``` Part of odoo/upgrade#5207 Signed-off-by: Alvaro Fuentes Suarez (afu) <[email protected]>
1 parent 0c36147 commit fe16619

File tree

1 file changed

+109
-25
lines changed

1 file changed

+109
-25
lines changed

src/util/inconsistencies.py

Lines changed: 109 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from textwrap import dedent
55

66
from psycopg2.extensions import quote_ident
7+
from psycopg2.extras import Json
78

89
from odoo.tools.misc import str2bool
910

@@ -18,6 +19,7 @@
1819
os.environ.get("ODOO_MIG_DO_NOT_IGNORE_ARCHIVED_PRODUCTS_FOR_UOM_INCONSISTENCIES"),
1920
default=False,
2021
)
22+
FIX_PRODUCT_UOM = str2bool(os.environ.get("ODOO_MIG_FIX_ALL_UOM_INCONSISTENCIES"), default=False)
2123

2224

2325
def verify_companies(
@@ -122,30 +124,43 @@ def verify_uoms(cr, model, uom_field="product_uom_id", product_field="product_id
122124
table = table_of_model(cr, model)
123125

124126
q = lambda s: quote_ident(s, cr._cnx)
127+
125128
query = """
126-
SELECT t.id,
127-
t.{uom_column},
128-
tu.category_id,
129-
pt.uom_id,
130-
ptu.category_id
131-
FROM {table} t
132-
JOIN uom_uom tu ON t.{uom_column} = tu.id
133-
JOIN product_product pp ON t.{product_column} = pp.id
134-
JOIN product_template pt ON pp.product_tmpl_id = pt.id
135-
JOIN uom_uom ptu ON pt.uom_id = ptu.id
136-
WHERE tu.category_id != ptu.category_id
129+
SELECT t.id line_id,
130+
t.{uom_column} line_uom_id,
131+
tu.{uom_name} line_uom_name,
132+
tuc.{category_name} line_uom_categ_name,
133+
pt.uom_id product_uom_id,
134+
ptu.{uom_name} product_uom_name,
135+
pt.id product_template_id,
136+
pt.{product_template_name} product_template_name,
137+
ptuc.{category_name} product_uom_categ_name
138+
FROM {table} t
139+
JOIN uom_uom tu ON t.{uom_column} = tu.id
140+
JOIN uom_category tuc ON tu.category_id = tuc.id
141+
JOIN product_product pp ON t.{product_column} = pp.id
142+
JOIN product_template pt ON pp.product_tmpl_id = pt.id
143+
JOIN uom_uom ptu ON pt.uom_id = ptu.id
144+
JOIN uom_category ptuc ON ptu.category_id = ptuc.id
145+
WHERE tu.category_id != ptu.category_id
146+
{ids}
147+
{active}
137148
""".format(
138149
table=q(table),
139150
uom_column=q(uom_field),
140151
product_column=q(product_field),
152+
uom_name=get_value_or_en_translation(cr, "uom_uom", "name"),
153+
category_name=get_value_or_en_translation(cr, "uom_category", "name"),
154+
product_template_name=get_value_or_en_translation(cr, "product_template", "name"),
155+
ids=" AND t.id IN %s" if ids else "",
156+
active=" AND pp.active" if INCLUDE_ARCHIVED_PRODUCTS else "",
141157
)
142158

143159
rows = []
144160
if ids is None:
145161
cr.execute(query)
146162
rows = cr.fetchall()
147163
elif ids:
148-
query += " AND t.id IN %s"
149164
ids_chunks = chunks(ids, size=cr.IN_MAX, fmt=tuple)
150165
for chunk in ids_chunks:
151166
cr.execute(query, [chunk])
@@ -155,24 +170,93 @@ def verify_uoms(cr, model, uom_field="product_uom_id", product_field="product_id
155170
return []
156171

157172
title = model.replace(".", " ").title()
158-
msg = dedent(
173+
174+
if FIX_PRODUCT_UOM:
175+
line_new_ids = {line_id: prod_uom_id for line_id, _, _, _, prod_uom_id, _, _, _, _ in rows}
176+
cr.execute(
177+
"""
178+
UPDATE {table} t
179+
SET {uom_column} = (%s::jsonb->t.id::text)::int
180+
WHERE t.id IN %s
181+
""".format(
182+
table=q(table),
183+
uom_column=q(uom_field),
184+
),
185+
[
186+
Json(line_new_ids),
187+
tuple(line_new_ids),
188+
],
189+
)
190+
191+
msg = dedent(
192+
"""
193+
Upon your request, we have automatically fixed the faulty UoMs by picking it from
194+
the Product Template and setting it on the {title}s.
195+
196+
Please, take the time to check that the following {title}s inconsistencies have
197+
been updated to the right UoM:\n\n{updated_uoms}
159198
"""
199+
).format(
200+
title=title,
201+
updated_uoms="\n".join(
202+
" * {}(id={}): Updated UoM from `{}`(id={}, category: `{}`) to `{}`(id={}, category: `{}`) for Product Template `{}`(id={})".format(
203+
title,
204+
line_id,
205+
line_uom,
206+
line_uom_id,
207+
line_uom_categ,
208+
prod_uom,
209+
prod_uom_id,
210+
prod_uom_categ,
211+
prod_temp,
212+
prod_temp_id,
213+
)
214+
for line_id, line_uom_id, line_uom, line_uom_categ, prod_uom_id, prod_uom, prod_temp_id, prod_temp, prod_uom_categ in rows
215+
),
216+
)
217+
faulty_ids = []
218+
219+
else:
220+
msg = """
160221
There is a UoM mismatch in some {title}s. The category of the UoM defined on the
161-
{title} is different from that defined on the Product Template. To allow the upgrade to
162-
continue, the UoM categories on the {title} and on the Product Template must be the same.
163-
These {title}s have inconsistencies:
222+
{title} is different from that defined on the Product Template and must be the same to
223+
avoid errors. We allowed the upgrade to continue, but these inconsistencies may cause error
224+
during the upgrade or issues on the upgraded database.
225+
226+
To avoid any issue, here are the options to consider:
227+
228+
* fix these inconsistencies manually (below, the details of the affected records)
229+
* let this script automatically fix the affected records by setting the environment variable
230+
ODOO_MIG_FIX_ALL_UOM_INCONSISTENCIES to 1. It will take the UoM from the Product Template
231+
and set it on the faulty {title}s.
232+
233+
You can also take the archived products into account for listing or fixing faulty lines by setting the
234+
environment variable ODOO_MIG_DO_NOT_IGNORE_ARCHIVED_PRODUCTS_FOR_UOM_INCONSISTENCIES to 1
235+
236+
These {title}s have UoM inconsistencies:\n\n{uom_inconsistencies}
164237
""".format(
165-
**locals()
166-
)
167-
)
168-
msg += "\n".join(
169-
" * {}(id={}) has UoM(id={},category={}), Product Template has UoM(id={},category={})".format(
170-
title, line_id, line_uom, line_uom_category, product_uom, product_uom_category
238+
title=title,
239+
uom_inconsistencies="\n".join(
240+
" * {}(id={}) has UoM `{}`(id={}, category: `{}`), Product Template `{}`(id={}) has UoM `{}`(id={}, category: `{}`)".format(
241+
title,
242+
line_id,
243+
line_uom,
244+
line_uom_id,
245+
line_uom_categ,
246+
prod_temp,
247+
prod_temp_id,
248+
prod_uom,
249+
prod_uom_id,
250+
prod_uom_categ,
251+
)
252+
for line_id, line_uom_id, line_uom, line_uom_categ, prod_uom_id, prod_uom, prod_temp_id, prod_temp, prod_uom_categ in rows
253+
),
171254
)
172-
for line_id, line_uom, line_uom_category, product_uom, product_uom_category in rows
173-
)
255+
faulty_ids = [r[0] for r in rows]
256+
174257
_logger.warning("\n%s\n", msg)
175-
return [r[0] for r in rows]
258+
add_to_migration_reports(category=title + " UoM Inconsistencies", message=msg, format="md")
259+
return faulty_ids
176260

177261

178262
def verify_products(

0 commit comments

Comments
 (0)