mirror of
https://github.com/ForgeFlow/stock-rma.git
synced 2025-01-21 12:57:49 +02:00
Merge pull request #268 from ForgeFlow/14.0-bp-265
[14.0][FIX] rma: prevent against warehouse mismatch or missing rules
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
# Copyright (C) 2017-22 ForgeFlow S.L.
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
|
||||
|
||||
from odoo import fields, models
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class ProcurementGroup(models.Model):
|
||||
@@ -13,3 +14,24 @@ class ProcurementGroup(models.Model):
|
||||
rma_line_id = fields.Many2one(
|
||||
comodel_name="rma.order.line", string="RMA line", ondelete="set null"
|
||||
)
|
||||
|
||||
@api.model
|
||||
def _get_rule(self, product_id, location_id, values):
|
||||
"""Ensure that the selected rule is from the configured route"""
|
||||
res = super()._get_rule(product_id, location_id, values)
|
||||
force_rule_ids = self.env.context.get("rma_force_rule_ids")
|
||||
if force_rule_ids:
|
||||
if res and res.id not in force_rule_ids:
|
||||
raise ValidationError(
|
||||
_(
|
||||
"No rule found in this RMA's configured route for product "
|
||||
"%(product)s and location %(location)s"
|
||||
)
|
||||
% {
|
||||
"product": product_id.default_code or product_id.name,
|
||||
"location": location_id.complete_name,
|
||||
}
|
||||
)
|
||||
# Don't enforce rules on any chained moves
|
||||
force_rule_ids.clear()
|
||||
return res
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# © 2017 ForgeFlow
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
|
||||
|
||||
from odoo.exceptions import ValidationError
|
||||
from odoo.tests import common
|
||||
from odoo.exceptions import UserError, ValidationError
|
||||
from odoo.tests import Form, common
|
||||
|
||||
|
||||
class TestRma(common.SavepointCase):
|
||||
@@ -1018,3 +1018,44 @@ class TestRma(common.SavepointCase):
|
||||
self.assertTrue(new_line.product_id.categ_id.rma_customer_operation_id)
|
||||
new_line._onchange_product_id()
|
||||
self.assertEqual(new_line.operation_id, rma_operation)
|
||||
|
||||
def test_06_warehouse_mismatch(self):
|
||||
"""Mismatch between operation warehouse and stock rule warehouse is raised.
|
||||
|
||||
* Create a second warehouse that is resupplied from the main warehouse
|
||||
* Update an RMA to receive into the second warehouse
|
||||
* When creating pickings, it is raised that the rules from the RMA
|
||||
* config are not used.
|
||||
"""
|
||||
wh2 = self.env["stock.warehouse"].create(
|
||||
{
|
||||
"name": "Shop.",
|
||||
"code": "SHP",
|
||||
}
|
||||
)
|
||||
wh2.resupply_wh_ids = self.env.ref("stock.warehouse0")
|
||||
wh2.rma_in_this_wh = True
|
||||
wh2.lot_rma_id = self.env["stock.location"].create(
|
||||
{
|
||||
"name": "WH2 RMA",
|
||||
"usage": "internal",
|
||||
"location_id": wh2.lot_stock_id.id,
|
||||
}
|
||||
)
|
||||
rma = self.rma_customer_id.copy().sudo()
|
||||
rma.rma_line_ids = self.rma_customer_id.rma_line_ids[0].copy()
|
||||
rma.rma_line_ids.product_id.sudo().route_ids += wh2.resupply_route_ids
|
||||
rma_form = Form(rma)
|
||||
rma_form.in_warehouse_id = wh2
|
||||
rma_form.save()
|
||||
rma.rma_line_ids.action_rma_approve()
|
||||
wizard = self.rma_make_picking.with_context(
|
||||
**{
|
||||
"active_ids": rma.rma_line_ids.ids,
|
||||
"active_model": "rma.order.line",
|
||||
"picking_type": "incoming",
|
||||
"active_id": 1,
|
||||
}
|
||||
).create({})
|
||||
with self.assertRaisesRegex(UserError, "No rule found"):
|
||||
wizard._create_picking()
|
||||
|
||||
@@ -144,8 +144,10 @@ class RmaMakePicking(models.TransientModel):
|
||||
group = self.env["procurement.group"].create(pg_data)
|
||||
if picking_type == "incoming":
|
||||
qty = item.qty_to_receive
|
||||
force_rule_ids = item.line_id.in_route_id.rule_ids.ids
|
||||
else:
|
||||
qty = item.qty_to_deliver
|
||||
force_rule_ids = item.line_id.out_route_id.rule_ids.ids
|
||||
values = self._get_procurement_data(item, group, qty, picking_type)
|
||||
values = dict(values, rma_line_id=item.line_id, rma_id=item.line_id.rma_id)
|
||||
# create picking
|
||||
@@ -163,7 +165,9 @@ class RmaMakePicking(models.TransientModel):
|
||||
)
|
||||
|
||||
procurements.append(procurement)
|
||||
self.env["procurement.group"].run(procurements)
|
||||
self.env["procurement.group"].with_context(
|
||||
rma_force_rule_ids=force_rule_ids
|
||||
).run(procurements)
|
||||
except UserError as error:
|
||||
errors.append(error.name)
|
||||
if errors:
|
||||
|
||||
Reference in New Issue
Block a user