AP44/include/newFiche.js

139 lines
3.9 KiB
JavaScript

$(document).ready(function () {
calcPrixTotalFrsF();
calcPrixTotalFrsHorsF();
updatePrixTotal();
/**
* Partie enregistrement frais F
*/
$('.frsFrt').on('change', function (e) {
console.log($(this).val())
val = $(this).val();
val = val.replace(',', '.')
if ($.isNumeric(val)) {
/**
* Calcul le prix de la ligne
*/
id = $(this).attr('id')
formTotal = $('#totalFrs-' + id)
mttFrs = $('#mttFrs-' + id).attr('data-price')
formTotal.html((val * mttFrs).toFixed(2) + ' €');
calcPrixTotalFrsF();
updatePrixTotal();
}
})
/**
* Enregistrement frais HF
*/
$('.validFraisHF').on('click', function () {
let date = $('#dateHf')
let libelle = $('#libelleHf')
let montant = $('#mttHf')
let canAdd = true;
if (date.val() == "") {
canAdd = false;
date.css("border-color", "red")
} else {
date.css("border-color", "var(--bs-border-color)")
}
if (libelle.val() == "") {
canAdd = false;
libelle.css("border-color", "red")
} else {
libelle.css("border-color", "var(--bs-border-color)")
}
if (montant.val() == "") {
canAdd = false;
montant.css("border-color", "red")
} else {
montant.css("border-color", "var(--bs-border-color)")
}
if (canAdd == true) {
var line = $('tr.fraisHF:first').clone();
lastId = $('tr.fraisHF').length
line.find('.btn').attr('id', 'frsSup-' + lastId)
var line = $('<tr id="fraisHf-' + lastId + '" class="fraisHF"></tr>');
var tdDate = $('<th scope="row" id="dateFrsHF"></th>');
tdDate.html(date.val());
var tdLibelle = $('<td id="LibelleFrsHF"></td>');
tdLibelle.html(libelle.val());
var tdMtt = $('<td id="MttFrsHF"></td>');
tdMtt.html(parseFloat(montant.val().replace(',', '.')).toFixed(2) + ' €');
var tdJust = $('<td></td>');
var btn = $('<td><button type="button" class="btn btn-outline-primary btnSuprFraisHf" id="frsSup-' + lastId + '">Supprimer</button></td>')
$(line).append(tdDate)
$(line).append(tdLibelle)
$(line).append(tdMtt)
$(line).append(tdJust)
$(line).append(btn)
line.insertBefore('.newFraisForm')
date.val('')
libelle.val('')
montant.val('')
}
calcPrixTotalFrsHorsF();
updatePrixTotal();
})
})
/**
* Supprimer fraisHf
*/
$(document).on('click', '.btnSuprFraisHf', function () {
id = $(this).attr('id').split('-')[1]
console.log(id)
$('#fraisHf-' + id).remove()
calcPrixTotalFrsHorsF();
updatePrixTotal();
})
/**
* Calcul prix total frais forfaitaires
*/
function calcPrixTotalFrsF() {
var prixTotal = 0;
$('td[id^="totalFrs-"]').each(function () {
prixTotal += parseFloat($(this).html().replace('€', ''))
})
$('.prixTotalFrsF').html('<strong>TOTAL :</strong> ' + prixTotal.toFixed(2) + ' €')
$('.prixTotalFrsF').attr('data-prix', prixTotal.toFixed(2))
}
/**
* Calcul prix total frais hors forfait
*/
function calcPrixTotalFrsHorsF() {
var prixTotal = 0;
$('td#MttFrsHF').each(function () {
prixTotal += parseFloat($(this).html().replace('€', ''))
})
$('#total-frais-HF').html('<strong>TOTAL :</strong> ' + prixTotal.toFixed(2) + ' €')
$('#total-frais-HF').attr('data-prix', prixTotal.toFixed(2))
}
/**
* Calcul prix total de la fiche
*/
function updatePrixTotal() {
var total = parseFloat($('#total-frais-HF').attr('data-prix'))
total += parseFloat($('.prixTotalFrsF').attr('data-prix'))
$('#total-fiche').html('<strong>TOTAL :</strong> ' + total.toFixed(2) + ' €')
}