diff --git a/g2p_programs/models/cycle_membership.py b/g2p_programs/models/cycle_membership.py index 683980bc..f4e90cc1 100644 --- a/g2p_programs/models/cycle_membership.py +++ b/g2p_programs/models/cycle_membership.py @@ -91,12 +91,17 @@ def unlink(self): ) for record in draft_records: - beneficiary = record.cycle_id.entitlement_ids.filtered( + entitlement = record.cycle_id.entitlement_ids.filtered( lambda x: x.partner_id.id == record.partner_id.id ) - if record.cycle_id.state == "approved" or beneficiary and beneficiary.state == "approved": - raise ValidationError( - _("Beneficiaries can only be deleted when both the cycle and entitlement are unapproved.") - ) + if entitlement: + if entitlement.state == "approved": + raise ValidationError( + _( + "Beneficiaries can only be deleted when both the cycle and" + "entitlement are unapproved." + ) + ) + entitlement.unlink() return super().unlink() diff --git a/g2p_programs/models/entitlement.py b/g2p_programs/models/entitlement.py index 9887c18d..935bcaff 100644 --- a/g2p_programs/models/entitlement.py +++ b/g2p_programs/models/entitlement.py @@ -96,6 +96,14 @@ def _generate_code(self): ), ] + @api.constrains("valid_from", "valid_until") + def _check_valid_dates(self): + for record in self: + if record.valid_until and record.valid_from and record.valid_until < record.valid_from: + raise ValidationError( + _('The "Valid Until" date cannot be earlier than the "Valid From" date.') + ) + def fields_view_get(self, view_id=None, view_type="list", toolbar=False, submenu=False): res = super(G2PEntitlement, self).fields_view_get( view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu