diff --git a/eo_gestion/eo_facture/models.py b/eo_gestion/eo_facture/models.py index b9947d7..46e2fde 100644 --- a/eo_gestion/eo_facture/models.py +++ b/eo_gestion/eo_facture/models.py @@ -308,6 +308,40 @@ class Contrat(models.Model): self.factures.update(numero_d_echeance=None) return super().save(*args, **kwargs) + def has_echeance_to_bill(self): + if self.periodicite is None: + return False + + until = (now() + relativedelta(months=6)).date() + + facture_par_numero_d_echeance = { + facture.numero_d_echeance: facture + for facture in self.factures.all() + if facture.echeance and not facture.proforma + } + for i, _, __ in self.periodicite_echeances(until=until): + if i not in facture_par_numero_d_echeance: + return True + return False + + def next_echeance_to_bill(self): + until = (now() + relativedelta(months=6)).date() + echeances = [] + + facture_par_numero_d_echeance = { + facture.numero_d_echeance: facture + for facture in self.factures.all() + if facture.echeance and not facture.proforma + } + for i, periode_debut, periode_fin in self.periodicite_echeances(until=until): + if i in facture_par_numero_d_echeance: + continue + echeances.append((periode_debut, periode_fin, i)) + echeances.sort() + if len(echeances) > 0: + return echeances[0][2] + return -1 + def __str__(self): # pylint: disable=invalid-str-returned return self.intitule diff --git a/eo_gestion/eo_facture/templates/admin/eo_facture/contrat/change_form.html b/eo_gestion/eo_facture/templates/admin/eo_facture/contrat/change_form.html index 0203ed3..de5efde 100644 --- a/eo_gestion/eo_facture/templates/admin/eo_facture/contrat/change_form.html +++ b/eo_gestion/eo_facture/templates/admin/eo_facture/contrat/change_form.html @@ -10,6 +10,14 @@