Skip to content

Commit 087c253

Browse files
[IMP] stock_account: add account_move_id information
1 parent 4052e56 commit 087c253

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

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

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def _prepare_common_svl_vals(move, product):
3333
"remaining_value": 0.0,
3434
"quantity": 0.0,
3535
"old_product_price_history_id": None,
36+
"account_move_id": move["account_move_id"],
3637
}
3738

3839

@@ -82,24 +83,45 @@ def _prepare_man_svl_vals(price_history_rec, previous_price, quantity, company,
8283
"remaining_value": 0.0,
8384
"quantity": 0.0,
8485
"old_product_price_history_id": price_history_rec["id"],
86+
"account_move_id": price_history_rec["account_move_id"],
8587
}
8688
return svl_vals
8789

8890

8991
def get_product_price_history(env, company_id, product_id):
9092
env.cr.execute("""
91-
SELECT id, company_id, product_id, datetime, cost, create_uid, write_uid, write_date
92-
FROM product_price_history
93-
WHERE company_id = %s AND product_id = %s
94-
ORDER BY datetime, id
93+
WITH account_move_rel AS (
94+
SELECT id, create_date
95+
FROM (
96+
SELECT id, create_date, COUNT(*) OVER(PARTITION BY create_date) AS qty
97+
FROM account_move
98+
WHERE stock_move_id IS NULL
99+
) foo
100+
WHERE qty = 1
101+
)
102+
SELECT pph.id, pph.company_id, pph.product_id, pph.datetime, pph.cost, rel.id AS account_move_id,
103+
pph.create_uid, pph.create_date, pph.write_uid, pph.write_date
104+
FROM product_price_history pph
105+
LEFT JOIN account_move_rel rel ON rel.create_date = pph.create_date
106+
WHERE pph.company_id = %s AND pph.product_id = %s
107+
ORDER BY pph.datetime, pph.id
95108
""", (company_id, product_id))
96109
return env.cr.dictfetchall()
97110

98111

99112
def get_stock_moves(env, company_id, product_id):
100113
env.cr.execute("""
114+
WITH account_move_rel AS (
115+
SELECT id, stock_move_id
116+
FROM (
117+
SELECT id, stock_move_id, COUNT(*) OVER(PARTITION BY stock_move_id) AS qty
118+
FROM account_move
119+
WHERE stock_move_id IS NOT NULL
120+
) foo
121+
WHERE qty = 1
122+
)
101123
SELECT sm.id, sm.company_id, sm.product_id, sm.date, sm.product_qty, sm.reference,
102-
COALESCE(sm.price_unit, 0.0) AS price_unit,
124+
COALESCE(sm.price_unit, 0.0) AS price_unit, rel.id AS account_move_id,
103125
sm.create_uid, sm.create_date, sm.write_uid, sm.write_date,
104126
CASE WHEN (sl.usage <> 'internal' AND (sl.usage <> 'transit' OR sl.company_id <> sm.company_id))
105127
AND (sld.usage = 'internal' OR (sld.usage = 'transit' AND sld.company_id = sm.company_id))
@@ -111,10 +133,11 @@ def get_stock_moves(env, company_id, product_id):
111133
WHEN sl.usage = 'customer' AND sld.usage = 'supplier' THEN 'dropship_return'
112134
ELSE 'other'
113135
END AS move_type
114-
FROM stock_move sm LEFT JOIN stock_location sl ON sl.id = sm.location_id
115-
LEFT JOIN stock_location sld ON sld.id = sm.location_dest_id
116-
WHERE sm.company_id = %s AND sm.product_id = %s
117-
AND state = 'done'
136+
FROM stock_move sm
137+
LEFT JOIN stock_location sl ON sl.id = sm.location_id
138+
LEFT JOIN stock_location sld ON sld.id = sm.location_dest_id
139+
LEFT JOIN account_move_rel rel ON rel.stock_move_id = sm.id
140+
WHERE sm.company_id = %s AND sm.product_id = %s AND state = 'done'
118141
ORDER BY sm.date, sm.id
119142
""", (company_id, product_id))
120143
return env.cr.dictfetchall()

0 commit comments

Comments
 (0)