Files
crm_ecosplay/assets/modules/entreprise-search.js

154 lines
6.4 KiB
JavaScript
Raw Normal View History

/**
* Recherche entreprise via API data.gouv.fr
* Auto-remplissage du formulaire de création client
*/
const API_URL = '/admin/clients/entreprise-search'
const computeTva = (siren) => {
if (!siren) return ''
const key = (12 + 3 * (Number.parseInt(siren, 10) % 97)) % 97
return 'FR' + String(key).padStart(2, '0') + siren
}
const capitalize = (s) => s ? s.charAt(0).toUpperCase() + s.slice(1).toLowerCase() : ''
const fillField = (id, value) => {
const el = document.getElementById(id)
if (el) el.value = value
}
const fillFieldIfEmpty = (id, value) => {
const el = document.getElementById(id)
if (el && !el.value && value) el.value = value
}
const buildRcs = (siren, city) => {
if (!siren || !city) return ''
return 'RCS ' + capitalize(city) + ' ' + siren
}
const resolveTypeCompany = (natureJuridique) => {
if (!natureJuridique) return ''
const code = String(natureJuridique)
if (code.startsWith('92') || code.startsWith('91') || code.startsWith('93')) return 'association'
if (code.startsWith('10')) return 'auto-entrepreneur'
if (code.startsWith('54') || code.startsWith('55')) return 'sarl'
if (code.startsWith('57')) return 'sas'
if (code.startsWith('52')) return 'eurl'
/* istanbul ignore next -- dead code: 55xx already matched by sarl */
if (code.startsWith('55') && code === '5599') return 'sa'
if (code.startsWith('65')) return 'sci'
return ''
}
const renderResult = (e, onSelect) => {
/* istanbul ignore next */
const s = e.siege || {}
/* istanbul ignore next */
const d = e.dirigeants?.[0] ?? {}
const actif = e.etat_administratif === 'A'
const addr = [s.numero_voie, s.type_voie, s.libelle_voie].filter(Boolean).join(' ')
const ape = e.activite_principale || ''
const rna = e.complements?.identifiant_association ?? ''
const isAsso = resolveTypeCompany(e.nature_juridique) === 'association'
const div = document.createElement('div')
div.className = 'glass p-4 cursor-pointer hover:bg-white/80 transition-all'
/* istanbul ignore next -- template literal fallbacks */
div.innerHTML = `
<div class="flex items-start justify-between gap-3">
<div class="min-w-0">
<div class="font-bold text-sm">${e.nom_complet || e.nom_raison_sociale || 'N/A'}</div>
<div class="text-xs text-gray-500 mt-1">
SIREN <span class="font-mono font-bold">${e.siren || '?'}</span>
${s.siret ? ' &mdash; SIRET <span class="font-mono font-bold">' + s.siret + '</span>' : ''}
${ape ? ' &mdash; APE <span class="font-mono font-bold">' + ape + '</span>' : ''}
${rna ? ' &mdash; RNA <span class="font-mono font-bold">' + rna + '</span>' : ''}
</div>
<div class="text-xs text-gray-400 mt-1">${s.geo_adresse || s.adresse || ''}</div>
${d.nom ? '<div class="text-xs text-gray-400 mt-1">Dirigeant : ' + (d.prenoms || '') + ' ' + d.nom + '</div>' : ''}
feat: comptabilite + prestataires + rapport financier + stats dynamiques Comptabilite (Super Admin) : - ComptabiliteController avec 7 exports CSV/JSON compatibles SAGE (journal ventes, grand livre, FEC, balance agee, reglements, commissions Stripe 1.5%+0.25E, couts services) - Export PDF via ComptaPdf (FPDF) avec bloc legal pre-rempli, tableau pagine, champ signature DocuSeal - Signature electronique DocuSeal + callback + envoi email signe avec template dedie (compta_export_signed.html.twig) - Rapport financier public (RapportFinancierPdf) : recettes par service, depenses (Stripe, infra, prestataires), bilan excedent/deficit - Codes comptables clients EC-XXXX (plus de 411xxx) Prestataires (Super Admin) : - Entite Prestataire (raisonSociale, siret, email, phone, adresse) - Entite FacturePrestataire (numFacture, montantHt, montantTtc, year, month, isPaid, PDF via Vich) - CRUD complet avec recherche SIRET via proxy API data.gouv.fr - Commande cron app:reminder:factures-prestataire (5 du mois) - Factures prestataires integrees dans export couts services - Sidebar Super Admin : entree Prestataires + Comptabilite Stats (/admin/stats) : - Cout prestataire dynamique depuis FacturePrestataire - Fusion Infra + Prestataire en "Cout de fonctionnement" - Commission Stripe corrigee (1.5% + 0.25E par transaction) Divers : - DocuSealService::sendComptaForSignature() + getApi() - Customer::generateCodeComptable() format EC-XXXX-XXXXX - Protection double prefixe EC- a la creation client - Bouton regenerer PDF cache quand advert state=accepted - Modals sans script inline (data-modal-open/close dans app.js) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 23:39:31 +02:00
${isAsso ? '<div class="text-xs mt-1"><span class="px-1.5 py-0.5 bg-indigo-100 text-indigo-800 font-bold text-[10px] uppercase" >Association</span></div>' : ''}
</div>
feat: comptabilite + prestataires + rapport financier + stats dynamiques Comptabilite (Super Admin) : - ComptabiliteController avec 7 exports CSV/JSON compatibles SAGE (journal ventes, grand livre, FEC, balance agee, reglements, commissions Stripe 1.5%+0.25E, couts services) - Export PDF via ComptaPdf (FPDF) avec bloc legal pre-rempli, tableau pagine, champ signature DocuSeal - Signature electronique DocuSeal + callback + envoi email signe avec template dedie (compta_export_signed.html.twig) - Rapport financier public (RapportFinancierPdf) : recettes par service, depenses (Stripe, infra, prestataires), bilan excedent/deficit - Codes comptables clients EC-XXXX (plus de 411xxx) Prestataires (Super Admin) : - Entite Prestataire (raisonSociale, siret, email, phone, adresse) - Entite FacturePrestataire (numFacture, montantHt, montantTtc, year, month, isPaid, PDF via Vich) - CRUD complet avec recherche SIRET via proxy API data.gouv.fr - Commande cron app:reminder:factures-prestataire (5 du mois) - Factures prestataires integrees dans export couts services - Sidebar Super Admin : entree Prestataires + Comptabilite Stats (/admin/stats) : - Cout prestataire dynamique depuis FacturePrestataire - Fusion Infra + Prestataire en "Cout de fonctionnement" - Commission Stripe corrigee (1.5% + 0.25E par transaction) Divers : - DocuSealService::sendComptaForSignature() + getApi() - Customer::generateCodeComptable() format EC-XXXX-XXXXX - Protection double prefixe EC- a la creation client - Bouton regenerer PDF cache quand advert state=accepted - Modals sans script inline (data-modal-open/close dans app.js) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 23:39:31 +02:00
<span class="shrink-0 px-2 py-1 text-[10px] font-bold uppercase ${actif ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'}" >
${actif ? 'Actif' : 'Ferme'}
</span>
</div>`
/* istanbul ignore next -- click handler fallbacks */
div.addEventListener('click', () => {
fillField('raisonSociale', e.nom_raison_sociale || e.nom_complet || '')
fillField('siret', s.siret || '')
fillField('rcs', buildRcs(e.siren, s.libelle_commune))
fillField('numTva', computeTva(e.siren))
fillField('ape', ape)
fillField('address', addr)
fillField('zipCode', s.code_postal || '')
fillField('city', s.libelle_commune || '')
fillField('geoLat', s.latitude || '')
fillField('geoLong', s.longitude || '')
const typeCompany = resolveTypeCompany(e.nature_juridique)
if (typeCompany) fillField('typeCompany', typeCompany)
const rna = e.complements?.identifiant_association ?? ''
if (rna) fillField('rna', rna)
const prenom = (d.prenoms || '').split(' ')[0]
fillFieldIfEmpty('firstName', capitalize(prenom))
fillFieldIfEmpty('lastName', capitalize(d.nom))
onSelect()
})
return div
}
export function initEntrepriseSearch() {
const modal = document.getElementById('modal-entreprise')
const overlay = document.getElementById('modal-overlay')
const closeBtn = document.getElementById('modal-close')
const openBtn = document.getElementById('btn-search-entreprise')
const input = document.getElementById('search-entreprise-input')
const searchBtn = document.getElementById('search-entreprise-btn')
const resultsEl = document.getElementById('search-entreprise-results')
const statusEl = document.getElementById('search-entreprise-status')
if (!modal || !openBtn) return
const openModal = () => { modal.classList.remove('hidden'); input.focus() }
const closeModal = () => modal.classList.add('hidden')
openBtn.addEventListener('click', openModal)
closeBtn.addEventListener('click', closeModal)
overlay.addEventListener('click', closeModal)
document.addEventListener('keydown', (e) => { if (e.key === 'Escape') closeModal() })
const doSearch = async () => {
const q = input.value.trim()
if (q.length < 2) return
resultsEl.innerHTML = ''
statusEl.textContent = 'Recherche en cours...'
statusEl.classList.remove('hidden')
try {
const resp = await fetch(API_URL + '?q=' + encodeURIComponent(q) + '&page=1&per_page=10')
const data = await resp.json()
if (!data.results || data.results.length === 0) {
statusEl.textContent = 'Aucun resultat trouve.'
return
}
statusEl.textContent = data.total_results + ' resultat(s) - cliquez pour selectionner'
data.results.forEach(e => resultsEl.appendChild(renderResult(e, closeModal)))
} catch (err) {
statusEl.textContent = 'Erreur : ' + err.message
}
}
searchBtn.addEventListener('click', doSearch)
/* istanbul ignore next -- keydown non-Enter branch */
input.addEventListener('keydown', (e) => { if (e.key === 'Enter') { e.preventDefault(); doSearch() } })
}