1- from odoo import api , fields , models
1+ from odoo import _ , api , fields , models
2+ from odoo .exceptions import ValidationError
23
34
45class SPPCRDetailUpdateID (models .Model ):
@@ -15,7 +16,7 @@ class SPPCRDetailUpdateID(models.Model):
1516 operation = fields .Selection (
1617 [
1718 ("add" , "Add New ID" ),
18- ("update" , "Update Existing ID" ),
19+ ("update" , "Edit ID" ),
1920 ("remove" , "Remove ID" ),
2021 ],
2122 string = "Operation" ,
@@ -30,8 +31,9 @@ class SPPCRDetailUpdateID(models.Model):
3031 help = "Select existing ID to update or remove" ,
3132 )
3233 id_type_id = fields .Many2one (
33- "spp.id.type " ,
34+ "spp.vocabulary.code " ,
3435 string = "ID Type" ,
36+ domain = "[('vocabulary_id.namespace_uri', '=', 'urn:openspp:vocab:id-type')]" ,
3537 tracking = True ,
3638 )
3739 id_value = fields .Char (
@@ -53,6 +55,15 @@ class SPPCRDetailUpdateID(models.Model):
5355 help = "Upload scanned copies or photos of the ID document" ,
5456 )
5557 remarks = fields .Text (string = "Remarks" , tracking = True )
58+ is_operation_locked = fields .Boolean (
59+ string = "Operation Locked" ,
60+ default = False ,
61+ help = "Set to True when user proceeds to Documents or Review stage, locking the operation selection." ,
62+ )
63+ operation_display = fields .Char (
64+ string = "Operation" ,
65+ compute = "_compute_operation_display" ,
66+ )
5667
5768 # ══════════════════════════════════════════════════════════════════════════
5869 # COMPUTED FIELDS
@@ -69,17 +80,82 @@ class SPPCRDetailUpdateID(models.Model):
6980 readonly = True ,
7081 )
7182
83+ allow_id_add = fields .Boolean (
84+ related = "change_request_id.request_type_id.allow_id_add" ,
85+ readonly = True ,
86+ )
87+ allow_id_edit = fields .Boolean (
88+ related = "change_request_id.request_type_id.allow_id_edit" ,
89+ readonly = True ,
90+ )
91+ allow_id_remove = fields .Boolean (
92+ related = "change_request_id.request_type_id.allow_id_remove" ,
93+ readonly = True ,
94+ )
95+
96+ @api .depends ("operation" )
97+ def _compute_operation_display (self ):
98+ labels = dict (self ._fields ["operation" ].selection )
99+ for rec in self :
100+ rec .operation_display = labels .get (rec .operation , "" )
101+
102+ def action_next_documents (self ):
103+ """Lock operation before proceeding to documents stage."""
104+ self .write ({"is_operation_locked" : True })
105+ return super ().action_next_documents ()
106+
107+ def action_skip_to_review (self ):
108+ """Lock operation before proceeding to review stage."""
109+ self .write ({"is_operation_locked" : True })
110+ return super ().action_skip_to_review ()
111+
112+ @api .constrains ("operation" )
113+ def _check_operation_allowed (self ):
114+ """Validate that the chosen operation is allowed by the CR type config."""
115+ for rec in self :
116+ if not rec .change_request_id or not rec .change_request_id .request_type_id :
117+ continue
118+ cr_type = rec .change_request_id .request_type_id
119+ if rec .operation == "update" and not cr_type .allow_id_edit :
120+ raise ValidationError (_ ("Edit ID operation is not allowed for this change request type." ))
121+ if rec .operation == "remove" and not cr_type .allow_id_remove :
122+ raise ValidationError (_ ("Remove ID operation is not allowed for this change request type." ))
123+
72124 @api .onchange ("existing_id_record_id" )
73125 def _onchange_existing_id (self ):
74126 """Pre-fill fields when updating existing ID."""
75127 if self .existing_id_record_id and self .operation in ("update" , "remove" ):
76128 self .id_type_id = self .existing_id_record_id .id_type_id
77129 self .id_value = self .existing_id_record_id .value
78130 self .expiry_date = self .existing_id_record_id .expiry_date
79- self .description = self .existing_id_record_id .description
80131
81132 @api .onchange ("operation" )
82133 def _onchange_operation (self ):
83- """Clear fields when operation changes."""
134+ """Clear fields when operation changes. Reset if operation not allowed."""
135+ # Check if selected operation is allowed
136+ warning = None
137+ if self .operation == "update" and not self .allow_id_edit :
138+ self .operation = "add"
139+ warning = {
140+ "title" : _ ("Operation Not Allowed" ),
141+ "message" : _ ("Edit ID is disabled for this change request type." ),
142+ }
143+ elif self .operation == "remove" and not self .allow_id_remove :
144+ self .operation = "add"
145+ warning = {
146+ "title" : _ ("Operation Not Allowed" ),
147+ "message" : _ ("Remove ID is disabled for this change request type." ),
148+ }
149+
150+ # Unlock operation when changed (user came back to edit)
151+ self .is_operation_locked = False
152+
153+ # Clear fields common to all operations
154+ self .id_type_id = False
155+ self .id_value = False
156+ self .expiry_date = False
84157 if self .operation == "add" :
85158 self .existing_id_record_id = False
159+
160+ if warning :
161+ return {"warning" : warning }
0 commit comments