/** * Chargement du détail client : en-tête, encours, factures, paiements (FR2, FR3, FR5). */ (function () { const params = new URLSearchParams(window.location.search); const clientId = parseInt(params.get('id'), 10); if (!clientId) { document.getElementById('client-header').innerHTML = '

ID client manquant.

'; return; } async function loadClient() { hideError(); try { const [client, factures, paiements, calculs, echeanciers] = await Promise.all([ apiGet('/clients.php', { id: clientId }), apiGet('/factures.php', { client_id: clientId }), apiGet('/paiements.php', { client_id: clientId }), apiGet('/calculs.php', { client_id: clientId }), apiGet('/echeanciers.php', { client_id: clientId }), ]); document.getElementById('client-header').innerHTML = `

${escapeHtml(client.nom)}

Encours : ${formatMoney(calculs.encours)}

`; const echeancierByFacture = {}; (echeanciers || []).forEach(e => { echeancierByFacture[e.facture_id] = e; }); const facturesList = document.getElementById('factures-list'); if (factures.length === 0) { facturesList.innerHTML = '

Aucune facture.

'; } else { // Colonnes alignées sur l’onglet Factures Excel (Écloserie marine - Suivi des Ventes) + Total payé, Encours, État, Écheancier const factureTypeLabel = (tp) => !tp ? '—' : (String(tp).toLowerCase().indexOf('alevin') !== -1 ? 'Alevin bar' : (String(tp).toLowerCase().indexOf('larve') !== -1 ? 'Larve bar' : tp)); facturesList.innerHTML = `
${factures.map(f => { const ech = echeancierByFacture[f.id]; let echText = '—'; if (ech) { const encours = parseFloat(f.encours_facture) || 0; const montantParEch = ech.montant_par_echeance != null ? parseFloat(ech.montant_par_echeance) : (ech.nb_echeances > 0 ? encours / ech.nb_echeances : 0); echText = `${ech.nb_echeances} éch. × ${formatMoney(montantParEch)}
dès ${formatDate(ech.date_debut_echeancier)}`; } const exo = (f.cycle || '').replace(/-/g, '/') || '—'; const typeLabel = factureTypeLabel(f.type_produit); const totalPaye = (parseFloat(f.montant_ht) || 0) - (parseFloat(f.encours_facture) || 0); const qteNum = parseFloat(f.quantites); const pu = (qteNum > 0 && f.montant_ht != null) ? (parseFloat(f.montant_ht) / qteNum) : null; return ` `; }).join('')}
EXOMoisCLIENTPAYSDEPART DEM NEG.TYPECYCLEproducPAI DATE FACTURN° DE VENTEN° FACTURE QTE FACTQTE LIVREE% GT CA FACTURE HTPU Total payéEncours ÉtatÉcheancier Actions
${escapeHtml(exo)} — ${escapeHtml(client.nom)} — Écloserie marine ${escapeHtml(typeLabel)} ${escapeHtml(typeLabel)} ${escapeHtml(f.cycle || '—')} — — ${formatDate(f.date_facture)} ${escapeHtml(f.n_vente || '—')} ${escapeHtml(f.n_facture)} ${f.quantites != null ? formatNumber(f.quantites) : '—'} ${f.qte_livree != null ? formatNumber(f.qte_livree) : '—'} — ${formatMoney(f.montant_ht)} ${pu != null ? formatMoney(pu) : '—'} ${formatMoney(totalPaye)} ${formatMoney(f.encours_facture)} ${f.etat === 'paye' ? 'Payé' : 'En attente'} ${echText}
`; } const echeanciersListEl = document.getElementById('echeanciers-list'); if (echeanciersListEl) { if (!echeanciers || echeanciers.length === 0) { echeanciersListEl.innerHTML = '

Aucun écheancier pour ce client.

'; } else { const factureById = {}; factures.forEach(f => { factureById[f.id] = f; }); echeanciersListEl.innerHTML = `
${echeanciers.map(e => { const f = factureById[e.facture_id]; if (!f) return ''; const encours = parseFloat(f.encours_facture) || 0; const montantParEch = e.montant_par_echeance != null ? parseFloat(e.montant_par_echeance) : (e.nb_echeances > 0 ? encours / e.nb_echeances : 0); return ` `; }).join('')}
Facture Nombre d'échéances Date de début Montant par échéance Encours Actions
${f ? escapeHtml(f.n_facture) : e.facture_id} ${e.nb_echeances} ${formatDate(e.date_debut_echeancier)} ${formatMoney(montantParEch)} ${formatMoney(encours)}
`; } } const paiementsList = document.getElementById('paiements-list'); if (paiements.length === 0) { paiementsList.innerHTML = '

Aucun paiement.

'; } else { paiementsList.innerHTML = `
${paiements.map(p => ` `).join('')}
FactureDateMontantActions
${escapeHtml(p.n_facture)} ${formatDate(p.date_paiement)} ${formatMoney(p.montant)}
`; } window.__clientId = clientId; window.__factures = factures; window.__echeanciers = echeanciers || []; if (typeof initPaiementsForm === 'function') initPaiementsForm(); if (typeof initFactureForm === 'function') initFactureForm(); if (typeof initEcheancierForm === 'function') initEcheancierForm(); } catch (e) { showError(e.message); document.getElementById('client-header').innerHTML = '

Erreur de chargement.

'; document.getElementById('factures-list').innerHTML = '-'; document.getElementById('paiements-list').innerHTML = '-'; } } function escapeHtml(text) { const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } // Fonctions globales pour les actions window.editFacture = function(factureId) { const facture = window.__factures.find(f => f.id === factureId); if (!facture) return; // Pré-remplir le formulaire document.getElementById('facture_client_id').value = facture.client_id; document.getElementById('n_facture').value = facture.n_facture; document.getElementById('n_vente').value = facture.n_vente || ''; document.getElementById('date_facture').value = facture.date_facture; document.getElementById('type_produit').value = facture.type_produit || ''; document.getElementById('cycle').value = facture.cycle || ''; document.getElementById('quantites').value = facture.quantites || ''; document.getElementById('qte_livree').value = facture.qte_livree || ''; document.getElementById('montant_ht').value = facture.montant_ht; // Modifier le formulaire pour utiliser PUT const form = document.getElementById('form-facture'); const submitBtn = form.querySelector('button[type="submit"]'); submitBtn.textContent = 'Modifier la facture'; submitBtn.classList.remove('bg-green-600', 'hover:bg-green-700'); submitBtn.classList.add('bg-blue-600', 'hover:bg-blue-700'); form.dataset.editId = factureId; // Afficher le bouton annuler s'il existe const cancelBtn = form.querySelector('button[type="button"]'); if (cancelBtn) cancelBtn.style.display = 'inline-block'; form.scrollIntoView({ behavior: 'smooth', block: 'start' }); }; window.deleteFacture = async function(factureId) { if (!confirm('Êtes-vous sûr de vouloir supprimer cette facture ? Cette action supprimera également les paiements et écheanciers associés.')) { return; } try { await apiDelete(`/factures.php?id=${factureId}`); window.location.reload(); } catch (err) { showError(err.message); } }; window.deleteEcheancier = async function(echeancierId) { if (!confirm('Êtes-vous sûr de vouloir supprimer cet écheancier ?')) { return; } try { await apiDelete(`/echeanciers.php?id=${echeancierId}`); window.location.reload(); } catch (err) { showError(err.message); } }; window.deletePaiement = async function(paiementId) { if (!confirm('Êtes-vous sûr de vouloir supprimer ce paiement ? L\'encours de la facture sera recalculé.')) { return; } try { await apiDelete(`/paiements.php?id=${paiementId}`); window.location.reload(); } catch (err) { showError(err.message); } }; loadClient(); })();