<?php echo $__env->make('layouts.commons.errors', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php echo $__env->make('layouts.commons.success', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>

<form method="POST"
    action="<?php echo e(route("projets.update_achats_investissements", $projet->id ?? '')); ?>">
    <?php echo csrf_field(); ?>
    <?php echo e(method_field('PUT')); ?>

    <input type="hidden" name="projet_id" value="<?php echo e($projet->id); ?>">

    <div class="row">
        <div class="col-6">
            <div class="form-group row">
                <label for="type_achat_investissement" class="col-6 col-form-label required">Type d'achat</label>
                <div class="col-6">
                    <select class="form-control" name="type_achat_investissement" id="type_achat_investissement">
                        <option value="<?php echo e(TYPE_ACHAT_INVESTISSEMENT::INVESTISSEMENT); ?>" <?php if(TYPE_ACHAT_INVESTISSEMENT::INVESTISSEMENT == old('type_achat_investissement', $projet->type_achat_investissement ?? '')): ?> selected <?php endif; ?>>Investissement</option>
                        <option value="<?php echo e(TYPE_ACHAT_INVESTISSEMENT::PRODUIT_PRESTATION); ?>" <?php if(TYPE_ACHAT_INVESTISSEMENT::PRODUIT_PRESTATION == old('type_achat_investissement', $projet->type_achat_investissement ?? '')): ?> selected <?php endif; ?>>Produit/Prestation</option>
                    </select>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-6">
            <div class="form-group row">
                <label for="duree_mois" class="col-9 col-form-label">Durée du marché pour les produits et services et durée d'amortissement pour les matériels et équipements</label>
                <div class="col-3">
                    <input type="number" class="form-control" name="duree_mois" id="duree_mois"
                    value="<?php echo e($projet->duree_mois ?? 12); ?>" step="12" min="12">
                </div>
            </div>
        </div>
        <div class="col-6 text-right">
            <div class="form-group row">
                <label for="duree_mois" class="col-6 col-form-label">Date de notification</label>
                <div class="col-6">
                    <div class="input-group">
                        <input type="text" class="datepicker form-control flatpickr-input" id="date_notification"
                            name="date_notification"
                            value="<?php echo e(old('date_notification', isset($projet->date_mise_en_oeuvre) ? date('d/m/Y', strtotime($projet->date_mise_en_oeuvre)) : '')); ?>">
                        <div class="input-group-append">
                            <span class="input-group-text">
                                <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-calendar3"
                                    fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                                    <path fill-rule="evenodd"
                                        d="M14 0H2a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2zM1 3.857C1 3.384 1.448 3 2 3h12c.552 0 1 .384 1 .857v10.286c0 .473-.448.857-1 .857H2c-.552 0-1-.384-1-.857V3.857z" />
                                    <path fill-rule="evenodd"
                                        d="M6.5 7a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm-9 3a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm-9 3a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2z" />
                                </svg>
                            </span>
                        </div>
                    </div>
                    <span class="text-danger"></span>
                </div>
            </div>
        </div>
    </div>


    
    <h4>Référentiel</h4>
    <div class="row custom-frame pt-3">
        <div class="col-6">
            <div class="form-group row">
                <label for="montant_reference" class="col-6 col-form-label required">Montant estimé</label>
                <div class="col-6">
                    <div class="input-group">
                        <input type="text" class="form-control autonumeric" id="montant_reference" name="montant_reference"
                            value="<?php echo e(old('montant_reference', $projet->montant_reference ?? '')); ?>" <?php echo e($projet->report ? 'readonly' : ''); ?>>
                        <div class="input-group-append">
                            <span class="input-group-text">€/an</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-6 text-right">
            <div class="form-group row">
                <label for="pourcentage_gains_cible" class="col-sm-6 col-form-label required">Gains cible</label>
                <div class="col-sm-6">
                    <div class="input-group">
                        <input type="number" class="form-control" id="pourcentage_gains_cible" name="pourcentage_gains_cible"
                            value="<?php echo e(old('pourcentage_gains_cible', $projet->pourcentage_gains_cible ?? '')); ?>" min="0" max="100"
                            <?php echo e($projet->report ? 'readonly' : ''); ?>>
                        <div class="input-group-append">
                            <span class="input-group-text">%</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-12">
            <div class="form-group row">
                <label class="col-3 col-form-label">Ventilation des gains cibles</label>
                <div class="col-3 text-center">
                    <strong><?php echo e($annee_N); ?></strong>
                    <input type="text" id="gains_cible_N" class="form-control" disabled>
                </div>
                <?php if(!$projet->report): ?>
                <div class="col-3 text-center">
                    <strong><?php echo e($annee_N1); ?></strong>
                    <input type="text" id="gains_cible_N1" class="form-control" disabled>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    
    <h4>Notifié</h4>
    <div class="row custom-frame pt-3">
        <div class="col-6">
            <div class="form-group row pb-0">
                <label for="montant_notifie" class="col-6 col-form-label">Montant notifié, valorisation des gains en base 12 mois</label>
                <div class="col-6">
                    <div class="input-group">
                        <input type="text" class="form-control autonumeric" id="montant_notifie" name="montant_notifie"
                            value="<?php echo e(old('montant_notifie', $projet->montant_notifie ?? '')); ?>">
                        <div class="input-group-append">
                            <span class="input-group-text">€/an</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-6 text-right">
            <div class="form-group row pb-0">
                <label for="pourcentage_gains_notifie" class="col-6 col-form-label ">Gains notifiés</label>
                <div class="col-6">
                    <div class="input-group">
                        <input type="number" id="pourcentage_gains_notifie" class="form-control" disabled>
                        <div class="input-group-append">
                            <span class="input-group-text">%</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-6">
            <div class="form-group row pb-0">
                <label for="moyenne_offres" class="col-6 col-form-label">Moyenne des offres</label>
                <div class="col-6">
                    <div class="input-group">
                        <input type="text" name="moyenne_offres" id="moyenne_offres" class="form-control autonumeric"
                        value="<?php echo e(old('moyenne_offres', $projet->moyenne_offres ?? '')); ?>">
                        <div class="input-group-append">
                            <span class="input-group-text">€/an</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-12">
            <div class="form-group row">
                <label class="col-3 col-form-label">Ventilation des gains notifiés</label>
                <div class="col-3 text-center">
                    <strong><?php echo e($annee_N); ?></strong>
                    <input type="text" id="gains_notifie_N" class="form-control" disabled>
                </div>
                <?php if(!$projet->report): ?>
                <div class="col-3 text-center">
                    <strong><?php echo e($annee_N1); ?></strong>
                    <input type="text" id="gains_notifie_N1" class="form-control" disabled>
                </div>
                <?php endif; ?>

            </div>

        </div>
    </div>


    <!-- Bloc Réalisé -->
    <h4>Réalisé</h4>
    <div class="row custom-frame pt-3">
        <div class="col-4">
            <div class="form-group row pb-0">
                <label for="montant_realise" class="col-6 col-form-label">Montant réalisé</label>
                <div class="col-6">
                    <div class="input-group">
                        <input type="text" class="form-control autonumeric" id="montant_realise" name="montant_realise"
                            value="<?php echo e(old('montant_realise', $projet->montant_realise ?? '')); ?>">
                        <div class="input-group-append">
                            <span class="input-group-text">€/an</span>
                        </div>
                    </div>
                    <span class="text-danger"></span>
                </div>
                <span class="col-12">
                    <i>(montant de la GEF au terme de la 1ère année d'exécution)</i>
                </span>
            </div>
        </div>
        <div class="col-4">
            <div class="form-group row pb-0">
                <label for="gains_securise" class="col-6 col-form-label">Gains sécurisés</label>
                <div class="col-6">
                    <div class="input-group">
                        <input type="text" id="gains_securise" class="form-control" disabled>
                        <div class="input-group-append">
                            <span class="input-group-text">€</span>
                        </div>
                    </div>
                    <span class="text-danger"></span>
                </div>
            </div>
        </div>
        <div class="col-4">
            <div class="form-group row pb-0">
                <label for="pourcentage_gains_securise" class="col-6 col-form-label">% de gains sécurisés</label>
                <div class="col-6">
                    <div class="input-group">
                        <input type="number" id="pourcentage_gains_securise" class="form-control" disabled>
                        <div class="input-group-append">
                            <span class="input-group-text">%</span>
                        </div>
                    </div>
                    <span class="text-danger"></span>
                </div>
            </div>
        </div>

        <div class="col-12">
            <div class="form-group row">
                <label class="col-3 col-form-label text-center">Ventilation des gains sécurisés</label>
                <div class="col-3 text-center">
                    <strong><?php echo e($annee_N); ?></strong>
                    <input type="text" id="gains_realise_N" class="form-control" disabled>
                </div>
                <?php if(!$projet->report): ?>
                <div class="col-3 text-center">
                    <strong><?php echo e($annee_N1); ?></strong>
                    <input type="text" id="gains_realise_N1" class="form-control" disabled>
                </div>
                <?php endif; ?>

            </div>

        </div>
    </div>


    <!-- Bloc Synthèse -->
    <h4>Synthèse</h4>
    <div class="row custom-frame pt-3">
        <div class="col-sm-8">
            <table class="table">
                <tbody>
                    <tr>
                        <th></th>
                        <?php if(!$projet->report): ?>
                        <th><?php echo e($annee_N); ?></th>
                        <th><?php echo e($annee_N1); ?></th>
                        <?php endif; ?>
                        <th>Total</th>
                    </tr>
                </tbody>
                <tbody>
                    <tr>
                        <td>Gains cibles en montant</td>
                        <?php if(!$projet->report): ?>
                        <td><span id="synthese_gains_cible_N"></span></td>
                        <td><span id="synthese_gains_cible_N1"></span></td>
                        <?php endif; ?>
                        <td><span id="synthese_gains_cible_total"></span></td>
                    </tr>
                    <tr>
                        <td>Gains notifiés en montant</td>
                        <?php if(!$projet->report): ?>
                        <td><span id="synthese_gains_notifie_N"></span></td>
                        <td><span id="synthese_gains_notifie_N1"></span></td>
                        <?php endif; ?>
                        <td><span id="synthese_gains_notifie_total"></span></td>
                    </tr>
                    <tr>
                        <td>Gains sécurisés en montant</td>
                        <?php if(!$projet->report): ?>
                        <td><span id="synthese_gains_realise_N"></span></td>
                        <td><span id="synthese_gains_realise_N1"></span></td>
                        <?php endif; ?>
                        <td><span id="synthese_gains_realise_total"></span></td>
                    </tr>
                    <tr>
                        <td>Gains budgétaires en montant</td>
                        <?php if(!$projet->report): ?>
                        <td><span id="synthese_gains_budgetaire_N"></span></td>
                        <td><span id="synthese_gains_budgetaire_N1"></span></td>
                        <?php endif; ?>
                        <td><span id="synthese_gains_budgetaire_total"></span></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>


    <!-- Zone commentaire -->
    <div class="row custom-frame pt-3">
        <div class="col-12">
            <div class="form-group">
                <label for="commentaire">Commentaire</label>
                <textarea class="form-control" name="commentaire" rows="3"><?php echo e(old('commentaire', $projet->commentaire ?? '')); ?></textarea>
            </div>
        </div>
    </div>


    <div class="row">
        <div class="col-12">
            <div class="form-group text-right">
                <a href="<?php echo e(route('projets.index')); ?>" class="btn btn-outline-warning">Annuler</a>
                <?php if($editable): ?>
                    <input type="submit" class="btn btn-outline-success" value="Enregistrer">
                <?php endif; ?>
            </div>
        </div>
    </div>
</form>

<?php if(isset($projet)): ?>
<script>
    $(document).ready(function(){
        calcul_achats_investissements();

        $("#duree_mois, #date_notification, #montant_reference, #pourcentage_gains_cible, #montant_notifie, #moyenne_offres, #montant_realise").on("change", function() {
            calcul_achats_investissements();
        });
    });

    function calcul_achats_investissements(){
        var duree_amortissement = $("#duree_mois").val()
		var montant_estime = parseInt($("#montant_reference").val().replace(/ /g, ''));
        var gains_cible = $("#pourcentage_gains_cible").val();

        // S'il manque le montant estimé ou le gain cible, on annule
        if (montant_estime == "" || gains_cible == "" || duree_amortissement == "")
            return;

        // Calcul des dates
        var nbAnnees = parseInt(duree_amortissement) / 12;
        var date_fin = new Date("<?php echo e($projet->date_fin); ?>");
        var date_fin_comptabilisation = new Date("<?php echo e($date_fin_comptabilisation); ?>");
        var nbJoursAnneeN = Math.floor((date_fin_comptabilisation.getTime() - date_fin.getTime())/ (1000 * 3600 * 24));


        // Ventilation des gains cibles
        var ventilation_gains_cibles = ((montant_estime / nbAnnees) * gains_cible) / 100;
        // Si la fiche est un report, les gains sont ceux reportés pour l'année N et 0 pour l'année N+1
        if (<?php echo e($projet->report); ?>) {
            var gains_cible_N = <?php echo e($gains_cibles_N); ?>;
            var gains_cible_N1 = 0;
        } else {
            var gains_cible_N = Math.ceil(( ventilation_gains_cibles * nbJoursAnneeN) / 364);
            var gains_cible_N1 = Math.ceil( ventilation_gains_cibles * (( 364 - nbJoursAnneeN) / 364));
        }
        $("#gains_cible_N").val(gains_cible_N);
        createOrUpdateAutoNumeric($("#gains_cible_N"));
        $("#gains_cible_N1").val(gains_cible_N1);
        createOrUpdateAutoNumeric($("#gains_cible_N1"));


        var date_notification = $('#date_notification').val();
        var montant_notifie = parseInt($("#montant_notifie").val().replace(/ /g, ''));
        var moyenne_offres = parseInt($("#moyenne_offres").val().replace(/ /g, ''));

        // S'il manque la date de notification ou le montant notifié ou la moyenne des offres, on annule
        if(date_notification != "" && montant_notifie != "" && moyenne_offres != "" ) {
            // Gains notifiés
            var gains_notifies = (parseInt(moyenne_offres) - parseInt(montant_notifie)) / nbAnnees;
            var pourcentage_gains_notifies = (((parseInt(moyenne_offres) - parseInt(montant_notifie)) / parseInt(moyenne_offres)) * 100).toFixed(2);
            $("#pourcentage_gains_notifie").val(pourcentage_gains_notifies);


            // Ventilation des gains notifiés
            var date_notification_from = date_notification.split("/")
            var date_notification_object = new Date(date_notification_from[2], date_notification_from[1] - 1, date_notification_from[0])
            var nb_jours_annee_N_date_notification = Math.floor((date_fin_comptabilisation.getTime() - date_notification_object.getTime())/ (1000 * 3600 * 24));
            // Si la fiche est un report, les gains sont ceux reportés pour l'année N et 0 pour l'année N+1
            if (<?php echo e($projet->report); ?>) {
                var gains_notifie_N = <?php echo e($gains_notifies_N); ?>;
                var gains_notifie_N1 = 0;
            } else {
                var gains_notifie_N = Math.ceil(( gains_notifies * nb_jours_annee_N_date_notification) / 364);
                var gains_notifie_N1 = Math.ceil( gains_notifies * (( 364 - nb_jours_annee_N_date_notification) / 364));
            }
            $("#gains_notifie_N").val(gains_notifie_N);
            createOrUpdateAutoNumeric($("#gains_notifie_N"));
            $("#gains_notifie_N1").val(gains_notifie_N1);
            createOrUpdateAutoNumeric($("#gains_notifie_N1"));

            // Gains budgétaires (pour le tableau de synthèse)
            var impact_budgetaire = <?php echo e($projet->impact_budgetaire); ?>;
            var gains_budgetaires = (montant_estime - gains_notifies) * impact_budgetaire / 100;
            // var gains_budgetaire_N = Math.round(( gains_notifie_N * impact_budgetaire) / 100);
            // var gains_budgetaire_N1 = Math.round(( gains_notifie_N1 * impact_budgetaire) / 100);
            var gains_budgetaire_N = Math.ceil(( gains_budgetaires * nb_jours_annee_N_date_notification) / 364);
            var gains_budgetaire_N1 = Math.ceil( gains_budgetaires * (( 364 - nb_jours_annee_N_date_notification) / 364));
        }

        var montant_realise = parseInt($("#montant_realise").val().replace(/ /g, ''));

        if(montant_realise != "") {
            // Gains sécurisés
            var gains_securise = Math.round(montant_estime - montant_realise);
            $("#gains_securise").val(gains_securise);
            createOrUpdateAutoNumeric($("#gains_securise"));

            // % de gains sécurisés
            var pourcentage_gains_securise = (((montant_estime - montant_realise) / montant_estime) * 100).toFixed(2);
            $("#pourcentage_gains_securise").val(pourcentage_gains_securise);


            // Ventilation des gains sécurisés
            var date_notification_from = date_notification.split("/")
            var date_notification_object = new Date(date_notification_from[2], date_notification_from[1] - 1, date_notification_from[0])
            var nb_jours_annee_N_date_notification = Math.floor((date_fin_comptabilisation.getTime() - date_notification_object.getTime())/ (1000 * 3600 * 24));
            // Si la fiche est un report, les gains sont ceux reportés pour l'année N et 0 pour l'année N+1
            if (<?php echo e($projet->report); ?>) {
                var gains_realise_N = <?php echo e($gains_realises_N); ?>;
                var gains_realise_N1 = 0;
            } else {
                var gains_realise_N = Math.ceil(( gains_securise * nb_jours_annee_N_date_notification) / 364);
                var gains_realise_N1 = Math.ceil(gains_securise * (( 364 - nb_jours_annee_N_date_notification) / 364));
            }
            $("#gains_realise_N").val(gains_realise_N);
            createOrUpdateAutoNumeric($("#gains_realise_N"));
            $("#gains_realise_N1").val(gains_realise_N1);
            createOrUpdateAutoNumeric($("#gains_realise_N1"));
        }


        // Tableau de synthèse
        if (gains_cible != "") {
            $("#synthese_gains_cible_N").html(gains_cible_N);
            createOrUpdateAutoNumeric($("#synthese_gains_cible_N"));
            $("#synthese_gains_cible_N1").html(gains_cible_N1);
            createOrUpdateAutoNumeric($("#synthese_gains_cible_N1"));
            $("#synthese_gains_cible_total").html((gains_cible_N + gains_cible_N1));
            createOrUpdateAutoNumeric($("#synthese_gains_cible_total"));
        }

        if(montant_notifie != "") {
            $("#synthese_gains_notifie_N").html(gains_notifie_N);
            createOrUpdateAutoNumeric($("#synthese_gains_notifie_N"));
            $("#synthese_gains_notifie_N1").html(gains_notifie_N1);
            createOrUpdateAutoNumeric($("#synthese_gains_notifie_N1"));
            $("#synthese_gains_notifie_total").html((gains_notifie_N + gains_notifie_N1));
            createOrUpdateAutoNumeric($("#synthese_gains_notifie_total"));

            $("#synthese_gains_budgetaire_N").html(gains_budgetaire_N);
            createOrUpdateAutoNumeric($("#synthese_gains_budgetaire_N"));
            $("#synthese_gains_budgetaire_N1").html(gains_budgetaire_N1);
            createOrUpdateAutoNumeric($("#synthese_gains_budgetaire_N1"));
            $("#synthese_gains_budgetaire_total").html((gains_budgetaire_N + gains_budgetaire_N1));
            createOrUpdateAutoNumeric($("#synthese_gains_budgetaire_total"));
        }

        if(montant_realise != "") {
            $("#synthese_gains_realise_N").html(gains_realise_N);
            createOrUpdateAutoNumeric($("#synthese_gains_realise_N"));
            $("#synthese_gains_realise_N1").html(gains_realise_N1);
            createOrUpdateAutoNumeric($("#synthese_gains_realise_N1"));
            $("#synthese_gains_realise_total").html((gains_realise_N + gains_realise_N1));
            createOrUpdateAutoNumeric($("#synthese_gains_realise_total"));
        }
	}

    function createOrUpdateAutoNumeric(DOMElement)
    {
        if(DOMElement.length) {
            if (AutoNumeric.getAutoNumericElement(DOMElement[0]) === null) {
                new AutoNumeric(DOMElement[0], autonumericConfig);
            }
            else {
                // Permet de gérer les élements de synthèse qui sont des span et non des inputs
                DOMElement.val() === '' ?
                    AutoNumeric.getAutoNumericElement(DOMElement[0]).set(DOMElement.html())
                    : AutoNumeric.getAutoNumericElement(DOMElement[0]).set(DOMElement.val());
            }
        }
    }
</script>
<?php endif; ?>
<?php /**PATH /var/www/html/resources/views/projets/form/onglets/achats/achats-investissements.blade.php ENDPATH**/ ?>