|
| 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 StockWarehouse(models.Model): |
| 9 | + |
| 10 | + _inherit = "stock.warehouse" |
| 11 | + |
| 12 | + lot_remove_enabled = fields.Boolean( |
| 13 | + string="Enable Expired Lot Removal", |
| 14 | + help="If checked, a move will be planned reserving the expired lot.", |
| 15 | + ) |
| 16 | + |
| 17 | + lot_remove_orig_location_ids = fields.Many2many( |
| 18 | + comodel_name="stock.location", |
| 19 | + string="Expired Lot Origin Locations", |
| 20 | + help="Locations from which expired lots will be moved.", |
| 21 | + compute="_compute_lot_remove_orig_location_ids", |
| 22 | + store=True, |
| 23 | + readonly=False, |
| 24 | + ) |
| 25 | + |
| 26 | + lot_remove_picking_type_id = fields.Many2one( |
| 27 | + comodel_name="stock.picking.type", |
| 28 | + string="Expired Lot Move Picking Type", |
| 29 | + help="Picking type used for moving expired lots.", |
| 30 | + ) |
| 31 | + |
| 32 | + @api.depends("lot_remove_enabled") |
| 33 | + def _onchange_lot_remove_enabled(self): |
| 34 | + """Ensure that the origin and destination locations are set when |
| 35 | + enabling expired lot move.""" |
| 36 | + if self.lot_remove_enabled and not self.lot_remove_orig_location_ids: |
| 37 | + self.lot_remove_orig_location_ids = self.lot_stock_id |
| 38 | + |
| 39 | + @api.constrains("lot_remove_orig_location_ids", "lot_remove_enabled") |
| 40 | + def _check_expired_lot_locations(self): |
| 41 | + """Ensure that: |
| 42 | + * the origin location is set, |
| 43 | + * the origin location is from the current warehouse, |
| 44 | + """ |
| 45 | + for record in self: |
| 46 | + if record.lot_remove_enabled: |
| 47 | + if not record.lot_remove_orig_location_ids: |
| 48 | + raise ValidationError( |
| 49 | + _( |
| 50 | + "Please set at least one origin location for expired lot moves." |
| 51 | + ) |
| 52 | + ) |
| 53 | + if record.lot_remove_orig_location_ids.mapped("warehouse_id") != record: |
| 54 | + raise ValidationError( |
| 55 | + _( |
| 56 | + "The origin locations for expired lot moves must be from the " |
| 57 | + "current warehouse." |
| 58 | + ) |
| 59 | + ) |
| 60 | + |
| 61 | + @api.constrains("lot_remove_picking_type_id", "lot_remove_enabled") |
| 62 | + def _check_lot_remove_picking_type(self): |
| 63 | + """Ensure that the picking type for expired lot moves is set when |
| 64 | + expired lot move is enabled.""" |
| 65 | + for record in self: |
| 66 | + if record.lot_remove_enabled and not record.lot_remove_picking_type_id: |
| 67 | + raise ValidationError( |
| 68 | + _("Please set the picking type for expired lot moves.") |
| 69 | + ) |
| 70 | + |
| 71 | + def _get_picking_type_create_values(self, max_sequence): |
| 72 | + """Override to set the picking type for expired lot moves.""" |
| 73 | + create_data, max_sequence = super()._get_picking_type_create_values( |
| 74 | + max_sequence |
| 75 | + ) |
| 76 | + max_sequence += 1 |
| 77 | + create_data["lot_remove_picking_type_id"] = { |
| 78 | + "name": _("Expired Lot Removal"), |
| 79 | + "code": "internal", |
| 80 | + "use_create_lots": False, |
| 81 | + "use_existing_lots": True, |
| 82 | + "default_location_src_id": self.lot_stock_id.id, |
| 83 | + "default_location_dest_id": self.wh_qc_stock_loc_id.id, |
| 84 | + "sequence": max_sequence, |
| 85 | + "sequence_code": "EXP", |
| 86 | + "company_id": self.company_id.id, |
| 87 | + } |
| 88 | + return create_data, max_sequence |
| 89 | + |
| 90 | + def _get_sequence_values(self, name=False, code=False): |
| 91 | + values = super(StockWarehouse, self)._get_sequence_values(name=name, code=code) |
| 92 | + count = self.env["ir.sequence"].search_count( |
| 93 | + [("prefix", "like", self.code + "/EXP%/%")] |
| 94 | + ) |
| 95 | + values.update( |
| 96 | + { |
| 97 | + "lot_remove_picking_type_id": { |
| 98 | + "name": self.name + " " + _("Sequence Expired Lot Removal"), |
| 99 | + "prefix": self.code |
| 100 | + + "/" |
| 101 | + + ( |
| 102 | + self.lot_remove_picking_type_id.sequence_code |
| 103 | + or (("EXP" + str(count)) if count else "EXP") |
| 104 | + ) |
| 105 | + + "/", |
| 106 | + "padding": 5, |
| 107 | + "company_id": self.company_id.id, |
| 108 | + }, |
| 109 | + } |
| 110 | + ) |
| 111 | + return values |
| 112 | + |
| 113 | + @api.model |
| 114 | + def _cron_remove_expired_lots(self): |
| 115 | + """Cron job to remove expired lots.""" |
| 116 | + warehouses = self.env["stock.warehouse"].search( |
| 117 | + [("lot_remove_enabled", "=", True)] |
| 118 | + ) |
| 119 | + for warehouse in warehouses: |
| 120 | + wizard = self.env["stock.lot.removal.wizard"].create( |
| 121 | + { |
| 122 | + "warehouse_id": warehouse.id, |
| 123 | + } |
| 124 | + ) |
| 125 | + wizard.action_run() |
0 commit comments