client-detail.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * Chargement du détail client : en-tête, encours, factures, paiements (FR2, FR3, FR5).
  3. */
  4. (function () {
  5. const params = new URLSearchParams(window.location.search);
  6. const clientId = parseInt(params.get('id'), 10);
  7. if (!clientId) {
  8. document.getElementById('client-header').innerHTML = '<p class="text-red-600">ID client manquant.</p>';
  9. return;
  10. }
  11. async function loadClient() {
  12. hideError();
  13. try {
  14. const [client, factures, paiements, calculs, echeanciers] = await Promise.all([
  15. apiGet('/clients.php', { id: clientId }),
  16. apiGet('/factures.php', { client_id: clientId }),
  17. apiGet('/paiements.php', { client_id: clientId }),
  18. apiGet('/calculs.php', { client_id: clientId }),
  19. apiGet('/echeanciers.php', { client_id: clientId }),
  20. ]);
  21. document.getElementById('client-header').innerHTML = `
  22. <h2 class="text-xl font-semibold text-gray-900">${escapeHtml(client.nom)}</h2>
  23. <p class="text-gray-600 mt-1">Encours : <strong>${formatMoney(calculs.encours)}</strong></p>
  24. `;
  25. const echeancierByFacture = {};
  26. (echeanciers || []).forEach(e => { echeancierByFacture[e.facture_id] = e; });
  27. const facturesList = document.getElementById('factures-list');
  28. if (factures.length === 0) {
  29. facturesList.innerHTML = '<p class="text-gray-500">Aucune facture.</p>';
  30. } else {
  31. // Colonnes alignées sur l’onglet Factures Excel (Écloserie marine - Suivi des Ventes) + Total payé, Encours, État, Écheancier
  32. const factureTypeLabel = (tp) => !tp ? '—' : (String(tp).toLowerCase().indexOf('alevin') !== -1 ? 'Alevin bar' : (String(tp).toLowerCase().indexOf('larve') !== -1 ? 'Larve bar' : tp));
  33. facturesList.innerHTML = `
  34. <div class="overflow-x-auto">
  35. <table class="min-w-[1000px] w-full text-sm">
  36. <thead><tr class="text-left text-gray-600 border-b">
  37. <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>
  38. <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>
  39. <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>
  40. <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>
  41. <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>
  42. <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>
  43. <th class="pb-2 pr-2 whitespace-nowrap">État</th><th class="pb-2 pr-2 whitespace-nowrap">Écheancier</th>
  44. <th class="pb-2 pr-2 whitespace-nowrap text-center">Actions</th>
  45. </tr></thead>
  46. <tbody>
  47. ${factures.map(f => {
  48. const ech = echeancierByFacture[f.id];
  49. let echText = '—';
  50. if (ech) {
  51. const encours = parseFloat(f.encours_facture) || 0;
  52. const montantParEch = ech.montant_par_echeance != null ? parseFloat(ech.montant_par_echeance) : (ech.nb_echeances > 0 ? encours / ech.nb_echeances : 0);
  53. echText = `${ech.nb_echeances} éch. × ${formatMoney(montantParEch)}<br><span class="text-xs text-gray-500">dès ${formatDate(ech.date_debut_echeancier)}</span>`;
  54. }
  55. const exo = (f.cycle || '').replace(/-/g, '/') || '—';
  56. const typeLabel = factureTypeLabel(f.type_produit);
  57. const totalPaye = (parseFloat(f.montant_ht) || 0) - (parseFloat(f.encours_facture) || 0);
  58. const qteNum = parseFloat(f.quantites);
  59. const pu = (qteNum > 0 && f.montant_ht != null) ? (parseFloat(f.montant_ht) / qteNum) : null;
  60. return `
  61. <tr class="border-b border-gray-100">
  62. <td class="py-2 pr-2 whitespace-nowrap">${escapeHtml(exo)}</td>
  63. <td class="py-2 pr-2 whitespace-nowrap">—</td>
  64. <td class="py-2 pr-2 whitespace-nowrap">${escapeHtml(client.nom)}</td>
  65. <td class="py-2 pr-2 whitespace-nowrap">—</td>
  66. <td class="py-2 pr-2 whitespace-nowrap">Écloserie marine</td>
  67. <td class="py-2 pr-2 whitespace-nowrap">${escapeHtml(typeLabel)}</td>
  68. <td class="py-2 pr-2 whitespace-nowrap">${escapeHtml(typeLabel)}</td>
  69. <td class="py-2 pr-2 whitespace-nowrap">${escapeHtml(f.cycle || '—')}</td>
  70. <td class="py-2 pr-2 whitespace-nowrap">—</td>
  71. <td class="py-2 pr-2 whitespace-nowrap">—</td>
  72. <td class="py-2 pr-2 whitespace-nowrap">${formatDate(f.date_facture)}</td>
  73. <td class="py-2 pr-2 whitespace-nowrap">${escapeHtml(f.n_vente || '—')}</td>
  74. <td class="py-2 pr-2 whitespace-nowrap">${escapeHtml(f.n_facture)}</td>
  75. <td class="py-2 pr-2 text-right whitespace-nowrap">${f.quantites != null ? formatNumber(f.quantites) : '—'}</td>
  76. <td class="py-2 pr-2 text-right whitespace-nowrap">${f.qte_livree != null ? formatNumber(f.qte_livree) : '—'}</td>
  77. <td class="py-2 pr-2 text-right whitespace-nowrap">—</td>
  78. <td class="py-2 pr-2 text-right whitespace-nowrap">${formatMoney(f.montant_ht)}</td>
  79. <td class="py-2 pr-2 text-right whitespace-nowrap">${pu != null ? formatMoney(pu) : '—'}</td>
  80. <td class="py-2 pr-2 text-right whitespace-nowrap">${formatMoney(totalPaye)}</td>
  81. <td class="py-2 pr-2 text-right whitespace-nowrap">${formatMoney(f.encours_facture)}</td>
  82. <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>
  83. <td class="py-2 pr-2 text-sm text-gray-600">${echText}</td>
  84. <td class="py-2 pr-2 whitespace-nowrap">
  85. <button onclick="editFacture(${f.id})" class="text-blue-600 hover:text-blue-800 mr-2" title="Modifier">
  86. <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>
  87. </button>
  88. <button onclick="deleteFacture(${f.id})" class="text-red-600 hover:text-red-800" title="Supprimer">
  89. <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>
  90. </button>
  91. </td>
  92. </tr>
  93. `; }).join('')}
  94. </tbody>
  95. </table>
  96. </div>
  97. `;
  98. }
  99. const echeanciersListEl = document.getElementById('echeanciers-list');
  100. if (echeanciersListEl) {
  101. if (!echeanciers || echeanciers.length === 0) {
  102. echeanciersListEl.innerHTML = '<p class="text-gray-500">Aucun écheancier pour ce client.</p>';
  103. } else {
  104. const factureById = {};
  105. factures.forEach(f => { factureById[f.id] = f; });
  106. echeanciersListEl.innerHTML = `
  107. <div class="overflow-x-auto text-sm">
  108. <table class="min-w-full">
  109. <thead>
  110. <tr class="text-left text-gray-600 border-b">
  111. <th class="pb-2 pr-4">Facture</th>
  112. <th class="pb-2 pr-4">Nombre d'échéances</th>
  113. <th class="pb-2 pr-4">Date de début</th>
  114. <th class="pb-2 pr-4 text-right">Montant par échéance</th>
  115. <th class="pb-2 text-right">Encours</th>
  116. <th class="pb-2 text-center">Actions</th>
  117. </tr>
  118. </thead>
  119. <tbody>
  120. ${echeanciers.map(e => {
  121. const f = factureById[e.facture_id];
  122. if (!f) return '';
  123. const encours = parseFloat(f.encours_facture) || 0;
  124. const montantParEch = e.montant_par_echeance != null ? parseFloat(e.montant_par_echeance) : (e.nb_echeances > 0 ? encours / e.nb_echeances : 0);
  125. return `
  126. <tr class="border-b border-gray-100">
  127. <td class="py-2 pr-4">${f ? escapeHtml(f.n_facture) : e.facture_id}</td>
  128. <td class="py-2 pr-4">${e.nb_echeances}</td>
  129. <td class="py-2 pr-4">${formatDate(e.date_debut_echeancier)}</td>
  130. <td class="py-2 pr-4 text-right">${formatMoney(montantParEch)}</td>
  131. <td class="py-2 text-right">${formatMoney(encours)}</td>
  132. <td class="py-2 text-center">
  133. <button onclick="deleteEcheancier(${e.id})" class="text-red-600 hover:text-red-800" title="Supprimer">
  134. <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>
  135. </button>
  136. </td>
  137. </tr>
  138. `;
  139. }).join('')}
  140. </tbody>
  141. </table>
  142. </div>
  143. `;
  144. }
  145. }
  146. const paiementsList = document.getElementById('paiements-list');
  147. if (paiements.length === 0) {
  148. paiementsList.innerHTML = '<p class="text-gray-500">Aucun paiement.</p>';
  149. } else {
  150. paiementsList.innerHTML = `
  151. <div class="overflow-x-auto">
  152. <table class="min-w-full text-sm">
  153. <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>
  154. <tbody>
  155. ${paiements.map(p => `
  156. <tr class="border-b border-gray-100">
  157. <td class="py-2">${escapeHtml(p.n_facture)}</td>
  158. <td class="py-2">${formatDate(p.date_paiement)}</td>
  159. <td class="py-2 text-right">${formatMoney(p.montant)}</td>
  160. <td class="py-2 text-center">
  161. <button onclick="deletePaiement(${p.id})" class="text-red-600 hover:text-red-800" title="Supprimer">
  162. <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>
  163. </button>
  164. </td>
  165. </tr>
  166. `).join('')}
  167. </tbody>
  168. </table>
  169. </div>
  170. `;
  171. }
  172. window.__clientId = clientId;
  173. window.__factures = factures;
  174. window.__echeanciers = echeanciers || [];
  175. if (typeof initPaiementsForm === 'function') initPaiementsForm();
  176. if (typeof initFactureForm === 'function') initFactureForm();
  177. if (typeof initEcheancierForm === 'function') initEcheancierForm();
  178. } catch (e) {
  179. showError(e.message);
  180. document.getElementById('client-header').innerHTML = '<p class="text-red-600">Erreur de chargement.</p>';
  181. document.getElementById('factures-list').innerHTML = '-';
  182. document.getElementById('paiements-list').innerHTML = '-';
  183. }
  184. }
  185. function escapeHtml(text) {
  186. const div = document.createElement('div');
  187. div.textContent = text;
  188. return div.innerHTML;
  189. }
  190. // Fonctions globales pour les actions
  191. window.editFacture = function(factureId) {
  192. const facture = window.__factures.find(f => f.id === factureId);
  193. if (!facture) return;
  194. // Pré-remplir le formulaire
  195. document.getElementById('facture_client_id').value = facture.client_id;
  196. document.getElementById('n_facture').value = facture.n_facture;
  197. document.getElementById('n_vente').value = facture.n_vente || '';
  198. document.getElementById('date_facture').value = facture.date_facture;
  199. document.getElementById('type_produit').value = facture.type_produit || '';
  200. document.getElementById('cycle').value = facture.cycle || '';
  201. document.getElementById('quantites').value = facture.quantites || '';
  202. document.getElementById('qte_livree').value = facture.qte_livree || '';
  203. document.getElementById('montant_ht').value = facture.montant_ht;
  204. // Modifier le formulaire pour utiliser PUT
  205. const form = document.getElementById('form-facture');
  206. const submitBtn = form.querySelector('button[type="submit"]');
  207. submitBtn.textContent = 'Modifier la facture';
  208. submitBtn.classList.remove('bg-green-600', 'hover:bg-green-700');
  209. submitBtn.classList.add('bg-blue-600', 'hover:bg-blue-700');
  210. form.dataset.editId = factureId;
  211. // Afficher le bouton annuler s'il existe
  212. const cancelBtn = form.querySelector('button[type="button"]');
  213. if (cancelBtn) cancelBtn.style.display = 'inline-block';
  214. form.scrollIntoView({ behavior: 'smooth', block: 'start' });
  215. };
  216. window.deleteFacture = async function(factureId) {
  217. if (!confirm('Êtes-vous sûr de vouloir supprimer cette facture ? Cette action supprimera également les paiements et écheanciers associés.')) {
  218. return;
  219. }
  220. try {
  221. await apiDelete(`/factures.php?id=${factureId}`);
  222. window.location.reload();
  223. } catch (err) {
  224. showError(err.message);
  225. }
  226. };
  227. window.deleteEcheancier = async function(echeancierId) {
  228. if (!confirm('Êtes-vous sûr de vouloir supprimer cet écheancier ?')) {
  229. return;
  230. }
  231. try {
  232. await apiDelete(`/echeanciers.php?id=${echeancierId}`);
  233. window.location.reload();
  234. } catch (err) {
  235. showError(err.message);
  236. }
  237. };
  238. window.deletePaiement = async function(paiementId) {
  239. if (!confirm('Êtes-vous sûr de vouloir supprimer ce paiement ? L\'encours de la facture sera recalculé.')) {
  240. return;
  241. }
  242. try {
  243. await apiDelete(`/paiements.php?id=${paiementId}`);
  244. window.location.reload();
  245. } catch (err) {
  246. showError(err.message);
  247. }
  248. };
  249. loadClient();
  250. })();