mirror of
https://github.com/ForgeFlow/stock-rma.git
synced 2025-01-21 12:57:49 +02:00
[REF] rma: change rules by routes in warehouse
This commit is contained in:
committed by
AaronHForgeFlow
parent
758a2a7d3f
commit
e33ffe9280
@@ -1,7 +1,7 @@
|
|||||||
# Copyright (C) 2017-20 ForgeFlow S.L.
|
# Copyright (C) 2017-20 ForgeFlow S.L.
|
||||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
|
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
|
||||||
|
|
||||||
from odoo import _, fields, models
|
from odoo import _, api, fields, models
|
||||||
|
|
||||||
|
|
||||||
class StockWarehouse(models.Model):
|
class StockWarehouse(models.Model):
|
||||||
@@ -28,19 +28,22 @@ class StockWarehouse(models.Model):
|
|||||||
help="If set, it will create RMA location, picking types and routes "
|
help="If set, it will create RMA location, picking types and routes "
|
||||||
"for this warehouse.",
|
"for this warehouse.",
|
||||||
)
|
)
|
||||||
rma_customer_in_pull_id = fields.Many2one(
|
rma_customer_pull_id = fields.Many2one(
|
||||||
comodel_name="stock.rule", string="RMA Customer In Rule"
|
comodel_name="stock.location.route",
|
||||||
|
string="RMA Customer Route",
|
||||||
)
|
)
|
||||||
rma_customer_out_pull_id = fields.Many2one(
|
rma_supplier_pull_id = fields.Many2one(
|
||||||
comodel_name="stock.rule", string="RMA Customer Out Rule"
|
comodel_name="stock.location.route",
|
||||||
)
|
string="RMA Supplier Route",
|
||||||
rma_supplier_in_pull_id = fields.Many2one(
|
|
||||||
comodel_name="stock.rule", string="RMA Supplier In Rule"
|
|
||||||
)
|
|
||||||
rma_supplier_out_pull_id = fields.Many2one(
|
|
||||||
comodel_name="stock.rule", string="RMA Supplier Out Rule"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@api.returns("self")
|
||||||
|
def _get_all_routes(self):
|
||||||
|
routes = super()._get_all_routes()
|
||||||
|
routes |= self.rma_customer_pull_id
|
||||||
|
routes |= self.rma_supplier_pull_id
|
||||||
|
return routes
|
||||||
|
|
||||||
def _get_rma_types(self):
|
def _get_rma_types(self):
|
||||||
return [
|
return [
|
||||||
self.rma_cust_out_type_id,
|
self.rma_cust_out_type_id,
|
||||||
@@ -79,18 +82,16 @@ class StockWarehouse(models.Model):
|
|||||||
for r_type in wh._get_rma_types():
|
for r_type in wh._get_rma_types():
|
||||||
if r_type:
|
if r_type:
|
||||||
r_type.active = True
|
r_type.active = True
|
||||||
# RMA rules:
|
# RMA routes:
|
||||||
wh._create_or_update_rma_pull()
|
wh._create_rma_pull()
|
||||||
else:
|
else:
|
||||||
for wh in self:
|
for wh in self:
|
||||||
for r_type in wh._get_rma_types():
|
for r_type in wh._get_rma_types():
|
||||||
if r_type:
|
if r_type:
|
||||||
r_type.active = False
|
r_type.active = False
|
||||||
# Unlink rules:
|
# Unlink rules:
|
||||||
self.mapped("rma_customer_in_pull_id").unlink()
|
self.mapped("rma_customer_pull_id").unlink()
|
||||||
self.mapped("rma_customer_out_pull_id").unlink()
|
self.mapped("rma_supplier_pull_id").unlink()
|
||||||
self.mapped("rma_supplier_in_pull_id").unlink()
|
|
||||||
self.mapped("rma_supplier_out_pull_id").unlink()
|
|
||||||
return super(StockWarehouse, self).write(vals)
|
return super(StockWarehouse, self).write(vals)
|
||||||
|
|
||||||
def _create_rma_picking_types(self):
|
def _create_rma_picking_types(self):
|
||||||
@@ -176,90 +177,121 @@ class StockWarehouse(models.Model):
|
|||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def get_rma_rules_dict(self):
|
def get_rma_route_customer(self):
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
rma_rules = dict()
|
customer_loc, _ = self._get_partner_locations()
|
||||||
customer_loc, supplier_loc = self._get_partner_locations()
|
rma_rules = [
|
||||||
rma_rules["rma_customer_in"] = {
|
{
|
||||||
"name": self._format_rulename(self, customer_loc, self.lot_rma_id.name),
|
"name": self.name + ": Customer RMA",
|
||||||
"action": "pull",
|
"rma_selectable": True,
|
||||||
"warehouse_id": self.id,
|
"rule_ids": [
|
||||||
"company_id": self.company_id.id,
|
(
|
||||||
"location_src_id": customer_loc.id,
|
0,
|
||||||
"location_dest_id": self.lot_rma_id.id,
|
0,
|
||||||
"procure_method": "make_to_stock",
|
{
|
||||||
"route_id": self.env.ref("rma.route_rma_customer").id,
|
"name": self._format_rulename(
|
||||||
"picking_type_id": self.rma_cust_in_type_id.id,
|
self, customer_loc, self.lot_rma_id.name
|
||||||
"active": True,
|
),
|
||||||
}
|
"action": "pull",
|
||||||
rma_rules["rma_customer_out"] = {
|
"warehouse_id": self.id,
|
||||||
"name": self._format_rulename(self, self.lot_rma_id, customer_loc.name),
|
"company_id": self.company_id.id,
|
||||||
"action": "pull",
|
"location_src_id": customer_loc.id,
|
||||||
"warehouse_id": self.id,
|
"location_id": self.lot_rma_id.id,
|
||||||
"company_id": self.company_id.id,
|
"procure_method": "make_to_stock",
|
||||||
"location_src_id": self.lot_rma_id.id,
|
"picking_type_id": self.rma_cust_in_type_id.id,
|
||||||
"location_dest_id": customer_loc.id,
|
"active": True,
|
||||||
"procure_method": "make_to_stock",
|
},
|
||||||
"route_id": self.env.ref("rma.route_rma_customer").id,
|
),
|
||||||
"picking_type_id": self.rma_cust_out_type_id.id,
|
(
|
||||||
"active": True,
|
0,
|
||||||
}
|
0,
|
||||||
rma_rules["rma_supplier_in"] = {
|
{
|
||||||
"name": self._format_rulename(self, supplier_loc, self.lot_rma_id.name),
|
"name": self._format_rulename(
|
||||||
"action": "pull",
|
self, self.lot_rma_id, customer_loc.name
|
||||||
"warehouse_id": self.id,
|
),
|
||||||
"company_id": self.company_id.id,
|
"action": "pull",
|
||||||
"location_src_id": supplier_loc.id,
|
"warehouse_id": self.id,
|
||||||
"location_dest_id": self.lot_rma_id.id,
|
"company_id": self.company_id.id,
|
||||||
"procure_method": "make_to_stock",
|
"location_src_id": self.lot_rma_id.id,
|
||||||
"route_id": self.env.ref("rma.route_rma_supplier").id,
|
"location_id": customer_loc.id,
|
||||||
"picking_type_id": self.rma_sup_in_type_id.id,
|
"procure_method": "make_to_stock",
|
||||||
"active": True,
|
"picking_type_id": self.rma_cust_out_type_id.id,
|
||||||
}
|
"active": True,
|
||||||
rma_rules["rma_supplier_out"] = {
|
},
|
||||||
"name": self._format_rulename(self, self.lot_rma_id, supplier_loc.name),
|
),
|
||||||
"action": "pull",
|
],
|
||||||
"warehouse_id": self.id,
|
}
|
||||||
"company_id": self.company_id.id,
|
]
|
||||||
"location_src_id": self.lot_rma_id.id,
|
|
||||||
"location_dest_id": supplier_loc.id,
|
|
||||||
"procure_method": "make_to_stock",
|
|
||||||
"route_id": self.env.ref("rma.route_rma_supplier").id,
|
|
||||||
"picking_type_id": self.rma_sup_out_type_id.id,
|
|
||||||
"active": True,
|
|
||||||
}
|
|
||||||
return rma_rules
|
return rma_rules
|
||||||
|
|
||||||
def _create_or_update_rma_pull(self):
|
def get_rma_route_supplier(self):
|
||||||
rule_obj = self.env["stock.rule"]
|
self.ensure_one()
|
||||||
|
_, supplier_loc = self._get_partner_locations()
|
||||||
|
rma_route = [
|
||||||
|
{
|
||||||
|
"name": self.name + ": Supplier RMA",
|
||||||
|
"rma_selectable": True,
|
||||||
|
"rule_ids": [
|
||||||
|
(
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
{
|
||||||
|
"name": self._format_rulename(
|
||||||
|
self, supplier_loc, self.lot_rma_id.name
|
||||||
|
),
|
||||||
|
"action": "pull",
|
||||||
|
"warehouse_id": self.id,
|
||||||
|
"company_id": self.company_id.id,
|
||||||
|
"location_src_id": supplier_loc.id,
|
||||||
|
"location_id": self.lot_rma_id.id,
|
||||||
|
"procure_method": "make_to_stock",
|
||||||
|
"picking_type_id": self.rma_sup_in_type_id.id,
|
||||||
|
"active": True,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
{
|
||||||
|
"name": self._format_rulename(
|
||||||
|
self, self.lot_rma_id, supplier_loc.name
|
||||||
|
),
|
||||||
|
"action": "pull",
|
||||||
|
"warehouse_id": self.id,
|
||||||
|
"company_id": self.company_id.id,
|
||||||
|
"location_src_id": self.lot_rma_id.id,
|
||||||
|
"location_id": supplier_loc.id,
|
||||||
|
"procure_method": "make_to_stock",
|
||||||
|
"picking_type_id": self.rma_sup_out_type_id.id,
|
||||||
|
"active": True,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
}
|
||||||
|
]
|
||||||
|
return rma_route
|
||||||
|
|
||||||
|
def _create_rma_pull(self):
|
||||||
|
route_obj = self.env["stock.location.route"]
|
||||||
for wh in self:
|
for wh in self:
|
||||||
rules_dict = wh.get_rma_rules_dict()
|
if not wh.rma_customer_pull_id:
|
||||||
if wh.rma_customer_in_pull_id:
|
wh.rma_customer_pull_id = (
|
||||||
wh.rma_customer_in_pull_id.write(rules_dict["rma_customer_in"])
|
route_obj.create(self.get_rma_route_customer())
|
||||||
else:
|
if wh
|
||||||
wh.rma_customer_in_pull_id = rule_obj.create(
|
not in self.env.ref("rma.route_rma_customer").rule_ids.mapped(
|
||||||
rules_dict["rma_customer_in"]
|
"warehouse_id"
|
||||||
|
)
|
||||||
|
else self.env.ref("rma.route_rma_customer")
|
||||||
)
|
)
|
||||||
|
|
||||||
if wh.rma_customer_out_pull_id:
|
if not wh.rma_supplier_pull_id:
|
||||||
wh.rma_customer_out_pull_id.write(rules_dict["rma_customer_out"])
|
wh.rma_supplier_pull_id = (
|
||||||
else:
|
route_obj.create(self.get_rma_route_supplier())
|
||||||
wh.rma_customer_out_pull_id = rule_obj.create(
|
if wh
|
||||||
rules_dict["rma_customer_out"]
|
not in self.env.ref("rma.route_rma_supplier").rule_ids.mapped(
|
||||||
)
|
"warehouse_id"
|
||||||
|
)
|
||||||
if wh.rma_supplier_in_pull_id:
|
else self.env.ref("rma.route_rma_supplier")
|
||||||
wh.rma_supplier_in_pull_id.write(rules_dict["rma_supplier_in"])
|
|
||||||
else:
|
|
||||||
wh.rma_supplier_in_pull_id = rule_obj.create(
|
|
||||||
rules_dict["rma_supplier_in"]
|
|
||||||
)
|
|
||||||
|
|
||||||
if wh.rma_supplier_out_pull_id:
|
|
||||||
wh.rma_supplier_out_pull_id.write(rules_dict["rma_supplier_out"])
|
|
||||||
else:
|
|
||||||
wh.rma_supplier_out_pull_id = rule_obj.create(
|
|
||||||
rules_dict["rma_supplier_out"]
|
|
||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
@@ -1069,7 +1069,101 @@ class TestRma(common.TransactionCase):
|
|||||||
with self.assertRaisesRegex(ValidationError, "No quantity to transfer"):
|
with self.assertRaisesRegex(ValidationError, "No quantity to transfer"):
|
||||||
wizard._create_picking()
|
wizard._create_picking()
|
||||||
|
|
||||||
def test_08_supplier_rma_single_line(self):
|
def test_08_customer_rma_multi_step(self):
|
||||||
|
"""
|
||||||
|
Receive a product and then return it using a multi-step route.
|
||||||
|
"""
|
||||||
|
# Alter the customer RMA route to make it multi-step
|
||||||
|
# Get rid of the duplicated rule
|
||||||
|
self.env.ref("rma.rule_rma_customer_out_pull").active = False
|
||||||
|
self.env.ref("rma.rule_rma_customer_in_pull").active = False
|
||||||
|
cust_in_pull_rule = self.customer_route.rule_ids.filtered(
|
||||||
|
lambda r: r.location_id == self.stock_rma_location
|
||||||
|
)
|
||||||
|
cust_in_pull_rule.location_id = self.input_location
|
||||||
|
cust_out_pull_rule = self.customer_route.rule_ids.filtered(
|
||||||
|
lambda r: r.location_src_id == self.env.ref("rma.location_rma")
|
||||||
|
)
|
||||||
|
cust_out_pull_rule.location_src_id = self.output_location
|
||||||
|
cust_out_pull_rule.procure_method = "make_to_order"
|
||||||
|
self.env["stock.rule"].create(
|
||||||
|
{
|
||||||
|
"name": "RMA->Output",
|
||||||
|
"action": "pull",
|
||||||
|
"warehouse_id": self.wh.id,
|
||||||
|
"location_src_id": self.env.ref("rma.location_rma").id,
|
||||||
|
"location_id": self.output_location.id,
|
||||||
|
"procure_method": "make_to_stock",
|
||||||
|
"route_id": self.customer_route.id,
|
||||||
|
"picking_type_id": self.env.ref("stock.picking_type_internal").id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
self.env["stock.rule"].create(
|
||||||
|
{
|
||||||
|
"name": "Output->Customer",
|
||||||
|
"action": "pull",
|
||||||
|
"warehouse_id": self.wh.id,
|
||||||
|
"location_src_id": self.output_location.id,
|
||||||
|
"location_id": self.customer_location.id,
|
||||||
|
"procure_method": "make_to_order",
|
||||||
|
"route_id": self.customer_route.id,
|
||||||
|
"picking_type_id": self.env.ref("stock.picking_type_internal").id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
self.env["stock.rule"].create(
|
||||||
|
{
|
||||||
|
"name": "Customer->Input",
|
||||||
|
"action": "pull",
|
||||||
|
"warehouse_id": self.wh.id,
|
||||||
|
"location_src_id": self.customer_location.id,
|
||||||
|
"location_id": self.input_location.id,
|
||||||
|
"procure_method": "make_to_stock",
|
||||||
|
"route_id": self.customer_route.id,
|
||||||
|
"picking_type_id": self.env.ref("stock.picking_type_internal").id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
self.env["stock.rule"].create(
|
||||||
|
{
|
||||||
|
"name": "Input->RMA",
|
||||||
|
"action": "pull",
|
||||||
|
"warehouse_id": self.wh.id,
|
||||||
|
"location_src_id": self.input_location.id,
|
||||||
|
"location_id": self.env.ref("rma.location_rma").id,
|
||||||
|
"procure_method": "make_to_order",
|
||||||
|
"route_id": self.customer_route.id,
|
||||||
|
"picking_type_id": self.env.ref("stock.picking_type_internal").id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
# Set a standard price on the products
|
||||||
|
self.product_1.standard_price = 10
|
||||||
|
self._create_inventory(
|
||||||
|
self.product_1, 20.0, self.env.ref("stock.stock_location_customers")
|
||||||
|
)
|
||||||
|
products2move = [
|
||||||
|
(self.product_1, 3),
|
||||||
|
]
|
||||||
|
self.product_1.categ_id.rma_customer_operation_id = self.rma_cust_replace_op_id
|
||||||
|
rma_customer_id = self._create_rma_from_move(
|
||||||
|
products2move,
|
||||||
|
"customer",
|
||||||
|
self.env.ref("base.res_partner_2"),
|
||||||
|
dropship=False,
|
||||||
|
)
|
||||||
|
rma = rma_customer_id.rma_line_ids
|
||||||
|
rma.action_rma_to_approve()
|
||||||
|
self.assertEqual(rma.qty_to_receive, 3)
|
||||||
|
self.assertEqual(rma.qty_received, 0)
|
||||||
|
self._receive_rma(rma)
|
||||||
|
self.assertEqual(len(rma.move_ids), 2)
|
||||||
|
self.assertEqual(rma.qty_to_receive, 0)
|
||||||
|
self.assertEqual(rma.qty_received, 3)
|
||||||
|
self.assertEqual(rma.qty_to_deliver, 3)
|
||||||
|
self._deliver_rma(rma)
|
||||||
|
self.assertEqual(rma.qty_to_deliver, 0)
|
||||||
|
self.assertEqual(rma.qty_delivered, 3)
|
||||||
|
self.assertEqual(len(rma.move_ids), 4)
|
||||||
|
|
||||||
|
def test_09_supplier_rma_single_line(self):
|
||||||
rma_line_id = self.rma_supplier_id.rma_line_ids[0].id
|
rma_line_id = self.rma_supplier_id.rma_line_ids[0].id
|
||||||
wizard = self.rma_make_picking.with_context(
|
wizard = self.rma_make_picking.with_context(
|
||||||
active_ids=[rma_line_id],
|
active_ids=[rma_line_id],
|
||||||
|
|||||||
Reference in New Issue
Block a user