diff --git a/stock_request/models/stock_request.py b/stock_request/models/stock_request.py index f807724f..656ec130 100644 --- a/stock_request/models/stock_request.py +++ b/stock_request/models/stock_request.py @@ -125,6 +125,18 @@ def _get_expected_date(): ("name_uniq", "unique(name, company_id)", "Stock Request name must be unique") ] + @api.constrains("state", "product_qty") + def _check_qty(self): + for rec in self: + if rec.state == "draft" and rec.product_qty <= 0: + raise ValidationError( + _("Stock Request product quantity has to be strictly positive.") + ) + elif rec.state != "draft" and rec.product_qty < 0: + raise ValidationError( + _("Stock Request product quantity cannot be negative.") + ) + def _get_all_origin_moves(self, move): all_moves = move if move.move_orig_ids: diff --git a/stock_request/models/stock_request_abstract.py b/stock_request/models/stock_request_abstract.py index 5ab1f142..76adf338 100644 --- a/stock_request/models/stock_request_abstract.py +++ b/stock_request/models/stock_request_abstract.py @@ -206,14 +206,6 @@ def _check_product_uom(self): ) ) - @api.constrains("product_qty") - def _check_qty(self): - for rec in self: - if rec.product_qty <= 0: - raise ValidationError( - _("Stock Request product quantity has to be strictly positive.") - ) - @api.onchange("warehouse_id") def onchange_warehouse_id(self): """Finds location id for changed warehouse.""" diff --git a/stock_request_purchase/models/stock_request.py b/stock_request_purchase/models/stock_request.py index 931ba36e..b2d1c36b 100644 --- a/stock_request_purchase/models/stock_request.py +++ b/stock_request_purchase/models/stock_request.py @@ -42,10 +42,11 @@ def _check_purchase_company_constrains(self): def action_cancel(self): """Propagate the cancellation to the generated purchase orders.""" res = super().action_cancel() - self.sudo().purchase_ids.filtered( - lambda x: x.state not in ("purchase", "done", "cancel") - and x.stock_request_ids == self - ).button_cancel() + if not self.env.context.get("skip_cancel_po_from_stock_request"): + self.sudo().purchase_ids.filtered( + lambda x: x.state not in ("purchase", "done", "cancel") + and x.stock_request_ids == self + ).button_cancel() return res def action_view_purchase(self):