|
| 1 | +from odoo import models, fields, api |
| 2 | +from odoo.exceptions import ValidationError, UserError |
| 3 | +from odoo.addons.queue_job.delay import group, chain |
| 4 | + |
| 5 | +from sqlalchemy import text |
| 6 | + |
| 7 | +class StockLocation(models.Model): |
| 8 | + _inherit = "stock.location" |
| 9 | + |
| 10 | + track_stage = fields.Boolean( |
| 11 | + string="Track location stage?", |
| 12 | + default=False, |
| 13 | + help="If enabled, the location stages will be tracked." |
| 14 | + ) |
| 15 | + stage_id = fields.Many2one( |
| 16 | + "location.stage", |
| 17 | + string="Stage" |
| 18 | + ) |
| 19 | + last_stage_id = fields.Many2one( |
| 20 | + "location.stage", |
| 21 | + string="Last stage" |
| 22 | + ) |
| 23 | + location_history_ids = fields.One2many( |
| 24 | + 'location.history', 'location_id', string="Location History" |
| 25 | + ) |
| 26 | + last_stage_validated = fields.Boolean( |
| 27 | + string="Is the last stage progress validated?" |
| 28 | + ) |
| 29 | + |
| 30 | + @api.constrains('stage_id') |
| 31 | + def check_stage_change(self): |
| 32 | + self.ensure_one() |
| 33 | + |
| 34 | + if self.stage_id and not any(g in self.env.user.groups_id for g in self.stage_id.change_group_ids): |
| 35 | + raise ValidationError("Stage change not allowed for your user") |
| 36 | + |
| 37 | + if self.past_stage_id and self.past_stage_id.validation and not self.last_stage_validated: |
| 38 | + raise ValidationError("Validation required") |
| 39 | + |
| 40 | + if self.stage_id != self.past_stage_id and self.past_stage_id: |
| 41 | + if not self.validate_stage_route(): |
| 42 | + raise ValidationError("Invalid stage change") |
| 43 | + else: |
| 44 | + if self.stage_id.is_first: |
| 45 | + self.create_location_history('free') |
| 46 | + elif self.stage_id.is_final: |
| 47 | + self.create_location_history('maint') |
| 48 | + elif self.stage_id.is_pause: |
| 49 | + self.create_location_history('paused') |
| 50 | + elif self.stage_id.is_use: |
| 51 | + if not self.verifyBDSource(): |
| 52 | + raise ValidationError("Database not found") |
| 53 | + else: |
| 54 | + self.with_delay().PLC_Complete() |
| 55 | + self.create_location_history('prog') |
| 56 | + elif self.past_stage_id.is_first and not self.macrolot_product_id and self.stage_id: |
| 57 | + raise ValidationError("Product required") |
| 58 | + elif self.past_stage_id.is_first and self.macrolot_product_id: |
| 59 | + self.create_new_macrolot() |
| 60 | + self.create_location_history('prog') |
| 61 | + elif self.past_stage_id.is_pause: |
| 62 | + self.create_location_history('reg') |
| 63 | + else: |
| 64 | + self.create_location_history('prog') |
| 65 | + elif not self.past_stage_id and self.stage_id: |
| 66 | + self.create_location_history('free') |
| 67 | + |
| 68 | + self.past_stage_id = self.stage_id |
| 69 | + |
| 70 | + if self.stage_id.validation: |
| 71 | + self.last_stage_validated = False |
| 72 | + else: |
| 73 | + self.last_stage_validated = True |
| 74 | + |
| 75 | + @api.constrains('location_history_ids') |
| 76 | + def check_last_history_change(self): |
| 77 | + last_history = self.env['location.history'].search( |
| 78 | + [('location_id', '=', self.id)], |
| 79 | + order='create_date desc', |
| 80 | + limit=1 |
| 81 | + ) |
| 82 | + if not last_history: |
| 83 | + self.last_stage_validated = True |
| 84 | + else: |
| 85 | + if last_history.registry_type == 'val': |
| 86 | + self.last_stage_validated = True |
| 87 | + |
| 88 | + def validate_stage_route(self): |
| 89 | + if not self.stage_id: |
| 90 | + return True |
| 91 | + destination_ids = self.last_stage_id.allowed_destination_location_ids |
| 92 | + return self.stage_id in destination_ids |
| 93 | + |
| 94 | + def create_location_history(self, type): |
| 95 | + history_vals = { |
| 96 | + 'location_id': self.id, |
| 97 | + 'before_stage_id': self.past_stage_id.id, |
| 98 | + 'after_stage_id': self.stage_id.id, |
| 99 | + 'registry_type': type, |
| 100 | + 'user_id': self.env.uid, |
| 101 | + } |
| 102 | + if self.actual_macrolot_id: |
| 103 | + history_vals['macrolot_id'] = self.actual_macrolot_id.id |
| 104 | + |
| 105 | + self.env['location.history'].create(history_vals) |
| 106 | + |
| 107 | + def validate_stage(self): |
| 108 | + if not any(g in self.env.user.groups_id for g in self.stage_id.validation_group_ids): |
| 109 | + raise ValidationError("Stage change not allowed for your user") |
| 110 | + else: |
| 111 | + self.create_location_history('val') |
| 112 | + self.last_stage_validated = True |
0 commit comments