mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
379 lines
19 KiB
JavaScript
379 lines
19 KiB
JavaScript
import { toastPresupuestoSummary } from "../../../components/alerts/sweetAlert.js";
|
|
|
|
class Resumen {
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
init() {
|
|
|
|
const self = this;
|
|
this.toastPresupuestoTotal = null
|
|
$(".update-totales").on("change", function () {
|
|
self.updateTotales(true, true, true)
|
|
});
|
|
|
|
$(document).on('update-totales', async function () {
|
|
await self.updateTotales();
|
|
$(document).trigger('update-totales-completed');
|
|
});
|
|
$("#totalDespuesDecuento").on('change',this.updateToastSummary.bind(this))
|
|
}
|
|
|
|
cargar(resumen) {
|
|
|
|
// Mapear los valores a los elementos HTML por ID
|
|
$("#totalCostePapel").text(resumen.total_coste_papel + "€" || "0€");
|
|
$("#porcentajeMargenPapel").text(resumen.total_margenPercent_papel ? resumen.total_margenPercent_papel + "%" : "0%");
|
|
$("#margenPapel").text(resumen.total_margen_papel + "€" || "0€");
|
|
|
|
$("#totalCosteImpresion").text(resumen.total_coste_impresion + "€" || "0€");
|
|
$("#porcentajeMargenImpresion").text(resumen.total_margenPercent_impresion ? resumen.total_margenPercent_impresion + "%" : "0%");
|
|
$("#margenImpresion").text(resumen.total_margen_impresion + "€" || "0€");
|
|
|
|
$("#totalServicios").text(resumen.total_coste_servicios + "€" || "0€");
|
|
$("#porcentajeMargenServicios").text(resumen.total_margenPercent_servicios ? resumen.total_margenPercent_servicios + "%" : "0%");
|
|
$("#margenServicios").text(resumen.total_margen_servicios + "€" || "0€");
|
|
|
|
//$("#costeEnvios").text(resumen.total_coste_envios + "€" || "0€");
|
|
$("#totalEnvios").text(resumen.total_coste_envios + "€" || "0€");
|
|
$("#margenEnvios").text(resumen.total_margen_envios + "€" || "0€");
|
|
|
|
$("#totalCostes").text(resumen.total_costes + "€" || "0€");
|
|
$("#porcentajeMargen").text(resumen.porcentajeMargen ? resumen.porcentajeMargen + "%" : "0%");
|
|
$("#totalMargenes").text(resumen.total_margenes + "€" || "0€");
|
|
|
|
$("#totalAntesDescuento").text(resumen.total_antes_descuento + "€" || "0€");
|
|
$("#total_descuentoPercent").val(resumen.total_descuentoPercent || "0");
|
|
$("#descuentoTotal").text(resumen.total_descuento + "€" || "0€");
|
|
$("#totalDespuesDecuento").text(resumen.total_presupuesto + "€" || "0€").trigger("change")
|
|
$("#precioUnidadPresupuesto").text(resumen.total_precio_unidad + "€" || "0€");
|
|
$("#factor").text(resumen.total_factor || "0");
|
|
$("#factor_ponderado").text(resumen.total_factor_ponderado || "0");
|
|
|
|
if (resumen.total_aceptado !== undefined) {
|
|
$("#totalAceptado").val(resumen.total_aceptado + "€" || "0€");
|
|
}
|
|
|
|
}
|
|
|
|
update() {
|
|
|
|
|
|
}
|
|
|
|
// Formatear agregando separadores de miles
|
|
addSeparatorsNF(nStr, inD, outD, sep) {
|
|
nStr += '';
|
|
let dpos = nStr.indexOf(inD);
|
|
let nStrEnd = '';
|
|
if (dpos != -1) {
|
|
nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
|
|
nStr = nStr.substring(0, dpos);
|
|
}
|
|
let rgx = /(\d+)(\d{3})/;
|
|
while (rgx.test(nStr)) {
|
|
nStr = nStr.replace(rgx, '$1' + sep + '$2');
|
|
}
|
|
return nStr + nStrEnd;
|
|
}
|
|
|
|
|
|
async updateTotales(event, data = {}) {
|
|
|
|
const self = this;
|
|
|
|
let updateLP = data.updateLP || true;
|
|
let updateServicios = data.updateServicios || true;
|
|
let updateEnvio = data.updateEnvio || true;
|
|
let totalPapel = 0;
|
|
let margenPapel = 0;
|
|
|
|
let totalImpresion = 0;
|
|
let totalImpresionforMargen = 0;
|
|
let margenImpresion = 0;
|
|
|
|
let totalServicios = parseFloat(0);
|
|
let margenServicios = parseFloat(0);
|
|
|
|
let totalEnvios = 0;
|
|
let margenEnvios = 0;
|
|
|
|
let sumForFactor = 0.0;
|
|
let sumForFactorPonderado = 0.0;
|
|
|
|
|
|
let margenPorHoras = 0.0;
|
|
|
|
if (updateLP) {
|
|
|
|
if (typeof $("#tableLineasPresupuesto").DataTable() !== 'undefined') {
|
|
$("#tableLineasPresupuesto").DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
let rowData = this.data();
|
|
|
|
if (rowData.check_papel_total) {
|
|
totalPapel += parseFloat($('#' + rowData.row_id + '_totalPapelPedido').val())
|
|
margenPapel += parseFloat($('#' + rowData.row_id + '_margenPapelPedido').val())
|
|
|
|
sumForFactor += parseFloat($('#' + rowData.row_id + '_totalPapelPedido').val())
|
|
sumForFactor -= parseFloat($('#' + rowData.row_id + '_margenPapelPedido').val())
|
|
|
|
}
|
|
if (rowData.check_impresion_total) {
|
|
|
|
totalImpresion += parseFloat($('#' + rowData.row_id + '_precioImpresion').val())
|
|
margenPorHoras += parseFloat($('#' + rowData.row_id + '_margenImpresion').val())
|
|
totalImpresion += parseFloat($('#' + rowData.row_id + '_totalClicks').val())
|
|
margenImpresion += parseFloat($('#' + rowData.row_id + '_margenClicks').val())
|
|
|
|
sumForFactor += parseFloat($('#' + rowData.row_id + '_totalClicks').val())
|
|
|
|
if (rowData.maquinaTipo == 'inkjet') {
|
|
totalImpresion += parseFloat(rowData.totalTinta)
|
|
totalImpresion += parseFloat(rowData.totalCorte)?? 0;
|
|
sumForFactor += (parseFloat(rowData.totalTinta) + parseFloat(rowData.totalCorte))
|
|
}
|
|
|
|
sumForFactor -= parseFloat($('#' + rowData.row_id + '_margenClicks').val())
|
|
|
|
if (!isNaN(parseFloat($('#' + rowData.row_id + '_totalTinta').val()))) {
|
|
totalImpresion += parseFloat($('#' + rowData.row_id + '_totalTinta').val())
|
|
sumForFactor += parseFloat($('#' + rowData.row_id + '_totalTinta').val())
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
totalImpresion -= parseFloat(margenImpresion);
|
|
totalPapel -= parseFloat(margenPapel);
|
|
|
|
margenImpresion += parseFloat(margenPorHoras);
|
|
|
|
let porcentajeMargenPapel = isNaN(margenPapel / (totalPapel) * 100.0) ? 0 : margenPapel / (totalPapel) * 100.0
|
|
$('#porcentajeMargenPapel').text(porcentajeMargenPapel.toFixed(0) + '%')
|
|
$('#totalCostePapel').text((this.addSeparatorsNF(totalPapel.toFixed(2), ".", ",", ".")) + "€")
|
|
$('#margenPapel').text((this.addSeparatorsNF(margenPapel.toFixed(2), ".", ",", ".")) + "€")
|
|
$('#totalCostePapel').attr('val', totalPapel.toFixed(2))
|
|
$('#margenPapel').attr('val', margenPapel.toFixed(2))
|
|
|
|
let porcentajeMargenImpresion = isNaN(margenImpresion / (totalImpresion) * 100.0) ? 0 : margenImpresion / (totalImpresion) * 100.0
|
|
$('#porcentajeMargenImpresion').text(porcentajeMargenImpresion.toFixed(0) + '%')
|
|
$('#totalCosteImpresion').text((this.addSeparatorsNF(totalImpresion.toFixed(2), ".", ",", ".")) + "€")
|
|
$('#margenImpresion').text((this.addSeparatorsNF(margenImpresion.toFixed(2), ".", ",", ".")) + "€")
|
|
$('#totalCosteImpresion').attr('val', totalImpresion.toFixed(2))
|
|
$('#margenImpresion').attr('val', margenImpresion.toFixed(2))
|
|
|
|
}
|
|
else {
|
|
totalPapel = parseFloat($('#totalCostePapel').attr('val'))
|
|
margenPapel = parseFloat($('#margenPapel').attr('val'))
|
|
totalImpresion = parseFloat($('#totalCosteImpresion').attr('val'))
|
|
margenImpresion = parseFloat($('#margenImpresion').attr('val'))
|
|
}
|
|
|
|
sumForFactorPonderado = sumForFactor;
|
|
|
|
if (updateServicios) {
|
|
|
|
if (typeof $("#tableOfServiciosEncuadernacion").DataTable() !== 'undefined' && $("#tableOfServiciosEncuadernacion").DataTable().rows().count() > 0) {
|
|
$('#tableOfServiciosEncuadernacion').DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
let rowData = this.data();
|
|
let total_servicio = self.roundToTwoDecimals(parseFloat(rowData.precio_total));
|
|
let margen_servicio = parseFloat(rowData.margen);
|
|
totalServicios += total_servicio
|
|
let base = self.roundToTwoDecimals(total_servicio / (1 + margen_servicio / 100.0));
|
|
base = self.roundToTwoDecimals(base / parseInt($('#tirada').val()))*parseInt($('#tirada').val());
|
|
margenServicios += self.roundToTwoDecimals(parseFloat(total_servicio - base));
|
|
});
|
|
}
|
|
|
|
if (typeof $("#tableOfServiciosAcabado").DataTable() !== 'undefined' && $("#tableOfServiciosAcabado").DataTable().rows().count() > 0) {
|
|
$('#tableOfServiciosAcabado').DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
let rowData = this.data();
|
|
let total_servicio = self.roundToTwoDecimals(parseFloat(rowData.precio_total));
|
|
let margen_servicio = parseFloat(rowData.margen);
|
|
totalServicios += total_servicio
|
|
let base = self.roundToTwoDecimals(total_servicio / (1 + margen_servicio / 100.0));
|
|
base = self.roundToTwoDecimals(base / parseInt($('#tirada').val()))*parseInt($('#tirada').val());
|
|
margenServicios += self.roundToTwoDecimals(parseFloat(total_servicio - base));
|
|
});
|
|
}
|
|
|
|
if (typeof $("#tableOfServiciosPreimpresion").DataTable() !== 'undefined' && $("#tableOfServiciosPreimpresion").DataTable().rows().count() > 0) {
|
|
$('#tableOfServiciosPreimpresion').DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
let rowData = this.data();
|
|
let total_servicio = self.roundToTwoDecimals(parseFloat(rowData.precio));
|
|
let coste_servicio = self.roundToTwoDecimals(parseFloat(rowData.coste));
|
|
totalServicios += total_servicio
|
|
margenServicios += total_servicio - coste_servicio;
|
|
});
|
|
}
|
|
|
|
if (typeof $("#tableOfServiciosExtra").DataTable() !== 'undefined' && $("#tableOfServiciosExtra").DataTable().rows().count() > 0) {
|
|
$('#tableOfServiciosExtra').DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
let rowData = this.data();
|
|
let total_servicio = self.roundToTwoDecimals(parseFloat(rowData.precio));
|
|
let coste_servicio = self.roundToTwoDecimals(parseFloat(rowData.coste));
|
|
totalServicios += total_servicio
|
|
margenServicios += total_servicio - coste_servicio;
|
|
});
|
|
}
|
|
|
|
if (typeof $("#tableOfServiciosManipulado").DataTable() !== 'undefined' && $("#tableOfServiciosManipulado").DataTable().rows().count() > 0) {
|
|
$('#tableOfServiciosManipulado').DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
let rowData = this.data();
|
|
let total_servicio = self.roundToTwoDecimals(parseFloat(rowData.precio_total));
|
|
let margen_servicio = parseFloat(rowData.margen);
|
|
totalServicios += total_servicio
|
|
let base = self.roundToTwoDecimals(total_servicio / (1 + margen_servicio / 100.0));
|
|
base = self.roundToTwoDecimals(base / parseInt($('#tirada').val()))*parseInt($('#tirada').val());
|
|
margenServicios += self.roundToTwoDecimals(parseFloat(total_servicio - base));
|
|
});
|
|
}
|
|
|
|
totalServicios -= margenServicios;
|
|
|
|
sumForFactorPonderado += totalServicios;
|
|
|
|
let porcentajeMargenServicios = margenServicios / (totalServicios) * 100
|
|
$('#porcentajeMargenServicios').text(isNaN(porcentajeMargenServicios.toFixed(0)) ? 0 : porcentajeMargenServicios.toFixed(0) + '%')
|
|
$('#totalServicios').text((this.addSeparatorsNF(totalServicios.toFixed(2), ".", ",", ".")) + "€")
|
|
$('#margenServicios').text((this.addSeparatorsNF(margenServicios.toFixed(2), ".", ",", ".")) + "€")
|
|
$('#totalServicios').attr('val', totalServicios.toFixed(2) + '€')
|
|
$('#margenServicios').attr('val', margenServicios.toFixed(2) + '€')
|
|
|
|
}
|
|
else {
|
|
|
|
totalServicios = parseFloat($('#totalServicios').attr('val'))
|
|
margenServicios = parseFloat($('#margenServicios').attr('val'))
|
|
|
|
sumForFactorPonderado += totalServicios;
|
|
}
|
|
|
|
if (updateEnvio) {
|
|
|
|
totalEnvios = parseFloat($('#envio_base').val());
|
|
if (typeof $('#tableOfDireccionesEnvio').DataTable() !== 'undefined' && $('#tableOfDireccionesEnvio').DataTable().rows().count() > 0) {
|
|
$('#tableOfDireccionesEnvio').DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
if (rowIdx == 0) {
|
|
return;
|
|
}
|
|
let data = this.data()
|
|
totalEnvios += parseFloat(data.precio)
|
|
margenEnvios += parseFloat((data.precio) * data.margen / 100)
|
|
});
|
|
}
|
|
|
|
totalEnvios -= margenEnvios
|
|
$('#totalEnvios').text((this.addSeparatorsNF(totalEnvios.toFixed(2), ".", ",", ".")) + "€")
|
|
$('#margenEnvios').text((this.addSeparatorsNF(margenEnvios.toFixed(2), ".", ",", ".")) + "€")
|
|
$('#totalEnvios').attr('val', totalEnvios.toFixed(2) + '€')
|
|
$('#margenEnvios').attr('val', margenEnvios.toFixed(2) + '€')
|
|
}
|
|
else {
|
|
totalEnvios = parseFloat($('#totalEnvios').attr('val'))
|
|
margenEnvios = parseFloat($('#margenEnvios').attr('val'))
|
|
}
|
|
|
|
let totalCostes = parseFloat(totalPapel.toFixed(2)) + parseFloat(totalImpresion.toFixed(2)) + parseFloat(totalServicios.toFixed(2)) + parseFloat(totalEnvios.toFixed(2))
|
|
let totalMargenes = parseFloat(margenPapel.toFixed(2)) + parseFloat(margenImpresion.toFixed(2)) + parseFloat(margenServicios.toFixed(2)) + parseFloat(margenEnvios.toFixed(2))
|
|
let porcentajeMargen = totalCostes + totalMargenes > 0 ? (100 * totalMargenes / (totalCostes + totalMargenes)).toFixed(0) : 0
|
|
$('#totalCostes').text((this.addSeparatorsNF(totalCostes.toFixed(2), ".", ",", ".")) + "€")
|
|
$('#totalMargenes').text((this.addSeparatorsNF(totalMargenes.toFixed(2), ".", ",", ".")) + "€")
|
|
$('#totalCostes').attr('val', (totalCostes).toFixed(2) + '€')
|
|
$('#totalMargenes').attr('val', (totalMargenes).toFixed(2) + '€')
|
|
$('#porcentajeMargen').text(porcentajeMargen + '%')
|
|
$('#porcentajeMargen').attr('val', porcentajeMargen)
|
|
|
|
if ($('#total_descuentoPercent').val() < 0) {
|
|
$('#total_descuentoPercent').val(0)
|
|
}
|
|
let totalAntesDescuento = totalCostes + totalMargenes
|
|
let totalDescuento = totalAntesDescuento * parseInt($('#total_descuentoPercent').val() || 0) / 100
|
|
let totalPresupuesto = totalAntesDescuento - totalDescuento
|
|
let precioUnidad = totalPresupuesto / parseInt($('#tirada').val())
|
|
|
|
$('#totalAntesDescuento').text((this.addSeparatorsNF(totalAntesDescuento.toFixed(2), ".", ",", ".")) + "€")
|
|
$('#descuentoTotal').text((this.addSeparatorsNF(totalDescuento.toFixed(2), ".", ",", ".")) + "€")
|
|
$('#totalDespuesDecuento').text((this.addSeparatorsNF(totalPresupuesto.toFixed(2), ".", ",", ".")) + "€")
|
|
$('#precioUnidadPresupuesto').text((this.addSeparatorsNF(precioUnidad.toFixed(4), ".", ",", ".")) + "€")
|
|
$('#totalAntesDescuento').attr('val', (totalAntesDescuento).toFixed(2))
|
|
$('#descuentoTotal').attr('val', (totalDescuento).toFixed(2))
|
|
$('#totalDespuesDecuento').attr('val', (totalPresupuesto).toFixed(2))
|
|
$('#precioUnidadPresupuesto').attr('val', (precioUnidad).toFixed(4))
|
|
|
|
|
|
$('#factor').text(this.addSeparatorsNF(((totalPresupuesto - totalEnvios - margenEnvios) / sumForFactor).toFixed(2), ".", ",", "."))
|
|
$('#factor').attr('val', ((totalPresupuesto - totalEnvios - margenEnvios) / sumForFactor).toFixed(2))
|
|
$('#factor_ponderado').text(this.addSeparatorsNF(((totalPresupuesto - totalEnvios - margenEnvios) / sumForFactorPonderado).toFixed(2), ".", ",", "."))
|
|
$('#factor_ponderado').attr('val', ((totalPresupuesto - totalEnvios - margenEnvios) / sumForFactorPonderado).toFixed(2))
|
|
|
|
}
|
|
|
|
|
|
getData(){
|
|
|
|
let data = {
|
|
'total_coste_papel': $('#totalCostePapel').text().replace('€', ''),
|
|
'total_margen_papel': $('#margenPapel').text().replace('€', ''),
|
|
'total_margenPercent_papel': $('#porcentajeMargenPapel').text().replace('%', ''),
|
|
|
|
'total_coste_impresion': $('#totalCosteImpresion').text().replace('€', ''),
|
|
'total_margen_impresion': $('#margenImpresion').text().replace('€', ''),
|
|
'total_margenPercent_impresion': $('#porcentajeMargenImpresion').text().replace('%', ''),
|
|
|
|
'total_coste_servicios': $('#totalServicios').text().replace('€', ''),
|
|
'total_margen_servicios': $('#margenServicios').text().replace('€', ''),
|
|
'total_margenPercent_servicios': $('#porcentajeMargenServicios').text().replace('%', ''),
|
|
|
|
'total_coste_envios': $('#totalEnvios').text().replace('€', ''),
|
|
'total_margen_envios': $('#margenEnvios').text().replace('€', ''),
|
|
|
|
'total_costes': $('#totalCostes').text().replace('€', ''),
|
|
'total_margenes': $('#totalMargenes').text().replace('€', ''),
|
|
|
|
'total_antes_descuento': $('#totalAntesDescuento').text().replace('€', ''),
|
|
'total_descuento': $('#descuentoTotal').text().replace('€', ''),
|
|
'total_descuentoPercent': $('#total_descuentoPercent').val().replace('€', ''),
|
|
'total_presupuesto': $('#totalDespuesDecuento').text().replace('€', ''),
|
|
'total_precio_unidad': $('#precioUnidadPresupuesto').text().replace('€', ''),
|
|
|
|
'total_factor': $('#factor').text().replace(/,/g, '.'),
|
|
'total_factor_ponderado': $('#factor_ponderado').text().replace(/,/g, '.'),
|
|
};
|
|
|
|
if($('#confirmar_presupuesto').prop('checked')){
|
|
data.confirmar = '1';
|
|
data.total_aceptado = $('#totalDespuesDecuento').text().replace('€', '');
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
roundToTwoDecimals(num) {
|
|
return parseFloat(num.toFixed(2));
|
|
}
|
|
updateToastSummary()
|
|
{
|
|
if(this.toastPresupuestoTotal){
|
|
this.toastPresupuestoTotal.close()
|
|
}
|
|
|
|
this.toastPresupuestoTotal = toastPresupuestoSummary($("#totalDespuesDecuento").text() ?? 0)
|
|
this.toastPresupuestoTotal.fire().then((result) => {
|
|
if(result.isDismissed){
|
|
this.updateToastSummary()
|
|
$('html, body').animate(
|
|
{
|
|
scrollTop: $("#totalDespuesDecuento").offset().top
|
|
},
|
|
1000)
|
|
}
|
|
})
|
|
|
|
}
|
|
}
|
|
|
|
export default Resumen; |