import ClassSelect from '../../components/select2.js'; import Ajax from '../../components/ajax.js'; class PresupuestoAdminAdd { constructor() { this.cosido = $("#isCosido"); this.tipo_impresion = $("#tipo_impresion_id"); this.POD = $("#POD"); this.inc_rei = $('#incRei'); this.cliente = new ClassSelect($('#clienteId'), '/clientes/cliente/getSelect2', 'Seleccione cliente'); this.pais = new ClassSelect($('#paisId'), '/paises/menuitems2', 'Seleccione País'); this.fecha = $('#updated_at'); this.estado = $('#estado_id'); this.titulo = $('#titulo'); this.autor = $('#autor'); this.coleccion = $('#coleccion'); this.numeroEdicion = $('#numeroEdicion'); this.isbn = $('#isbn'); this.referenciaCliente = $('#referenciaCliente'); this.paginas = $('#paginas'); this.tirada = $('#tirada'); this.merma = $('#merma'); this.mermaCubierta = $('#mermacubierta'); this.tamanio = new ClassSelect($("#papelFormatoId"), '/papel-formato/getSelect2', window.language.Presupuestos.formatoLibro); this.tamanioPersonalizado = $('#papelFormatoPersonalizado'); this.anchoPersonalizado = $('#papelFormatoAncho'); this.altoPersonalizado = $('#papelFormatoAlto'); this.guardar = $('#saveForm'); this.init(); } init() { // Fuerza el foco en el campo de búsqueda de select2 $(document).on('select2:open', () => { document.querySelector('.select2-search__field').focus(); }); $('#solapas').addClass('d-none'); $('#solapas_sobrecubierta').addClass('d-none'); $('#div_solapas_ancho').addClass('d-none'); $('#div_solapas_ancho_sobrecubierta').addClass('d-none'); $('.solapas-cubierta-div').addClass('d-none'); $('.solapas-sobrecubierta-div').addClass('d-none'); $('.container-faja').addClass('d-none'); this.cliente.init(); this.pais.init(); this.tamanio.init(); this.inc_rei.select2({ allowClear: false, }); this.tamanioPersonalizado.on('change', this.changeTipoTamanio.bind(this)); this.guardar.on('click', this.guardarPresupuesto.bind(this)); this.tirada.on('change', this.calcular_mermas.bind(this)); } 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 + '*'); } } async guardarPresupuesto() { $('#loader').modal('show'); try { let data = this.getFormData(); new Ajax('/presupuestoadmin/add/', data, {}, function (response) { popSuccessAlert(response.mensaje); $('#loader').modal('hide'); }, function (error) { console.error(error); $('#loader').modal('hide'); } ).post(); } catch (error) { console.error(error); $('#loader').modal('hide'); } } getFormData() { let datos = {}; datos.cliente_id = this.cliente.getVal(); datos.tipo_impresion_id = this.tipo_impresion.val(); datos.cosido = this.cosido.val(); datos = { ...datos, ...{ inc_rei: this.inc_rei.select2('data')[0].id, titulo: this.titulo.val(), autor: this.autor.val(), isbn: this.isbn.val(), pais_id: this.pais.getVal(), coleccion: this.coleccion.val(), numero_edicion: this.numeroEdicion.val(), referencia_cliente: this.referenciaCliente.val(), updated_at: new Date().toISOString().slice(0, 19).replace('T', ' '), } } datos = { ...datos, ...{ paginas: this.paginas.val(), tirada: this.tirada.val(), papel_formato_id: this.tamanio.item.hasClass('d-none') ? 0 : this.tamanio.getVal(), papel_formato_personalizado: this.tamanio.item.hasClass('d-none') ? 1 : 0, merma: this.merma.val(), merma_cubierta: this.mermaCubierta.val(), solapas_ancho: "0", solapas_ancho_sobrecubierta: "0", } } if (datos.papel_formato_personalizado) { datos.papel_formato_ancho = this.anchoPersonalizado.val(), datos.papel_formato_alto = this.altoPersonalizado.val() } datos.selectedTirada = this.tirada.val(); return datos; } calcular_mermas() { const tirada = parseInt($('#tirada').val()); const POD = parseInt($('#POD').val()); let merma = 0; merma = tirada * 0.1 <= POD ? tirada * 0.1 : POD; $('#mermacubierta').val(parseInt(merma)) $('#merma').val(parseInt(merma)) } } document.addEventListener('DOMContentLoaded', function () { const locale = document.querySelector('meta[name="locale"]').getAttribute('content'); new Ajax('/translate/getTranslation', { locale: locale, translationFile: ['Presupuestos', 'PresupuestosDirecciones'] }, {}, function (translations) { window.language = JSON.parse(translations); new PresupuestoAdminAdd().init(); }, function (error) { console.log("Error getting translations:", error); } ).post(); });