Skip to content

Commit

Permalink
Merge pull request #2853 from ForgeFlow/14.0-mig-mrp
Browse files Browse the repository at this point in the history
[14.0][OU-ADD] mrp: Migration scripts
  • Loading branch information
pedrobaeza authored Mar 27, 2022
2 parents 0a19d18 + a09e412 commit 8bb4674
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docsource/modules130-140.rst
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ Module coverage 13.0 -> 14.0
+--------------------------------------------+-------------------------------------------------+
| |new| microsoft_calendar | |
+--------------------------------------------+-------------------------------------------------+
|mrp | |
|mrp | Done |
+--------------------------------------------+-------------------------------------------------+
|mrp_account | |
+--------------------------------------------+-------------------------------------------------+
Expand Down
4 changes: 2 additions & 2 deletions openupgrade_scripts/scripts/mrp/14.0.2.0/noupdate_changes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version='1.0' encoding='utf-8'?>
<odoo>
<record id="mrp_bom_byproduct_rule" model="ir.rule">
<!-- <record id="mrp_bom_byproduct_rule" model="ir.rule">
<field name="global"/>
</record>
<record id="mrp_bom_line_rule" model="ir.rule">
Expand All @@ -26,5 +26,5 @@
</record>
<record id="mrp_workorder_rule" model="ir.rule">
<field name="global"/>
</record>
</record> -->
</odoo>
94 changes: 94 additions & 0 deletions openupgrade_scripts/scripts/mrp/14.0.2.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Copyright 2021 ForgeFlow S.L. <https://www.forgeflow.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade


def merge_priorities(env):
openupgrade.map_values(
env.cr,
openupgrade.get_legacy_name("priority"),
"priority",
[("1", "0"), ("2", "1"), ("3", "1")],
table="mrp_production",
)


def map_stock_move_line_lot_produced_ids(env):
openupgrade.logged_query(
env.cr,
"""
UPDATE stock_move_line sml
SET lot_id = rel.stock_production_lot_id
FROM stock_move_line_stock_production_lot_rel rel
JOIN stock_move_line_consume_rel rel2
ON rel2.produce_line_id = rel.stock_move_line_id
WHERE sml.lot_id IS NULL AND rel2.consume_line_id = sml.id""",
)
openupgrade.logged_query(
env.cr,
"""
INSERT INTO stock_move_line_consume_rel (
produce_line_id, consume_line_id)
SELECT rel.stock_move_line_id, sml.id
FROM stock_move_line_stock_production_lot_rel rel
JOIN stock_move_line sml ON (
sml.lot_id = rel.stock_production_lot_id
AND rel.stock_move_line_id != sml.id)
LEFT JOIN stock_move_line_consume_rel rel2 ON (
rel2.produce_line_id = rel.stock_move_line_id
AND rel2.consume_line_id = sml.id)
WHERE rel2.produce_line_id IS NULL""",
)


def map_mrp_production_state_planned(env):
env["mrp.production"].search([("state", "=", "planned")])._compute_state()


def generate_workorders_for_draft_orders(env):
"""Recreate workorders for draft production orders, as in v13, this is only done
later when planning production.
"""
env.cr.execute(
"SELECT id, routing_id FROM mrp_production "
"WHERE state = 'draft' AND routing_id IS NOT NULL"
)
for production_id, routing_id in env.cr.fetchall():
production = env["mrp.production"].browse(production_id)
workorders_values = []
env.cr.execute(
"""
SELECT mrw.id, mrw.name, mrw.workcenter_id
FROM mrp_routing_workcenter mrw
WHERE mrw.routing_id = %s AND mrw.bom_id = %s
""",
(routing_id, production.bom_id.id),
)
# TODO: If there are phantom BoMs with other routings, it won't be covered
for operation_id, operation_name, workcenter_id in env.cr.fetchall():
workorders_values += [
{
"name": operation_name,
"production_id": production_id,
"workcenter_id": workcenter_id,
"product_uom_id": production.product_uom_id.id,
"operation_id": operation_id,
"state": "pending",
"consumption": production.consumption,
}
]
workorders = production.workorder_ids.create(workorders_values)
for workorder in workorders:
workorder.duration_expected = workorder._get_duration_expected()


@openupgrade.migrate()
def migrate(env, version):
merge_priorities(env)
map_stock_move_line_lot_produced_ids(env)
map_mrp_production_state_planned(env)
generate_workorders_for_draft_orders(env)
openupgrade.load_data(env.cr, "mrp", "14.0.2.0/noupdate_changes.xml")
openupgrade.delete_records_safely_by_xml_id(
env, ["mrp.mrp_routing_rule", "mrp.sequence_mrp_route"]
)
135 changes: 135 additions & 0 deletions openupgrade_scripts/scripts/mrp/14.0.2.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Copyright 2021 ForgeFlow S.L. <https://www.forgeflow.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade

