[IMP] scrap_reason_code: define product categories allowed for each code

Also redefine reason code form view a bit.
This commit is contained in:
Lois Rilo
2023-05-18 15:59:47 +02:00
committed by John Herholz
parent dfe0afeade
commit 11380bad25
6 changed files with 124 additions and 12 deletions

View File

@@ -1,5 +1,6 @@
# Copyright (C) 2019 IBM Corp.
# Copyright (C) 2019 Open Source Integrators
# Copyright 2023 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import fields, models
@@ -16,3 +17,10 @@ class ScrapReasonCode(models.Model):
string="Scrap Location",
domain="[('scrap_location', '=', True)]",
)
product_category_ids = fields.Many2many(
string="Allowed Product Categories",
comodel_name="product.category",
help="Indicate the cateogories of products that can use this reason code "
"when doing a scrap. If left empy, this reason code can be used "
"with any product.",
)

View File

@@ -1,18 +1,53 @@
# Copyright (C) 2019 IBM Corp.
# Copyright (C) 2019 Open Source Integrators
# Copyright 2023 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
class StockScrap(models.Model):
_inherit = "stock.scrap"
reason_code_id = fields.Many2one(
"scrap.reason.code", states={"done": [("readonly", True)]}
comodel_name="scrap.reason.code",
states={"done": [("readonly", True)]},
domain="[('id', 'in', allowed_reason_code_ids)]",
)
allowed_reason_code_ids = fields.Many2many(
comodel_name="scrap.reason.code",
compute="_compute_allowed_reason_code_ids",
)
scrap_location_id = fields.Many2one(readonly=True)
@api.depends("product_id", "product_id.categ_id")
def _compute_allowed_reason_code_ids(self):
for rec in self:
codes = self.env["scrap.reason.code"]
if rec.product_id:
codes = codes.search(
[
"|",
("product_category_ids", "=", False),
("product_category_ids", "in", rec.product_id.categ_id.id),
]
)
rec.allowed_reason_code_ids = codes
@api.constrains("reason_code_id", "product_id")
def _check_reason_code_id(self):
for rec in self:
if (
rec.reason_code_id
and rec.reason_code_id not in rec.allowed_reason_code_ids
):
raise ValidationError(
_(
"The selected reason code is not allowed for this product category."
)
)
def _prepare_move_values(self):
res = super(StockScrap, self)._prepare_move_values()
res["reason_code_id"] = self.reason_code_id.id