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 @@
  • Dupliquer
  • {% endif %}
  • Factures
  • + {% if original.has_echeance_to_bill %} +
  • Facturer
  • +
    + {% csrf_token %} + +
    + + {% endif %} {% if not original.periodicite %}
  • Ajouter une facture
  • Ajouter une facture comme pourcentage du total
  • diff --git a/tests/test_admin.py b/tests/test_admin.py index 4eadc2b..827b5da 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -44,8 +44,8 @@ class TestLoggedIn: response.form['periodicite'] = 'annuelle' response.form['periodicite_debut'] = '2018-12-01' response = response.form.submit('_continue').follow() - response.form['periodicite_debut'] = '2018-12-02' - response = response.form.submit('_continue').follow() + response.forms["contrat_form"]['periodicite_debut'] = '2018-12-02' + response = response.forms["contrat_form"].submit('_continue').follow() # Créer la facture de première échéance response = app.get('/')