_column_copies = {
"mrp_production": [
("priority", None, None),
("state", None, None),
],
}


def switch_flexible_consumption_to_warning(env):
"""For benefiting from this new feature."""
openupgrade.logged_query(
env.cr,
"UPDATE mrp_bom SET consumption = 'warning' WHERE consumption='flexible'",
)
openupgrade.logged_query(
env.cr,
"UPDATE mrp_workorder SET consumption = 'warning' WHERE consumption='flexible'",
)


def fill_mrp_production__consumption(env):
openupgrade.logged_query(
env.cr, "ALTER TABLE mrp_production ADD consumption VARCHAR"
)
openupgrade.logged_query(
env.cr,
"""UPDATE mrp_production mp
SET consumption = mb.consumption
FROM mrp_bom mb
WHERE mb.id = mp.bom_id
""",
)


def fill_mrp_routing_workcenter_bom_id(env):
openupgrade.logged_query(
env.cr,
"""
ALTER TABLE mrp_routing_workcenter
ADD COLUMN bom_id integer""",
)
openupgrade.logged_query(
env.cr,
"""
ALTER TABLE mrp_routing_workcenter
ADD COLUMN old_routing_workcenter_id integer""",
)
openupgrade.logged_query(
env.cr,
"""
ALTER TABLE mrp_routing_workcenter
ALTER COLUMN routing_id DROP NOT NULL""",
)
openupgrade.logged_query(
env.cr,
"""
ALTER TABLE mrp_routing_workcenter
ALTER COLUMN batch DROP NOT NULL""",
)
openupgrade.logged_query(
env.cr,
"""
INSERT INTO mrp_routing_workcenter (name, workcenter_id, sequence,
company_id, worksheet_type, note, worksheet_google_slide, time_mode,
time_mode_batch, time_cycle_manual, create_uid, write_uid,
create_date, write_date, bom_id, routing_id,
old_routing_workcenter_id)
SELECT mrw.name, mrw.workcenter_id, mrw.sequence, mrw.company_id,
mrw.worksheet_type, mrw.note, mrw.worksheet_google_slide,
mrw.time_mode, mrw.time_mode_batch, mrw.time_cycle_manual,
mrw.create_uid, mb.write_uid, mb.create_date, mb.create_date,
mb.id, mrw.routing_id, mrw.id
FROM mrp_routing_workcenter mrw
JOIN mrp_bom mb ON mb.routing_id = mrw.routing_id""",
)
openupgrade.logged_query(
env.cr,
"""
UPDATE mrp_bom_line mbl
SET operation_id = mrw.id
FROM mrp_bom mb
JOIN mrp_routing_workcenter mrw ON mrw.bom_id = mb.id
JOIN mrp_routing_workcenter mrw2 ON mrw.old_routing_workcenter_id = mrw2.id
WHERE mbl.bom_id = mb.id AND mbl.operation_id = mrw2.id""",
)
openupgrade.logged_query(
env.cr,
"""
UPDATE mrp_bom_byproduct mbp
SET operation_id = mrw.id
FROM mrp_bom mb
JOIN mrp_routing_workcenter mrw ON mrw.bom_id = mb.id
JOIN mrp_routing_workcenter mrw2 ON mrw.old_routing_workcenter_id = mrw2.id
WHERE mbp.bom_id = mb.id AND mbp.operation_id = mrw2.id""",
)
openupgrade.logged_query(
env.cr,
"""
UPDATE mrp_workorder mw
SET operation_id = mrw.id
FROM mrp_production mp
JOIN mrp_bom mb ON mp.bom_id = mb.id
JOIN mrp_routing_workcenter mrw ON mrw.bom_id = mb.id
JOIN mrp_routing_workcenter mrw2 ON mrw.old_routing_workcenter_id = mrw2.id
WHERE mw.production_id = mp.id AND mw.operation_id = mrw2.id""",
)
openupgrade.logged_query(
env.cr,
"""
UPDATE stock_move sm
SET operation_id = mrw.id
FROM mrp_production mp
JOIN mrp_bom mb ON mp.bom_id = mb.id
JOIN mrp_routing_workcenter mrw ON mrw.bom_id = mb.id
JOIN mrp_routing_workcenter mrw2 ON mrw.old_routing_workcenter_id = mrw2.id
WHERE sm.raw_material_production_id = mp.id AND sm.operation_id = mrw2.id""",
)
openupgrade.logged_query(
env.cr,
"""
DELETE FROM mrp_routing_workcenter
WHERE old_routing_workcenter_id IS NULL""",
)


@openupgrade.migrate()
def migrate(env, version):
openupgrade.copy_columns(env.cr, _column_copies)
switch_flexible_consumption_to_warning(env)
fill_mrp_production__consumption(env)
fill_mrp_routing_workcenter_bom_id(env)
Loading

0 comments on commit 8bb4674

Please sign in to comment.