guardando direcciones ferro en presupuesto cliente

This commit is contained in:
2025-06-28 11:27:39 +02:00
parent 410d21dc5f
commit 9fcda514f0
8 changed files with 324 additions and 120 deletions

View File

@ -1,24 +1,24 @@
class Ajax{
constructor(url, data, headers, success, error, type='default'){
this.url = url;
this.data = data;
this.headers = headers;
this.success = success;
this.error = error;
this.type = type;
this.jqXHR = null;
}
post(){
(this.type == 'default') ? this.ajax('POST'): this.ajaxForm('POST');
class Ajax {
constructor(url, data, headers, success, error, type = 'default') {
this.url = url;
this.data = data;
this.headers = headers;
this.success = success;
this.error = error;
this.type = type;
this.jqXHR = null;
}
get(){
post() {
(this.type == 'default') ? this.ajax('POST') : this.ajaxForm('POST');
}
get() {
this.ajax('GET');
}
put(){
(this.type == 'default') ? this.ajax('PUT'): this.ajaxForm('PUT');
put() {
(this.type == 'default') ? this.ajax('PUT') : this.ajaxForm('PUT');
}
delete(){
(this.type == 'default') ? this.ajax('DELETE'): this.ajaxForm('DELETE');
delete() {
(this.type == 'default') ? this.ajax('DELETE') : this.ajaxForm('DELETE');
}
abort() {
if (this.jqXHR) {
@ -26,31 +26,31 @@ class Ajax{
this.jqXHR = null; // Limpiamos la referencia a la petición
}
}
setData(data){
setData(data) {
this.data = data;
}
ajax(method){
ajax(method) {
if (this.jqXHR) {
this.jqXHR.abort();
}
this.jqXHR = $.ajax({
url : this.url,
type : method,
url: this.url,
type: method,
data: this.data,
headers: this.headers,
success: this.success,
error: this.error
})
}
ajaxForm(method){
ajaxForm(method) {
if (this.jqXHR) {
this.jqXHR.abort();
}
this.jqXHR = $.ajax({
url : this.url,
type : method,
url: this.url,
type: method,
data: this.data,
processData: false,
contentType: false,
@ -59,6 +59,28 @@ class Ajax{
error: this.error
})
}
getPromise() {
if (this.jqXHR) {
this.jqXHR.abort();
}
return new Promise((resolve, reject) => {
this.jqXHR = $.ajax({
url: this.url,
type: 'GET',
data: this.data,
headers: this.headers,
success: (response) => {
if (this.success) this.success(response);
resolve(response);
},
error: (xhr) => {
if (this.error) this.error(xhr);
reject(xhr);
}
});
});
}
}
export default Ajax

View File

@ -5,12 +5,13 @@ class tarjetaDireccion {
this.container = container;
this.id = id;
this.direccionId = direccion.id;
this.direccion = direccion;
this.isFerroPrototipo = isFerroPrototipo;
this.numFerro = numFerro || 0;
this.card = this.#generateHTML(id, direccion);
this.deleteBtn = this.card.find('.direccion-eliminar');
this.editBtn = this.card.find('.direccion-editar');
this.direccion = direccion;
this.isFerroPrototipo = direccion.isFerroPrototipo;
this.numFerro = this.numFerro || 0;
}
@ -40,7 +41,13 @@ class tarjetaDireccion {
var $hr = $('<hr>', { class: 'my-2' });
var $eliminar = $('<a>', { class: 'ms-auto direccion-eliminar', href: 'javascript:void(0)', text: 'Eliminar' });
var $editar = $('<a>', { class: 'me-auto direccion-editar', href: 'javascript:void(0)', text: 'Editar' });
var $eliminarContainer = $('<span>', { class: 'd-flex w-100 position-relative' }).append($editar, $eliminar);
var $eliminarContainer = '';
if(!this.isFerroPrototipo) {
$eliminarContainer = $('<span>', { class: 'd-flex w-100 position-relative' }).append($editar, $eliminar);
}
else{
$eliminarContainer = $('<span>', { class: 'd-flex w-100 position-relative' }).append($eliminar);
}
// Construir la estructura del HTML
$header.append($name, $unidades);

View File

@ -70,6 +70,13 @@ class Direcciones {
this.btnAdd.on('click', this.#insertDireccion.bind(this));
this.btnNew.on('click', this.#nuevaDireccion.bind(this));
$('#insertarDireccionFP1').on('click', () => {
this.#insertDireccionFP(1);
});
$('#insertarDireccionFP2').on('click', () => {
this.#insertDireccionFP(2);
});
// evento para actualizar el selector de tiradas
$(document).on('update-tiradas-envios', (event, data) => {
self.divTiradas.empty();
@ -165,29 +172,6 @@ class Direcciones {
tarjeta.card.find('.direccion-eliminar').on('click', this.#deleteDireccion.bind(self));
this.divDirecciones.append(tarjeta.card);
this.direcciones.push(tarjeta);
/*let peticion = new Ajax('/misdirecciones/getDireccionPresupuesto/' + id, {}, {},
(response) => {
let divId = "dirEnvio-1";
let direccionesActuales = this.divDirecciones.find('.direccion-cliente');
if (direccionesActuales.length > 0) {
// the the lass item
let last = direccionesActuales[direccionesActuales.length - 1];
divId = "dirEnvio-" + (parseInt(last.id.split('-')[1]) + 1);
}
let tarjeta = new tarjetaDireccion(this.divDirecciones, divId, response.data[0]);
tarjeta.setUnidades(unidades);
tarjeta.setEntregaPalets(entregaPalets);
tarjeta.card.find('.direccion-editar').on('click', this.#editUnits.bind(self));
tarjeta.card.find('.direccion-eliminar').on('click', this.#deleteDireccion.bind(self));
this.divDirecciones.append(tarjeta.card);
this.direcciones.push(tarjeta);
},
() => {
console.error('Error getting address');
});
peticion.get();*/
}
}
} catch (e) {
@ -357,42 +341,107 @@ class Direcciones {
return;
}
}
this.#addTarjetaDireccion(this.divDirecciones, this.direcciones, divId, id, unidades, entregaPalets);
$('#loader').modal('show');
let peticion = new Ajax('/misdirecciones/get/' + id, {}, {},
(response) => {
if (this.direcciones.length == 0) {
$('#loader').modal('hide');
if (entregaPalets) {
this.calcularPresupuesto = true;
}
}
else {
this.calcularPresupuesto = true;
}
let tarjeta = new tarjetaDireccion(this.divDirecciones, divId, response.data[0]);
tarjeta.setUnidades(unidades);
tarjeta.setEntregaPalets(entregaPalets);
tarjeta.card.find('.direccion-editar').on('click', this.#editUnits.bind(self));
tarjeta.card.find('.direccion-eliminar').on('click', this.#deleteDireccion.bind(self));
this.divDirecciones.append(tarjeta.card);
this.direcciones.push(tarjeta);
self.divDirecciones.trigger('change');
},
() => {
console.error('Error getting address');
$('#loader').modal('hide');
});
peticion.get();
} catch (e) {
console.error(e);
$('#loader').modal('hide');
}
}
#insertDireccionFP(num_ferroPrototipo) {
self = this;
try {
let id = 0;
if( num_ferroPrototipo === 1) {
id = this.direccionesClienteFP1.getVal();
}
if( num_ferroPrototipo === 2) {
id = this.direccionesClienteFP2.getVal();
}
let unidades = 1;
if (id == null || id <= 0 || id == undefined)
return;
let divId = "dirEnvio-FP-" + num_ferroPrototipo;
let containerId = num_ferroPrototipo === 1 ? "#divDireccionesFerroPrototipo" : "#divDireccionesFerroPrototipo2";
let arrayName = num_ferroPrototipo === 1 ? this.direccionesFP1 : this.direccionesFP2;
this.#addTarjetaDireccion(
$(containerId),
arrayName,
divId,
id,
1, false, true, num_ferroPrototipo);
$('.div-direcciones-fp' + num_ferroPrototipo).addClass('d-none');
} catch (e) {
console.error(e);
$('#loader').modal('hide');
}
}
async #addTarjetaDireccion(container, addArray, divId, data, unidades, entregaPalets = false, isFerroPrototipo = false, numFerroPrototipo = 0) {
const self = this;
let id = data;
try {
if (typeof data === 'object') {
const ajaxGetId = new Ajax('/misdirecciones/getId', {
data: data,
cliente_id: $("#clienteId").select2('data')[0].id
}, {}, null, null);
const response = await ajaxGetId.getPromise();
id = response;
}
$('#loader').modal('show');
const ajaxGetDireccion = new Ajax('/misdirecciones/get/' + id, {}, {}, null, null);
const response = await ajaxGetDireccion.getPromise();
if (this.direcciones.length === 0) {
$('#loader').modal('hide');
if (entregaPalets) {
this.calcularPresupuesto = true;
}
} else {
this.calcularPresupuesto = true;
}
const tarjeta = new tarjetaDireccion(container, divId, response.data[0], isFerroPrototipo, numFerroPrototipo);
tarjeta.setUnidades(unidades);
tarjeta.setEntregaPalets(entregaPalets);
if (!isFerroPrototipo) {
tarjeta.card.find('.direccion-eliminar').on('click', this.#deleteDireccion.bind(self));
tarjeta.card.find('.direccion-editar').on('click', this.#editUnits.bind(self));
}
else {
tarjeta.card.find('.direccion-eliminar').on('click', (event) => {
this.#deleteDireccionFP(event, numFerroPrototipo);
$('.div-direcciones-fp' + numFerroPrototipo).removeClass('d-none');
});
}
container.append(tarjeta.card);
addArray.push(tarjeta);
container.trigger('change');
} catch (err) {
console.error('Error en petición Ajax:', err);
$('#loader').modal('hide');
}
}
handleChangeCliente() {
@ -453,22 +502,43 @@ class Direcciones {
this.divDirecciones.trigger('change');
}
#deleteDireccionFP(event, num_ferroPrototipo = 1) {
let tarjeta = event.currentTarget.closest('.direccion-cliente');
this.calcularPresupuesto = true;
if (num_ferroPrototipo === 1) {
this.direccionesFP1 = [];
this.sameAddPrincipalFP1.prop('checked', false);
$('#divDireccionesFerroPrototipo1').trigger('change');
}
if (num_ferroPrototipo === 2) {
this.direccionesFP2 = [];
this.sameAddPrincipalFP2.prop('checked', false);
this.sameAddFP1.prop('checked', false);
$('#divDireccionesFerroPrototipo2').trigger('change');
}
tarjeta.remove();
}
#handleSameAddPrincipalFP1() {
if (this.sameAddPrincipalFP2.is(':checked')) {
if (this.sameAddPrincipalFP1.is(':checked')) {
$('.div-direcciones-fp1').addClass('d-none');
$('#divDireccionesFerroPrototipo').empty();
if (this.direcciones.length > 0) {
// get first element
let firstDireccion = this.direcciones[0];
let tarjeta = new tarjetaDireccion(
$("#divDireccionesFerroPrototipo"),
'dirEnvio-FP-1',
firstDireccion.getData(), true, 1);
tarjeta.setUnidades(1);
$('#divDireccionesFerroPrototipo').append(tarjeta.card);
let data = this.direcciones[0].getDireccion();
this.direccionesFP1 = [];
this.direccionesFP1.push(tarjeta);
this.#addTarjetaDireccion(
$("#divDireccionesFerroPrototipo"),
this.direccionesFP1,
'dirEnvio-FP-1',
data,
1, false, true, 1);
$('#divDireccionesFerroPrototipo').removeClass('d-none');
}
else {
@ -486,19 +556,21 @@ class Direcciones {
#handleSameAddPrincipalFP2() {
if (this.sameAddPrincipalFP2.is(':checked')) {
if(this.sameAddFP1.is(':checked')) {
this.sameAddFP1.prop('checked', false);
}
$('.div-direcciones-fp2').addClass('d-none');
$('#divDireccionesFerroPrototipo2').empty();
if (this.direcciones.length > 0) {
// get first element
let firstDireccion = this.direcciones[0];
let tarjeta = new tarjetaDireccion(
$("#divDireccionesFerroPrototipo2"),
'dirEnvio-FP-2',
firstDireccion.getData(), true, 1);
tarjeta.setUnidades(1);
$('#divDireccionesFerroPrototipo2').append(tarjeta.card);
let data = this.direcciones[0].getDireccion();
this.direccionesFP2 = [];
this.direccionesFP2.push(tarjeta);
this.#addTarjetaDireccion(
$("#divDireccionesFerroPrototipo2"),
this.direccionesFP2,
'dirEnvio-FP-2',
data,
1, false, true, 2);
$('#divDireccionesFerroPrototipo2').removeClass('d-none');
}
else {
@ -516,19 +588,23 @@ class Direcciones {
#handleSameAddFP1() {
if (this.sameAddFP1.is(':checked')) {
if(this.sameAddPrincipalFP2.is(':checked')) {
this.sameAddPrincipalFP2.prop('checked', false);
}
$('.div-direcciones-fp2').addClass('d-none');
$('#divDireccionesFerroPrototipo2').empty();
if (this.direccionesFP1.length > 0) {
// get first element
let firstDireccion = this.direccionesFP1[0];
let tarjeta = new tarjetaDireccion(
let data = this.direccionesFP1[0].getDireccion();
this.direccionesFP1 = [];
this.#addTarjetaDireccion(
$("#divDireccionesFerroPrototipo2"),
this.direccionesFP2,
'dirEnvio-FP-2',
firstDireccion.getData(), true, 1);
tarjeta.setUnidades(1);
$('#divDireccionesFerroPrototipo2').append(tarjeta.card);
this.direccionesFP2 = [];
this.direccionesFP2.push(tarjeta);
data,
1, false, true, 2);
$('#divDireccionesFerroPrototipo2').removeClass('d-none');
}
else {

View File

@ -793,6 +793,19 @@ class PresupuestoCliente {
};
}
}
if( this.direcciones.direccionesFP1.length > 0) {
this.datos.direccionesFP1 = this.direcciones.direccionesFP1.map((direccion) => {
return direccion.getFormData();
});
}
if( this.direcciones.direccionesFP2.length > 0) {
this.datos.direccionesFP2 = this.direcciones.direccionesFP2.map((direccion) => {
return direccion.getFormData();
});
}
if (save) {
this.datos.datosCabecera = {
titulo: this.datosGenerales.titulo.val(),