diff --git a/itou/approvals/models.py b/itou/approvals/models.py index bfbc572d1a..d75b44f840 100644 --- a/itou/approvals/models.py +++ b/itou/approvals/models.py @@ -558,7 +558,7 @@ def clean(self): "start_at": ( f"Pour la date de début de suspension, vous pouvez remonter " f"{self.MAX_RETROACTIVITY_DURATION_DAYS} jours avant la date du jour." - f"Date de début minimum: {next_min_start_at.strftime('%d/%m/%Y')}." + f"Date de début minimum : {next_min_start_at.strftime('%d/%m/%Y')}." ) } ) @@ -626,25 +626,20 @@ def next_min_start_at(approval): """ Returns the minimum date on which a suspension can begin. """ - # by default the next min start is the date of last hiring start of user - min_suspension_start_at = approval.user.last_accepted_job_application.hiring_start_at today = datetime.date.today() - # We set the next min to the date of last old suspension if suspension exist + # Default starting date. + start_at = approval.user.last_accepted_job_application.hiring_start_at + start_at_threshold = today - datetime.timedelta(days=Suspension.MAX_RETROACTIVITY_DURATION_DAYS) + + # Start at overrides to handle edge cases. if approval.last_old_suspension: - min_suspension_start_at = approval.last_old_suspension.end_at + relativedelta(days=1) - # else we set the next min to the date of last accepted job from the pe approval + start_at = approval.last_old_suspension.end_at + relativedelta(days=1) elif approval.user.last_accepted_job_application.created_from_pe_approval: - min_suspension_start_at = today - - is_old_than_max_retroactivity = today - min_suspension_start_at > datetime.timedelta( - days=Suspension.MAX_RETROACTIVITY_DURATION_DAYS - ) + start_at = today - return ( - today - relativedelta(days=Suspension.MAX_RETROACTIVITY_DURATION_DAYS) - if is_old_than_max_retroactivity - else min_suspension_start_at - ) + if start_at < start_at_threshold: + return start_at_threshold + return start_at class ProlongationQuerySet(models.QuerySet): diff --git a/itou/approvals/tests.py b/itou/approvals/tests.py index c83b0d15c8..8a14ac3981 100644 --- a/itou/approvals/tests.py +++ b/itou/approvals/tests.py @@ -948,23 +948,25 @@ class SuspensionModelTest(TestCase): def test_clean(self): today = timezone.now().date() - start_at = timezone.now().date() - relativedelta(months=2) + start_at = today - relativedelta(days=Suspension.MAX_RETROACTIVITY_DURATION_DAYS * 2) end_at = start_at + relativedelta(months=2) - approval = ApprovalFactory(start_at=start_at, end_at=end_at) - suspension = SuspensionFactory(approval=approval) + approval = ApprovalFactory.build(start_at=start_at, end_at=end_at) - # test the when suspension is out of boundaries - suspension.start_at = start_at - relativedelta(months=1) + # Suspension.start_date is too old. + suspension = SuspensionFactory.build(approval=approval) + suspension.start_at = start_at - relativedelta(days=Suspension.MAX_RETROACTIVITY_DURATION_DAYS + 1) with self.assertRaises(ValidationError): suspension.clean() - # the case when end_at < start_at + # suspension.end_at < suspension.start_at + suspension = SuspensionFactory.build(approval=approval) suspension.start_at = start_at suspension.end_at = start_at - relativedelta(months=1) with self.assertRaises(ValidationError): suspension.clean() - # declare suspension in the future + # Suspension.start_at is in the future. + suspension = SuspensionFactory.build(approval=approval) suspension.start_at = today + relativedelta(days=2) suspension.end_at = end_at with self.assertRaises(ValidationError):