templates/entreprise/details.html.twig line 1

Open in your IDE?
  1. {% extends 'base.html.twig' %}
  2. {% block title %}Consulter entreprise{% endblock %}
  3. {% block stylesheets %}
  4.     {{ parent() }}
  5.     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
  6.     <style>
  7.         .pdl-details-container {
  8.             margin-bottom: 20px;
  9.         }
  10.         .pdl-select {
  11.             width: 100%;
  12.             padding: 8px;
  13.             border: 1px solid #ced4da;
  14.             border-radius: 4px;
  15.             margin-bottom: 15px;
  16.         }
  17.         .pdl-details-compact {
  18.             background: #f8f9fa;
  19.             border-radius: 4px;
  20.             padding: 10px;
  21.             margin-bottom: 15px;
  22.         }
  23.         .pdl-details-row {
  24.             display: flex;
  25.             flex-wrap: wrap;
  26.             gap: 20px;
  27.             margin-bottom: 5px;
  28.         }
  29.         .pdl-detail-item {
  30.             display: flex;
  31.             align-items: center;
  32.         }
  33.         .pdl-detail-label {
  34.             font-weight: bold;
  35.             margin-right: 8px;
  36.             color: #495057;
  37.         }
  38.         .pdl-detail-value {
  39.             color: #212529;
  40.         }
  41.         .pdl-actions {
  42.             margin-top: 10px;
  43.             display: flex;
  44.             gap: 10px;
  45.         }
  46.         .contracts-section {
  47.             margin-top: 20px;
  48.             border-top: 1px solid #dee2e6;
  49.             padding-top: 10px;
  50.         }
  51.         .related-contracts {
  52.             margin-top: 15px;
  53.             background: #fff;
  54.             border: 1px solid #dee2e6;
  55.             border-radius: 4px;
  56.         }
  57.         .related-contracts table {
  58.             width: 100%;
  59.             margin-bottom: 0;
  60.         }
  61.         .related-contracts th {
  62.             background: #f8f9fa;
  63.             padding: 8px;
  64.             border-bottom: 1px solid #dee2e6;
  65.         }
  66.         .related-contracts td {
  67.             padding: 8px;
  68.             border-bottom: 1px solid #dee2e6;
  69.         }
  70.         @keyframes fadeIn {
  71.             from { opacity: 0; }
  72.             to { opacity: 1; }
  73.         }
  74.         #pdl-details, #gas-pdl-details {
  75.             animation: fadeIn 0.5s ease-in-out;
  76.         }
  77.         .error-message {
  78.             color: #dc3545;
  79.             padding: 10px;
  80.             margin-top: 10px;
  81.             border-radius: 4px;
  82.             background-color: #f8d7da;
  83.             border: 1px solid #f5c6cb;
  84.         }
  85.     .entreprise-layout {
  86.         display: flex;
  87.         height: calc(100vh - 80px);
  88.         overflow: hidden;
  89.     }
  90.     .entreprise-sidebar {
  91.         width: 15%;
  92.         min-width: 200px;
  93.         height: 60%;
  94.         background-color: #f8f9fa;
  95.         padding: 20px;
  96.         border-right: 1px solid #dee2e6;
  97.         overflow-y: auto;
  98.         border-radius: 10px;
  99.         box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  100.     }
  101.     .entreprise-sidebar h5 {
  102.         font-weight: bold;
  103.         margin-bottom: 1rem;
  104.     }
  105.     .entreprise-sidebar p {
  106.         margin-bottom: 0.5rem;
  107.         font-size: 0.9rem;
  108.     }
  109.     .entreprise-content {
  110.         flex: 1;
  111.         padding: 20px;
  112.         overflow-y: auto;
  113.     }
  114.     .entreprise-header {
  115.         display: flex;
  116.         justify-content: space-between;
  117.         align-items: center;
  118.         flex-wrap: wrap;
  119.     }
  120.     .entreprise-header h1 {
  121.         font-size: 2rem;
  122.     }
  123.     .entreprise-tabs {
  124.         display: flex;
  125.         flex-wrap: nowrap;
  126.         gap: 10px;
  127.         margin-top: 20px;
  128.         border-bottom: 1px solid #dee2e6;
  129.         padding-bottom: 10px;
  130.     }
  131.     .entreprise-tabs .list-group-item {
  132.         border: 1px;
  133.         border-radius: 10px;
  134.         padding: 10px 15px;
  135.         font-weight: 500;
  136.         font-size: 0.9rem;
  137.         border-bottom: 2px solid transparent;
  138.     }
  139.     .entreprise-tabs .list-group-item.active {
  140.         border-bottom: 2px solid #006288;
  141.         background-color: transparent;
  142.         color: #006288;
  143.         font-weight: bold;
  144.     }
  145.     </style>
  146. {% endblock %}
  147. {% block body %}
  148. <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.css">
  149. <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  150. <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.js"></script>
  151. <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  152. {{ include('_header.html.twig') }}
  153. <div class="entreprise-layout">
  154.     <div class="entreprise-sidebar">
  155.         <h5>Informations</h5>
  156.         <p><strong>SIRET :</strong> {{ entreprise.siret }}</p>
  157.         <p><strong>NAF :</strong> {{ entreprise.naf }}</p>
  158.         <p><strong>RCS :</strong> {{ entreprise.rcs }}</p>
  159.         <p><strong>Adresse :</strong><br>{{ entreprise.num_voie }} {{ entreprise.adresse }}<br>{{ entreprise.code_postal }} {{ entreprise.commune }}</p>
  160.         <p><strong>Code INSEE :</strong> {{ entreprise.code_insee }}</p>
  161.         <p><strong>Statut :</strong> {{ entreprise.statut }}</p>
  162.         <p><strong>Utilisateurs :</strong><br>
  163.             {% if entreprise.utilisateur is not empty %}
  164.                 {% for username in entreprise.utilisateur %}{{ username }}<br>{% endfor %}
  165.             {% else %}Aucun utilisateur{% endif %}
  166.         </p>
  167.     </div>
  168.     <div class="entreprise-content">
  169.         <div class="entreprise-header">
  170.             <h1>{{ entreprise.raison_sociale }}</h1>
  171.             <div class="d-flex gap-2">
  172.                 <a href="{{ path('app_entreprise') }}" class="btn btn-danger">Retour</a>
  173.                 <a href="{{ path('app_entreprise_edit', {'id': entreprise.id}) }}" class="btn btn-warning">Modifier</a>
  174.             </div>
  175.         </div>
  176.         <div class="entreprise-tabs list-group list-group-horizontal" id="list-tab" role="tablist">
  177.             <a class="list-group-item list-group-item-action active" id="list-notes-list" data-toggle="list" href="#list-notes" role="tab" aria-controls="notes">
  178.                 <span class="badge badge-primary bg-black badge-pill">{{notes|length}}</span>
  179.                 Notes
  180.             </a>
  181.             <a class="list-group-item list-group-item-action" id="list-rappels-list" data-toggle="list" href="#list-rappels" role="tab" aria-controls="rappels">
  182.                 <span class="badge badge-primary bg-black badge-pill">{{rappels|length}}</span> 
  183.                  Rappels
  184.             </a>
  185.             <a class="list-group-item list-group-item-action" id="list-electric-meters-list" data-toggle="list" href="#list-electric-meters" role="tab" aria-controls="electric-meters">
  186.                 <span class="badge badge-primary bg-black badge-pill">{{electric_meters|length}}</span> 
  187.                  Electric
  188.             </a>
  189.             <a class="list-group-item list-group-item-action" id="list-gas-meters-list" data-toggle="list" href="#list-gas-meters" role="tab" aria-controls="gas-meters">
  190.                 <span class="badge badge-primary bg-black badge-pill">{{gas_meters|length}}</span>
  191.                  Gas
  192.             </a>
  193.             <a class="list-group-item list-group-item-action" id="list-cspes-list" data-toggle="list" href="#list-cspes" role="tab" aria-controls="cspes">
  194.                 <span class="badge badge-primary bg-black badge-pill">{{cspes|length}}</span>
  195.                  CSPE
  196.             </a>
  197.             <a class="list-group-item list-group-item-action" id="list-contacts-list" data-toggle="list" href="#list-contacts" role="tab" aria-controls="contacts">
  198.                 <span class="badge badge-primary bg-black badge-pill">{{contacts|length}}</span>
  199.                  Contacts
  200.             </a>
  201.             <a class="list-group-item list-group-item-action" id="list-contrats-list" data-toggle="list" href="#list-contrats" role="tab" aria-controls="contrats">
  202.                 <span class="badge badge-primary bg-black badge-pill">{{contrats|length}}</span>
  203.                  Contrats
  204.             </a>
  205.             <a class="list-group-item list-group-item-action" id="list-espace-clients-list" data-toggle="list" href="#list-espace-clients" role="tab" aria-controls="espace-clients">
  206.                 {# <span class="badge badge-primary bg-black badge-pill">{{espace_clients|length}}</span> #}
  207.                  Espaces Clients
  208.             </a>
  209.             <a class="list-group-item list-group-item-action" id="panel-docs-list" data-toggle="list" href="#panel-docs" role="tab" aria-controls="documents">
  210.                 Génération document
  211.             </a>
  212.         </div>
  213.         <div class="tab-content" id="nav-tabContent">
  214.             <div class="tab-pane fade show active" id="list-notes" role="tabpanel" aria-labelledby="list-notes-list">
  215.                 <div class="row mb-3">
  216.                     <a type="button" href="{{path('app_note_add',{'id':entreprise.id})}}" class="btn btn-success">Ajouter une note</a>
  217.                 </div>
  218.                 <div id="notes-container">
  219.                     {% for note in notes %}
  220.                         <div class="card mb-3">
  221.                             <div class="card-header d-flex justify-content-between align-items-center">
  222.                                 <span><strong>{{ note.user }}</strong> - {{ note.date_creation }}</span>
  223.                                 <div>
  224.                                     <a href="{{ note.edit_url }}" class="btn btn-sm btn-warning mb-1">Modifier</a>
  225.                                     <a href="{{ note.delete_url }}" class="btn btn-sm btn-danger ">Supprimer</a>
  226.                                 </div>
  227.                             </div>
  228.                             <div class="card-body">
  229.                                 <p class="card-text">{{ note.texte }}</p>
  230.                             </div>
  231.                         </div>
  232.                     {% endfor %}
  233.                 </div>
  234.             </div>
  235.             <div class="tab-pane fade" id="list-rappels" role="tabpanel" aria-labelledby="list-rappels-list">
  236.                 <div class="row">
  237.                     <a type="button" href="{{path('app_rappel_add',{'id':entreprise.id})}}" class="btn btn-success">Ajouter un rappel</a>
  238.                 </div>
  239.                 <table id="rappels" class="text-center table table-light table-bordered table-striped" style="width:100%">
  240.                     <thead>
  241.                         <tr class="bg-dark">
  242.                             <th>Titre</th>
  243.                             <th>Description</th>
  244.                             <th>Echeance</th>
  245.                             <th>Compléter</th>
  246.                             <th>Action</th>
  247.                         </tr>
  248.                     </thead>
  249.                 </table>
  250.             </div>
  251.             <div class="tab-pane fade" id="list-electric-meters" role="tabpanel" aria-labelledby="list-electric-meters-list">
  252.                 <div class="row">
  253.                     <a type="button" href="{{path('app_electric_meter_add',{'id':entreprise.id})}}" class="btn btn-success">Ajouter un Electric Meter</a>
  254.                 </div>
  255.                 <div class="pdl-details-container">
  256.                     <h4>Point de Livraison</h4>
  257.                     <select class="pdl-select" id="electric-pdl-select">
  258.                         <option value="">Sélectionnez un PDL</option>
  259.                         {% for meter in electric_meters %}
  260.                             <option value="{{ meter.pdl }}" data-id="{{ meter.id }}">{{ meter.pdl }}</option>
  261.                         {% endfor %}
  262.                     </select>
  263.                     <div id="pdl-details" class="pdl-details-compact">
  264.                         Sélectionnez un PDL pour voir les détails
  265.                     </div>
  266.                 </div>
  267.             </div>
  268.             <div class="tab-pane fade" id="list-gas-meters" role="tabpanel" aria-labelledby="list-gas-meters-list">
  269.                 <div class="row">
  270.                     <a type="button" href="{{path('app_gas_meter_add',{'id':entreprise.id})}}" class="btn btn-success">Ajouter un Gas Meter</a>
  271.                 </div>
  272.                 <div class="pdl-details-container">
  273.                     <h4>Point de Livraison</h4>
  274.                     <select class="pdl-select" id="gas-pdl-select">
  275.                         <option value="">Sélectionnez un PDL</option>
  276.                         {% for meter in gas_meters %}
  277.                             <option value="{{ meter.pdl }}" data-id="{{ meter.id }}">{{ meter.pdl }}</option>
  278.                         {% endfor %}
  279.                     </select>
  280.                     <div id="gas-pdl-details" class="pdl-details-compact">
  281.                         Sélectionnez un PDL pour voir les détails
  282.                     </div>
  283.                 </div>
  284.             </div>
  285.             <div class="tab-pane fade" id="list-cspes" role="tabpanel" aria-labelledby="list-cspes-list">
  286.                 <div class="row">
  287.                     <a type="button" href="{{path('app_cspe_add',{'id':entreprise.id})}}" class="btn btn-success">Ajouter une CSPE</a>
  288.                 </div>
  289.                 <table id="cspes" class="text-center table table-light table-bordered table-striped" style="width:100%">
  290.                     <thead>
  291.                         <tr class="bg-dark">
  292.                             <th>Notes</th>
  293.                             <th>Date</th>
  294.                             <th>Valeur annuelle</th>
  295.                             <th>Action</th>
  296.                         </tr>
  297.                     </thead>
  298.                 </table>
  299.             </div>
  300.             <div class="tab-pane fade" id="list-espace-clients" role="tabpanel" aria-labelledby="list-espace-clients-list">
  301.                 <div class="row">
  302.                     <a type="button" href="{{path('app_espace_client_add',{'id':entreprise.id})}}" class="btn btn-success">Ajouter un Espace Client</a>
  303.                 </div>
  304.                 <table id="espace_clients" class="text-center table table-light table-bordered table-striped" style="width:100%">
  305.                     <thead>
  306.                         <tr class="bg-dark">
  307.                             <th>Fournisseur</th>
  308.                             <th>Login</th>
  309.                             <th>Mdp</th>
  310.                             <th>Lien</th>
  311.                             <th>Action</th>
  312.                         </tr>
  313.                     </thead>
  314.                 </table>
  315.             </div>
  316.             <div class="tab-pane fade" id="list-contacts" role="tabpanel" aria-labelledby="list-contacts-list">
  317.                 <div class="row">
  318.                     <a type="button" href="{{path('app_contact_add',{'id':entreprise.id})}}" class="btn btn-success">Ajouter un contact</a>
  319.                 </div>
  320.                 <table id="contacts" class="text-center table table-light table-bordered table-striped" style="width:100%">
  321.                     <thead>
  322.                         <tr class="bg-dark">
  323.                             <th>Nom</th>
  324.                             <th>Prénom</th>
  325.                             <th>Civilite</th>
  326.                             <th>Fonction</th>
  327.                             <th>Téléphone</th>
  328.                             <th>Mail</th>
  329.                             <th>Mobile</th>
  330.                             <th>Action</th>
  331.                         </tr>
  332.                     </thead>
  333.                     <tbody>
  334.                         {% for contact in contacts %}
  335.                             <tr>
  336.                                 <td>{{ contact[0] }}</td>
  337.                                 <td>{{ contact[1] }}</td>
  338.                                 <td>{{ contact[2] }}</td>
  339.                                 <td>{{ contact[3] }}</td>
  340.                                 <td>{{ contact[4] }}</td>
  341.                                 <td>{{ contact[5] }}</td>
  342.                                 <td>{{ contact[6] }}</td>
  343.                                 <td>{{ contact[7]|raw }}</td>
  344.                             </tr>
  345.                         {% endfor %}
  346.                     </tbody>
  347.                 </table>
  348.             </div>
  349.             <div class="tab-pane fade" id="list-contrats" role="tabpanel" aria-labelledby="list-contrats-list">
  350.                 <div class="row mb-3">
  351.                     <a type="button" href="{{path('app_entreprise_new_contrat', {'id': entreprise.id})}}" class="btn btn-success">Ajouter un contrat</a>
  352.                 </div>
  353.                 <table id="contrats" class="text-center table table-light table-bordered table-striped" style="width:100%">
  354.                     <thead>
  355.                         <tr class="bg-dark">
  356.                             <th>Type</th>
  357.                             <th>Durée (mois)</th>
  358.                             <th>Valeur</th>
  359.                             <th>PDL</th>
  360.                             <th>CAR</th>
  361.                             <th>Prix moyen</th>
  362.                             <th>Fournisseur</th>
  363.                             <th>Date de début</th>
  364.                             <th>Date de fin</th>
  365.                             <th>Date signature</th>
  366.                             <th>Action</th>
  367.                         </tr>
  368.                     </thead>
  369.                     <tbody>
  370.                     {% for contrat in contrats %}
  371.                         <tr>
  372.                             <td>
  373.                                 {% if contrat.pdl in electric_pdls|default([]) %}
  374.                                     Électricité
  375.                                 {% elseif contrat.pdl in gas_pdls|default([]) %}
  376.                                     Gaz
  377.                                 {% else %}
  378.                                     Non défini
  379.                                 {% endif %}
  380.                             </td>
  381.                             <td>{{ contrat.duree }}</td>
  382.                             <td>{{ contrat.valeur }}</td>
  383.                             <td>{{ contrat.pdl }}</td>
  384.                             <td>{{ contrat.car }}</td>
  385.                             <td>{{ contrat.prix_moyen }}</td>
  386.                             <td>{{ contrat.fournisseur }}</td>
  387.                             <td>{{ contrat.date_debut ? contrat.date_debut : 'N/A' }}</td>
  388.                             <td>{{ contrat.date_fin ? contrat.date_fin : 'N/A' }}</td>
  389.                             <td>{{ contrat.date_signature ? contrat.date_signature : 'N/A' }}</td>
  390.                             <td>
  391.                                 <a href="{{ path('app_entreprise_edit_contrat', {'entrepriseId': entreprise.id, 'id': contrat.id}) }}" class="btn btn-sm btn-warning mb-1">Modifier</a>
  392.                                 <form method="post" action="{{ path('app_entreprise_delete_contrat', {'entrepriseId': entreprise.id, 'id': contrat.id}) }}" onsubmit="return confirmDelete(event);" style="display: inline-block;">
  393.                                     <input type="hidden" name="_token" value="{{ csrf_token('delete' ~ contrat.id) }}">
  394.                                     <button type="submit" class="btn btn-sm btn-danger">Supprimer</button>
  395.                                 </form>
  396.                             </td>
  397.                         </tr>
  398.                     {% endfor %}
  399.                     </tbody>
  400.                 </table>
  401.             </div>
  402.         </div>        
  403.     </div>
  404. </div>
  405.     {{ include('_footer.html.twig') }}
  406. {% endblock %}
  407. {% block javascripts %}
  408.     <script>
  409.      
  410.         function confirmDelete(event) {
  411.             event.preventDefault();
  412.             if (confirm('Êtes-vous sûr de vouloir supprimer ce contrat ?')) {
  413.                 event.target.submit();
  414.             }
  415.         }
  416.         
  417.         function formatPDLDetails(details, meterType) {
  418.             console.log('Formatting details:', details); // Debug log
  419.             let html = '<div class="pdl-details-compact">';
  420.             
  421.             // First row - PDL and dates
  422.             html += '<div class="pdl-details-row">';
  423.             if (details.PDL) {
  424.                 html += `
  425.                     <div class="pdl-detail-item">
  426.                         <i class="fas fa-fingerprint mr-2"></i>
  427.                         <span class="pdl-detail-label">PDL:</span>
  428.                         <span class="pdl-detail-value">${details.PDL}</span>
  429.                     </div>`;
  430.             }
  431.             /*if (details.dateDebut) {
  432.                 html += `
  433.                     <div class="pdl-detail-item">
  434.                         <i class="fas fa-calendar-alt mr-2"></i>
  435.                         <span class="pdl-detail-label">Date début:</span>
  436.                         <span class="pdl-detail-value">${details.dateDebut}</span>
  437.                     </div>`;
  438.             }
  439.             if (details.dateFin) {
  440.                 html += `
  441.                     <div class="pdl-detail-item">
  442.                         <i class="fas fa-calendar-check mr-2"></i>
  443.                         <span class="pdl-detail-label">Date fin:</span>
  444.                         <span class="pdl-detail-value">${details.dateFin}</span>
  445.                     </div>`;
  446.             }*/
  447.             html += '</div>';
  448.             
  449.             // Second row - Address and supplier
  450.             html += '<div class="pdl-details-row">';
  451.             if (details.adresseCompteur) {
  452.                 html += `
  453.                     <div class="pdl-detail-item">
  454.                         <i class="fas fa-map-marker-alt mr-2"></i>
  455.                         <span class="pdl-detail-label">Adresse:</span>
  456.                         <span class="pdl-detail-value">${details.adresseCompteur}</span>
  457.                     </div>`;
  458.             }
  459.             /*if (details.fournisseur) {
  460.                 html += `
  461.                     <div class="pdl-detail-item">
  462.                         <i class="fas fa-industry mr-2"></i>
  463.                         <span class="pdl-detail-label">Fournisseur:</span>
  464.                         <span class="pdl-detail-value">${details.fournisseur}</span>
  465.                     </div>`;
  466.             }*/
  467.             html += '</div>';
  468.             
  469.             // Third row - Technical details
  470.             html += '<div class="pdl-details-row">';
  471.             if (details.profil) {
  472.                 html += `
  473.                     <div class="pdl-detail-item">
  474.                         <i class="fas fa-chart-bar mr-2"></i>
  475.                         <span class="pdl-detail-label">Profil:</span>
  476.                         <span class="pdl-detail-value">${details.profil}</span>
  477.                     </div>`;
  478.             }
  479.             if (details.PS) {
  480.                 html += `
  481.                     <div class="pdl-detail-item">
  482.                         <i class="fas fa-bolt mr-2"></i>
  483.                         <span class="pdl-detail-label">PS:</span>
  484.                         <span class="pdl-detail-value">${details.PS}</span>
  485.                     </div>`;
  486.             }
  487.             if (details.CAR) {
  488.                 html += `
  489.                     <div class="pdl-detail-item">
  490.                         <i class="fas fa-chart-line mr-2"></i>
  491.                         <span class="pdl-detail-label">CAR:</span>
  492.                         <span class="pdl-detail-value">${details.CAR}</span>
  493.                     </div>`;
  494.             }
  495.             if (details.prix) {
  496.                 html += `
  497.                     <div class="pdl-detail-item">
  498.                         <i class="fas fa-euro-sign mr-2"></i>
  499.                         <span class="pdl-detail-label">Prix:</span>
  500.                         <span class="pdl-detail-value">${formatValue('prix', details.prix)}</span>
  501.                     </div>`;
  502.             }
  503.             html += '</div>';
  504.             // Add related contracts section
  505.             html += '<div class="contracts-section">';
  506.             html += '<h5>Contrats liés</h5>';
  507.             html += '<div class="related-contracts">';
  508.             html += '<table class="table">';
  509.             html += '<thead><tr>';
  510.             html += '<th>Date de début</th>';
  511.             html += '<th>Date de fin</th>';
  512.             html += '<th>Date signature</th>';
  513.             html += '<th>Fournisseur</th>';
  514.             html += '<th>Prix moyen</th>';
  515.             html += '</tr></thead>';
  516.             html += '<tbody>';
  517.             
  518.             // Filter contracts for this PDL
  519.             const relatedContracts = window.contrats.filter(c => c.pdl === details.PDL);
  520.             if (relatedContracts.length > 0) {
  521.                 relatedContracts.forEach(contract => {
  522.                     html += '<tr>';
  523.                     html += `<td>${contract.date_debut || ''}</td>`;
  524.                     html += `<td>${contract.date_fin || ''}</td>`;
  525.                     html += `<td>${contract.date_signature || ''}</td>`;
  526.                     html += `<td>${contract.fournisseur || ''}</td>`;
  527.                     html += `<td>${formatValue('prix', contract.prix_moyen)}</td>`;
  528.                     html += '</tr>';
  529.                 });
  530.             } else {
  531.                 html += '<tr><td colspan="5" class="text-center">Aucun contrat lié</td></tr>';
  532.             }
  533.             
  534.             html += '</tbody></table>';
  535.             html += '</div></div>';
  536.             
  537.             // Actions with meter type and ID
  538.             const meterId = meterType === 'gas' ? 
  539.                 $('#gas-pdl-select option:selected').data('id') : 
  540.                 $('#electric-pdl-select option:selected').data('id');
  541.             html += `
  542.                 <div class="pdl-actions">
  543.                     <a href="{{ path('app_entreprise_new_contrat', {'id': entreprise.id}) }}?meterId=${meterId}&meterType=${meterType}" class="btn btn-sm btn-success">Nouveau contrat</a>
  544.                     <button class="btn btn-sm btn-warning edit-pdl">Modifier</button>
  545.                     <button class="btn btn-sm btn-danger delete-pdl">Supprimer</button>`;
  546.             
  547.             // Add study generation buttons
  548.             if (meterType === 'electric') {
  549.                 const studyUrlC5 = "{{ path('app_electric_meter_generate_study', {'pdl': '__PDL__', 'type': 'C5'}) }}".replace('__PDL__', details.PDL);
  550.                 const studyUrlC4 = "{{ path('app_electric_meter_generate_study', {'pdl': '__PDL__', 'type': 'C4'}) }}".replace('__PDL__', details.PDL);
  551.                 
  552.                 html += `
  553.                     <a href="${studyUrlC5}" class="btn btn-sm btn-info">
  554.                         Générer étude C5
  555.                     </a>
  556.                     <a href="${studyUrlC4}" class="btn btn-sm btn-info">
  557.                         Générer étude C4/C3/C2
  558.                     </a>`;
  559.             } else if (meterType === 'gas') {
  560.                 const studyUrl = "{{ path('app_gas_meter_pre_etude_t2', {'pdl': '__PDL__'}) }}".replace('__PDL__', details.PDL);
  561.                 html += `
  562.                     <a href="${studyUrl}" class="btn btn-sm btn-info">
  563.                         Générer étude T2
  564.                     </a>`;
  565.             }
  566.             
  567.             html += `</div>`;
  568.             
  569.             html += '</div>';
  570.             return html;
  571.         }
  572.         function formatValue(key, value) {
  573.             if (key.toLowerCase().includes('prix') && !isNaN(value)) {
  574.                 return new Intl.NumberFormat('fr-FR', { style: 'currency', currency: 'EUR' }).format(value);
  575.             }
  576.             if (typeof value === 'boolean') {
  577.                 return value ? '<span class="text-success">Actif</span>' : '<span class="text-danger">Inactif</span>';
  578.             }
  579.             return value || '';
  580.         }
  581.         $(document).ready(function() {
  582.             // Store contracts data globally
  583.             window.contrats = {{ contrats|json_encode|raw }};
  584.             // Common DataTable configuration
  585.             const commonConfig = {
  586.                 "language": {
  587.                     "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/French.json"
  588.                 },
  589.                 "pageLength": 10,
  590.                 "destroy": true
  591.             };
  592.             // Handle automatic PDL selection when tabs are clicked
  593.             $('#list-electric-meters-list').on('click', function() {
  594.                 const select = $('#electric-pdl-select');
  595.                 if (select.find('option').length === 2) { // 2 because there's always a default empty option
  596.                     select.val(select.find('option:last').val()).trigger('change');
  597.                 }
  598.             });
  599.             $('#list-gas-meters-list').on('click', function() {
  600.                 const select = $('#gas-pdl-select');
  601.                 if (select.find('option').length === 2) { // 2 because there's always a default empty option
  602.                     select.val(select.find('option:last').val()).trigger('change');
  603.                 }
  604.             });
  605.             // Initialize tables that have data in tbody
  606.             $('#contacts, #contrats').each(function() {
  607.                 if (!$.fn.DataTable.isDataTable(this)) {
  608.                     $(this).DataTable({
  609.                         ...commonConfig,
  610.                         "order": [[0, 'asc']]
  611.                     });
  612.                 }
  613.             });
  614.             // Initialize tables that need data from variables
  615.             const tableData = {
  616.                 rappels: {{ rappels|json_encode|raw }},
  617.                 cspes: {{ cspes|json_encode|raw }},
  618.                 espace_clients: {{ espace_clients|json_encode|raw }}
  619.             };
  620.             Object.entries(tableData).forEach(([tableId, data]) => {
  621.                 const table = $(`#${tableId}`);
  622.                 if (!$.fn.DataTable.isDataTable(table)) {
  623.                     table.DataTable({
  624.                         ...commonConfig,
  625.                         data: data
  626.                     });
  627.                 }
  628.             });
  629.             // Electric Meters PDL selection
  630.             $('#electric-pdl-select').change(function() {
  631.                 var pdl = $(this).val();
  632.                 if (!pdl) {
  633.                     $('#pdl-details').html('Sélectionnez un PDL pour voir les détails');
  634.                     return;
  635.                 }
  636.                 
  637.                 $.ajax({
  638.                     url: '{{ path('app_electric_meter_details', {'id': entreprise.id}) }}',
  639.                     method: 'GET',
  640.                     data: { pdl: pdl },
  641.                     success: function(response) {
  642.                         console.log('Electric meter response:', response);
  643.                         $('#pdl-details').html(formatPDLDetails(response, 'electric'));
  644.                         initializePDLActions('electric', $('#electric-pdl-select option:selected').data('id'));
  645.                     },
  646.                     error: function(xhr, status, error) {
  647.                         console.error('Error details:', {
  648.                             status: xhr.status,
  649.                             statusText: xhr.statusText,
  650.                             responseText: xhr.responseText
  651.                         });
  652.                         $('#pdl-details').html(`<div class="error-message">Erreur lors du chargement des détails: ${error}</div>`);
  653.                     }
  654.                 });
  655.             });
  656.             // Gas Meters PDL selection
  657.             $('#gas-pdl-select').change(function() {
  658.                 var pdl = $(this).val();
  659.                 if (!pdl) {
  660.                     $('#gas-pdl-details').html('Sélectionnez un PDL pour voir les détails');
  661.                     return;
  662.                 }
  663.                 
  664.                 $.ajax({
  665.                     url: '{{ path('app_gas_meter_details', {'id': entreprise.id}) }}',
  666.                     method: 'GET',
  667.                     data: { pdl: pdl },
  668.                     success: function(response) {
  669.                         console.log('Gas meter response:', response);
  670.                         $('#gas-pdl-details').html(formatPDLDetails(response, 'gas'));
  671.                         initializePDLActions('gas', $('#gas-pdl-select option:selected').data('id'));
  672.                     },
  673.                     error: function(xhr, status, error) {
  674.                         console.error('Error details:', {
  675.                             status: xhr.status,
  676.                             statusText: xhr.statusText,
  677.                             responseText: xhr.responseText
  678.                         });
  679.                         $('#gas-pdl-details').html(`<div class="error-message">Erreur lors du chargement des détails: ${error}</div>`);
  680.                     }
  681.                 });
  682.             });
  683.             function initializePDLActions(type, id) {
  684.                 // Handle edit PDL
  685.                 $('.edit-pdl').click(function() {
  686.                     var url = type === 'electric' 
  687.                         ? '{{ path('app_electric_meter_edit', {'id': 'METER_ID'}) }}'
  688.                         : '{{ path('app_gas_meter_edit', {'id': 'METER_ID'}) }}';
  689.                     url = url.replace('METER_ID', id);
  690.                     window.location.href = url;
  691.                 });
  692.                 // Handle delete PDL
  693.                 $('.delete-pdl').click(function() {
  694.                     if (confirm('Êtes-vous sûr de vouloir supprimer ce PDL ?')) {
  695.                         var url = type === 'electric' 
  696.                             ? '{{ path('app_electric_meter_suppr', {'id': 'METER_ID'}) }}'
  697.                             : '{{ path('app_gas_meter_suppr', {'id': 'METER_ID'}) }}';
  698.                         url = url.replace('METER_ID', id);
  699.                         
  700.                         $.ajax({
  701.                             url: url,
  702.                             method: 'POST',
  703.                             data: {
  704.                                 '_token': '{{ csrf_token('delete-meter') }}'
  705.                             },
  706.                             success: function() {
  707.                                 location.reload();
  708.                             },
  709.                             error: function(xhr, status, error) {
  710.                                 console.error('Error deleting meter:', error);
  711.                                 alert('Erreur lors de la suppression du compteur');
  712.                             }
  713.                         });
  714.                     }
  715.                 });
  716.             }
  717.         });
  718.     </script>
  719. {% endblock %}