|
| 1 | +# Copyright 2022 ForgeFlow S.L. |
| 2 | +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) |
| 3 | +from odoo import api, fields, models |
| 4 | + |
| 5 | + |
| 6 | +class RmaOrderLine(models.Model): |
| 7 | + _inherit = "rma.order.line" |
| 8 | + |
| 9 | + @api.depends( |
| 10 | + "move_ids", |
| 11 | + "move_ids.state", |
| 12 | + "move_ids.is_rma_scrap", |
| 13 | + "qty_scrap", |
| 14 | + "scrap_policy", |
| 15 | + "product_qty", |
| 16 | + "scrap_ids", |
| 17 | + "scrap_ids.state", |
| 18 | + "scrap_ids.is_rma_scrap", |
| 19 | + ) |
| 20 | + def _compute_qty_to_scrap(self): |
| 21 | + for rec in self: |
| 22 | + rec.qty_to_scrap = 0.0 |
| 23 | + if rec.scrap_policy == "ordered": |
| 24 | + rec.qty_to_scrap = rec.product_qty - rec.qty_scrap |
| 25 | + elif rec.scrap_policy == "received": |
| 26 | + rec.qty_to_scrap = rec.qty_received - rec.qty_scrap |
| 27 | + |
| 28 | + @api.depends( |
| 29 | + "move_ids", |
| 30 | + "move_ids.state", |
| 31 | + "move_ids.is_rma_scrap", |
| 32 | + "scrap_ids", |
| 33 | + "scrap_ids.state", |
| 34 | + "scrap_ids.is_rma_scrap", |
| 35 | + ) |
| 36 | + def _compute_qty_in_scrap(self): |
| 37 | + product_obj = self.env["uom.uom"] |
| 38 | + for rec in self: |
| 39 | + qty = 0.0 |
| 40 | + for move in self.scrap_ids.filtered(lambda m: m.state in ["draft"]): |
| 41 | + qty += product_obj._compute_quantity(move.scrap_qty, rec.uom_id) |
| 42 | + rec.qty_in_scrap = qty |
| 43 | + |
| 44 | + @api.depends("scrap_ids", "scrap_ids.state", "scrap_ids.is_rma_scrap") |
| 45 | + def _compute_qty_scrap(self): |
| 46 | + product_obj = self.env["uom.uom"] |
| 47 | + for rec in self: |
| 48 | + qty = 0.0 |
| 49 | + for move in rec.move_ids.filtered( |
| 50 | + lambda m: m.state in ["done"] and m.is_rma_scrap |
| 51 | + ): |
| 52 | + qty += product_obj._compute_quantity(move.product_uom_qty, rec.uom_id) |
| 53 | + rec.qty_scrap = qty |
| 54 | + |
| 55 | + def _compute_scrap_count(self): |
| 56 | + for line in self: |
| 57 | + line.scrap_count = len(self.scrap_ids) |
| 58 | + |
| 59 | + qty_to_scrap = fields.Float( |
| 60 | + copy=False, |
| 61 | + digits="Product Unit of Measure", |
| 62 | + readonly=True, |
| 63 | + compute="_compute_qty_to_scrap", |
| 64 | + store=True, |
| 65 | + ) |
| 66 | + qty_in_scrap = fields.Float( |
| 67 | + copy=False, |
| 68 | + digits="Product Unit of Measure", |
| 69 | + readonly=True, |
| 70 | + compute="_compute_qty_in_scrap", |
| 71 | + store=True, |
| 72 | + ) |
| 73 | + qty_scrap = fields.Float( |
| 74 | + copy=False, |
| 75 | + digits="Product Unit of Measure", |
| 76 | + readonly=True, |
| 77 | + compute="_compute_qty_scrap", |
| 78 | + store=True, |
| 79 | + ) |
| 80 | + scrap_policy = fields.Selection( |
| 81 | + selection=[ |
| 82 | + ("no", "Not required"), |
| 83 | + ("ordered", "Based on Ordered Quantities"), |
| 84 | + ("received", "Based on Received Quantities"), |
| 85 | + ], |
| 86 | + default="no", |
| 87 | + required=True, |
| 88 | + readonly=False, |
| 89 | + ) |
| 90 | + scrap_count = fields.Integer(compute="_compute_scrap_count", string="# Scraps") |
| 91 | + scrap_ids = fields.One2many("stock.scrap", "rma_line_id") |
| 92 | + |
| 93 | + @api.onchange("operation_id") |
| 94 | + def _onchange_operation_id(self): |
| 95 | + res = super()._onchange_operation_id() |
| 96 | + if self.operation_id: |
| 97 | + self.scrap_policy = self.operation_id.scrap_policy or "no" |
| 98 | + return res |
| 99 | + |
| 100 | + def action_view_scrap_transfers(self): |
| 101 | + action = self.env.ref("stock.action_stock_scrap") |
| 102 | + result = action.sudo().read()[0] |
| 103 | + if len(self.scrap_ids) > 1: |
| 104 | + result["domain"] = [("id", "in", self.scrap_ids.ids)] |
| 105 | + elif len(self.scrap_ids) == 1: |
| 106 | + res = self.env.ref("stock.stock_scrap_form_view", False) |
| 107 | + result["views"] = [(res and res.id or False, "form")] |
| 108 | + result["res_id"] = self.scrap_ids.ids[0] |
| 109 | + return result |
0 commit comments