paiements.js 1.3 KB

123456789101112131415161718192021222324252627282930
  1. /**
  2. * Formulaire d'enregistrement d'un paiement (FR4).
  3. */
  4. function initPaiementsForm() {
  5. const form = document.getElementById('form-paiement');
  6. const selectFacture = document.getElementById('facture_id');
  7. if (!form || !selectFacture || !window.__factures) return;
  8. const facturesEnAttente = window.__factures.filter(f => f.etat === 'en_attente' && f.encours_facture > 0);
  9. selectFacture.innerHTML = facturesEnAttente.length === 0
  10. ? '<option value="">Aucune facture en attente</option>'
  11. : facturesEnAttente.map(f => `<option value="${f.id}" data-max="${f.encours_facture}">${f.n_facture} - encours ${formatMoney(f.encours_facture)}</option>`).join('');
  12. document.getElementById('date_paiement').value = new Date().toISOString().slice(0, 10);
  13. form.addEventListener('submit', async (e) => {
  14. e.preventDefault();
  15. hideError();
  16. const factureId = parseInt(selectFacture.value, 10);
  17. const montant = parseFloat(document.getElementById('montant').value);
  18. const datePaiement = document.getElementById('date_paiement').value;
  19. if (!factureId || montant <= 0) return;
  20. try {
  21. await apiPost('/paiements.php', { facture_id: factureId, montant, date_paiement: datePaiement });
  22. window.location.reload();
  23. } catch (err) {
  24. showError(err.message);
  25. }
  26. });
  27. }