Skip to content

Commit 41e2a9d

Browse files
committed
[FIX] purchase_duplicate_check: _compute_pending_order_ids method is updated
1 parent 1dda2e5 commit 41e2a9d

File tree

1 file changed

+47
-19
lines changed

1 file changed

+47
-19
lines changed

purchase_duplicate_check/models/purchase_order_line.py

+47-19
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,53 @@ class PurchaseOrderLine(models.Model):
1111
)
1212

1313
def _compute_pending_order_ids(self):
14-
purchase_order_obj = self.env["purchase.order"]
15-
for rec in self:
16-
if rec.product_type != "product":
17-
rec.pending_order_ids = False
18-
continue
19-
rfq_orders = purchase_order_obj.search(
20-
[
21-
("order_line.product_id", "=", rec.product_id.id),
22-
("id", "!=", rec.order_id._origin.id),
23-
"|",
24-
("state", "in", ["draft", "sent"]),
25-
"&",
26-
"&",
27-
("state", "not in", ["draft", "sent"]),
28-
("picking_ids.picking_type_id.code", "=", "incoming"),
29-
("picking_ids.state", "not in", ["done", "cancel"]),
30-
]
31-
)
32-
rec.pending_order_ids = rfq_orders
14+
product_lines = self.filtered(lambda rec: rec.product_type == "product")
15+
other_lines = self - product_lines
16+
17+
if other_lines:
18+
other_lines.pending_order_ids = False
19+
20+
if not product_lines:
21+
return
22+
23+
product_ids = tuple(product_lines.mapped("product_id.id"))
24+
order_ids = tuple(product_lines.mapped("order_id.id"))
25+
26+
if not product_ids:
27+
product_lines.pending_order_ids = False
28+
return
29+
query = """
30+
SELECT po.id, pol.product_id
31+
FROM purchase_order po
32+
JOIN purchase_order_line pol ON pol.order_id = po.id
33+
LEFT JOIN stock_move sm ON sm.purchase_line_id = pol.id
34+
LEFT JOIN stock_picking sp ON sp.id = sm.picking_id
35+
36+
WHERE pol.product_id IN %s
37+
AND po.id NOT IN %s
38+
AND (
39+
po.state IN ('draft', 'sent')
40+
OR (
41+
po.state NOT IN ('draft', 'sent')
42+
AND sp.picking_type_id IN (
43+
SELECT id FROM stock_picking_type WHERE code = 'incoming'
44+
)
45+
AND sp.state NOT IN ('done', 'cancel')
46+
)
47+
)
48+
"""
49+
self.env.cr.execute(query, (product_ids, order_ids))
50+
result = self.env.cr.fetchall()
51+
product_orders_map = {}
52+
for order_id, product_id in result:
53+
if product_id not in product_orders_map:
54+
product_orders_map[product_id] = []
55+
product_orders_map[product_id].append(order_id)
56+
57+
for rec in product_lines:
58+
rec.pending_order_ids = [
59+
(6, 0, product_orders_map.get(rec.product_id.id, []))
60+
]
3361

3462
def _get_order_confirm_message(self):
3563
"""Get order confirmation message for pending orders"""

0 commit comments

Comments
 (0)