| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- /**
- * 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 = '<p class="text-red-600">ID client manquant.</p>';
- 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 = `
- <h2 class="text-xl font-semibold text-gray-900">${escapeHtml(client.nom)}</h2>
- <p class="text-gray-600 mt-1">Encours : <strong>${formatMoney(calculs.encours)}</strong></p>
- `;
- const echeancierByFacture = {};
- (echeanciers || []).forEach(e => { echeancierByFacture[e.facture_id] = e; });
- const facturesList = document.getElementById('factures-list');
- if (factures.length === 0) {
- facturesList.innerHTML = '<p class="text-gray-500">Aucune facture.</p>';
- } 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 = `
- <div class="overflow-x-auto">
- <table class="min-w-[1000px] w-full text-sm">
- <thead><tr class="text-left text-gray-600 border-b">
- <th class="pb-2 pr-2 whitespace-nowrap">EXO</th><th class="pb-2 pr-2 whitespace-nowrap">Mois</th><th class="pb-2 pr-2 whitespace-nowrap">CLIENT</th><th class="pb-2 pr-2 whitespace-nowrap">PAYS</th><th class="pb-2 pr-2 whitespace-nowrap">DEPART DEM</th>
- <th class="pb-2 pr-2 whitespace-nowrap">NEG.</th><th class="pb-2 pr-2 whitespace-nowrap">TYPE</th><th class="pb-2 pr-2 whitespace-nowrap">CYCLE</th><th class="pb-2 pr-2 whitespace-nowrap">produc</th><th class="pb-2 pr-2 whitespace-nowrap">PAI</th>
- <th class="pb-2 pr-2 whitespace-nowrap">DATE FACTUR</th><th class="pb-2 pr-2 whitespace-nowrap">N° DE VENTE</th><th class="pb-2 pr-2 whitespace-nowrap">N° FACTURE</th>
- <th class="pb-2 pr-2 text-right whitespace-nowrap">QTE FACT</th><th class="pb-2 pr-2 text-right whitespace-nowrap">QTE LIVREE</th><th class="pb-2 pr-2 text-right whitespace-nowrap">% GT</th>
- <th class="pb-2 pr-2 text-right whitespace-nowrap">CA FACTURE HT</th><th class="pb-2 pr-2 text-right whitespace-nowrap">PU</th>
- <th class="pb-2 pr-2 text-right whitespace-nowrap">Total payé</th><th class="pb-2 pr-2 text-right whitespace-nowrap">Encours</th>
- <th class="pb-2 pr-2 whitespace-nowrap">État</th><th class="pb-2 pr-2 whitespace-nowrap">Écheancier</th>
- <th class="pb-2 pr-2 whitespace-nowrap text-center">Actions</th>
- </tr></thead>
- <tbody>
- ${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)}<br><span class="text-xs text-gray-500">dès ${formatDate(ech.date_debut_echeancier)}</span>`;
- }
- 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 `
- <tr class="border-b border-gray-100">
- <td class="py-2 pr-2 whitespace-nowrap">${escapeHtml(exo)}</td>
- <td class="py-2 pr-2 whitespace-nowrap">—</td>
- <td class="py-2 pr-2 whitespace-nowrap">${escapeHtml(client.nom)}</td>
- <td class="py-2 pr-2 whitespace-nowrap">—</td>
- <td class="py-2 pr-2 whitespace-nowrap">Écloserie marine</td>
- <td class="py-2 pr-2 whitespace-nowrap">${escapeHtml(typeLabel)}</td>
- <td class="py-2 pr-2 whitespace-nowrap">${escapeHtml(typeLabel)}</td>
- <td class="py-2 pr-2 whitespace-nowrap">${escapeHtml(f.cycle || '—')}</td>
- <td class="py-2 pr-2 whitespace-nowrap">—</td>
- <td class="py-2 pr-2 whitespace-nowrap">—</td>
- <td class="py-2 pr-2 whitespace-nowrap">${formatDate(f.date_facture)}</td>
- <td class="py-2 pr-2 whitespace-nowrap">${escapeHtml(f.n_vente || '—')}</td>
- <td class="py-2 pr-2 whitespace-nowrap">${escapeHtml(f.n_facture)}</td>
- <td class="py-2 pr-2 text-right whitespace-nowrap">${f.quantites != null ? formatNumber(f.quantites) : '—'}</td>
- <td class="py-2 pr-2 text-right whitespace-nowrap">${f.qte_livree != null ? formatNumber(f.qte_livree) : '—'}</td>
- <td class="py-2 pr-2 text-right whitespace-nowrap">—</td>
- <td class="py-2 pr-2 text-right whitespace-nowrap">${formatMoney(f.montant_ht)}</td>
- <td class="py-2 pr-2 text-right whitespace-nowrap">${pu != null ? formatMoney(pu) : '—'}</td>
- <td class="py-2 pr-2 text-right whitespace-nowrap">${formatMoney(totalPaye)}</td>
- <td class="py-2 pr-2 text-right whitespace-nowrap">${formatMoney(f.encours_facture)}</td>
- <td class="py-2 pr-2 whitespace-nowrap"><span class="px-2 py-0.5 rounded text-xs ${f.etat === 'paye' ? 'bg-green-100 text-green-800' : 'bg-amber-100 text-amber-800'}">${f.etat === 'paye' ? 'Payé' : 'En attente'}</span></td>
- <td class="py-2 pr-2 text-sm text-gray-600">${echText}</td>
- <td class="py-2 pr-2 whitespace-nowrap">
- <button onclick="editFacture(${f.id})" class="text-blue-600 hover:text-blue-800 mr-2" title="Modifier">
- <svg class="w-4 h-4 inline" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path></svg>
- </button>
- <button onclick="deleteFacture(${f.id})" class="text-red-600 hover:text-red-800" title="Supprimer">
- <svg class="w-4 h-4 inline" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
- </button>
- </td>
- </tr>
- `; }).join('')}
- </tbody>
- </table>
- </div>
- `;
- }
- const echeanciersListEl = document.getElementById('echeanciers-list');
- if (echeanciersListEl) {
- if (!echeanciers || echeanciers.length === 0) {
- echeanciersListEl.innerHTML = '<p class="text-gray-500">Aucun écheancier pour ce client.</p>';
- } else {
- const factureById = {};
- factures.forEach(f => { factureById[f.id] = f; });
-
- echeanciersListEl.innerHTML = `
- <div class="overflow-x-auto text-sm">
- <table class="min-w-full">
- <thead>
- <tr class="text-left text-gray-600 border-b">
- <th class="pb-2 pr-4">Facture</th>
- <th class="pb-2 pr-4">Nombre d'échéances</th>
- <th class="pb-2 pr-4">Date de début</th>
- <th class="pb-2 pr-4 text-right">Montant par échéance</th>
- <th class="pb-2 text-right">Encours</th>
- <th class="pb-2 text-center">Actions</th>
- </tr>
- </thead>
- <tbody>
- ${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 `
- <tr class="border-b border-gray-100">
- <td class="py-2 pr-4">${f ? escapeHtml(f.n_facture) : e.facture_id}</td>
- <td class="py-2 pr-4">${e.nb_echeances}</td>
- <td class="py-2 pr-4">${formatDate(e.date_debut_echeancier)}</td>
- <td class="py-2 pr-4 text-right">${formatMoney(montantParEch)}</td>
- <td class="py-2 text-right">${formatMoney(encours)}</td>
- <td class="py-2 text-center">
- <button onclick="deleteEcheancier(${e.id})" class="text-red-600 hover:text-red-800" title="Supprimer">
- <svg class="w-4 h-4 inline" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
- </button>
- </td>
- </tr>
- `;
- }).join('')}
- </tbody>
- </table>
- </div>
- `;
- }
- }
- const paiementsList = document.getElementById('paiements-list');
- if (paiements.length === 0) {
- paiementsList.innerHTML = '<p class="text-gray-500">Aucun paiement.</p>';
- } else {
- paiementsList.innerHTML = `
- <div class="overflow-x-auto">
- <table class="min-w-full text-sm">
- <thead><tr class="text-left text-gray-600 border-b"><th class="pb-2">Facture</th><th class="pb-2">Date</th><th class="pb-2 text-right">Montant</th><th class="pb-2 text-center">Actions</th></tr></thead>
- <tbody>
- ${paiements.map(p => `
- <tr class="border-b border-gray-100">
- <td class="py-2">${escapeHtml(p.n_facture)}</td>
- <td class="py-2">${formatDate(p.date_paiement)}</td>
- <td class="py-2 text-right">${formatMoney(p.montant)}</td>
- <td class="py-2 text-center">
- <button onclick="deletePaiement(${p.id})" class="text-red-600 hover:text-red-800" title="Supprimer">
- <svg class="w-4 h-4 inline" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
- </button>
- </td>
- </tr>
- `).join('')}
- </tbody>
- </table>
- </div>
- `;
- }
- 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 = '<p class="text-red-600">Erreur de chargement.</p>';
- 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();
- })();
|