Files
safekat/httpdocs/assets/js/safekat/pages/presupuestoCliente/datosGenerales.js
2024-10-22 14:14:52 +02:00

637 lines
27 KiB
JavaScript

import ClassSelect from '../../components/select2.js';
import tarjetaTiradasPrecio from './tarjetaTiradasPrecio.js';
class DatosGenerales {
constructor(domItem, wizardForm, validatorStepper) {
this.domItem = domItem;
this.wizardStep = wizardForm.querySelector('#datos-generales');
this.validatorStepper = validatorStepper;
this.formatoLibro = new ClassSelect($("#papelFormatoId"), '/papel-formato/getSelect2', window.translations["formatoLibro"]);
this.cliente = new ClassSelect($("#clienteId"), '/clientes/cliente/getSelect2', window.translations["selectCliente"]);
this.titulo = this.domItem.find("#titulo");
this.autor = this.domItem.find("#autor");
this.isbn = this.domItem.find("#isbn");
this.coleccion = this.domItem.find("#coleccion");
this.referenciaCliente = this.domItem.find("#referenciaCliente");
this.tirada1 = this.domItem.find("#tirada");
this.tirada2 = this.domItem.find("#tirada2");
this.tirada3 = this.domItem.find("#tirada3");
this.tirada4 = this.domItem.find("#tirada4");
this.papelFormatoId = this.domItem.find("#papelFormatoId");
this.checkFormatoPersonalizado = this.domItem.find("#papelFormatoPersonalizado");
this.formatoPersonalizado = this.domItem.find("#formatoPersonalizado");
this.formatoEstandar = this.domItem.find("#formatoEstandar");
this.anchoPersonalizado = this.domItem.find("#papelFormatoAncho");
this.altoPersonalizado = this.domItem.find("#papelFormatoAlto");
this.fresado = $(this.domItem.find("#fresado")[0]);
this.cosido = $(this.domItem.find("#cosido")[0]);
this.grapado = $(this.domItem.find("#grapado")[0]);
this.espiral = $(this.domItem.find("#espiral")[0]);
this.tiposLibro = this.domItem.find(".tipo-libro");
this.paginas = this.domItem.find("#paginas");
this.paginasNegro = this.domItem.find("#paginasNegro");
this.paginasColor = this.domItem.find("#paginasColor");
this.divPaginasCuaderillo = this.domItem.find("#divPaginasCuadernillo");
this.paginasCuadernillo = this.domItem.find("#paginasCuadernillo");
this.divPaginasColorConsecutivas = this.domItem.find("#divPaginasColorConsecutivas");
this.pagColorConsecutivas = this.domItem.find("#pagColorConsecutivas");
this.divPapelDiferente = this.domItem.find("#divPapelDiferente");
this.papelDiferente = this.domItem.find("#papelDiferente");
this.divPosPaginasColor = this.domItem.find("#divPosPaginasColor");
this.posPaginasColor = this.domItem.find("#posPaginasColor");
this.ivaReducido = this.domItem.find("#ivaReducido");
this.excluirRotativa = this.domItem.find("#excluirRotativa");
this.prototipo = this.domItem.find("#prototipo");
this.initValidation();
}
init() {
// Selects
this.formatoLibro.init();
this.cliente.init();
// Inicializa el tipo de impresion
this.#handlePaginas();
// Eventos
this.checkFormatoPersonalizado.bind('change', this.#handleFormatoLibro.bind(this));
this.tiposLibro.on('click', this.#handleTipolibro.bind(this));
this.domItem.find('.input-paginas').on('change', this.#handlePaginas.bind(this));
this.anchoPersonalizado.on('blur', this.#handleCheckFormatoPersonalizado.bind(this));
this.altoPersonalizado.on('blur', this.#handleCheckFormatoPersonalizado.bind(this));
this.pagColorConsecutivas.on('change', this.#handPaginasConsecutivas.bind(this));
this.papelDiferente.on('change', this.#handlePapelDiferente.bind(this));
this.titulo.on('change', () => { $(".titulo").html(this.titulo.val()); });
}
initValidation() {
const stepper = this.validatorStepper;
this.formValidation = FormValidation.formValidation(this.wizardStep, {
fields: {
titulo: {
validators: {
notEmpty: {
message: window.translations["validation"].requerido_short
},
}
},
cliente_id: {
validators: {
callback: {
message: window.translations["validation"].cliente,
callback: function (input) {
// Get the selected options
const options = $("#clienteId").select2('data');
const hasValidOption = options.some(option => parseInt(option.id) > 0);
return options !== null && options.length > 0 && hasValidOption;
},
}
}
},
tirada: {
validators: {
callback: {
message: window.translations["validation"].integer_greatherThan_0,
callback: function (input) {
const value = $("#tirada").val();
return value.length > 0 && Number.isInteger(parseInt(value)) && parseInt(value) > 0;
},
}
}
},
tirada2: {
validators: {
callback: {
message: window.translations["validation"].integer_greatherThan_0,
callback: function (input) {
let field = $("#tirada2");
field.val(field.val().replace(/[^0-9]/g, ''));
let value = field.value;
if (value == '')
return true;
return value.length > 0 && Number.isInteger(parseInt(value)) && parseInt(value) > 0;
},
}
}
},
tirada3: {
validators: {
callback: {
message: window.translations["validation"].integer_greatherThan_0,
callback: function (input) {
let field = $("#tirada3");
field.val(field.val().replace(/[^0-9]/g, ''));
let value = field.value;
if (value == '')
return true;
return value.length > 0 && Number.isInteger(parseInt(value)) && parseInt(value) > 0;
},
}
}
},
tirada4: {
validators: {
callback: {
message: window.translations["validation"].integer_greatherThan_0,
callback: function (input) {
let field = $("#tirada4");
field.val(field.val().replace(/[^0-9]/g, ''));
let value = field.value;
if (value == '')
return true;
return value.length > 0 && Number.isInteger(parseInt(value)) && parseInt(value) > 0;
},
}
}
},
papel_formato_id: {
validators: {
callback: {
message: window.translations["validation"].papelFormato,
callback: function (input) {
// Get the selected options
const options = $("#papelFormatoId").select2('data');
const hasValidOption = options.some(option => parseInt(option.id) > 0);
const custom_format = $("#papelFormatoPersonalizado").is(":checked");
if (custom_format)
return true;
return options !== null && options.length > 0 && hasValidOption;
},
}
}
},
papel_formato_ancho: {
validators: {
callback: {
message: window.translations["validation"].integer_greatherThan_0,
callback: function (input) {
// Get the selected options
const custom_format = $("#papelFormatoPersonalizado").is(":checked");
if (!custom_format)
return true;
const value = $("#papelFormatoAncho").val();
return value.length > 0 && Number.isInteger(parseInt(value)) && parseInt(value) > 0;
},
}
}
},
papel_formato_alto: {
validators: {
callback: {
message: window.translations["validation"].integer_greatherThan_0,
callback: function (input) {
// Get the selected options
const custom_format = $("#papelFormatoPersonalizado").is(":checked");
if (!custom_format)
return true;
const value = $("#papelFormatoAlto").val();
return value.length > 0 && Number.isInteger(parseInt(value)) && parseInt(value) > 0;
},
}
}
},
paginasColor: {
validators: {
callback: {
message: window.translations["validation"].integer_greatherThan_0,
callback: function (input) {
// Get the selected options
const value = $("#paginasColor").val();
if (value.length >= 0 && Number.isInteger(parseInt(value)) && parseInt(value) >= 0) {
if (parseInt(value) % 2 != 0) {
return {
valid: false,
message: window.translations["validation"].paginas_pares
};
}
return true;
}
return false;
},
}
}
},
paginasNegro: {
validators: {
callback: {
message: window.translations["validation"].integer_greatherThan_0,
callback: function (input) {
// Get the selected options
const value = $("#paginasNegro").val();
if (value.length >= 0 && Number.isInteger(parseInt(value)) && parseInt(value) >= 0) {
if (parseInt(value) % 2 != 0) {
return {
valid: false,
message: window.translations["validation"].paginas_pares
};
}
return true;
}
return false;
},
}
}
},
paginas: {
validators: {
callback: {
message: window.translations["validation"].integer_greatherThan_0,
callback: function (input) {
// Get the selected options
const value = $("#paginas").val();
return value.length > 0 && Number.isInteger(parseInt(value)) && parseInt(value) > 0;
},
}
}
},
div_tipo_libro: {
validators: {
callback: {
callback: function (input) {
const divTipoLibro = $('#divTipoLibro'); // Selecciona el div
divTipoLibro.find('.fv-plugins-message-container').remove();
if ($('.tipo-libro.selected').length > 0) {
if ($('#cosido').hasClass('selected')) {
const value = parseInt($("#paginas").val());
if (value % 4 != 0) {
divTipoLibro.append(`
<div class="fv-plugins-message-container invalid-feedback">
<div data-field="div_tipo_libro" data-validator="callback" style="margin-top: 50px;">
${window.translations["validation"].paginas_cosido}
</div>
</div>
`);
}
}
return true;
}
else {
divTipoLibro.append(`
<div class="fv-plugins-message-container invalid-feedback">
<div data-field="div_tipo_libro" data-validator="callback" style="margin-top: 50px;">
${window.translations["validation"].tipo_libro}
</div>
</div>
`);
}
return false;
},
}
}
}
},
plugins: {
trigger: new FormValidation.plugins.Trigger(),
bootstrap5: new FormValidation.plugins.Bootstrap5({
// Use this for enabling/changing valid/invalid class
// eleInvalidClass: '',
eleValidClass: '',
rowSelector: function (field, ele) {
// field is the field name
// ele is the field element
switch (field) {
case 'div_tipo_libro':
return '.col-sm-10';
case 'titulo':
return '.col-sm-8';
case 'cliente_id':
return '.col-sm-5';
case 'papel_formato_id':
return '.col-sm-4';
case 'papel_formato_ancho':
case 'papel_formato_ancho':
return '.col-sm-3';
case 'tirada':
case 'tirada2':
case 'tirada3':
case 'tirada4':
return '.col-sm-2';
default:
return '.col-sm-3';
}
}
}),
autoFocus: new FormValidation.plugins.AutoFocus(),
submitButton: new FormValidation.plugins.SubmitButton()
}
}).on('core.form.valid', () => {
stepper.next();
});
}
cargarDatos(datos) {
this.titulo.val(datos.titulo);
this.autor.val(datos.autor);
this.isbn.val(datos.isbn);
this.coleccion.val(datos.coleccion);
this.referenciaCliente.val(datos.referenciaCliente);
this.cliente.setOption(datos.clienteId, datos.clienteNombre);
this.cliente.setVal(datos.clienteId);
$(this.cliente).trigger('change');
if (datos.excluirRotativa) {
this.excluirRotativa.prop('checked', true);
}
this.tirada1.val(parseInt(datos.tirada));
if (datos.tirada2)
this.tirada2.val(parseInt(datos.tirada2));
if (datos.tirada3)
this.tirada3.val(parseInt(datos.tirada3));
if (datos.tirada4)
this.tirada4.val(parseInt(datos.tirada4));
if (datos.papelFormatoPersonalizado) {
this.checkFormatoPersonalizado.prop('checked', true);
this.formatoEstandar.addClass('d-none');
this.formatoPersonalizado.removeClass('d-none');
this.formValidation.revalidateField('papel_formato_id');
this.altoPersonalizado.val(datos.papelFormatoAlto);
this.anchoPersonalizado.val(datos.papelFormatoAncho);
}
else {
this.formatoLibro.setOption(datos.papelFormatoId, datos.papelFormatoNombre);
this.formatoLibro.setVal(datos.papelFormatoId);
}
this.paginas.val(parseInt(datos.paginas));
this.paginasNegro.val(parseInt(datos.paginasNegro));
this.paginasColor.val(parseInt(datos.paginasColor)).trigger('change');
this.pagColorConsecutivas.prop('checked', datos.paginasColorConsecutivas);
if (datos.paginasColorConsecutivas) {
this.divPapelDiferente.removeClass('d-none');
this.papelDiferente.prop('checked', datos.papelInteriorDiferente).trigger('change');
}
this.posPaginasColor.val(datos.posPaginasColor);
this.paginasCuadernillo.val(datos.paginasCuadernillo);
if (datos.tipo != "") {
this.tiposLibro.removeClass('selected');
this.tiposLibro.find('.image-presupuesto').removeClass('selected');
this.domItem.find('#' + datos.tipo).addClass('selected');
}
this.prototipo.prop('checked', datos.prototipo);
this.ivaReducido.val(datos.ivaReducido ? 1 : 0).trigger('change');
}
getDimensionLibro() {
let ancho = 0;
let alto = 0;
if (this.checkFormatoPersonalizado.is(':checked')) {
ancho = parseFloat(this.anchoPersonalizado.val());
alto = parseFloat(this.altoPersonalizado.val());
}
else {
if (this.papelFormatoId.val() != null) {
const selectedText = this.papelFormatoId.find('option:selected').text();
if (selectedText.length > 0) {
ancho = parseFloat(selectedText.trim().split(" x ")[0]);
alto = parseFloat(selectedText.trim().split(" x ")[1]);
}
}
else
return null;
}
return {
ancho: ancho,
alto: alto
}
}
getTiradas() {
let tiradas = [];
tiradas.push(parseInt(this.tirada1.val()));
if (this.tirada2.val().length > 0 && parseInt(this.tirada2.val()) > 0)
tiradas.push(parseInt(this.tirada2.val()));
if (this.tirada3.val().length > 0 && parseInt(this.tirada3.val()) > 0)
tiradas.push(parseInt(this.tirada3.val()));
if (this.tirada4.val().length > 0 && parseInt(this.tirada4.val()) > 0)
tiradas.push(parseInt(this.tirada4.val()));
return tiradas;
}
getIsColor() {
if (this.paginasColor.val() > 0) {
return true;
}
return false;
}
#handleFormatoLibro() {
if (this.checkFormatoPersonalizado.is(':checked')) {
this.formatoEstandar.addClass('d-none');
this.formatoPersonalizado.removeClass('d-none');
this.formValidation.revalidateField('papel_formato_id');
}
else {
this.formatoEstandar.removeClass('d-none');
this.formatoPersonalizado.addClass('d-none');
this.formValidation.revalidateField('papel_formato_ancho');
this.formValidation.revalidateField('papel_formato_alto');
}
}
#handleTipolibro(event) {
// Accede al ID del elemento que disparó el evento
const element = $(event.target);
let containers = element.closest('.tipo-libro').parent().find('.tipo-libro');
for (let container of containers) {
if (container != element.closest('.tipo-libro')[0]) {
$(container).removeClass('selected');
$(container).find('.image-presupuesto').removeClass('selected');
}
}
element.closest('.tipo-libro').toggleClass('selected');
element.closest('.image-presupuesto').toggleClass('selected');
if (this.fresado.hasClass('selected') || this.cosido.hasClass('selected')) {
$('#tapaDuraLomoRedondo').removeClass('d-none');
if (this.cosido.hasClass('selected')) {
$('#tapaDuraLomoRedondo').addClass('selected');
this.divPaginasCuaderillo.removeClass('d-none');
}
else {
this.divPaginasCuaderillo.addClass('d-none');
}
}
else {
$('#tapaDuraLomoRedondo').addClass('d-none');
$('#tapaDuraLomoRedondo').removeClass('selected');
this.divPaginasCuaderillo.addClass('d-none');
}
if (this.grapado.hasClass('selected') || this.espiral.hasClass('selected')) {
$('#addSobrecubierta').prop('checked', false).trigger('change');
$(".sobrecubierta-items").addClass('d-none');
}
else{
if($('#addSobrecubierta').hasClass('d-none')){
$('#addSobrecubierta').removeClass('d-none');
}
}
// Para recalcular el presupuesto
element.trigger('change');
}
#handlePaginas() {
let paginasNegro = this.paginasNegro.val();
let paginasColor = this.paginasColor.val();
if (paginasNegro == '' || isNaN(paginasNegro)) {
paginasNegro = 0;
}
if (paginasColor == '' || isNaN(paginasColor)) {
paginasColor = 0;
}
let totalPaginas = parseInt(paginasNegro) + parseInt(paginasColor);
this.paginas.val(totalPaginas);
let tipos = [this.domItem.find('#fresado'), this.domItem.find('#cosido')];
if (totalPaginas < 32) {
for (let tipo of tipos) {
tipo.removeClass('selected');
tipo.find('.image-presupuesto').removeClass('selected');
tipo.hide();
}
}
else {
for (let tipo of tipos) {
tipo.show();
}
}
if (totalPaginas < 12 || totalPaginas > 40) {
this.domItem.find('#grapado').removeClass('selected');
this.domItem.find('#grapado').find('.image-presupuesto').removeClass('selected');
this.domItem.find('#grapado').hide();
}
else {
this.domItem.find('#grapado').show();
}
this.formValidation.revalidateField('paginas');
// Se configura dependiento si hay color o no
if (paginasColor == 0) {
this.#handleInteriorLayout('negro');
this.divPaginasColorConsecutivas.addClass('d-none');
this.divPosPaginasColor.addClass('d-none');
this.posPaginasColor.val("");
this.pagColorConsecutivas.prop('checked', false);
}
else {
if (this.papelDiferente.is(":checked"))
this.#handleInteriorLayout('mixto');
else
this.#handleInteriorLayout('color');
this.divPaginasColorConsecutivas.removeClass('d-none');
this.divPosPaginasColor.removeClass('d-none');
}
}
#handPaginasConsecutivas() {
if (this.pagColorConsecutivas.is(':checked')) {
this.divPapelDiferente.removeClass('d-none');
}
else {
this.divPapelDiferente.addClass('d-none');
this.papelDiferente.prop('checked', false);
}
}
#handlePapelDiferente() {
if (this.papelDiferente.is(':checked')) {
$(".papel-interior").removeClass('selected');
$(".interior-color").removeClass('d-none');
this.#handleInteriorLayout('mixto');
}
else {
$(".interior-color").addClass('d-none');
$("#divGramajeInteriorColor").addClass('d-none');
$(".papel-interior").removeClass('selected');
$(".gramaje-interior").prop('checked', false);
$(".gramaje-interior-color").prop('checked', false);
this.#handlePaginas();
}
}
#handleInteriorLayout(layout) {
switch (layout) {
case 'negro':
case 'mixto':
$('#negroEstandar').removeClass('d-none');
$('#negroPremium').removeClass('d-none');
$('#colorEstandar').addClass('d-none');
$('#colorPremium').addClass('d-none');
break;
case 'color':
$('#negroEstandar').addClass('d-none');
$('#negroPremium').addClass('d-none');
$('#colorEstandar').removeClass('d-none');
$('#colorPremium').removeClass('d-none');
break;
default:
break;
}
}
#handleCheckFormatoPersonalizado() {
const minAncho = this.anchoPersonalizado.min;
const minAlto = this.altoPersonalizado.min;
if (this.anchoPersonalizado.val() < minAncho || this.anchoPersonalizado.val() == '' || isNaN(this.anchoPersonalizado.val())) {
this.anchoPersonalizado.val(minAncho);
}
if (this.altoPersonalizado.val() < minAlto || this.altoPersonalizado.val() == '' || isNaN(this.altoPersonalizado.val())) {
this.altoPersonalizado.val(minAlto);
}
}
}
export default DatosGenerales;