diff --git a/cotisations/models.py b/cotisations/models.py index 9865e905..1a593dad 100644 --- a/cotisations/models.py +++ b/cotisations/models.py @@ -511,7 +511,7 @@ class Vente(RevMixin, AclMixin, models.Model): ) return - def create_cotis(self): + def create_cotis(self, date_start=False): """ Creates a cotisation without initializing the dates (start and end ar set to self.facture.facture.date) and without saving it. You should use Facture.reorder_purchases to set the right dates. """ @@ -522,8 +522,17 @@ class Vente(RevMixin, AclMixin, models.Model): if not hasattr(self, "cotisation") and self.type_cotisation: cotisation = Cotisation(vente=self) cotisation.type_cotisation = self.type_cotisation - cotisation.date_start = invoice.date - cotisation.date_end = invoice.date + if date_start: + cotisation.date_start = date_start + cotisation.date_end = cotisation.date_start + relativedelta( + months=(self.duration or 0) * self.number, + days=(self.duration_days or 0) * self.number, + ) + self.save() + cotisation.save() + else: + cotisation.date_start = invoice.date + cotisation.date_end = invoice.date def save(self, *args, **kwargs): """ diff --git a/cotisations/test_models.py b/cotisations/test_models.py index bc86b7a7..6e90d882 100644 --- a/cotisations/test_models.py +++ b/cotisations/test_models.py @@ -79,6 +79,24 @@ class VenteModelTests(TestCase): self.assertEqual(end.month, expected_end.month) self.assertEqual(end.year, expected_end.year) + def test_date_start_cotisation(self): + """ + It should be possible to add a cotisation with a specific start date + """ + v = Vente( + facture=self.f, + number=1, + name="Test purchase", + duration=0, + duration_days=1, + type_cotisation = 'All', + prix=0 + ) + v.create_cotis(date_start=timezone.make_aware(datetime.datetime(1998, 10, 16))) + v.save() + self.assertEqual(v.cotisation.date_end, timezone.make_aware(datetime.datetime(1998, 10, 17))) + + def tearDown(self): self.f.delete() self.user.delete()