Skip to content

Commit ef58eba

Browse files
authored
Merge pull request #2379 from ForgeFlow/13.0-mig-stock_dropshipping-script
[13.0][MIG] stock_dropshipping
2 parents dee8235 + 7be23b7 commit ef58eba

File tree

5 files changed

+168
-13
lines changed

5 files changed

+168
-13
lines changed

addons/stock/migrations/13.0.1.1/post-migration.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Copyright 2020 Payam Yasaie <https://www.tashilgostar.com>
22
# Copyright 2020 ForgeFlow <https://www.forgeflow.com>
3+
# Copyright 2021 Tecnativa - Pedro M. Baeza
34
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
45
from openupgradelib import openupgrade
6+
import re
57

68

79
def _get_main_company(cr):
@@ -184,18 +186,16 @@ def map_stock_location_usage(env):
184186

185187

186188
def fill_stock_picking_type_sequence_code(env):
187-
openupgrade.logged_query(
188-
env.cr, """
189-
UPDATE stock_picking_type
190-
SET sequence_code = CASE WHEN code = 'incoming' THEN 'IN'
191-
WHEN code = 'outgoing' THEN 'OUT'
192-
WHEN code = 'internal' AND barcode like '%-PACK' THEN 'PACK'
193-
WHEN code = 'internal' AND barcode like '%-PICK' THEN 'PICK'
194-
WHEN code = 'internal' AND barcode like '%-INTERNAL' THEN 'INT'
195-
ELSE 'TO_FILL' END
196-
WHERE sequence_code IS NULL
197-
"""
198-
)
189+
"""Deduce sequence code from current sequence pattern """
190+
picking_types = env["stock.picking.type"].with_context(active_text=False).search([])
191+
for picking_type in picking_types:
192+
prefix = picking_type.sequence_id.prefix
193+
if picking_type.warehouse_id:
194+
groups = re.findall(r"(.*)\/(.*)\/", prefix)
195+
if groups and len(groups[0]) == 2:
196+
picking_type.sequence_code = groups[0][1]
197+
else:
198+
picking_type.sequence_code = prefix
199199

200200

201201
def convert_many2one_stock_inventory_product_and_location(env):

addons/stock/migrations/13.0.1.1/pre-migration.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ def fill_inventory_line_categ(env):
7373
)
7474

7575

