@@ -197,13 +197,37 @@ def _compute_same_dni_partner_id(self):
197
197
partner .same_dni_partner_id = Partner .search (domain , limit = 1 )
198
198
(self - filtered_partners ).same_dni_partner_id = False
199
199
200
+ @api .constrains ('current_main_group_id' )
201
+ def _check_unique_main_group_per_year (self ):
202
+ """La validación se realiza sobre el campo `current_main_group_id` en lugar de `student_group_ids`
203
+ porque, al usar un campo many2many, la validación no se activaba al agregar un estudiante
204
+ directamente desde un grupo.
205
+ """
206
+ for partner in self :
207
+ domain = [
208
+ ('student_ids' , '=' , partner .id ),
209
+ ('subject_id' , '=' , False )
210
+ ]
211
+ grouped_data = self .env ['academic.group' ].read_group (
212
+ domain ,
213
+ ['year' ],
214
+ ['year' ]
215
+ )
216
+ duplicate_years = [group ['year' ] for group in grouped_data if group ['year_count' ] > 1 ]
217
+ if duplicate_years :
218
+ raise ValidationError (_ (
219
+ "The partner '%s' cannot belong to multiple groups "
220
+ "without a subject in the same year. Conflicting year(s): %s."
221
+ ) % (
222
+ partner .name ,
223
+ ', ' .join (map (str , duplicate_years ))
224
+ ))
225
+
200
226
@api .depends ('student_group_ids' )
201
227
def _compute_current_main_group (self ):
202
228
for rec in self :
203
229
student_group = rec .student_group_ids .filtered (lambda g : g .year == date .today ().year and not g .subject_id )
204
- if len (student_group ) > 1 :
205
- raise ValidationError ("There shouldn't be two groups in the same year without a subject for partner %s." % rec .name )
206
- rec .current_main_group_id = student_group
230
+ rec .current_main_group_id = student_group [:1 ]
207
231
208
232
@api .model
209
233
def get_payment_responsible (self ):
0 commit comments