falta borrar direcciones e implementar una vista de cliente

This commit is contained in:
2025-10-25 15:18:17 +02:00
parent 2ed032d7c6
commit 8e011e7fca
10 changed files with 292 additions and 226 deletions

View File

@ -27,7 +27,7 @@
pageLength: 50,
language: { url: '/assets/libs/datatables/i18n/' + language + '.json' },
responsive: true,
dom: 'lrBtip',
dom: $('#isUser').val() == 1 ? 'lrtip' : 'lrBtip',
buttons: {
dom: {
button: {
@ -49,10 +49,10 @@
},
order: [[0, 'asc']],
columns: [
{ data: 'id', name: 'id', orderable: true, visible: $('#isUser').val() },
{ data: 'cliente', name: 'cliente', orderable: true },
{ data: 'id', name: 'id', orderable: true, visible: $('#isUser').val() == 1 ? false : true },
{ data: 'cliente', name: 'cliente', orderable: true, visible: $('#isUser').val() == 1 ? false : true },
{ data: 'alias', name: 'alias', orderable: true },
{ data: 'nombre', name: 'nombre', orderable: true },
{ data: 'att', name: 'att', orderable: true },
{ data: 'direccion', name: 'direccion', orderable: true },
{ data: 'cp', name: 'cp', orderable: true },
{ data: 'ciudad', name: 'ciudad', orderable: true },
@ -77,7 +77,7 @@
$(document).on("change", ".direccionFacturacion", function () {
const isChecked = $(this).is(':checked');
if(isChecked) {
if (isChecked) {
$('.direccionFacturacionItems').removeClass('d-none');
} else {
$('.direccionFacturacionItems').addClass('d-none');
@ -95,24 +95,50 @@
const title = $('#direccionFormModalBody #direccionForm').data('add');
$('#direccionFormModal .modal-title').text(title);
modal.show();
initSelect2Cliente(true);
});
});
function initSelect2Cliente(initialize = false) {
if ($('#isUser').val() == 0) {
const $sel = $('#user_id').select2({
dropdownParent: modalEl,
width: '100%',
ajax: {
url: 'users/api/get-users',
dataType: 'json',
delay: 250,
},
allowClear: true
});
if (initialize) {
const id = $sel.data('init-id');
const text = $sel.data('init-name');
const option = new Option(text, id, true, true);
$('#user_id').append(option).trigger('change');
}
}
}
// Abrir "Editar"
$(document).on('click', '.btn-edit-direccion', function (e) {
e.preventDefault();
const id = $(this).data('id');
/*$.get('/configuracion/margenes-presupuesto/form', { id }, function (html) {
$('#margenesPresupuestoModalBody').html(html);
const title = $('#margenesPresupuestoModalBody #margenesPresupuestoForm').data('edit');
$('#margenesPresupuestoModal .modal-title').text(title);
modal.show();*/
e.preventDefault();
$.get('/direcciones/form', { id }, function (html) {
$('#direccionFormModalBody').html(html);
const title = $('#direccionFormModalBody #direccionForm').data('edit');
$('#direccionFormModal .modal-title').text(title);
modal.show();
initSelect2Cliente(true);
});
});
// Botón "Eliminar"
$(document).on('click', '.btn-delete-margen', function (e) {
$(document).on('click', '.btn-delete-direccion', function (e) {
e.preventDefault();
const id = $(this).data('id');
@ -132,12 +158,12 @@
if (!result.isConfirmed) return;
$.ajax({
url: '/configuracion/margenes-presupuesto/' + id,
url: '/direcciones/' + id,
type: 'DELETE',
success: function () {
Swal.fire({
icon: 'success', title: window.languageBundle.get(['margenes-presupuesto.delete.ok.title']) || 'Eliminado',
text: window.languageBundle.get(['margenes-presupuesto.delete.ok.text']) || 'El margen ha sido eliminado con éxito.',
icon: 'success', title: window.languageBundle.get(['direcciones.delete.ok.title']) || 'Eliminado',
text: window.languageBundle.get(['direcciones.delete.ok.text']) || 'La dirección ha sido eliminada con éxito.',
showConfirmButton: true,
customClass: {
confirmButton: 'btn btn-secondary w-xs mt-2',
@ -148,7 +174,7 @@
error: function (xhr) {
// usa el mensaje del backend; fallback genérico por si no llega JSON
const msg = (xhr.responseJSON && xhr.responseJSON.message)
|| 'Error al eliminar el usuario.';
|| 'Error al eliminar la direccion.';
Swal.fire({ icon: 'error', title: 'No se pudo eliminar', text: msg });
}
});
@ -156,7 +182,7 @@
});
// Submit del form en el modal
$(document).on('submit', '#margenesPresupuestoForm', function (e) {
$(document).on('submit', '#direccionForm', function (e) {
e.preventDefault();
const $form = $(this);
@ -167,11 +193,11 @@
dataType: 'html',
success: function (html) {
// Si por cualquier motivo llega 200 con fragmento, lo insertamos igual
if (typeof html === 'string' && html.indexOf('id="margenesPresupuestoForm"') !== -1 && html.indexOf('<html') === -1) {
$('#margenesPresupuestoModalBody').html(html);
const isEdit = $('#margenesPresupuestoModalBody #margenesPresupuestoForm input[name="_method"][value="PUT"]').length > 0;
const title = $('#margenesPresupuestoModalBody #margenesPresupuestoForm').data(isEdit ? 'edit' : 'add');
$('#margenesPresupuestoModal .modal-title').text(title);
if (typeof html === 'string' && html.indexOf('id="direccionForm"') !== -1 && html.indexOf('<html') === -1) {
$('#direccionFormModalBody').html(html);
const isEdit = $('#direccionFormModalBody #direccionForm input[name="_method"][value="PUT"]').length > 0;
const title = $('#direccionFormModalBody #direccionForm').data(isEdit ? 'edit' : 'add');
$('#direccionModal .modal-title').text(title);
return;
}
// Éxito real: cerrar y recargar tabla
@ -181,14 +207,15 @@
error: function (xhr) {
// Con 422 devolvemos el fragmento con errores aquí
if (xhr.status === 422 && xhr.responseText) {
$('#margenesPresupuestoModalBody').html(xhr.responseText);
const isEdit = $('#margenesPresupuestoModalBody #margenesPresupuestoForm input[name="_method"][value="PUT"]').length > 0;
const title = $('#margenesPresupuestoModalBody #margenesPresupuestoForm').data(isEdit ? 'edit' : 'add');
$('#margenesPresupuestoModal .modal-title').text(title);
$('#direccionFormModalBody').html(xhr.responseText);
const isEdit = $('#direccionFormModalBody #direccionForm input[name="_method"][value="PUT"]').length > 0;
const title = $('#direccionFormModalBody #direccionForm').data(isEdit ? 'edit' : 'add');
$('#direccionModal .modal-title').text(title);
initSelect2Cliente(true);
return;
}
// Fallback
$('#margenesPresupuestoModalBody').html('<div class="p-3 text-danger">Error inesperado.</div>');
$('#direccionFormModalBody').html('<div class="p-3 text-danger">Error inesperado.</div>');
}
});
});