76+
def prefill_stock_picking_type_sequence_code(env):
77+
"""Prefill this field for avoiding the not null warning during update."""
78+
openupgrade.logged_query(
79+
env.cr, "ALTER TABLE stock_picking_type ADD sequence_code VARCHAR"
80+
)
81+
openupgrade.logged_query(
82+
env.cr, "UPDATE stock_picking_type SET sequence_code = '-'"
83+
)
84+
85+
7686
@openupgrade.migrate()
7787
def migrate(env, version):
7888
openupgrade.copy_columns(env.cr, _column_copies)
@@ -83,3 +93,4 @@ def migrate(env, version):
8393
openupgrade.rename_xmlids(env.cr, _xmlid_renames)
8494
assure_stock_rule_company_is_correct(env)
8595
fill_inventory_line_categ(env)
96+
prefill_stock_picking_type_sequence_code(env)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---Models in module 'stock_dropshipping'---
2+
---Fields in module 'stock_dropshipping'---
3+
---XML records in module 'stock_dropshipping'---
4+
DEL ir.sequence: stock_dropshipping.seq_picking_type_dropship (noupdate)
5+
DEL stock.picking.type: stock_dropshipping.picking_type_dropship (noupdate)
6+
DEL stock.rule: stock_dropshipping.stock_rule_drop_shipping (noupdate)
7+
# DONE: post-migration: create new records with dropshipping_functions
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Copyright 2020 ForgeFlow <http://www.forgeflow.com>
2+
# Copyright 2021 Tecnativa - Pedro M. Baeza
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
4+
from openupgradelib import openupgrade
5+
from openupgradelib import openupgrade_merge_records
6+
7+
8+
def run_dropshipping_functions(env):
9+
companies = env["res.company"].with_context(active_test=False).search([], order="id")
10+
mapping = {company.id: {} for company in companies}
11+
# Duplicate/create/adjust sequences
12+
sequence = env.ref('stock_dropshipping.seq_picking_type_dropship', False)
13+
if sequence and sequence.exists():
14+
for i, company in enumerate(companies):
15+
vals = {
16+
"code": "stock.dropshipping",
17+
"name": "Dropship (%s)" % company.name,
18+
"company_id": company.id,
19+
}
20+
if i == 0:
21+
sequence.write(vals)
22+
new_sequence = sequence
23+
else:
24+
new_sequence = sequence.copy(vals)
25+
mapping[company.id]["sequence"] = new_sequence
26+
env['res.company'].create_missing_dropship_sequence()
27+
for company in companies:
28+
# Look for missing mapped sequences
29+
if not mapping[company.id].get("sequence"):
30+
sequence = env['ir.sequence'].search([
31+
('code', '=', 'stock.dropshipping'),
32+
('company_id', '=', company.id),
33+
])
34+
mapping[company.id]["sequence"] = sequence
35+
# Duplicate/create/adjust picking types
36+
picking_type = env.ref('stock_dropshipping.picking_type_dropship', False)
37+
if picking_type and picking_type.exists():
38+
for i, company in enumerate(companies):
39+
vals = {
40+
"sequence_id": mapping[company.id]["sequence"].id,
41+
"company_id": company.id,
42+
}
43+
if picking_type.default_location_dest_id.usage != "customer":
44+
vals["default_location_dest_id"] = env.ref('stock.stock_location_customers').id
45+
if picking_type.default_location_src_id.usage != "supplier":
46+
vals["default_location_src_id"] = env.ref('stock.stock_location_suppliers').id
47+
if i == 0:
48+
picking_type.write(vals)
49+
new_picking_type = picking_type
50+
else:
51+
new_picking_type = picking_type.copy(vals)
52+
mapping[company.id]["picking_type"] = new_picking_type
53+
for company in companies:
54+
# Look for missing mapped picking types
55+
if not mapping[company.id].get("picking_type"):
56+
dropship_picking_type = env['stock.picking.type'].search([
57+
('company_id', '=', company.id),
58+
('default_location_src_id.usage', '=', 'supplier'),
59+
('default_location_dest_id.usage', '=', 'customer'),
60+
], limit=1, order='sequence')
61+
mapping[company.id]["picking_type"] = dropship_picking_type
62+
env['res.company'].create_missing_dropship_picking_type()
63+
# Duplicate/create/adjust stock rules
64+
stock_rule = env.ref('stock_dropshipping.stock_rule_drop_shipping', False)
65+
dropship_route = env.ref('stock_dropshipping.route_drop_shipping')
66+
if stock_rule and stock_rule.exists():
67+
for i, company in enumerate(companies):
68+
supplier_location = stock_rule.location_src_id
69+
customer_location = stock_rule.location_id
70+
if supplier_location.usage != "supplier":
71+
supplier_location = env.ref('stock.stock_location_suppliers').id
72+
if customer_location.usage != "customer":
73+
customer_location = env.ref('stock.stock_location_customers').id
74+
dropship_picking_type = mapping[company.id]["picking_type"]
75+
vals = ({
76+
"route_id": dropship_route.id,
77+
"picking_type_id": dropship_picking_type.id,
78+
"company_id": company.id,
79+
"location_src_id": supplier_location.id,
80+
"location_id": customer_location.id,
81+
"name": "%s → %s" % (supplier_location.name, customer_location.name),
82+
})
83+
if i == 0:
84+
stock_rule.write(vals)
85+
new_stock_rule = stock_rule
86+
else:
87+
new_stock_rule = stock_rule.copy(vals)
88+
mapping[company.id]["stock_rule"] = new_stock_rule
89+
env['res.company'].create_missing_dropship_rule()
90+
for company in companies:
91+
# Look for missing mapped stock rules
92+
if not mapping[company.id].get("stock_rule"):
93+
stock_rule = env["stock.rule"].search([
94+
("route_id", "=", dropship_route.id),
95+
("company_id", "=", company.id),
96+
])
97+
mapping[company.id]["stock_rule"] = stock_rule
98+
# Remove old XML-IDs
99+
env["ir.model.data"].search([
100+
("module", "=", "stock_dropshipping"),
101+
("name", "in", ["seq_picking_type_dropship",
102+
"picking_type_dropship", "stock_rule_drop_shipping"]),
103+
]).unlink()
104+
# now, we redirect referenced records to new records if needed company-wise
105+
for company in companies[1:]:
106+
openupgrade_merge_records._change_foreign_key_refs(
107+
env,
108+
"ir.sequence",
109+
sequence.ids,
110+
mapping[company.id]["sequence"].id,
111+
[],
112+
"ir_sequence",
113+
extra_where=" AND company_id = %s" % company.id,
114+
)
115+
openupgrade_merge_records._change_foreign_key_refs(
116+
env,
117+
"stock.picking.type",
118+
picking_type.ids,
119+
mapping[company.id]["picking_type"].id,
120+
[],
121+
"stock_picking_type",
122+
extra_where=" AND company_id = %s" % company.id,
123+
)
124+
openupgrade_merge_records._change_foreign_key_refs(
125+
env,
126+
"stock.rule",
127+
stock_rule.ids,
128+
mapping[company.id]["stock_rule"].id,
129+
[],
130+
"stock_rule",
131+
extra_where=" AND company_id = %s" % company.id,
132+
)
133+
134+
135+
@openupgrade.migrate()
136+
def migrate(env, version):
137+
run_dropshipping_functions(env)

odoo/openupgrade/doc/source/modules120-130.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ missing in the new release are marked with |del|.
599599
+----------------------------------------------+-------------------------------------------------+
600600
|stock_account | Done |
601601
+----------------------------------------------+-------------------------------------------------+
602-
|stock_dropshipping | |
602+
|stock_dropshipping | Done |
603603
+----------------------------------------------+-------------------------------------------------+
604604
|stock_landed_costs | |
605605
+----------------------------------------------+-------------------------------------------------+

0 commit comments

Comments
 (0)