|
| 1 | +# Copyright 2024 Camptocamp |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +from odoo import _, api, models |
| 5 | +from odoo.exceptions import ValidationError |
| 6 | + |
| 7 | + |
| 8 | +class FSMOrder(models.Model): |
| 9 | + _inherit = "fsm.order" |
| 10 | + |
| 11 | + def _prepare_return_procurement_group_values(self): |
| 12 | + return { |
| 13 | + "name": self.display_name, |
| 14 | + "fsm_order_id": self.id, |
| 15 | + "move_type": "direct", |
| 16 | + } |
| 17 | + |
| 18 | + def _prepare_return_stock_picking_values(self): |
| 19 | + source_location_id = self._get_equipment_current_location() |
| 20 | + return { |
| 21 | + "picking_type_id": self.type.picking_type_id.id, |
| 22 | + "origin": self.display_name, |
| 23 | + "location_dest_id": self.type.picking_type_id.default_location_dest_id.id, |
| 24 | + "location_id": source_location_id and source_location_id.id, |
| 25 | + "fsm_order_id": self.id, |
| 26 | + "group_id": self.procurement_group_id.id, |
| 27 | + } |
| 28 | + |
| 29 | + def _prepare_return_stock_move_values(self): |
| 30 | + source_location_id = self._get_equipment_current_location() |
| 31 | + return { |
| 32 | + "name": self.display_name, |
| 33 | + "product_id": self.equipment_id.product_id.id, |
| 34 | + "product_uom_qty": 1, |
| 35 | + "product_uom": self.equipment_id.product_id.uom_id.id, |
| 36 | + "location_id": source_location_id.id, |
| 37 | + "location_dest_id": self.type.picking_type_id.default_location_dest_id.id, |
| 38 | + "group_id": self.procurement_group_id.id, |
| 39 | + "fsm_order_id": self.id, |
| 40 | + "lot_ids": [(4, self.equipment_id.lot_id.id)] |
| 41 | + if self.equipment_id.lot_id |
| 42 | + else False, |
| 43 | + } |
| 44 | + |
| 45 | + def _get_equipment_current_location(self): |
| 46 | + self.ensure_one() |
| 47 | + return self.equipment_id and self.equipment_id.location_id.inventory_location_id |
| 48 | + |
| 49 | + @api.model |
| 50 | + def create(self, vals): |
| 51 | + # if FSM order with type return is created then |
| 52 | + # create a picking order |
| 53 | + order = super().create(vals) |
| 54 | + if order.type.internal_type == "return": |
| 55 | + if order.equipment_id and order.type.picking_type_id: |
| 56 | + group = self.env["procurement.group"].search( |
| 57 | + [("fsm_order_id", "=", order.id)] |
| 58 | + ) |
| 59 | + if not group: |
| 60 | + values = order._prepare_return_procurement_group_values() |
| 61 | + group = self.env["procurement.group"].create(values) |
| 62 | + order.procurement_group_id = group and group.id |
| 63 | + return_picking_values = order._prepare_return_stock_picking_values() |
| 64 | + new_picking = self.env["stock.picking"].create(return_picking_values) |
| 65 | + return_move_values = order._prepare_return_stock_move_values() |
| 66 | + return_move_values["picking_id"] = new_picking.id |
| 67 | + self.env["stock.move"].create(return_move_values) |
| 68 | + new_picking.action_confirm() |
| 69 | + |
| 70 | + elif not order.type.pick_type_id: |
| 71 | + raise ValidationError( |
| 72 | + _( |
| 73 | + "Cannot create Return Order because " |
| 74 | + "order type does not have a current " |
| 75 | + "Picking Type." |
| 76 | + ) |
| 77 | + ) |
| 78 | + return order |
0 commit comments