diff --git a/rma_scrap/README.rst b/rma_scrap/README.rst index ab2a49b9..de2fe17c 100644 --- a/rma_scrap/README.rst +++ b/rma_scrap/README.rst @@ -36,7 +36,8 @@ Credits Contributors ------------ -* David Jimenez +* Jordi Ballester Alomar +* David Jimenez Maintainer diff --git a/rma_scrap/__manifest__.py b/rma_scrap/__manifest__.py index e45d13e4..60f1a017 100644 --- a/rma_scrap/__manifest__.py +++ b/rma_scrap/__manifest__.py @@ -1,9 +1,9 @@ { - "name": "RMA Put Away", + "name": "RMA Scrap", "version": "14.0.1.0.0", "license": "LGPL-3", "category": "RMA", - "summary": "Allows to put away the received products in odoo", + "summary": "Allows to scrap the received/ordered products in odoo", "author": "ForgeFlow", "website": "https://github.com/ForgeFlow/stock-rma", "depends": ["rma"], @@ -12,6 +12,7 @@ "views/rma_operation_view.xml", "views/rma_order_line_view.xml", "views/rma_order_view.xml", + "views/stock_scrap_view.xml", "wizards/rma_scrap_view.xml", ], "installable": True, diff --git a/rma_scrap/models/rma_order.py b/rma_scrap/models/rma_order.py index a38e22ac..42aa677c 100644 --- a/rma_scrap/models/rma_order.py +++ b/rma_scrap/models/rma_order.py @@ -8,12 +8,8 @@ class RmaOrder(models.Model): def _compute_scrap_count(self): for order in self: - moves = ( - order.mapped("rma_line_ids.move_ids") - .filtered(lambda m: m.is_rma_scrap) - .move_line_ids - ) - order.scrap_count = len(moves) + scraps = order.mapped("rma_line_ids.scrap_ids") + order.scrap_count = len(scraps) scrap_count = fields.Integer(compute="_compute_scrap_count", string="# Scrap") diff --git a/rma_scrap/models/rma_order_line.py b/rma_scrap/models/rma_order_line.py index 654af904..92b824f5 100644 --- a/rma_scrap/models/rma_order_line.py +++ b/rma_scrap/models/rma_order_line.py @@ -13,6 +13,9 @@ class RmaOrderLine(models.Model): "qty_scrap", "scrap_policy", "product_qty", + "scrap_ids", + "scrap_ids.state", + "scrap_ids.is_rma_scrap", ) def _compute_qty_to_scrap(self): for rec in self: @@ -22,18 +25,23 @@ class RmaOrderLine(models.Model): elif rec.scrap_policy == "received": rec.qty_to_scrap = rec.qty_received - rec.qty_scrap - @api.depends("move_ids", "move_ids.state", "move_ids.is_rma_scrap") + @api.depends( + "move_ids", + "move_ids.state", + "move_ids.is_rma_scrap", + "scrap_ids", + "scrap_ids.state", + "scrap_ids.is_rma_scrap", + ) def _compute_qty_in_scrap(self): product_obj = self.env["uom.uom"] for rec in self: qty = 0.0 - for move in self.env["stock.scrap"].search( - [("origin", "=", rec.name), ("state", "=", "draft")] - ): + for move in self.scrap_ids.filtered(lambda m: m.state in ["draft"]): qty += product_obj._compute_quantity(move.scrap_qty, rec.uom_id) rec.qty_in_scrap = qty - @api.depends("move_ids", "move_ids.state", "move_ids.is_rma_scrap") + @api.depends("scrap_ids", "scrap_ids.state", "scrap_ids.is_rma_scrap") def _compute_qty_scrap(self): product_obj = self.env["uom.uom"] for rec in self: @@ -46,8 +54,7 @@ class RmaOrderLine(models.Model): def _compute_scrap_count(self): for line in self: - scraps = self.env["stock.scrap"].search([("origin", "=", line.name)]) - line.scrap_count = len(scraps) + line.scrap_count = len(self.scrap_ids) qty_to_scrap = fields.Float( string="Qty To Scrap", @@ -85,6 +92,7 @@ class RmaOrderLine(models.Model): readonly=False, ) scrap_count = fields.Integer(compute="_compute_scrap_count", string="# Scraps") + scrap_ids = fields.One2many("stock.scrap", "rma_line_id") @api.onchange("operation_id") def _onchange_operation_id(self): @@ -96,11 +104,10 @@ class RmaOrderLine(models.Model): def action_view_scrap_transfers(self): action = self.env.ref("stock.action_stock_scrap") result = action.sudo().read()[0] - scraps = self.env["stock.scrap"].search([("origin", "=", self.name)]) - if len(scraps) > 1: - result["domain"] = [("id", "in", scraps.ids)] - elif len(scraps) == 1: + if len(self.scrap_ids) > 1: + result["domain"] = [("id", "in", self.scrap_ids.ids)] + elif len(self.scrap_ids) == 1: res = self.env.ref("stock.stock_scrap_form_view", False) result["views"] = [(res and res.id or False, "form")] - result["res_id"] = scraps.ids[0] + result["res_id"] = self.scrap_ids.ids[0] return result diff --git a/rma_scrap/models/stock_move.py b/rma_scrap/models/stock_move.py index 68ddfe94..ac26f009 100644 --- a/rma_scrap/models/stock_move.py +++ b/rma_scrap/models/stock_move.py @@ -12,9 +12,3 @@ class StockMove(models.Model): copy=False, help="This Stock Move has been created from a Scrap operation in " "the RMA.", ) - - def _is_in_out_rma_move(self, op, states, location_type): - res = super(StockMove, self)._is_in_out_rma_move(op, states, location_type) - if self.is_rma_scrap: - return False - return res diff --git a/rma_scrap/models/stock_scrap.py b/rma_scrap/models/stock_scrap.py index 49dd7ac9..0613d9e4 100644 --- a/rma_scrap/models/stock_scrap.py +++ b/rma_scrap/models/stock_scrap.py @@ -21,3 +21,21 @@ class StockScrap(models.Model): if self.is_rma_scrap: self.move_id.is_rma_scrap = True self.rma_line_id.move_ids |= self.move_id + + def _prepare_move_values(self): + res = super(StockScrap, self)._prepare_move_values() + res["rma_line_id"] = self.rma_line_id.id + return res + + def action_view_rma_line(self): + if self.rma_line_id.type == "customer": + action = self.env.ref("rma.action_rma_customer_lines") + res = self.env.ref("rma.view_rma_line_form", False) + else: + action = self.env.ref("rma.action_rma_supplier_lines") + res = self.env.ref("rma.view_rma_line_supplier_form", False) + result = action.sudo().read()[0] + # choose the view_mode accordingly + result["views"] = [(res and res.id or False, "form")] + result["res_id"] = self.rma_line_id.id + return result diff --git a/rma_scrap/views/stock_scrap_view.xml b/rma_scrap/views/stock_scrap_view.xml new file mode 100644 index 00000000..4eda2969 --- /dev/null +++ b/rma_scrap/views/stock_scrap_view.xml @@ -0,0 +1,23 @@ + + + + stock.scrap.form - RMA + stock.scrap + + + + + + + + diff --git a/rma_scrap/wizards/rma_make_scrap.py b/rma_scrap/wizards/rma_make_scrap.py index f687d995..1c393ace 100644 --- a/rma_scrap/wizards/rma_make_scrap.py +++ b/rma_scrap/wizards/rma_make_scrap.py @@ -22,6 +22,7 @@ class RmaMakeScrap(models.TransientModel): "product_id": line.product_id.id, "product_qty": line.product_qty, "location_id": line.location_id.id, + "scrap_location_id": line.operation_id.scrap_location_id.id, "uom_id": line.uom_id.id, "qty_to_scrap": line.qty_to_scrap, "line_id": line.id, @@ -57,11 +58,11 @@ class RmaMakeScrap(models.TransientModel): raise ValidationError(_("RMA %s is not approved") % line.name) scrap = self._prepare_scrap(item) scraps.append(scrap) + item.line_id.scrap_ids |= scrap return scraps def action_create_scrap(self): self._create_scrap() - self.item_ids[0].line_id._compute_qty_in_scrap() return self.item_ids[0].line_id.action_view_scrap_transfers() @api.model @@ -74,12 +75,12 @@ class RmaMakeScrap(models.TransientModel): "product_id": item.line_id.product_id.id, "scrap_qty": item.qty_to_scrap, "product_uom_id": item.line_id.product_id.product_tmpl_id.uom_id.id, - "scrap_location_id": line.operation_id.scrap_location_id.id, + "location_id": item.location_id.id, + "scrap_location_id": item.scrap_location_id.id, "rma_line_id": line.id, "create_date": fields.Datetime.now(), "company_id": line.company_id.id, "is_rma_scrap": True, - "location_id": line.location_id.id, } ) return scrap @@ -91,19 +92,27 @@ class RmaMakeScrapItem(models.TransientModel): wiz_id = fields.Many2one("rma_make_scrap.wizard", string="Wizard", required=True) line_id = fields.Many2one( - "rma.order.line", string="RMA order Line", ondelete="cascade" + "rma.order.line", string="RMA order Line", ondelete="cascade", required=True ) rma_id = fields.Many2one("rma.order", related="line_id.rma_id", string="RMA Group") - product_id = fields.Many2one("product.product", string="Product") + product_id = fields.Many2one("product.product", string="Product", required=True) product_qty = fields.Float( related="line_id.product_qty", string="Quantity Ordered", copy=False, digits="Product Unit of Measure", ) + company_id = fields.Many2one("res.company", related="line_id.company_id") location_id = fields.Many2one( + "stock.location", + string="Source Location", + required=True, + domain="[('usage', '=', 'internal'), ('company_id', 'in', [company_id, False])]", + ) + scrap_location_id = fields.Many2one( "stock.location", string="Scrap Location", + required=True, domain="[('scrap_location', '=', True)]", ) qty_to_scrap = fields.Float( diff --git a/rma_scrap/wizards/rma_scrap_view.xml b/rma_scrap/wizards/rma_scrap_view.xml index 2f7bfe89..03bedb49 100644 --- a/rma_scrap/wizards/rma_scrap_view.xml +++ b/rma_scrap/wizards/rma_scrap_view.xml @@ -13,6 +13,8 @@ + +