Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][IMP] stock_request_purchase: Cancel stock request when unlinking related purchase order lines #2152

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions stock_request_purchase/models/purchase_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ class PurchaseOrderLine(models.Model):
comodel_name="stock.request", string="Stock Requests", copy=False
)

def unlink(self):
pedrobaeza marked this conversation as resolved.
Show resolved Hide resolved
"""
Cancel the stock.request
related to the purchase order line
because it does not occur automatically
and causes inconsistency by keeping the SR state as 'In Progress' (open).
"""
stock_request_to_cancel = self.env["stock.request"]
for purchase_line in self:
stock_request_to_cancel |= purchase_line.stock_request_ids
res = super().unlink()
if stock_request_to_cancel:
stock_request_to_cancel.action_cancel()
return res

def _prepare_stock_moves(self, picking):
res = super()._prepare_stock_moves(picking)

Expand Down
71 changes: 71 additions & 0 deletions stock_request_purchase/tests/test_stock_request_purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,77 @@ def test_create_request_cancel_purchase(self):
stock_request.action_cancel()
self.assertEqual(stock_request.purchase_ids.state, "cancel")

def test_unlink_purchase_order_line(self):
"""
Test that when a purchase order line is unlinked,
the related stock requests are cancelled
"""
expected_date = fields.Datetime.now()
product2 = self._create_product("P2", "product2", False)
vals = {
"company_id": self.main_company.id,
"warehouse_id": self.warehouse.id,
"location_id": self.warehouse.lot_stock_id.id,
"expected_date": expected_date,
"stock_request_ids": [
(
0,
0,
{
"product_id": self.product.id,
"product_uom_id": self.product.uom_id.id,
"product_uom_qty": 10.0,
"company_id": self.main_company.id,
"warehouse_id": self.warehouse.id,
"location_id": self.warehouse.lot_stock_id.id,
"expected_date": expected_date,
},
),
(
0,
0,
{
"product_id": product2.id,
"product_uom_id": product2.uom_id.id,
"product_uom_qty": 20.0,
"company_id": self.main_company.id,
"warehouse_id": self.warehouse.id,
"location_id": self.warehouse.lot_stock_id.id,
"expected_date": expected_date,
},
),
],
}

order = (
self.env["stock.request.order"]
.with_user(self.stock_request_user)
.create(vals)
)

order.action_confirm()
self.assertEqual(order.state, "open")
self.assertEqual(len(order.purchase_ids), 1)
purchase = order.purchase_ids[0]
purchase_line1 = purchase.order_line.filtered(
lambda x: x.product_id == self.product
)
purchase_line2 = purchase.order_line.filtered(
lambda x: x.product_id == product2
)
stock_request1 = order.stock_request_ids.filtered(
lambda x: x.product_id == self.product
)
stock_request2 = order.stock_request_ids.filtered(
lambda x: x.product_id == product2
)
purchase_line1.sudo().unlink()
self.assertEqual(stock_request1.state, "cancel")
self.assertEqual(order.state, "open")
purchase_line2.sudo().unlink()
self.assertEqual(stock_request2.state, "cancel")
self.assertEqual(order.state, "cancel")

def test_view_actions(self):
expected_date = fields.Datetime.now()
vals = {
Expand Down
Loading