-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_program_wizard_cel.py
More file actions
968 lines (868 loc) · 38.9 KB
/
create_program_wizard_cel.py
File metadata and controls
968 lines (868 loc) · 38.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
# Part of OpenSPP. See LICENSE file for full copyright and licensing details.
"""CEL support for Create Program Wizard.
This module extends the program creation wizard to support CEL expressions
for eligibility, entitlement conditions, amount formulas, and compliance criteria.
"""
import logging
from odoo import Command, _, api, fields, models
from odoo.exceptions import UserError
_logger = logging.getLogger(__name__)
class SPPCreateProgramWizardCEL(models.TransientModel):
"""Extend wizard with CEL eligibility and compliance support."""
_inherit = "spp.program.create.wizard"
# -------------------------------------------------------------------------
# Eligibility Criteria Selection (Primary UX - template dropdown)
# -------------------------------------------------------------------------
eligibility_criteria_id = fields.Many2one(
comodel_name="spp.cel.expression",
string="Eligibility Criteria",
domain="["
"('expression_type', '=', 'filter'), "
"('is_template', '=', True), "
"('state', '=', 'active'), "
"'|', "
"('context_type', '=', target_type), "
"('context_type', '=', 'both')"
"]",
help="Select who is eligible for this program. "
"Leave empty for manual selection (no automatic filter). "
"You can customize the expression in the Advanced section unless the template is locked.",
)
eligibility_is_locked = fields.Boolean(
string="Criteria Locked",
related="eligibility_criteria_id.is_locked",
readonly=True,
help="When checked, the eligibility expression cannot be modified.",
)
# -------------------------------------------------------------------------
# CEL Profile (computed for widget context)
# -------------------------------------------------------------------------
eligibility_cel_profile = fields.Char(
string="CEL Profile",
compute="_compute_eligibility_cel_profile",
store=False,
)
# -------------------------------------------------------------------------
# Eligibility CEL Fields
# -------------------------------------------------------------------------
eligibility_cel_expression = fields.Text(
string="Eligibility CEL Expression",
help="Define eligibility criteria using CEL syntax.\n"
"Examples:\n"
"- age_years(me.birthdate) >= 60\n"
"- me.gender == 'female' and age_years(me.birthdate) >= 18\n"
"- me.household_size >= 4",
)
eligibility_cel_preview_count = fields.Integer(
string="Matching Beneficiaries",
compute="_compute_eligibility_cel_preview",
store=False,
)
eligibility_cel_is_valid = fields.Boolean(
string="Expression Valid",
compute="_compute_eligibility_cel_preview",
store=False,
)
eligibility_cel_error = fields.Text(
string="Expression Error",
compute="_compute_eligibility_cel_preview",
store=False,
)
eligibility_preview_partner_ids = fields.Many2many(
comodel_name="res.partner",
relation="wizard_eligibility_preview_partner_rel",
column1="wizard_id",
column2="partner_id",
string="Preview Registrants",
help="Partners matching the eligibility criteria (for preview)",
)
is_previewing = fields.Boolean(
string="Is Previewing",
default=False,
help="Indicates if preview results are currently being displayed",
)
# -------------------------------------------------------------------------
# Compliance Criteria (Optional)
# -------------------------------------------------------------------------
enable_compliance_cel = fields.Boolean(
string="Enable Compliance Monitoring",
default=False,
help="Enable ongoing compliance criteria checking for beneficiaries.",
)
compliance_criteria_id = fields.Many2one(
comodel_name="spp.cel.expression",
string="Compliance Criteria",
domain="["
"('expression_type', '=', 'filter'), "
"('is_template', '=', True), "
"('state', '=', 'active'), "
"'|', "
"('context_type', '=', target_type), "
"('context_type', '=', 'both')"
"]",
help="Select ongoing compliance criteria for beneficiaries. "
"Beneficiaries must continue to meet these criteria.",
)
compliance_is_locked = fields.Boolean(
string="Compliance Criteria Locked",
related="compliance_criteria_id.is_locked",
readonly=True,
help="When checked, the compliance expression cannot be modified.",
)
compliance_cel_expression = fields.Text(
string="Compliance CEL Expression",
help="Define compliance criteria using CEL syntax.\n"
"Beneficiaries must continue to meet these criteria.\n"
"Example: me.last_health_checkup_date > today() - duration('90d')",
)
compliance_cel_preview_count = fields.Integer(
string="Compliant Beneficiaries",
compute="_compute_compliance_cel_preview",
store=False,
)
compliance_cel_is_valid = fields.Boolean(
string="Compliance Expression Valid",
compute="_compute_compliance_cel_preview",
store=False,
)
compliance_cel_error = fields.Text(
string="Compliance Expression Error",
compute="_compute_compliance_cel_preview",
store=False,
)
# -------------------------------------------------------------------------
# Compute Methods
# -------------------------------------------------------------------------
@api.depends("target_type")
def _compute_eligibility_cel_profile(self):
"""Compute the CEL profile based on target type."""
for rec in self:
rec.eligibility_cel_profile = "registry_groups" if rec.target_type == "group" else "registry_individuals"
@api.onchange("eligibility_criteria_id", "eligibility_cel_expression")
def _onchange_eligibility_hide_preview(self):
"""Hide preview when eligibility criteria changes."""
if self.is_previewing:
self.is_previewing = False
self.eligibility_preview_partner_ids = [(5, 0, 0)]
@api.depends("eligibility_cel_expression", "target_type")
def _compute_eligibility_cel_preview(self):
"""Compute preview count and validation for eligibility CEL expression."""
for rec in self:
rec.eligibility_cel_preview_count = 0
rec.eligibility_cel_is_valid = True
rec.eligibility_cel_error = ""
if not rec.eligibility_cel_expression:
continue
try:
profile = "registry_groups" if rec.target_type == "group" else "registry_individuals"
service = self.env["spp.cel.service"]
base_domain = [("disabled", "=", False)]
result = service.compile_expression(
rec.eligibility_cel_expression,
profile=profile,
base_domain=base_domain,
limit=0,
)
if result.get("valid"):
rec.eligibility_cel_preview_count = result.get("count", 0)
rec.eligibility_cel_is_valid = True
else:
rec.eligibility_cel_is_valid = False
rec.eligibility_cel_error = result.get("error", _("Unknown error"))
except Exception as e:
rec.eligibility_cel_is_valid = False
rec.eligibility_cel_error = str(e)
@api.depends("compliance_cel_expression", "target_type", "enable_compliance_cel")
def _compute_compliance_cel_preview(self):
"""Compute preview count and validation for compliance CEL expression."""
for rec in self:
rec.compliance_cel_preview_count = 0
rec.compliance_cel_is_valid = True
rec.compliance_cel_error = ""
if not rec.enable_compliance_cel or not rec.compliance_cel_expression:
continue
try:
profile = "registry_groups" if rec.target_type == "group" else "registry_individuals"
service = self.env["spp.cel.service"]
base_domain = [("disabled", "=", False)]
result = service.compile_expression(
rec.compliance_cel_expression,
profile=profile,
base_domain=base_domain,
limit=0,
)
if result.get("valid"):
rec.compliance_cel_preview_count = result.get("count", 0)
rec.compliance_cel_is_valid = True
else:
rec.compliance_cel_is_valid = False
rec.compliance_cel_error = result.get("error", _("Unknown error"))
except Exception as e:
rec.compliance_cel_is_valid = False
rec.compliance_cel_error = str(e)
# -------------------------------------------------------------------------
# Onchange Methods
# -------------------------------------------------------------------------
@api.onchange("target_type")
def _onchange_target_type_clear_template(self):
"""Clear template selection when target type changes (templates are filtered)."""
self.eligibility_criteria_id = False
self.eligibility_cel_expression = False
self.compliance_criteria_id = False
self.compliance_cel_expression = False
@api.onchange("eligibility_criteria_id")
def _onchange_eligibility_criteria(self):
"""Populate expression when template is selected."""
if self.eligibility_criteria_id:
self.eligibility_cel_expression = self.eligibility_criteria_id.cel_expression
@api.onchange("compliance_criteria_id")
def _onchange_compliance_criteria(self):
"""Populate compliance expression when template is selected."""
if self.compliance_criteria_id:
self.compliance_cel_expression = self.compliance_criteria_id.cel_expression
# -------------------------------------------------------------------------
# Override Manager Creation Methods
# -------------------------------------------------------------------------
def _get_default_eligibility_manager_val(self, program_id):
"""Override to add CEL expression to eligibility manager.
This adds both eligibility and compliance CEL expressions to the same
eligibility manager, since compliance filtering is applied to already
enrolled beneficiaries.
"""
vals = super()._get_default_eligibility_manager_val(program_id)
# Add CEL eligibility if expression provided
if self.eligibility_cel_expression:
vals.update(
{
"eligibility_mode": "cel",
"cel_expression": self.eligibility_cel_expression,
}
)
# Add template lineage tracking (Option C: Hybrid)
if self.eligibility_criteria_id:
vals.update(
{
"source_expression_id": self.eligibility_criteria_id.id,
"is_eligibility_locked": self.eligibility_criteria_id.is_locked,
}
)
# NOTE: Compliance is now handled by dedicated spp.compliance.manager.default
# instead of being added to the eligibility manager.
# See create_program() override below.
return vals
def create_program(self):
"""Override to create dedicated compliance manager when enabled."""
action = super().create_program()
# Create dedicated compliance manager if CEL compliance is enabled
if self.enable_compliance_cel and self.compliance_cel_expression:
program = self.env["spp.program"].browse(action["params"]["program_id"])
self._create_cel_compliance_manager(program)
return action
def _create_cel_compliance_manager(self, program):
"""Create a dedicated compliance manager with CEL expression."""
self.ensure_one()
program.ensure_one()
# Build values for compliance manager
vals = {
"name": f"{program.name} Compliance",
"program_id": program.id,
"compliance_cel_expression": self.compliance_cel_expression,
}
# Add template lineage tracking if criteria template is selected
if self.compliance_criteria_id:
vals.update(
{
"source_expression_id": self.compliance_criteria_id.id,
"is_compliance_locked": self.compliance_criteria_id.is_locked,
}
)
# Create the compliance manager implementation
compliance_impl = self.env["spp.compliance.manager.default"].sudo().create(vals)
# Create the wrapper and link to program
program.write(
{
"compliance_manager_ids": [
Command.create(
{
"manager_ref_id": f"spp.compliance.manager.default,{compliance_impl.id}",
}
),
],
}
)
def _check_required_fields(self):
"""Override to validate CEL expressions before program creation."""
res = super()._check_required_fields()
# Validate eligibility CEL if provided
if self.eligibility_cel_expression and not self.eligibility_cel_is_valid:
raise UserError(_("Eligibility CEL expression is invalid: %s") % self.eligibility_cel_error)
# Validate compliance CEL if enabled
if self.enable_compliance_cel:
if not self.compliance_cel_expression:
raise UserError(_("Compliance CEL expression is required when compliance monitoring is enabled."))
if not self.compliance_cel_is_valid:
raise UserError(_("Compliance CEL expression is invalid: %s") % self.compliance_cel_error)
# Validate cash item CEL expressions
if self.entitlement_type == "cash":
for idx, item in enumerate(self.entitlement_cash_item_ids, 1):
if hasattr(item, "cel_is_valid") and not item.cel_is_valid:
raise UserError(
_("Cash item %d has invalid CEL expression: %s") % (idx, item.cel_error or _("Unknown error"))
)
# Validate in-kind item CEL expressions
if self.entitlement_type == "inkind":
for idx, item in enumerate(self.entitlement_item_ids, 1):
if hasattr(item, "quantity_cel_is_valid") and not item.quantity_cel_is_valid:
raise UserError(
_("In-kind item %d has invalid quantity formula: %s")
% (idx, item.quantity_cel_error or _("Unknown error"))
)
if hasattr(item, "condition_cel_is_valid") and item.has_condition and not item.condition_cel_is_valid:
raise UserError(
_("In-kind item %d has invalid condition expression: %s")
% (idx, item.condition_cel_error or _("Unknown error"))
)
return res
# -------------------------------------------------------------------------
# Action Methods
# -------------------------------------------------------------------------
def action_preview_eligibility(self):
"""Preview beneficiaries matching eligibility CEL expression."""
self.ensure_one()
if not self.eligibility_cel_expression:
raise UserError(_("Please enter a CEL expression first."))
if not self.eligibility_cel_is_valid:
raise UserError(_("Cannot preview invalid expression: %s") % self.eligibility_cel_error)
try:
profile = "registry_groups" if self.target_type == "group" else "registry_individuals"
service = self.env["spp.cel.service"]
result = service.compile_expression(
self.eligibility_cel_expression,
profile=profile,
base_domain=[("disabled", "=", False)],
limit=100, # Limit to 100 for preview
materialize_sql=True, # Required for window actions - domain must be serializable
)
# Update wizard with preview results
self.write(
{
"eligibility_preview_partner_ids": [(6, 0, result.get("ids", []))],
"is_previewing": True,
}
)
# Reload the form to show the preview tab
return {
"type": "ir.actions.act_window",
"name": _("Set Program Settings"),
"res_model": "spp.program.create.wizard",
"res_id": self.id,
"view_mode": "form",
"target": "new",
"context": self.env.context,
}
except Exception as e:
raise UserError(_("Error previewing expression: %s") % str(e)) from e
def action_hide_preview(self):
"""Hide the preview results."""
self.ensure_one()
self.write(
{
"eligibility_preview_partner_ids": [(5, 0, 0)], # Clear all
"is_previewing": False,
}
)
# Reload the form
return {
"type": "ir.actions.act_window",
"name": _("Set Program Settings"),
"res_model": "spp.program.create.wizard",
"res_id": self.id,
"view_mode": "form",
"target": "new",
"context": self.env.context,
}
def _get_entitlement_manager(self, program_id):
"""Override to add CEL fields to entitlement items."""
# For cash entitlement, prepare items with CEL fields
if self.entitlement_type == "cash":
entitlement_item_ids = []
for item in self.entitlement_cash_item_ids:
item_vals = {
"sequence": item.sequence,
"currency_id": item.currency_id.id,
"multiplier_field": item.multiplier_field.id if item.multiplier_field else False,
"max_multiplier": item.max_multiplier,
}
# Amount: CEL or Fixed
if hasattr(item, "amount_mode") and item.amount_mode == "cel":
item_vals.update(
{
"amount_mode": "cel",
"amount_cel_expression": item.amount_cel_expression or "",
"amount": item.amount, # base_amount for formula
}
)
else:
item_vals["amount"] = item.amount
# Condition: CEL or Domain
if hasattr(item, "has_condition") and item.has_condition:
if hasattr(item, "condition_mode") and item.condition_mode == "cel":
item_vals.update(
{
"condition_mode": "cel",
"cel_condition": item.cel_condition or "",
}
)
else:
item_vals["condition"] = item.condition or ""
else:
item_vals["condition"] = item.condition or ""
entitlement_item_ids.append(Command.create(item_vals))
def_mgr_obj = "spp.program.entitlement.manager.cash"
def_mgr = self.env[def_mgr_obj].create(
{
"name": "Cash",
"program_id": program_id,
"is_evaluate_one_item": self.is_evaluate_one_item,
"entitlement_item_ids": entitlement_item_ids,
"max_amount": self.max_amount,
"entitlement_validation_group_id": self.entitlement_validation_group_id.id,
"id_type_id": self.id_type_id.id if self.id_type_id else False,
"approval_definition_id": self.entitlement_approval_definition_id.id
if self.entitlement_approval_definition_id
else False,
}
)
man_obj = self.env["spp.program.entitlement.manager"]
mgr = man_obj.create(
{
"program_id": program_id,
"manager_ref_id": f"{def_mgr_obj},{str(def_mgr.id)}",
}
)
return {"entitlement_manager_ids": [(4, mgr.id)]}
# For in-kind entitlement, prepare items with CEL fields
elif self.entitlement_type == "inkind":
entitlement_item_ids = []
for item in self.entitlement_item_ids:
item_vals = {
"sequence": item.sequence,
"product_id": item.product_id.id,
"multiplier_field": item.multiplier_field.id if item.multiplier_field else False,
"max_multiplier": item.max_multiplier,
}
# Quantity: CEL or Fixed
if hasattr(item, "quantity_mode") and item.quantity_mode == "cel":
item_vals.update(
{
"quantity_mode": "cel",
"quantity_cel_expression": item.quantity_cel_expression or "",
"quantity": item.quantity, # base_quantity for formula
}
)
else:
item_vals["quantity"] = item.quantity
# Condition: CEL or Domain
if hasattr(item, "has_condition") and item.has_condition:
if hasattr(item, "condition_mode") and item.condition_mode == "cel":
item_vals.update(
{
"condition_mode": "cel",
"condition_cel_expression": item.condition_cel_expression or "",
}
)
else:
item_vals["condition"] = item.condition or ""
else:
item_vals["condition"] = item.condition or ""
entitlement_item_ids.append(Command.create(item_vals))
def_mgr_obj = "spp.program.entitlement.manager.inkind"
def_mgr = self.env[def_mgr_obj].create(
{
"name": "In-kind",
"program_id": program_id,
"is_evaluate_single_item": self.is_evaluate_single_item,
"entitlement_item_ids": entitlement_item_ids,
"entitlement_validation_group_id": self.entitlement_validation_group_id.id,
"id_type_id": self.id_type_id.id if self.id_type_id else False,
"manage_inventory": self.manage_inventory,
"warehouse_id": self.warehouse_id.id if self.warehouse_id else False,
"approval_definition_id": self.entitlement_approval_definition_id.id
if self.entitlement_approval_definition_id
else False,
}
)
man_obj = self.env["spp.program.entitlement.manager"]
mgr = man_obj.create(
{
"program_id": program_id,
"manager_ref_id": f"{def_mgr_obj},{str(def_mgr.id)}",
}
)
return {"entitlement_manager_ids": [(4, mgr.id)]}
# Default: use parent implementation
return super()._get_entitlement_manager(program_id)
class SPPCreateProgramWizardCashItemCEL(models.TransientModel):
"""Extend cash entitlement item with CEL support."""
_inherit = "spp.program.create.wizard.entitlement.cash.item"
# -------------------------------------------------------------------------
# Condition Template Selection (Primary UX)
# -------------------------------------------------------------------------
condition_template_id = fields.Many2one(
comodel_name="spp.cel.expression",
string="Condition",
domain="[" "('expression_type', '=', 'filter'), " "('is_template', '=', True), " "('state', '=', 'active')" "]",
help="Select a condition to restrict which beneficiaries receive this item. "
"Leave empty to apply to all eligible beneficiaries.",
)
condition_is_locked = fields.Boolean(
string="Condition Locked",
related="condition_template_id.is_locked",
readonly=True,
)
# -------------------------------------------------------------------------
# Amount Formula Template Selection
# -------------------------------------------------------------------------
amount_template_id = fields.Many2one(
comodel_name="spp.cel.expression",
string="Amount Formula",
domain="["
"('expression_type', '=', 'formula'), "
"('output_type', '=', 'money'), "
"('is_template', '=', True), "
"('state', '=', 'active')"
"]",
help="Select a formula to calculate the amount. " "Leave empty for a fixed amount.",
)
amount_is_locked = fields.Boolean(
string="Amount Formula Locked",
related="amount_template_id.is_locked",
readonly=True,
)
# -------------------------------------------------------------------------
# Amount CEL Fields
# -------------------------------------------------------------------------
amount_mode = fields.Selection(
selection=[
("fixed", "Fixed Amount"),
("cel", "CEL Formula"),
],
string="Amount Mode",
default="fixed",
help="Choose between fixed amount or dynamic CEL formula.",
)
amount_cel_expression = fields.Text(
string="Amount Formula",
help="Formula to calculate amount using CEL syntax.\n"
"Available: me (beneficiary), base_amount\n"
"Examples:\n"
"- 500 (fixed)\n"
"- me.household_size * 100\n"
"- base_amount * (1 + me.dependents_count * 0.1)",
)
amount_cel_preview = fields.Char(
string="Amount Preview",
compute="_compute_amount_cel_preview",
store=False,
)
amount_cel_is_valid = fields.Boolean(
string="Amount Formula Valid",
compute="_compute_amount_cel_preview",
store=False,
)
amount_cel_error = fields.Text(
string="Amount Formula Error",
compute="_compute_amount_cel_preview",
store=False,
)
# -------------------------------------------------------------------------
# Condition CEL Fields
# -------------------------------------------------------------------------
has_condition = fields.Boolean(
string="Apply Condition",
default=False,
help="Enable to restrict this item to specific beneficiaries.",
)
condition_mode = fields.Selection(
selection=[
("domain", "Domain Filter"),
("cel", "CEL Expression"),
],
string="Condition Mode",
default="cel",
help="Choose how to define the condition.",
)
cel_condition = fields.Text(
string="CEL Condition",
help="Condition expression using CEL syntax.\n"
"Examples:\n"
"- me.has_disability == true\n"
"- me.household_size >= 4\n"
"- age_years(me.birthdate) >= 60",
)
cel_preview_count = fields.Integer(
string="Matching Count",
compute="_compute_condition_cel_preview",
store=False,
)
cel_is_valid = fields.Boolean(
string="Condition Valid",
compute="_compute_condition_cel_preview",
store=False,
)
cel_error = fields.Text(
string="Condition Error",
compute="_compute_condition_cel_preview",
store=False,
)
# -------------------------------------------------------------------------
# Compute Methods
# -------------------------------------------------------------------------
@api.depends("amount_mode", "amount_cel_expression", "amount")
def _compute_amount_cel_preview(self):
"""Compute preview and validation for amount CEL formula."""
for rec in self:
rec.amount_cel_preview = ""
rec.amount_cel_is_valid = True
rec.amount_cel_error = ""
if rec.amount_mode != "cel" or not rec.amount_cel_expression:
continue
try:
# Validate by attempting to compile the expression
# Use Python's compile as a basic syntax check
compile(rec.amount_cel_expression, "<string>", "eval")
rec.amount_cel_is_valid = True
rec.amount_cel_preview = _("Formula syntax is valid")
except SyntaxError as e:
rec.amount_cel_is_valid = False
rec.amount_cel_error = _("Syntax error: %s") % str(e)
@api.depends("has_condition", "condition_mode", "cel_condition", "program_id.target_type")
def _compute_condition_cel_preview(self):
"""Compute preview and validation for condition CEL expression."""
for rec in self:
rec.cel_preview_count = 0
rec.cel_is_valid = True
rec.cel_error = ""
if not rec.has_condition:
continue
if rec.condition_mode != "cel" or not rec.cel_condition:
rec.cel_is_valid = rec.condition_mode == "domain"
continue
try:
target_type = rec.program_id.target_type if rec.program_id else "individual"
profile = "registry_groups" if target_type == "group" else "registry_individuals"
service = self.env["spp.cel.service"]
result = service.compile_expression(
rec.cel_condition,
profile=profile,
base_domain=[("disabled", "=", False)],
limit=0,
)
if result.get("valid"):
rec.cel_preview_count = result.get("count", 0)
rec.cel_is_valid = True
else:
rec.cel_is_valid = False
rec.cel_error = result.get("error", _("Unknown error"))
except Exception as e:
rec.cel_is_valid = False
rec.cel_error = str(e)
# -------------------------------------------------------------------------
# Onchange Methods for Template Selection
# -------------------------------------------------------------------------
@api.onchange("condition_template_id")
def _onchange_condition_template(self):
"""Populate condition expression when template is selected."""
if self.condition_template_id:
self.has_condition = True
self.condition_mode = "cel"
self.cel_condition = self.condition_template_id.cel_expression
@api.onchange("amount_template_id")
def _onchange_amount_template(self):
"""Populate amount formula when template is selected."""
if self.amount_template_id:
self.amount_mode = "cel"
self.amount_cel_expression = self.amount_template_id.cel_expression
class SPPCreateProgramWizardInKindItemCEL(models.TransientModel):
"""Extend in-kind entitlement item with CEL support."""
_inherit = "spp.program.create.wizard.entitlement.item"
# -------------------------------------------------------------------------
# Condition Template Selection (Primary UX)
# -------------------------------------------------------------------------
condition_template_id = fields.Many2one(
comodel_name="spp.cel.expression",
string="Condition",
domain="[" "('expression_type', '=', 'filter'), " "('is_template', '=', True), " "('state', '=', 'active')" "]",
help="Select a condition to restrict which beneficiaries receive this item. "
"Leave empty to apply to all eligible beneficiaries.",
)
condition_is_locked = fields.Boolean(
string="Condition Locked",
related="condition_template_id.is_locked",
readonly=True,
)
# -------------------------------------------------------------------------
# Quantity Formula Template Selection
# -------------------------------------------------------------------------
quantity_template_id = fields.Many2one(
comodel_name="spp.cel.expression",
string="Quantity Formula",
domain="["
"('expression_type', '=', 'formula'), "
"('output_type', '=', 'number'), "
"('is_template', '=', True), "
"('state', '=', 'active')"
"]",
help="Select a formula to calculate the quantity. " "Leave empty for a fixed quantity.",
)
quantity_is_locked = fields.Boolean(
string="Quantity Formula Locked",
related="quantity_template_id.is_locked",
readonly=True,
)
# -------------------------------------------------------------------------
# Quantity CEL Fields
# -------------------------------------------------------------------------
quantity_mode = fields.Selection(
selection=[
("fixed", "Fixed Quantity"),
("cel", "CEL Formula"),
],
string="Quantity Mode",
default="fixed",
help="Choose between fixed quantity or dynamic CEL formula.",
)
quantity_cel_expression = fields.Text(
string="Quantity Formula",
help="Formula to calculate quantity using CEL syntax.\n"
"Examples:\n"
"- me.household_size * 2\n"
"- base_quantity + me.children_count\n"
"- 10 if me.is_woman_headed else 8",
)
quantity_cel_preview = fields.Char(
string="Quantity Preview",
compute="_compute_quantity_cel_preview",
store=False,
)
quantity_cel_is_valid = fields.Boolean(
string="Quantity Formula Valid",
compute="_compute_quantity_cel_preview",
store=False,
)
quantity_cel_error = fields.Text(
string="Quantity Formula Error",
compute="_compute_quantity_cel_preview",
store=False,
)
# -------------------------------------------------------------------------
# Condition CEL Fields
# -------------------------------------------------------------------------
has_condition = fields.Boolean(
string="Apply Condition",
default=False,
help="Enable to restrict this item to specific beneficiaries.",
)
condition_mode = fields.Selection(
selection=[
("domain", "Domain Filter"),
("cel", "CEL Expression"),
],
string="Condition Mode",
default="cel",
help="Choose how to define the condition.",
)
condition_cel_expression = fields.Text(
string="Condition CEL Expression",
help="Condition expression using CEL syntax.\n"
"Examples:\n"
"- me.household_size >= 4\n"
"- me.has_young_children == true",
)
condition_cel_preview_count = fields.Integer(
string="Matching Count",
compute="_compute_condition_cel_preview",
store=False,
)
condition_cel_is_valid = fields.Boolean(
string="Condition Valid",
compute="_compute_condition_cel_preview",
store=False,
)
condition_cel_error = fields.Text(
string="Condition Error",
compute="_compute_condition_cel_preview",
store=False,
)
# -------------------------------------------------------------------------
# Compute Methods
# -------------------------------------------------------------------------
@api.depends("quantity_mode", "quantity_cel_expression", "quantity")
def _compute_quantity_cel_preview(self):
"""Compute preview and validation for quantity CEL formula."""
for rec in self:
rec.quantity_cel_preview = ""
rec.quantity_cel_is_valid = True
rec.quantity_cel_error = ""
if rec.quantity_mode != "cel" or not rec.quantity_cel_expression:
continue
try:
# Validate by attempting to compile the expression
compile(rec.quantity_cel_expression, "<string>", "eval")
rec.quantity_cel_is_valid = True
rec.quantity_cel_preview = _("Formula syntax is valid")
except SyntaxError as e:
rec.quantity_cel_is_valid = False
rec.quantity_cel_error = _("Syntax error: %s") % str(e)
@api.depends(
"has_condition",
"condition_mode",
"condition_cel_expression",
"program_id.target_type",
)
def _compute_condition_cel_preview(self):
"""Compute preview and validation for condition CEL expression."""
for rec in self:
rec.condition_cel_preview_count = 0
rec.condition_cel_is_valid = True
rec.condition_cel_error = ""
if not rec.has_condition:
continue
if rec.condition_mode != "cel" or not rec.condition_cel_expression:
rec.condition_cel_is_valid = rec.condition_mode == "domain"
continue
try:
target_type = rec.program_id.target_type if rec.program_id else "group"
profile = "registry_groups" if target_type == "group" else "registry_individuals"
service = self.env["spp.cel.service"]
result = service.compile_expression(
rec.condition_cel_expression,
profile=profile,
base_domain=[("disabled", "=", False)],
limit=0,
)
if result.get("valid"):
rec.condition_cel_preview_count = result.get("count", 0)
rec.condition_cel_is_valid = True
else:
rec.condition_cel_is_valid = False
rec.condition_cel_error = result.get("error", _("Unknown error"))
except Exception as e:
rec.condition_cel_is_valid = False
rec.condition_cel_error = str(e)
# -------------------------------------------------------------------------
# Onchange Methods for Template Selection
# -------------------------------------------------------------------------
@api.onchange("condition_template_id")
def _onchange_condition_template(self):
"""Populate condition expression when template is selected."""
if self.condition_template_id:
self.has_condition = True
self.condition_mode = "cel"
self.condition_cel_expression = self.condition_template_id.cel_expression
@api.onchange("quantity_template_id")
def _onchange_quantity_template(self):
"""Populate quantity formula when template is selected."""
if self.quantity_template_id:
self.quantity_mode = "cel"
self.quantity_cel_expression = self.quantity_template_id.cel_expression