|
| 1 | +# Copyright 2025 ACSONE SA/NV |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +from odoo import _, api, fields, models |
| 5 | +from odoo.exceptions import ValidationError |
| 6 | + |
| 7 | + |
| 8 | +class RepairOrder(models.Model): |
| 9 | + |
| 10 | + _inherit = "repair.order" |
| 11 | + |
| 12 | + preparation_picking_type_id = fields.Many2one( |
| 13 | + "stock.picking.type", |
| 14 | + help="Picking type used to bring spare parts before the repair starts.", |
| 15 | + domain="[('warehouse_id.company_id', '=', company_id)]", |
| 16 | + compute="_compute_preparation_picking_type_id", |
| 17 | + readonly=False, |
| 18 | + store=True, |
| 19 | + ) |
| 20 | + preparation_group_id = fields.Many2one( |
| 21 | + "procurement.group", |
| 22 | + string="Preparation Procurement Group", |
| 23 | + readonly=True, |
| 24 | + copy=False, |
| 25 | + help="Procurement group used to gather all preparation moves/pickings.", |
| 26 | + ) |
| 27 | + stock_move_ids = fields.Many2many("stock.move", compute="_compute_stock_move_ids") |
| 28 | + preparation_picking_ids = fields.Many2many( |
| 29 | + "stock.picking", compute="_compute_preparation_picking_ids" |
| 30 | + ) |
| 31 | + repair_preparation_enabled = fields.Boolean( |
| 32 | + related="company_id.repair_preparation_enabled", readonly=False |
| 33 | + ) |
| 34 | + |
| 35 | + @api.depends("company_id", "repair_preparation_enabled") |
| 36 | + def _compute_preparation_picking_type_id(self): |
| 37 | + for rec in self: |
| 38 | + if rec.repair_preparation_enabled and not rec.preparation_picking_type_id: |
| 39 | + rec.preparation_picking_type_id = ( |
| 40 | + rec.company_id.repair_preparation_picking_type_id |
| 41 | + ) |
| 42 | + |
| 43 | + @api.depends("operations.preparation_move_ids", "operations.move_id") |
| 44 | + def _compute_stock_move_ids(self): |
| 45 | + for rec in self: |
| 46 | + rec.stock_move_ids = ( |
| 47 | + rec.operations.preparation_move_ids + rec.operations.move_id |
| 48 | + ) |
| 49 | + |
| 50 | + @api.depends("operations.preparation_move_ids") |
| 51 | + def _compute_preparation_picking_ids(self): |
| 52 | + for rec in self: |
| 53 | + rec.preparation_picking_ids = rec.operations.preparation_move_ids.picking_id |
| 54 | + |
| 55 | + def _ensure_preparation_group(self): |
| 56 | + self.ensure_one() |
| 57 | + if not self.preparation_group_id: |
| 58 | + self.preparation_group_id = self.env["procurement.group"].create( |
| 59 | + { |
| 60 | + "name": _("Preparation for") + self.name, |
| 61 | + "partner_id": self.partner_id.id, |
| 62 | + } |
| 63 | + ) |
| 64 | + |
| 65 | + @api.model |
| 66 | + def _get_consumed_lines_for_preparation(self, operations): |
| 67 | + return operations.filtered( |
| 68 | + lambda line: line.type == "add" |
| 69 | + and line.product_id |
| 70 | + and line.product_uom_qty > 0 |
| 71 | + ) |
| 72 | + |
| 73 | + def _get_repair_line_procurement_values(self, line): |
| 74 | + return { |
| 75 | + "company_id": self.company_id, |
| 76 | + "group_id": self.preparation_group_id, |
| 77 | + "warehouse_id": self.preparation_picking_type_id.warehouse_id, |
| 78 | + "picking_type_id": self.preparation_picking_type_id.id, |
| 79 | + "repair_line_id": line.id, |
| 80 | + } |
| 81 | + |
| 82 | + def _get_repair_line_procurement(self, line): |
| 83 | + return self.env["procurement.group"].Procurement( |
| 84 | + line.product_id, |
| 85 | + line.product_uom_qty, |
| 86 | + line.product_uom, |
| 87 | + self.preparation_picking_type_id.default_location_dest_id, |
| 88 | + f"{self.name} {line.product_id.display_name}", |
| 89 | + self.name, |
| 90 | + self.company_id, |
| 91 | + self._get_repair_line_procurement_values(line), |
| 92 | + ) |
| 93 | + |
| 94 | + def _run_preparation_procurements(self, operations): |
| 95 | + self.ensure_one() |
| 96 | + if not operations: |
| 97 | + return |
| 98 | + if not self.preparation_picking_type_id: |
| 99 | + return |
| 100 | + lines = self._get_consumed_lines_for_preparation(operations) |
| 101 | + if not lines: |
| 102 | + return |
| 103 | + self._ensure_preparation_group() |
| 104 | + self.env["procurement.group"].run( |
| 105 | + [self._get_repair_line_procurement(line) for line in lines] |
| 106 | + ) |
| 107 | + |
| 108 | + def action_validate(self): |
| 109 | + res = super().action_validate() |
| 110 | + for rec in self: |
| 111 | + if not rec.repair_preparation_enabled: |
| 112 | + continue |
| 113 | + rec._run_preparation_procurements(rec.operations) |
| 114 | + return res |
| 115 | + |
| 116 | + def action_repair_end(self): |
| 117 | + for rec in self: |
| 118 | + if not rec.repair_preparation_enabled: |
| 119 | + continue |
| 120 | + consumed_lines = rec._get_consumed_lines_for_preparation(rec.operations) |
| 121 | + if consumed_lines and not rec.preparation_picking_ids: |
| 122 | + raise ValidationError( |
| 123 | + _( |
| 124 | + "Preparation picking not found. Please procure/prepare parts " |
| 125 | + "first." |
| 126 | + ) |
| 127 | + ) |
| 128 | + if consumed_lines and rec.preparation_picking_ids.filtered( |
| 129 | + lambda p: p.state != "done" |
| 130 | + ): |
| 131 | + raise ValidationError( |
| 132 | + _( |
| 133 | + "Preparation picking is not done yet. Validate it before " |
| 134 | + "starting the repair." |
| 135 | + ) |
| 136 | + ) |
| 137 | + return super().action_repair_end() |
| 138 | + |
| 139 | + def action_view_preparation_picking(self): |
| 140 | + self.ensure_one() |
| 141 | + action_xmlid = "stock.action_picking_tree_all" |
| 142 | + action = self.env["ir.actions.act_window"]._for_xml_id(action_xmlid) |
| 143 | + action["domain"] = [("id", "in", self.preparation_picking_ids.ids)] |
| 144 | + return action |
| 145 | + |
| 146 | + def action_view_move(self): |
| 147 | + self.ensure_one() |
| 148 | + return { |
| 149 | + "type": "ir.actions.act_window", |
| 150 | + "name": _("Moves"), |
| 151 | + "res_model": self.stock_move_ids._name, |
| 152 | + "domain": [("id", "in", self.stock_move_ids.ids)], |
| 153 | + "view_mode": "tree,form", |
| 154 | + "context": self.env.context, |
| 155 | + } |
0 commit comments