Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][FWD][REF] rma: change rules to route in warehouse #472

Open
wants to merge 4 commits into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions rma/models/rma_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RmaOperation(models.Model):
def _default_warehouse_id(self):
company_id = self.env.user.company_id.id
warehouse = self.env["stock.warehouse"].search(
[("company_id", "=", company_id)], limit=1
[("company_id", "=", company_id)], order="sequence asc", limit=1
)
return warehouse

Expand All @@ -24,13 +24,33 @@ def _default_customer_location_id(self):
def _default_supplier_location_id(self):
return self.env.ref("stock.stock_location_suppliers") or False

@api.onchange("in_warehouse_id")
def onchange_warehouse_id(self):
op_type = self.env.context.get("default_type")
if op_type == "customer":
warehouse = self.in_warehouse_id
if not warehouse or not warehouse.rma_customer_pull_id:
return
self.in_route_id = warehouse.rma_customer_pull_id
self.out_route_id = warehouse.rma_customer_pull_id
elif op_type == "supplier":
warehouse = self.out_warehouse_id
if not warehouse or not warehouse.rma_supplier_pull_id:
return
self.in_route_id = warehouse.rma_supplier_pull_id
self.out_route_id = warehouse.rma_supplier_pull_id

@api.model
def _default_routes(self):
company_id = self.env.user.company_id.id
warehouse = self.env["stock.warehouse"].search(
[("company_id", "=", company_id)], limit=1
)
op_type = self.env.context.get("default_type")
if op_type == "customer":
return self.env.ref("rma.route_rma_customer")
return warehouse.rma_customer_pull_id.id
elif op_type == "supplier":
return self.env.ref("rma.route_rma_supplier")
return warehouse.rma_supplier_pull_id.id

name = fields.Char("Description", required=True)
code = fields.Char(required=True)
Expand Down
8 changes: 5 additions & 3 deletions rma/models/rma_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ def _get_default_type(self):
@api.model
def _default_warehouse_id(self):
rma_id = self.env.context.get("default_rma_id", False)
warehouse = self.env["stock.warehouse"]
company_id = self.env.user.company_id.id
warehouse = self.env["stock.warehouse"].search(
[("company_id", "=", company_id)], order="sequence asc", limit=1
)
if rma_id:
rma = self.env["rma.order"].browse(rma_id)
warehouse = self.env["stock.warehouse"].search(
[("company_id", "=", rma.company_id.id)], limit=1
[("company_id", "=", rma.company_id.id)], order="sequence asc", limit=1
)
return warehouse

Expand Down Expand Up @@ -593,7 +596,6 @@ def _prepare_rma_line_from_stock_move(self, sm, lot=False):
operation.location_id.id
or operation.in_warehouse_id.lot_rma_id.id
or operation.out_warehouse_id.lot_rma_id.id
or warehouse.lot_rma_id.id
),
}
return data
Expand Down
222 changes: 127 additions & 95 deletions rma/models/stock_warehouse.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (C) 2017-20 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


class StockWarehouse(models.Model):
Expand All @@ -28,19 +28,22 @@ class StockWarehouse(models.Model):
help="If set, it will create RMA location, picking types and routes "
"for this warehouse.",
)
rma_customer_in_pull_id = fields.Many2one(
comodel_name="stock.rule", string="RMA Customer In Rule"
rma_customer_pull_id = fields.Many2one(
comodel_name="stock.route",
string="RMA Customer Route",
)
rma_customer_out_pull_id = fields.Many2one(
comodel_name="stock.rule", string="RMA Customer Out Rule"
)
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"
rma_supplier_pull_id = fields.Many2one(
comodel_name="stock.route",
string="RMA Supplier Route",
)

@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):
return [
self.rma_cust_out_type_id,
Expand Down Expand Up @@ -79,18 +82,16 @@ def write(self, vals):
for r_type in wh._get_rma_types():
if r_type:
r_type.active = True
# RMA rules:
wh._create_or_update_rma_pull()
# RMA routes:
wh._create_rma_pull()
else:
for wh in self:
for r_type in wh._get_rma_types():
if r_type:
r_type.active = False
# Unlink rules:
self.mapped("rma_customer_in_pull_id").unlink()
self.mapped("rma_customer_out_pull_id").unlink()
self.mapped("rma_supplier_in_pull_id").unlink()
self.mapped("rma_supplier_out_pull_id").unlink()
self.mapped("rma_customer_pull_id").active = False
self.mapped("rma_supplier_pull_id").active = False
return super(StockWarehouse, self).write(vals)

def _create_rma_picking_types(self):
Expand Down Expand Up @@ -176,90 +177,121 @@ def _create_rma_picking_types(self):
)
return True

