|
| 1 | +# Copyright 2025 Manuel Regidor <manuel.regidor@sygel.es> |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +from odoo import models |
| 5 | + |
| 6 | + |
| 7 | +class RepairOrder(models.Model): |
| 8 | + _inherit = "repair.order" |
| 9 | + |
| 10 | + def _get_complete_repairs_to_validate(self): |
| 11 | + return self.filtered(lambda a: a.state == "draft") |
| 12 | + |
| 13 | + def _get_complete_repairs_to_start(self): |
| 14 | + return self.filtered( |
| 15 | + lambda a: ( |
| 16 | + a.state == "confirmed" and a.invoice_method in ["none", "after_repair"] |
| 17 | + ) |
| 18 | + or (a.state == "ready" and a.invoice_method == "b4repair") |
| 19 | + ) |
| 20 | + |
| 21 | + def _get_complete_repairs_to_finish(self): |
| 22 | + return self.filtered( |
| 23 | + lambda a: ( |
| 24 | + a.state == "under_repair" |
| 25 | + and a.invoice_method in ["none", "after_repair"] |
| 26 | + ) |
| 27 | + or (a.invoice_method == "b4repair" and a.state == "under_repair") |
| 28 | + ) |
| 29 | + |
| 30 | + def _get_complete_repairs_to_invoice(self): |
| 31 | + return self.filtered(lambda a: a.state == "2binvoiced") |
| 32 | + |
| 33 | + def complete_repair_validate(self): |
| 34 | + draft_repair_orders = self._get_complete_repairs_to_validate() |
| 35 | + for repair in draft_repair_orders: |
| 36 | + repair.action_validate() |
| 37 | + |
| 38 | + def complete_repair_start(self): |
| 39 | + self._get_complete_repairs_to_start().action_repair_start() |
| 40 | + |
| 41 | + def complete_repair_end(self): |
| 42 | + repair_orders_to_finish = self._get_complete_repairs_to_finish() |
| 43 | + repair_orders_to_finish.action_repair_end() |
| 44 | + |
| 45 | + def complete_repair_invoice(self, group): |
| 46 | + repairs_to_invoice = self._get_complete_repairs_to_invoice() |
| 47 | + repairs_to_invoice._create_invoices(group) |
| 48 | + # It is necessary to call action_repair_invoice_create after creating |
| 49 | + # the invoice so the state of the repairs change. It is dones with the |
| 50 | + # invoicing wizard in the same way. |
| 51 | + repairs_to_invoice.action_repair_invoice_create() |
| 52 | + |
| 53 | + def complete(self, invoice=False, group=False): |
| 54 | + |
| 55 | + # 1. Validate repairs in draft state |
| 56 | + self.complete_repair_validate() |
| 57 | + |
| 58 | + # 2. Start confirmed repairs with no invoice method or invoice method after repair |
| 59 | + self.complete_repair_start() |
| 60 | + |
| 61 | + # 3. Complete repairs with no invoice method or invoice method after repair |
| 62 | + self.complete_repair_end() |
| 63 | + |
| 64 | + # 4. Invoice repairs to be invoiced |
| 65 | + if invoice: |
| 66 | + self.complete_repair_invoice(group) |
| 67 | + |
| 68 | + # 5. action_repair_start is called again so the repairs with invoice method |
| 69 | + # before repair that has just been invoiced can be started |
| 70 | + self.complete_repair_start() |
| 71 | + |
| 72 | + # 6. complete_repair_end is called again so the repairs with invoice method |
| 73 | + # before repair that has just been invoiced can be finished |
| 74 | + self.complete_repair_end() |
0 commit comments