mirror of
https://github.com/OCA/stock-logistics-warehouse.git
synced 2025-01-21 14:27:28 +02:00
[IMP] scrap_reason_code: define product categories allowed for each code
Also redefine reason code form view a bit.
This commit is contained in:
@@ -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.",
|
||||
)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -4,3 +4,4 @@
|
||||
* Serpent Consulting Services Pvt. Ltd. <support@serpentcs.com>
|
||||
* Chandresh Thakkar <cthakkar@opensourceintegrators.com>
|
||||
* Hughes Damry <hughes.damry@acsone.eu>
|
||||
* Lois Rilo <lois.rilo@forgeflow.com>
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
# 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.exceptions import ValidationError
|
||||
from odoo.tests.common import TransactionCase
|
||||
|
||||
|
||||
@@ -11,6 +13,8 @@ class StockScrap(TransactionCase):
|
||||
|
||||
self.stock_location = self.env.ref("stock.stock_location_stock")
|
||||
self.customer_location = self.env.ref("stock.stock_location_customers")
|
||||
self.categ_1 = self.env.ref("product.product_category_all")
|
||||
self.categ_2 = self.env["product.category"].create({"name": "Test category"})
|
||||
stock_location_locations_virtual = self.env["stock.location"].create(
|
||||
{"name": "Virtual Locations", "usage": "view", "posz": 1}
|
||||
)
|
||||
@@ -27,7 +31,14 @@ class StockScrap(TransactionCase):
|
||||
{
|
||||
"name": "Scrap Product A",
|
||||
"type": "product",
|
||||
"categ_id": self.env.ref("product.product_category_all").id,
|
||||
"categ_id": self.categ_1.id,
|
||||
}
|
||||
)
|
||||
self.scrap_product_2 = self.env["product.product"].create(
|
||||
{
|
||||
"name": "Scrap Product A",
|
||||
"type": "product",
|
||||
"categ_id": self.categ_2.id,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -38,6 +49,13 @@ class StockScrap(TransactionCase):
|
||||
"location_id": self.scrapped_location.id,
|
||||
}
|
||||
)
|
||||
self.reason_code_only_categ_2 = self.env["scrap.reason.code"].create(
|
||||
{
|
||||
"name": "Test Code 2",
|
||||
"description": "Test description",
|
||||
"product_category_ids": [(6, 0, self.categ_2.ids)],
|
||||
}
|
||||
)
|
||||
|
||||
self.uom_unit = self.env.ref("uom.product_uom_unit")
|
||||
|
||||
@@ -164,3 +182,40 @@ class StockScrap(TransactionCase):
|
||||
|
||||
scrapped_move.quantity_done = 8
|
||||
self.assertEqual(scrap2.scrap_qty, 8, "Scrap quantity is not updated.")
|
||||
|
||||
def test_allowed_reason_codes(self):
|
||||
with self.assertRaises(ValidationError):
|
||||
self.env["stock.scrap"].create(
|
||||
{
|
||||
"product_id": self.scrap_product.id,
|
||||
"product_uom_id": self.scrap_product_2.uom_id.id,
|
||||
"scrap_qty": 5,
|
||||
"reason_code_id": self.reason_code_only_categ_2.id,
|
||||
}
|
||||
)
|
||||
scrap = self.env["stock.scrap"].create(
|
||||
{
|
||||
"product_id": self.scrap_product.id,
|
||||
"product_uom_id": self.scrap_product.uom_id.id,
|
||||
"scrap_qty": 5,
|
||||
"reason_code_id": self.reason_code.id,
|
||||
}
|
||||
)
|
||||
self.assertEqual(scrap.allowed_reason_code_ids, self.reason_code)
|
||||
with self.assertRaises(ValidationError):
|
||||
scrap.write({"reason_code_id": self.reason_code_only_categ_2.id})
|
||||
scrap = self.env["stock.scrap"].create(
|
||||
{
|
||||
"product_id": self.scrap_product_2.id,
|
||||
"product_uom_id": self.scrap_product_2.uom_id.id,
|
||||
"scrap_qty": 5,
|
||||
"reason_code_id": self.reason_code_only_categ_2.id,
|
||||
}
|
||||
)
|
||||
with self.assertRaises(ValidationError):
|
||||
scrap.write({"product_id": self.scrap_product.id})
|
||||
self.assertEqual(
|
||||
scrap.allowed_reason_code_ids,
|
||||
(self.reason_code + self.reason_code_only_categ_2),
|
||||
)
|
||||
scrap.write({"reason_code_id": self.reason_code.id})
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<!-- Copyright 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). -->
|
||||
<odoo>
|
||||
<!-- Scrap Reason Code Type -->
|
||||
@@ -7,15 +8,22 @@
|
||||
<field name="model">scrap.reason.code</field>
|
||||
<field name="arch" type="xml">
|
||||
<form string="Reason Code">
|
||||
<group>
|
||||
<field name="name" />
|
||||
</group>
|
||||
<group>
|
||||
<field name="description" />
|
||||
</group>
|
||||
<group>
|
||||
<field name="location_id" />
|
||||
</group>
|
||||
<sheet>
|
||||
<div class="oe_title">
|
||||
<h1><field name="name" nolabel="1" /></h1>
|
||||
</div>
|
||||
<group>
|
||||
<field name="description" />
|
||||
</group>
|
||||
<group>
|
||||
<field name="location_id" />
|
||||
<field
|
||||
name="product_category_ids"
|
||||
widget="many2many_tags"
|
||||
options="{'no_create': True}"
|
||||
/>
|
||||
</group>
|
||||
</sheet>
|
||||
</form>
|
||||
</field>
|
||||
</record>
|
||||
@@ -27,6 +35,7 @@
|
||||
<field name="name" />
|
||||
<field name="description" />
|
||||
<field name="location_id" />
|
||||
<field name="product_category_ids" widget="many2many_tags" />
|
||||
</tree>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<!-- Copyright 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). -->
|
||||
<odoo>
|
||||
<record id="stock_scrap_form_reason_code" model="ir.ui.view">
|
||||
@@ -8,6 +9,7 @@
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//group/group" position='inside'>
|
||||
<field name="reason_code_id" />
|
||||
<field name="allowed_reason_code_ids" invisible="1" />
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
@@ -18,6 +20,7 @@
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//field[@name='product_uom_id']" position='after'>
|
||||
<field name="reason_code_id" />
|
||||
<field name="allowed_reason_code_ids" invisible="1" />
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
@@ -28,6 +31,7 @@
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//group/group" position='inside'>
|
||||
<field name="reason_code_id" />
|
||||
<field name="allowed_reason_code_ids" invisible="1" />
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
Reference in New Issue
Block a user