Skip to content

Commit d40c41f

Browse files
kos94ok-3DMiquelRForgeFlow
authored andcommitted
[MIG] stock_landed_costs: generate stock valuation layers
1 parent 26cd60a commit d40c41f

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright 2020 ForgeFlow <http://www.forgeflow.com>
2+
# Copyright 2021 Andrii Skrypka
23
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
34
from openupgradelib import openupgrade
45

@@ -15,9 +16,52 @@ def fill_account_move_line_is_landed_costs_line(env):
1516
)
1617

1718

19+
def _generate_stock_valuation_layer(env):
20+
openupgrade.logged_query(
21+
env.cr, """
22+
INSERT INTO stock_valuation_layer (value, unit_cost, quantity, remaining_qty,
23+
stock_valuation_layer_id, description, stock_move_id, product_id,
24+
stock_landed_cost_id, company_id, account_move_id, create_date, create_uid,
25+
write_date, write_uid)
26+
SELECT sval.additional_landed_cost, 0.0, 0.0, 0.0,
27+
svl.id, slc.name, sm.id, sm.product_id,
28+
slc.id, am.company_id, am.id, am.create_date, am.create_uid,
29+
am.write_date, am.write_uid
30+
FROM stock_landed_cost slc
31+
JOIN account_move am ON am.id = slc.account_move_id
32+
JOIN stock_valuation_adjustment_lines sval ON sval.cost_id = slc.id
33+
JOIN stock_move sm ON sm.id = sval.move_id
34+
LEFT JOIN (
35+
SELECT MIN(id) as id, stock_move_id
36+
FROM stock_valuation_layer
37+
GROUP BY stock_move_id
38+
) svl ON svl.stock_move_id = sval.move_id
39+
WHERE slc.state = 'done'
40+
""",
41+
)
42+
43+
44+
def _fix_value_for_svl(env):
45+
openupgrade.logged_query(
46+
env.cr, """
47+
UPDATE stock_valuation_layer svl
48+
SET value = svl.value - svl_value.diff
49+
FROM (
50+
SELECT stock_valuation_layer_id as id, SUM(value) as diff
51+
FROM stock_valuation_layer
52+
WHERE stock_valuation_layer_id IS NOT NULL
53+
GROUP BY stock_valuation_layer_id
54+
) svl_value
55+
WHERE svl_value.id = svl.id
56+
""",
57+
)
58+
59+
1860
@openupgrade.migrate()
1961
def migrate(env, version):
2062
fill_account_move_line_is_landed_costs_line(env)
2163
openupgrade.load_data(
2264
env.cr, "stock_landed_costs",
2365
"migrations/13.0.1.1/noupdate_changes.xml")
66+
_generate_stock_valuation_layer(env)
67+
_fix_value_for_svl(env)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2020 ForgeFlow <http://www.forgeflow.com>
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
3+
from openupgradelib import openupgrade
4+
5+
6+
def fix_price_unit_of_stock_move(env):
7+
# in v13 price_unit of stock.move exclude landed_cost_value
8+
openupgrade.logged_query(
9+
env.cr, """
10+
UPDATE stock_move
11+
SET price_unit = (value - landed_cost_value) / product_qty
12+
WHERE landed_cost_value != 0.0
13+
""",
14+
)
15+
16+
17+
@openupgrade.migrate()
18+
def migrate(env, version):
19+
fix_price_unit_of_stock_move(env)

0 commit comments

Comments
 (0)