templates/entreprise/new_contrat.html.twig line 1

Open in your IDE?
  1. {% extends 'base.html.twig' %}
  2. {% block title %}Nouveau Contrat pour {{ entreprise.raisonSociale }}{% endblock %}
  3. {% block stylesheets %}
  4.     {{ parent() }}
  5.     <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
  6. {% endblock %}
  7. {% block body %}
  8. {{ include('_header.html.twig') }}
  9. <div class="container mt-4">
  10.     <div class="row">
  11.         <div class="col-md-8 offset-md-2">
  12.             <div class="card">
  13.                 <div class="card-header">
  14.                     <h1 class="h3 mb-0">Créer un nouveau contrat pour {{ entreprise.raisonSociale }}</h1>
  15.                 </div>
  16.                 <div class="card-body">
  17.                 {{ form_start(form, {'attr': {'class': 'needs-validation', 'novalidate': 'novalidate', 'id': 'contrat-form'}}) }}
  18.                     <div class="form-fields-container">
  19.                         {{ form_row(form.entreprise, {
  20.                             'label': 'Entreprise',
  21.                             'label_attr': {'class': 'form-label'},
  22.                             'attr': {'class': 'form-control', 'readonly': 'readonly'}
  23.                         }) }}
  24.                         
  25.                         {{ form_row(form.duree, {
  26.                             'label': 'Durée (en mois)',
  27.                             'label_attr': {'class': 'form-label'},
  28.                             'attr': {'class': 'form-control', 'id': 'duree-input'}
  29.                         }) }}
  30.                         
  31.                         {{ form_row(form.valeur, {
  32.                             'label': 'Valeur',
  33.                             'label_attr': {'class': 'form-label'},
  34.                             'attr': {'class': 'form-control', 'id': 'valeur-input'}
  35.                         }) }}
  36.                         
  37.                         {{ form_row(form.pdl, {
  38.                             'label': 'Point de Livraison (PDL)',
  39.                             'label_attr': {'class': 'form-label'},
  40.                             'attr': {'class': 'form-control select2', 'placeholder': 'Sélectionnez le PDL'}
  41.                         }) }}
  42.                         
  43.                         {{ form_row(form.car, {
  44.                             'label': 'Consommation Annuelle de Référence (CAR)',
  45.                             'label_attr': {'class': 'form-label'},
  46.                             'attr': {'class': 'form-control', 'placeholder': 'Entrez la CAR', 'id': 'car-input'}
  47.                         }) }}
  48.                         {{ form_row(form.prix_moyen, {
  49.                             'label': 'Prix moyen',
  50.                             'label_attr': {'class': 'form-label'},
  51.                             'attr': {'class': 'form-control', 'placeholder': 'Entrez le prix moyen'}
  52.                         }) }}
  53.                         {{ form_row(form.fournisseur, {
  54.                             'label': 'Fournisseur',
  55.                             'label_attr': {'class': 'form-label'},
  56.                             'attr': {'class': 'form-control', 'placeholder': 'Entrez le fournisseur'}
  57.                         }) }}
  58.                         {{ form_row(form.date_debut, {
  59.                             'label': 'Date de début',
  60.                             'label_attr': {'class': 'form-label'},
  61.                             'attr': {'class': 'form-control', 'id': 'date-debut-input'}
  62.                         }) }}
  63.                         {{ form_row(form.date_fin, {
  64.                             'label': 'Date de fin',
  65.                             'label_attr': {'class': 'form-label'},
  66.                             'attr': {'class': 'form-control', 'id': 'date-fin-input'}
  67.                         }) }}
  68.                     </div>
  69.                     
  70.                     <div class="d-grid gap-2 mt-3">
  71.                         <button type="submit" class="btn btn-primary">Créer le contrat</button>
  72.                     </div>
  73.                 {{ form_end(form) }}
  74.                 </div>
  75.             </div>
  76.             <div class="mt-3">
  77.                 <a href="{{ path('app_entreprise_details', {'id': entreprise.id}) }}" class="btn btn-secondary">Retour aux détails de l'entreprise</a>
  78.             </div>
  79.         </div>
  80.     </div>
  81. </div>
  82. {{ include('_footer.html.twig') }}
  83. {% endblock %}
  84. {% block javascripts %}
  85.     {{ parent() }}
  86.     <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  87.     <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
  88.     <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
  89.     <script>
  90.         $(document).ready(function() {
  91.             $('.select2').select2();
  92.             // Handle form submission
  93.             $('#contrat-form').on('submit', function(e) {
  94.                 e.preventDefault();
  95.                 
  96.                 // Submit form via AJAX
  97.                 $.ajax({
  98.                     url: $(this).attr('action'),
  99.                     method: 'POST',
  100.                     data: $(this).serialize(),
  101.                     success: function(response) {
  102.                         if (response.success) {
  103.                             // Show confirmation popup for invoice creation
  104.                             Swal.fire({
  105.                                 title: 'Contrat créé avec succès',
  106.                                 text: 'Voulez-vous créer une facture associée à ce contrat ?',
  107.                                 icon: 'success',
  108.                                 showCancelButton: true,
  109.                                 confirmButtonText: 'Oui, créer une facture',
  110.                                 cancelButtonText: 'Non, plus tard'
  111.                             }).then((result) => {
  112.                                 // Always redirect to enterprise details first
  113.                                 window.location.href = '{{ path('app_entreprise_details', {'id': entreprise.id}) }}';
  114.                                 
  115.                                 // If user wants to create invoice, open it in a new tab
  116.                                 if (result.isConfirmed) {
  117.                                     window.open('{{ path('app_compta_facture_new_from_contract', {'id': 'CONTRACT_ID'}) }}'.replace('CONTRACT_ID', response.contractId), '_blank');
  118.                                 }
  119.                             });
  120.                         }
  121.                     }
  122.                 });
  123.             });
  124.             // Function to calculate duration in months
  125.             function calculateDuration() {
  126.                 var dateDebut = $('#date-debut-input').val();
  127.                 var dateFin = $('#date-fin-input').val();
  128.                 
  129.                 if (dateDebut && dateFin) {
  130.                     var start = new Date(dateDebut);
  131.                     var end = new Date(dateFin);
  132.                     var months = (end.getFullYear() - start.getFullYear()) * 12;
  133.                     months -= start.getMonth();
  134.                     months += end.getMonth();
  135.                     // Add 1 if day of month in end date is greater
  136.                     if (end.getDate() > start.getDate()) {
  137.                         months++;
  138.                     }
  139.                     return months <= 0 ? 0 : months;
  140.                 }
  141.                 return null;
  142.             }
  143.             // Function to format date with time component
  144.             function formatDateWithTime(date) {
  145.                 return date.getFullYear() + '-' + 
  146.                     String(date.getMonth() + 1).padStart(2, '0') + '-' + 
  147.                     String(date.getDate()).padStart(2, '0') + ' ' +
  148.                     '00:00:00';
  149.             }
  150.             // Function to calculate contract value and duration
  151.             function calculateContractValues() {
  152.                 var car = parseFloat($('#car-input').val());
  153.                 var dateDebut = $('#date-debut-input').val();
  154.                 var dateFin = $('#date-fin-input').val();
  155.                 var pdl = $('#contrat_pdl').val();
  156.                 var currentValue = parseFloat($('#valeur-input').val());
  157.                 
  158.                 // Only calculate if there's no value or value is 0
  159.                 if (car && dateDebut && dateFin && (!currentValue || currentValue === 0)) {
  160.                     // Format dates with time component
  161.                     var startDate = new Date(dateDebut);
  162.                     var endDate = new Date(dateFin);
  163.                     
  164.                     var formattedStartDate = formatDateWithTime(startDate);
  165.                     var formattedEndDate = formatDateWithTime(endDate);
  166.                     // Calculate and set duration first
  167.                     var duration = calculateDuration();
  168.                     if (duration !== null) {
  169.                         $('#duree-input').val(duration);
  170.                     }
  171.                     // Make AJAX call to get pricing and calculate value
  172.                     console.log('Calculating value...', {
  173.                         car: car,
  174.                         dateDebut: formattedStartDate,
  175.                         dateFin: formattedEndDate,
  176.                         pdl: pdl
  177.                     });
  178.                     $.ajax({
  179.                         url: '{{ path('app_entreprise_calculate_value') }}',
  180.                         method: 'POST',
  181.                         headers: {
  182.                             'X-CSRF-Token': '{{ csrf_token('calculate-value') }}'
  183.                         },
  184.                         data: {
  185.                             car: car,
  186.                             dateDebut: formattedStartDate,
  187.                             dateFin: formattedEndDate,
  188.                             pdl: pdl
  189.                         },
  190.                         success: function(response) {
  191.                             console.log('Response:', response);
  192.                             if (response.value !== undefined) {
  193.                                 $('#valeur-input').val(response.value);
  194.                             } else if (response.error) {
  195.                                 console.error('Error:', response.error);
  196.                             }
  197.                         },
  198.                         error: function(xhr, status, error) {
  199.                             console.error('Error calculating value:', error);
  200.                             console.error('Status:', status);
  201.                             console.error('Response:', xhr.responseText);
  202.                         }
  203.                     });
  204.                 }
  205.             }
  206.             // Trigger calculation when CAR or dates change
  207.             $('#car-input, #date-debut-input, #date-fin-input').change(function() {
  208.                 calculateContractValues();
  209.             });
  210.             // Calculate initial values if all required fields are filled
  211.             if ($('#car-input').val() && $('#date-debut-input').val() && $('#date-fin-input').val()) {
  212.                 calculateContractValues();
  213.             }
  214.             // Also trigger calculation when PDL changes (in case it auto-fills other fields)
  215.             $('#contrat_pdl').change(function() {
  216.                 // Wait a bit for other fields to be populated
  217.                 setTimeout(function() {
  218.                     if ($('#car-input').val() && $('#date-debut-input').val() && $('#date-fin-input').val()) {
  219.                         calculateContractValues();
  220.                     }
  221.                 }, 500);
  222.             });
  223.         });
  224.     </script>
  225. {% endblock %}