import ClassSelect from '../../../components/select2.js'; import { getToken } from '../../../common/common.js'; class DatosLibro { constructor(domItem, cargando, functions = {}) { this.domItem = domItem; this.checkPaginasPresupuesto = functions.checkPaginasPresupuesto; this.getDimensionLibro = functions.getDimensionLibro; this.calcularSolapas = functions.calcularSolapas; this.csrf_token = getToken(); this.csrf_hash = $('#mainContainer').find('input[name="' + this.csrf_token + '"]').val(); this.tipo_impresion_id = parseInt($("#tipo_impresion_id").val()); this.paginas = this.domItem.find('#paginas'); this.tirada = this.domItem.find('#tirada'); this.tamanio = new ClassSelect($("#papelFormatoId"), '/papel-formato/getSelect2', window.language.Presupuestos.formatoLibro); this.tamanioPersonalizado = this.domItem.find('#papelFormatoPersonalizado'); this.anchoPersonalizado = this.domItem.find('#papelFormatoAncho'); this.altoPersonalizado = this.domItem.find('#papelFormatoAlto'); this.merma = this.domItem.find('#merma'); this.mermaCubierta = this.domItem.find('#mermacubierta'); this.solapasCubierta = this.domItem.find('#solapas'); this.solapasSobrecubierta = this.domItem.find('#solapas_sobrecubierta'); this.anchoSolapasCubierta = this.domItem.find('#solapas_ancho'); this.anchoSolapasSobrecubierta = this.domItem.find('#solapas_ancho_sobrecubierta'); this.divSolapasCubierta = this.domItem.find('#div_solapas_ancho'); this.divSolapasSobrecubierta = this.domItem.find('#div_solapas_ancho_sobrecubierta'); this.acabadoCubierta = new ClassSelect($("#acabado_cubierta_id"), '/serviciosacabados/getacabados', '', false, { [this.csrf_token]: this.csrf_hash, "cubierta": 1 } ); this.acabadoSobrecubierta = new ClassSelect($("#acabado_sobrecubierta_id"), '/serviciosacabados/getacabados', '', false, { [this.csrf_token]: this.csrf_hash, "sobrecubierta": 1 } ); this.acabadoFaja = new ClassSelect($("#acabado_faja_id"), '/serviciosacabados/getacabados', '', false, { [this.csrf_token]: this.csrf_hash, "sobrecubierta": 1 } ); this.retractilado = this.domItem.find('#retractilado'); this.retractilado5 = this.domItem.find('#retractilado5'); this.prototipo = this.domItem.find('#prototipo'); this.ferro = this.domItem.find('#ferro'); this.ferroDigital = this.domItem.find('#ferroDigital'); this.marcapaginas = this.domItem.find('#marcapaginas'); this.faja = this.domItem.find('#faja'); this.fajaAlto = this.domItem.find('#faja_alto'); this.fajaSolapasAncho = this.domItem.find('#faja_solapas_ancho'); this.div_faja = this.domItem.find('.faja-div'); this.cargando = cargando; this._bloqueoCambioFaja = false; } init() { const self = this; this.actualizarLimitesPaginas(); this.tamanio.init(); this.acabadoCubierta.init(); this.acabadoSobrecubierta.init(); this.acabadoFaja.init(); this.acabadoCubierta.item.on('select2:select', function () { if (this.cargando) { return; } if (self.acabadoCubierta.getVal() == 0) { $(document).trigger('remove-servicio-lineas', 'acabadoCubierta'); } else { const table = $('#tableLineasPresupuesto').DataTable(); const rows = table.rows().data(); for (let i = 0; i < rows.length; i++) { const rowData = rows[i]; if (rowData.row_id === 'lp_cubierta') { $(document).trigger('add-servicio-lineas', 'acabadoCubierta'); break; } } } }); this.acabadoSobrecubierta.item.on('select2:select', function () { if (this.cargando) { return; } if (self.acabadoCubierta.getVal() == 0) { $(document).trigger('remove-servicio-lineas', 'acabadoSobrecubierta'); } else { const table = $('#tableLineasPresupuesto').DataTable(); const rows = table.rows().data(); for (let i = 0; i < rows.length; i++) { const rowData = rows[i]; if (rowData.row_id === 'lp_sobrecubierta') { $(document).trigger('add-servicio-lineas', 'acabadoSobrecubierta'); break; } } } }); this.acabadoFaja.item.on('select2:select', function () { if (this.cargando) return; if (self.acabadoFaja.getVal() == 0) { $(document).trigger('remove-servicio-lineas', 'acabadoFaja'); } else { const table = $('#tableLineasPresupuesto').DataTable(); const rows = table.rows().data(); for (let i = 0; i < rows.length; i++) { const rowData = rows[i]; if (rowData.row_id === 'lp_faja') { $(document).trigger('add-servicio-lineas', 'acabadoFaja'); break; } } } }); if (window.location.href.includes("edit")) { this.retractilado.on('change', this.checkRetractilado.bind(this)); this.retractilado5.on('change', this.checkRetractilado.bind(this)); this.ferro.on('change', this.changeFerro.bind(this)); this.prototipo.on('change', this.changePrototipo.bind(this)); this.ferroDigital.on('change', this.changeFerroDigital.bind(this)); this.tamanio.item.on('select2:select', this.changeFormato.bind(this)); $('.formato_libro').on('change', this.changeFormato.bind(this)); if ($('#tipo_impresion_id').val() != 1 && $('#tipo_impresion_id').val() != 3 && $('#tipo_impresion_id').val() != 5 && $('#tipo_impresion_id').val() != 7) { this.solapasCubierta.on('change', this.changeSolapasCubierta.bind(this)); } this.solapasSobrecubierta.on('change', this.changeSolapasSobrecubierta.bind(this)); this.tamanioPersonalizado.on('change', this.changeTipoTamanio.bind(this)); this.anchoSolapasCubierta.on('change', this.changeAnchoSolapasCubierta.bind(this)); this.anchoSolapasSobrecubierta.on('change', this.changeAnchoSolapasSobrecubierta.bind(this)); this.fajaSolapasAncho.on('change', this.changeAnchoSolapasFaja.bind(this)); this.paginas.on('change', this.changePaginas.bind(this)); this.tirada.on('change', this.changeTirada.bind(this)); this.anchoSolapasCubierta.on('focusout', this.checkMinMaxInput.bind(this)); this.anchoSolapasSobrecubierta.on('focusout', this.checkMinMaxInput.bind(this)); this.fajaSolapasAncho.on('focusout', this.checkMinMaxInput.bind(this)); this.fajaAlto.on('focusout', this.checkMinMaxInput.bind(this)); this.faja.on('change', this.changeFaja.bind(this)); } } changeFaja() { if (this._bloqueoCambioFaja) return; this._bloqueoCambioFaja = true; if (this.faja.prop('checked')) { this.div_faja.removeClass('d-none'); $('#compFaja').val(1).trigger('change'); if (this.cargando) return; $(document).trigger('add-servicio-lineas', 'solapas_faja'); $('.acabado-faja').removeClass('d-none'); } else { this.div_faja.addClass('d-none'); $('#compFaja').val(0).trigger('change'); $('.acabado-faja').addClass('d-none'); if (this.cargando) return; $(document).trigger('remove-servicio-lineas', 'acabadoFaja'); $(document).trigger('remove-servicio-lineas', 'solapas_faja'); $(document).trigger('remove-servicio-lineas', 'solapas_grandes_faja'); } this._bloqueoCambioFaja = false; } checkMinMaxInput(event) { if (this.cargando) return; const el = event.target; if (el.value != "") { if (parseInt(el.value) < parseInt(el.min)) { el.value = el.min; } if (parseInt(el.value) > parseInt(el.max)) { el.value = el.max; } } else { el.value = el.min; } } changeFerro() { if (this.cargando) return; if (this.ferro.prop('checked')) { $(document).trigger('add-servicio-lineas', 'ferro'); $(document).trigger('update-presupuesto', { update_lineas: false, update_servicios: false, update_envios: true, update_resumen: false, update_tiradas_alternativas: false }); } else { $(document).trigger('remove-servicio-lineas', 'ferro'); if (!this.prototipo.prop('checked')) { const table = $('#tableOfDireccionesEnvio').DataTable(); const rows = table.rows().data(); for (let i = 0; i < rows.length; i++) { const rowData = rows[i]; if (rowData.is_ferro_prototipo == 1) { table.rows(i).remove(); table.draw(); break; } } $(document).trigger('update-presupuesto', { update_lineas: false, update_servicios: false, update_envios: true, update_resumen: true, update_tiradas_alternativas: true }); } } } changeFerroDigital() { if (this.cargando) return; if (this.ferro.prop('checked')) { $(document).trigger('add-servicio-lineas', 'ferroDigital'); } else { $(document).trigger('remove-servicio-lineas', 'ferroDigital'); } } changePrototipo() { if (this.cargando) return; if (this.prototipo.prop('checked')) { $(document).trigger('add-servicio-lineas', 'prototipo'); $(document).trigger('update-presupuesto', { update_lineas: false, update_servicios: false, update_envios: true, update_resumen: false, update_tiradas_alternativas: false }); } else { $(document).trigger('remove-servicio-lineas', 'prototipo'); if (!this.ferro.prop('checked')) { const table = $('#tableOfDireccionesEnvio').DataTable(); const rows = table.rows().data(); for (let i = 0; i < rows.length; i++) { const rowData = rows[i]; if (rowData.is_ferro_prototipo == 1) { table.rows(i).remove(); table.draw(); break; } } $(document).trigger('update-presupuesto', { update_lineas: false, update_servicios: false, update_envios: true, update_resumen: true, update_tiradas_alternativas: true }); } } } checkRetractilado(event) { if (this.cargando) return; switch (event.currentTarget.id) { case 'retractilado': if ($('#' + event.currentTarget.id).prop('checked')) { $('#retractilado5').prop('checked', false); $(document).trigger('remove-servicio-lineas', 'retractilado5'); $(document).trigger('add-servicio-lineas', 'retractilado'); } else $(document).trigger('remove-servicio-lineas', 'retractilado'); break; case 'retractilado5': if ($('#' + event.currentTarget.id).prop('checked')) { $('#retractilado').prop('checked', false); $(document).trigger('remove-servicio-lineas', 'retractilado'); $(document).trigger('add-servicio-lineas', 'retractilado5'); } else $(document).trigger('remove-servicio-lineas', 'retractilado5'); break; default: break; } } changeFormato() { // Si es negro o color if ($('#tipoImpresion').select2('data')[0].id == 'negro' || $('#tipoImpresion').select2('data')[0].id == 'color') { $('#compPaginasNegro').trigger('change') } // Si es negrohq o colorhq if ($('#tipoImpresion').select2('data')[0].id == 'negrohq' || $('#tipoImpresion').select2('data')[0].id == 'colorhq') { $('#compPaginasNegrohq').trigger('change'); } $('.solapas_cubierta').trigger('change'); $('.solapas_sobrecubierta').trigger('change'); this.checkPaginasPresupuesto(); this.calcularSolapas(); if ($('#faja_alto').length > 0 && $('#faja_alto').attr('max') != this.getDimensionLibro().alto) { $('#faja_alto').attr('max', this.getDimensionLibro().alto); $('#faja_alto').trigger('change'); $('#faja_alto').closest('.mb-1').find('.form-text').text('Entre 50 y ' + this.getDimensionLibro().alto + ' mm'); } $(document).trigger('update-presupuesto', { update_lineas: true, update_servicios: true, update_envios: true, update_resumen: true, update_tiradas_alternativas: true }); } changeTipoTamanio() { if (this.tamanioPersonalizado.prop('checked')) { $('.tamanio-personalizado').removeClass('d-none'); $('.tamanio-estandar').addClass('d-none'); this.tamanio.setVal(''); $("#label_papelFormatoId").text( window.language.Presupuestos.papelFormatoId + " (" + window.language.Presupuestos.papelFormatoAncho + " x " + window.language.Presupuestos.papelFormatoAncho + ")*"); } else { this.anchoPersonalizado.val(""); this.altoPersonalizado.val(""); $('.tamanio-personalizado').addClass('d-none'); $('.tamanio-estandar').removeClass('d-none'); $("#label_papelFormatoId").text( window.language.Presupuestos.papelFormatoId + '*'); } $(document).trigger('update-presupuesto', { update_lineas: true, update_servicios: false, update_envios: false, update_resumen: false, update_tiradas_alternativas: false }); } changeSolapasCubierta() { setTimeout(() => { if (this.solapasCubierta.prop('checked')) { $('#div_solapas_ancho').removeClass('d-none'); if (this.cargando) return; $(document).trigger('add-servicio-lineas', 'solapas_cubierta'); } else { $('#div_solapas_ancho').addClass('d-none'); if (this.cargando) return; $(document).trigger('remove-servicio-lineas', 'solapas_cubierta'); $(document).trigger('remove-servicio-lineas', 'solapas_grandes_cubierta'); } }, 100); // para que se actualice el comparador $('#compCarasCubierta').trigger('change'); } changeAnchoSolapasCubierta() { // para que se actualice el comparador $('#compCarasCubierta').trigger('change'); if (this.cargando) return; if (this.checkSolapasGrandes('cubierta')) { $(document).trigger('add-servicio-lineas', 'solapas_grandes_cubierta'); } else { $(document).trigger('remove-servicio-lineas', 'solapas_grandes_cubierta'); } } changeSolapasSobrecubierta() { if (this.solapasSobrecubierta.prop('checked')) { this.divSolapasSobrecubierta.removeClass('d-none'); if (this.cargando) return; $(document).trigger('add-servicio-lineas', 'solapas_sobrecubierta'); } else { this.divSolapasSobrecubierta.addClass('d-none'); if ($('#compSobrecubierta').length) { $('#compSobrecubierta').val(0).trigger('change'); } if (this.cargando) return; $(document).trigger('remove-servicio-lineas', 'solapas_sobrecubierta'); $(document).trigger('remove-servicio-lineas', 'solapas_grandes_sobrecubierta'); } } changeAnchoSolapasSobrecubierta() { // para que se actualice el comparador if ($('#compGramajeSobrecubierta').select2('data') && $('#compGramajeSobrecubierta').select2('data').length > 0) $('#compGramajeSobrecubierta').trigger('change'); if (this.cargando) return; if (this.checkSolapasGrandes('sobrecubierta')) { $(document).trigger('add-servicio-lineas', 'solapas_grandes_sobrecubierta'); } else { $(document).trigger('remove-servicio-lineas', 'solapas_grandes_sobrecubierta'); } } changeAnchoSolapasFaja() { // para que se actualice el comparador if ($('#compGramajeFaja').select2('data') && $('#compGramajeFaja').select2('data').length > 0) { $('#compGramajeFaja').trigger('change'); } if (this.cargando) return; if (this.checkSolapasGrandes('faja')) { $(document).trigger('add-servicio-lineas', 'solapas_grandes_faja'); } else { $(document).trigger('remove-servicio-lineas', 'solapas_grandes_faja'); } } checkSolapasGrandes(elemento) { const ancho_libro = 2 * this.getDimensionLibro().ancho; let ancho_solapas = 0.0; let lomo = 0.0; if (elemento == 'cubierta') { ancho_solapas = 2 * parseFloat($('#solapas_ancho').val()); lomo = parseFloat($('#lomo_cubierta').val()); } else if (elemento == 'sobrecubierta') { ancho_solapas = 2 * parseFloat($('#solapas_ancho_sobrecubierta').val()); lomo = parseFloat($('#lomo_sobrecubierta').val()); } else if (elemento == 'faja') { ancho_solapas = 2 * parseFloat($('#faja_solapas_ancho').val()); lomo = parseFloat($('#lomo_sobrecubierta').val()); } else return false; if (ancho_libro + ancho_solapas + lomo > 630) return true; else return false } changePaginas() { if ($('#tipo_impresion_id').val() == 21) { if (parseInt($('#paginas').val()) > 80) { $('#paginas').val(80) } } if (window.location.href.includes('edit')) { this.checkPaginasPresupuesto(); // Si es negro o color if ($('#tipoImpresion').select2('data')[0].id == 'negro' || $('#tipoImpresion').select2('data')[0].id == 'color') { $('#compPaginasNegro').val(parseInt($('#paginas').val()) - parseInt($('#compPaginasColor').val())); $('#compPaginasNegro').trigger('change') } // Si es negrohq o colorhq if ($('#tipoImpresion').select2('data')[0].id == 'negrohq' || $('#tipoImpresion').select2('data')[0].id == 'colorhq') { $('#compPaginasNegrohq').val(parseInt($('#paginas').val()) - parseInt($('#compPaginasColorhq').val())); $('#compPaginasNegrohq').trigger('change'); } $(document).trigger('update-presupuesto', { update_lineas: true, update_servicios: true, update_envios: true, update_resumen: true, update_tiradas_alternativas: true }); this.actualizarLimitesPaginas(); } this.calcularSolapas(); } updateComparador() { if ($("#compPaginasNegro").hasClass('d-none')) { $('#compPaginasNegrohq').trigger('change'); } else { $('#compPaginasNegro').trigger('change'); } $('#compCarasCubierta').trigger('change'); if ($('#compSobrecubierta').length) { $('#compSobrecubierta').trigger('change'); } if ($('#compCarasGuardas').length) { $('#compCarasGuardas').trigger('change'); } } changeTirada(update_tiradas_alternativas = true) { this.calcular_mermas(); this.updateComparador(); const url = window.location.href; if (url.includes('edit')) { $(document).trigger('update-presupuesto', { update_lineas: true, update_servicios: true, update_envios: false, update_resumen: false, update_tiradas_alternativas: false }); $(document).trigger('ckeck-lineas-envios'); } } calcular_mermas() { const tirada = parseInt($('#tirada').val()); const POD = parseInt($('#POD').val()); let merma = 0; let htmlString = ''; if (tirada == 1) { merma = 0; } else if (tirada > POD) { merma = tirada * 0.1 <= POD ? tirada * 0.1 : POD; } else { let merma_lineas = [] const table = $('#tableLineasPresupuesto').DataTable(); const rows = table.rows().data(); for (let i = 0; i < rows.length; i++) { const rowData = rows[i]; if (rowData.row_id != 'lp_guardas' && rowData.row_id != 'lp_cubierta' && rowData.row_id != 'lp_sobrecubierta') { const formas_linea = parseInt($('#isCosido').val()) == 0 ? parseInt(rowData.formas) : parseInt(rowData.formas) / 2; if (formas_linea > tirada) merma_lineas.push(formas_linea - tirada); else { var total_pliegos = Math.ceil(1.0 * tirada / formas_linea); var total_formas = total_pliegos * formas_linea; merma_lineas.push(total_formas - tirada); } } } if (merma_lineas.length > 0) merma = Math.max(...merma_lineas) else { htmlString = ` `; merma = 0 } } $('#mermacubierta').val(parseInt(merma)) $('#merma').val(parseInt(merma)) $('#alert-datosLibro').html(htmlString) } cargarDatos(datos) { this.paginas.val(datos.paginas); this.tirada.val(datos.tirada); this.calcular_mermas(); if (datos.papelFormatoPersonalizado) { this.tamanioPersonalizado.prop('checked', true); $(".tamanio-personalizado").removeClass('d-none'); $(".tamanio-estandar").addClass('d-none'); this.anchoPersonalizado.val(datos.papelFormatoAncho); this.altoPersonalizado.val(datos.papelFormatoAlto); } else { $(".tamanio-personalizado").addClass('d-none'); $(".tamanio-estandar").removeClass('d-none'); this.tamanio.setOption(datos.papelFormatoId, datos.papelFormatoNombre); } this.merma.val(datos.merma); this.mermaCubierta.val(datos.mermaCubierta); if ($('#tipo_impresion_id').val() == 1 || $('#tipo_impresion_id').val() == 3 || $('#tipo_impresion_id').val() == 5 || $('#tipo_impresion_id').val() == 7) { $(".solapas-cubierta-div").addClass('d-none'); } else { if (datos.solapasCubierta) { this.solapasCubierta.prop('checked', true); this.anchoSolapasCubierta.val(datos.solapasCubiertaAncho); this.divSolapasCubierta.removeClass('d-none'); } else { this.solapasCubierta.prop('checked', false); this.anchoSolapasCubierta.val(60); this.divSolapasSobrecubierta.addClass('d-none'); } } if (datos.solapasSobrecubierta) { this.solapasSobrecubierta.prop('checked', true); this.anchoSolapasSobrecubierta.val(datos.solapasSobrecubiertaAncho); $('#div_solapas_ancho_sobrecubierta').removeClass('d-none'); } else { this.solapasSobrecubierta.prop('checked', false); this.anchoSolapasSobrecubierta.val(60); $('#div_solapas_ancho_sobrecubierta').addClass('d-none'); } this.faja.prop('checked', datos.fajaColor); if (datos.fajaColor) { this.div_faja.removeClass('d-none'); this.fajaAlto.val(datos.fajaColorAlto).trigger('change'); this.fajaSolapasAncho.val(datos.fajaColorSolapasAncho).trigger('change'); $('.acabado-faja').removeClass('d-none'); } this.acabadoCubierta.setOption(datos.acabadoCubierta.id, datos.acabadoCubierta.text); if ($('#tipo_impresion_id').val() == 5 || $('#tipo_impresion_id').val() == 6 || $('#tipo_impresion_id').val() == 7 || $('#tipo_impresion_id').val() == 8 || $('#tipo_impresion_id').val() == 21) { $(".impresion-con-sobrecubierta").addClass('d-none'); } else { this.acabadoSobrecubierta.setOption(datos.acabadoSobrecubierta.id, datos.acabadoSobrecubierta.text); } this.acabadoFaja.setOption(datos.acabadoFaja.id, datos.acabadoFaja.text); this.retractilado.prop('checked', datos.retractilado); this.retractilado5.prop('checked', datos.retractilado5); this.prototipo.prop('checked', datos.prototipo); this.ferro.prop('checked', datos.ferro); this.ferroDigital.prop('checked', datos.ferroDigital); this.marcapaginas.prop('checked', datos.marcapaginas); } actualizarLimitesPaginas() { let min = 1; let max = null; switch (this.tipo_impresion_id) { case 1: // Fresado case 2: case 3: // Cosido case 4: min = 32; max = null; // Sin límite superior break; case 5: // Espiral case 6: case 7: // Wire-O case 8: min = 20; max = null; // Sin límite superior break; case 21: // Grapado min = 12; max = 4; break; default: min = 1; max = null; } this.paginas.attr('min', min); this.paginas.attr('max', max); // Verificamos y corregimos el valor actual si está fuera de rango let valorActual = parseInt(this.paginas.val(), 10); if (isNaN(valorActual) || valorActual < min) { this.paginas.val(min); } else if (valorActual > max && max !== null) { this.paginas.val(max); } } } export default DatosLibro;