def get_rma_rules_dict(self):
def get_rma_route_customer(self):
self.ensure_one()
rma_rules = dict()
customer_loc, supplier_loc = self._get_partner_locations()
rma_rules["rma_customer_in"] = {
"name": self._format_rulename(self, customer_loc, self.lot_rma_id.name),
"action": "pull",
"warehouse_id": self.id,
"company_id": self.company_id.id,
"location_src_id": customer_loc.id,
"location_dest_id": self.lot_rma_id.id,
"procure_method": "make_to_stock",
"route_id": self.env.ref("rma.route_rma_customer").id,
"picking_type_id": self.rma_cust_in_type_id.id,
"active": True,
}
rma_rules["rma_customer_out"] = {
"name": self._format_rulename(self, self.lot_rma_id, customer_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": customer_loc.id,
"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,
}
rma_rules["rma_supplier_in"] = {
"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_dest_id": self.lot_rma_id.id,
"procure_method": "make_to_stock",
"route_id": self.env.ref("rma.route_rma_supplier").id,
"picking_type_id": self.rma_sup_in_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,
}
customer_loc, _ = self._get_partner_locations()
rma_rules = [
{
"name": self.name + ": Customer RMA",
"rma_selectable": True,
"rule_ids": [
(
0,
0,
{
"name": self._format_rulename(
self, customer_loc, self.lot_rma_id.name
),
"action": "pull",
"warehouse_id": self.id,
"company_id": self.company_id.id,
"location_src_id": customer_loc.id,
"location_dest_id": self.lot_rma_id.id,
"procure_method": "make_to_stock",
"picking_type_id": self.rma_cust_in_type_id.id,
"active": True,
},
),
(
0,
0,
{
"name": self._format_rulename(
self, self.lot_rma_id, customer_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": customer_loc.id,
"procure_method": "make_to_stock",
"picking_type_id": self.rma_cust_out_type_id.id,
"active": True,
},
),
],
}
]
return rma_rules

def _create_or_update_rma_pull(self):
rule_obj = self.env["stock.rule"]
for wh in self:
rules_dict = wh.get_rma_rules_dict()
if wh.rma_customer_in_pull_id:
wh.rma_customer_in_pull_id.write(rules_dict["rma_customer_in"])
else:
wh.rma_customer_in_pull_id = rule_obj.create(
rules_dict["rma_customer_in"]
)

if wh.rma_customer_out_pull_id:
wh.rma_customer_out_pull_id.write(rules_dict["rma_customer_out"])
else:
wh.rma_customer_out_pull_id = rule_obj.create(
rules_dict["rma_customer_out"]
)
def get_rma_route_supplier(self):
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_dest_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_dest_id": supplier_loc.id,
"procure_method": "make_to_stock",
"picking_type_id": self.rma_sup_out_type_id.id,
"active": True,
},
),
],
}
]
return rma_route

if wh.rma_supplier_in_pull_id:
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"]
def _create_rma_pull(self):
route_obj = self.env["stock.route"]
for wh in self:
if not wh.rma_customer_pull_id:
wh.rma_customer_pull_id = (
route_obj.create(self.get_rma_route_customer())
if wh
not in self.env.ref("rma.route_rma_customer").rule_ids.mapped(
"warehouse_id"
)
else self.env.ref("rma.route_rma_customer")
)

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"]
if not wh.rma_supplier_pull_id:
wh.rma_supplier_pull_id = (
route_obj.create(self.get_rma_route_supplier())
if wh
not in self.env.ref("rma.route_rma_supplier").rule_ids.mapped(
"warehouse_id"
)
else self.env.ref("rma.route_rma_supplier")
)
return True

Expand Down
Loading
Loading