mirror of
https://github.com/ForgeFlow/stock-rma.git
synced 2025-01-21 12:57:49 +02:00
[fix] rma: improve logic to count in and out pickings
In the scenario where we use a 2 or 3 step receipts or pickings we want to make sure that we correctly count and classify the pickings.
This commit is contained in:
committed by
AaronHForgeFlow
parent
08b55799ef
commit
964248f089
@@ -20,33 +20,17 @@ class RmaOrder(models.Model):
|
||||
|
||||
def _compute_in_shipment_count(self):
|
||||
for rec in self:
|
||||
picking_ids = []
|
||||
if not rec.rma_line_ids:
|
||||
rec.in_shipment_count = 0
|
||||
continue
|
||||
pickings = self.env["stock.picking"]
|
||||
for line in rec.rma_line_ids:
|
||||
for move in line.move_ids:
|
||||
if move.location_dest_id.usage == "internal":
|
||||
picking_ids.append(move.picking_id.id)
|
||||
else:
|
||||
if line.customer_to_supplier:
|
||||
picking_ids.append(move.picking_id.id)
|
||||
shipments = list(set(picking_ids))
|
||||
rec.in_shipment_count = len(shipments)
|
||||
pickings |= line._get_in_pickings()
|
||||
rec.in_shipment_count = len(pickings)
|
||||
|
||||
def _compute_out_shipment_count(self):
|
||||
picking_ids = []
|
||||
for rec in self:
|
||||
if not rec.rma_line_ids:
|
||||
rec.out_shipment_count = 0
|
||||
continue
|
||||
pickings = self.env["stock.picking"]
|
||||
for line in rec.rma_line_ids:
|
||||
for move in line.move_ids:
|
||||
if move.location_dest_id.usage in ("supplier", "customer"):
|
||||
if not line.customer_to_supplier:
|
||||
picking_ids.append(move.picking_id.id)
|
||||
shipments = list(set(picking_ids))
|
||||
rec.out_shipment_count = len(shipments)
|
||||
pickings |= line._get_out_pickings()
|
||||
rec.in_shipment_count = len(pickings)
|
||||
|
||||
def _compute_supplier_line_count(self):
|
||||
self.supplier_line_count = len(
|
||||
@@ -183,47 +167,31 @@ class RmaOrder(models.Model):
|
||||
vals["name"] = self.env["ir.sequence"].next_by_code("rma.order.customer")
|
||||
return super(RmaOrder, self).create(vals)
|
||||
|
||||
def _view_shipments(self, result, shipments):
|
||||
# choose the view_mode accordingly
|
||||
if len(shipments) > 1:
|
||||
result["domain"] = [("id", "in", shipments.ids)]
|
||||
elif len(shipments) == 1:
|
||||
res = self.env.ref("stock.view_picking_form", False)
|
||||
result["views"] = [(res and res.id or False, "form")]
|
||||
result["res_id"] = shipments.ids[0]
|
||||
return result
|
||||
|
||||
def action_view_in_shipments(self):
|
||||
action = self.env.ref("stock.action_picking_tree_all")
|
||||
result = action.sudo().read()[0]
|
||||
picking_ids = []
|
||||
shipments = self.env["stock.picking"]
|
||||
for line in self.rma_line_ids:
|
||||
for move in line.move_ids:
|
||||
if move.location_dest_id.usage == "internal":
|
||||
picking_ids.append(move.picking_id.id)
|
||||
else:
|
||||
if line.customer_to_supplier:
|
||||
picking_ids.append(move.picking_id.id)
|
||||
if picking_ids:
|
||||
shipments = list(set(picking_ids))
|
||||
# choose the view_mode accordingly
|
||||
if len(shipments) > 1:
|
||||
result["domain"] = [("id", "in", shipments)]
|
||||
else:
|
||||
res = self.env.ref("stock.view_picking_form", False)
|
||||
result["views"] = [(res and res.id or False, "form")]
|
||||
result["res_id"] = shipments[0]
|
||||
return result
|
||||
shipments |= line._get_in_pickings()
|
||||
return self._view_shipments(result, shipments)
|
||||
|
||||
def action_view_out_shipments(self):
|
||||
action = self.env.ref("stock.action_picking_tree_all")
|
||||
result = action.sudo().read()[0]
|
||||
picking_ids = []
|
||||
shipments = self.env["stock.picking"]
|
||||
for line in self.rma_line_ids:
|
||||
for move in line.move_ids:
|
||||
if move.location_dest_id.usage in ("supplier", "customer"):
|
||||
if not line.customer_to_supplier:
|
||||
picking_ids.append(move.picking_id.id)
|
||||
if picking_ids:
|
||||
shipments = list(set(picking_ids))
|
||||
# choose the view_mode accordingly
|
||||
if len(shipments) != 1:
|
||||
result["domain"] = [("id", "in", shipments)]
|
||||
else:
|
||||
res = self.env.ref("stock.view_picking_form", False)
|
||||
result["views"] = [(res and res.id or False, "form")]
|
||||
result["res_id"] = shipments[0]
|
||||
return result
|
||||
shipments |= line._get_out_pickings()
|
||||
return self._view_shipments(result, shipments)
|
||||
|
||||
def _get_valid_lines(self):
|
||||
""":return: A recordset of rma lines."""
|
||||
|
||||
@@ -42,27 +42,43 @@ class RmaOrderLine(models.Model):
|
||||
self.partner_id.address_get(["delivery"])["delivery"]
|
||||
)
|
||||
|
||||
@api.model
|
||||
def _get_in_pickings(self):
|
||||
# We consider an in move one where the first origin is outside
|
||||
# of the company and the final destination is outside. In case
|
||||
# of 2 or 3 step pickings, we should categorize as in shipments
|
||||
# even when they are technically internal transfers.
|
||||
pickings = self.env["stock.picking"]
|
||||
for move in self.move_ids:
|
||||
first_usage = move._get_first_usage()
|
||||
last_usage = move._get_last_usage()
|
||||
if last_usage == "internal" and first_usage != "internal":
|
||||
pickings |= move.picking_id
|
||||
elif last_usage == "supplier" and first_usage == "customer":
|
||||
pickings |= move.picking_id
|
||||
return pickings
|
||||
|
||||
@api.model
|
||||
def _get_out_pickings(self):
|
||||
pickings = self.env["stock.picking"]
|
||||
for move in self.move_ids:
|
||||
first_usage = move._get_first_usage()
|
||||
last_usage = move._get_last_usage()
|
||||
if first_usage == "internal" and last_usage != "internal":
|
||||
pickings |= move.picking_id
|
||||
elif last_usage == "customer" and first_usage == "supplier":
|
||||
pickings |= move.picking_id
|
||||
return pickings
|
||||
|
||||
def _compute_in_shipment_count(self):
|
||||
for line in self:
|
||||
picking_ids = []
|
||||
for move in line.move_ids:
|
||||
if move.location_dest_id.usage == "internal":
|
||||
picking_ids.append(move.picking_id.id)
|
||||
else:
|
||||
if line.customer_to_supplier:
|
||||
picking_ids.append(move.picking_id.id)
|
||||
shipments = list(set(picking_ids))
|
||||
line.in_shipment_count = len(shipments)
|
||||
pickings = line._get_in_pickings()
|
||||
line.in_shipment_count = len(pickings)
|
||||
|
||||
def _compute_out_shipment_count(self):
|
||||
picking_ids = []
|
||||
for line in self:
|
||||
for move in line.move_ids:
|
||||
if move.location_dest_id.usage in ("supplier", "customer"):
|
||||
if not line.customer_to_supplier:
|
||||
picking_ids.append(move.picking_id.id)
|
||||
shipments = list(set(picking_ids))
|
||||
line.out_shipment_count = len(shipments)
|
||||
pickings = line._get_out_pickings()
|
||||
line.out_shipment_count = len(pickings)
|
||||
|
||||
def _get_rma_move_qty(self, states, direction="in"):
|
||||
for rec in self:
|
||||
@@ -669,42 +685,31 @@ class RmaOrderLine(models.Model):
|
||||
def action_view_in_shipments(self):
|
||||
action = self.env.ref("stock.action_picking_tree_all")
|
||||
result = action.sudo().read()[0]
|
||||
picking_ids = []
|
||||
shipments = self.env["stock.picking"]
|
||||
for line in self:
|
||||
for move in line.move_ids:
|
||||
if move.location_dest_id.usage == "internal":
|
||||
picking_ids.append(move.picking_id.id)
|
||||
else:
|
||||
if line.customer_to_supplier:
|
||||
picking_ids.append(move.picking_id.id)
|
||||
|
||||
shipments = list(set(picking_ids))
|
||||
shipments |= line._get_in_pickings()
|
||||
# choose the view_mode accordingly
|
||||
if len(shipments) != 1:
|
||||
result["domain"] = "[('id', 'in', " + str(shipments) + ")]"
|
||||
result["domain"] = "[('id', 'in', " + str(shipments.ids) + ")]"
|
||||
elif len(shipments) == 1:
|
||||
res = self.env.ref("stock.view_picking_form", False)
|
||||
result["views"] = [(res and res.id or False, "form")]
|
||||
result["res_id"] = shipments[0]
|
||||
result["res_id"] = shipments.ids[0]
|
||||
return result
|
||||
|
||||
def action_view_out_shipments(self):
|
||||
action = self.env.ref("stock.action_picking_tree_all")
|
||||
result = action.sudo().read()[0]
|
||||
picking_ids = []
|
||||
shipments = self.env["stock.picking"]
|
||||
for line in self:
|
||||
for move in line.move_ids:
|
||||
if move.location_dest_id.usage in ("supplier", "customer"):
|
||||
if not line.customer_to_supplier:
|
||||
picking_ids.append(move.picking_id.id)
|
||||
shipments = list(set(picking_ids))
|
||||
shipments |= line._get_out_pickings()
|
||||
# choose the view_mode accordingly
|
||||
if len(shipments) != 1:
|
||||
result["domain"] = "[('id', 'in', " + str(shipments) + ")]"
|
||||
result["domain"] = "[('id', 'in', " + str(shipments.ids) + ")]"
|
||||
elif len(shipments) == 1:
|
||||
res = self.env.ref("stock.view_picking_form", False)
|
||||
result["views"] = [(res and res.id or False, "form")]
|
||||
result["res_id"] = shipments[0]
|
||||
result["res_id"] = shipments.ids[0]
|
||||
return result
|
||||
|
||||
def action_view_rma_lines(self):
|
||||
|
||||
@@ -44,3 +44,19 @@ class StockMove(models.Model):
|
||||
if move.rma_line_id:
|
||||
move.partner_id = move.rma_line_id.partner_id.id or False
|
||||
return res
|
||||
|
||||
@api.model
|
||||
def _get_first_usage(self):
|
||||
if self.move_orig_ids:
|
||||
# We assume here that all origin moves come from the same place
|
||||
return self.move_orig_ids[0]._get_first_usage()
|
||||
else:
|
||||
return self.location_id.usage
|
||||
|
||||
@api.model
|
||||
def _get_last_usage(self):
|
||||
if self.move_dest_ids:
|
||||
# We assume here that all origin moves come from the same place
|
||||
return self.move_dest_ids[0]._get_last_usage()
|
||||
else:
|
||||
return self.location_dest_id.usage
|
||||
|
||||
Reference in New Issue
Block a user