1- from odoo import models , fields , api
2- from odoo .exceptions import ValidationError , UserError
3- from odoo .addons .queue_job .delay import group , chain
1+ from odoo import _ , api , fields , models
2+ from odoo .exceptions import ValidationError
43
5- from sqlalchemy import text
64
75class StockLocation (models .Model ):
86 _inherit = "stock.location"
97
108 track_stage = fields .Boolean (
119 string = "Track location stage?" ,
1210 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"
11+ help = "If enabled, the location stages will be tracked." ,
2212 )
13+ stage_id = fields .Many2one ("location.stage" , string = "Stage" )
14+ last_stage_id = fields .Many2one ("location.stage" , string = "Last stage" )
2315 location_history_ids = fields .One2many (
24- ' location.history' , ' location_id' , string = "Location History"
16+ " location.history" , " location_id" , string = "Location History"
2517 )
2618 last_stage_validated = fields .Boolean (
2719 string = "Is the last stage progress validated?"
2820 )
2921
30- @api .constrains (' stage_id' )
22+ @api .constrains (" stage_id" )
3123 def check_stage_change (self ):
3224 self .ensure_one ()
3325
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" )
26+ if self .stage_id and not any (
27+ g in self .env .user .groups_id for g in self .stage_id .change_group_ids
28+ ):
29+ raise ValidationError (_ ("Stage change not allowed for your user" ))
3630
37- if self .past_stage_id and self .past_stage_id .validation and not self .last_stage_validated :
38- raise ValidationError ("Validation required" )
31+ if (
32+ self .past_stage_id
33+ and self .past_stage_id .validation
34+ and not self .last_stage_validated
35+ ):
36+ raise ValidationError (_ ("Validation required" ))
3937
4038 if self .stage_id != self .past_stage_id and self .past_stage_id :
4139 if not self .validate_stage_route ():
42- raise ValidationError ("Invalid stage change" )
40+ raise ValidationError (_ ( "Invalid stage change" ) )
4341 else :
4442 if self .stage_id .is_first :
45- self .create_location_history (' free' )
43+ self .create_location_history (" free" )
4644 elif self .stage_id .is_final :
47- self .create_location_history (' maint' )
45+ self .create_location_history (" maint" )
4846 elif self .stage_id .is_pause :
49- self .create_location_history (' paused' )
47+ self .create_location_history (" paused" )
5048 elif self .stage_id .is_use :
5149 if not self .verifyBDSource ():
52- raise ValidationError ("Database not found" )
50+ raise ValidationError (_ ( "Database not found" ) )
5351 else :
5452 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" )
53+ self .create_location_history ("prog" )
54+ elif (
55+ self .past_stage_id .is_first
56+ and not self .macrolot_product_id
57+ and self .stage_id
58+ ):
59+ raise ValidationError (_ ("Product required" ))
5860 elif self .past_stage_id .is_first and self .macrolot_product_id :
5961 self .create_new_macrolot ()
60- self .create_location_history (' prog' )
62+ self .create_location_history (" prog" )
6163 elif self .past_stage_id .is_pause :
62- self .create_location_history (' reg' )
64+ self .create_location_history (" reg" )
6365 else :
64- self .create_location_history (' prog' )
66+ self .create_location_history (" prog" )
6567 elif not self .past_stage_id and self .stage_id :
66- self .create_location_history (' free' )
68+ self .create_location_history (" free" )
6769
6870 self .past_stage_id = self .stage_id
6971
@@ -72,17 +74,15 @@ def check_stage_change(self):
7274 else :
7375 self .last_stage_validated = True
7476
75- @api .constrains (' location_history_ids' )
77+ @api .constrains (" location_history_ids" )
7678 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
79+ last_history = self .env ["location.history" ].search (
80+ [("location_id" , "=" , self .id )], order = "create_date desc" , limit = 1
8181 )
8282 if not last_history :
8383 self .last_stage_validated = True
8484 else :
85- if last_history .registry_type == ' val' :
85+ if last_history .registry_type == " val" :
8686 self .last_stage_validated = True
8787
8888 def validate_stage_route (self ):
@@ -91,22 +91,24 @@ def validate_stage_route(self):
9191 destination_ids = self .last_stage_id .allowed_destination_location_ids
9292 return self .stage_id in destination_ids
9393
94- def create_location_history (self , type ):
94+ def create_location_history (self , registry_type ):
9595 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 ,
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" : registry_type ,
100+ " user_id" : self .env .uid ,
101101 }
102102 if self .actual_macrolot_id :
103- history_vals [' macrolot_id' ] = self .actual_macrolot_id .id
103+ history_vals [" macrolot_id" ] = self .actual_macrolot_id .id
104104
105- self .env [' location.history' ].create (history_vals )
105+ self .env [" location.history" ].create (history_vals )
106106
107107 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' )
108+ if not any (
109+ g in self .env .user .groups_id for g in self .stage_id .validation_group_ids
110+ ):
111+ raise ValidationError (_ ("Stage change not allowed for your user" ))
112+ else :
113+ self .create_location_history ("val" )
112114 self .last_stage_validated = True
0 commit comments