mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
trabajando en albaranes. Voy a quitar las cajas
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import ClassSelect from '../../components/select2.js';
|
||||
import Ajax from '../../components/ajax.js';
|
||||
import AlbaranComponent from '../../components/albaranComponent.js';
|
||||
|
||||
class EnvioEdit {
|
||||
|
||||
@ -8,6 +9,7 @@ class EnvioEdit {
|
||||
|
||||
this.tableCols = [
|
||||
{ data: "rowSelected" },
|
||||
{ data: "cajasRaw", defaultContent: "" },
|
||||
{ data: "cajas", defaultContent: "" },
|
||||
{ data: "pedido" },
|
||||
{ data: "presupuesto" },
|
||||
@ -15,9 +17,9 @@ class EnvioEdit {
|
||||
{ data: "unidadesEnvio" },
|
||||
{ data: "unidadesEnviadas" },
|
||||
{ data: "unidadesTotal" },
|
||||
{ data: "action" },
|
||||
{ data: "id" },
|
||||
{ data: "pesoUnidad" }
|
||||
{ data: "pesoUnidad" },
|
||||
{ data: "unidadesEnvioRaw" }
|
||||
];
|
||||
|
||||
this.table = null;
|
||||
@ -29,6 +31,8 @@ class EnvioEdit {
|
||||
this.btnAddCaja = $("#btnAddCaja");
|
||||
this.btnDeleteLinea = $("#btnEliminarLineas");
|
||||
this.btnGuardarComentarios = $("#guardarComentarios");
|
||||
this.btnSelectAll = $("#btnSelectAll");
|
||||
this.btnGenerarAlbaran = $("#btnGenerarAlbaran");
|
||||
}
|
||||
|
||||
init() {
|
||||
@ -51,43 +55,45 @@ class EnvioEdit {
|
||||
url: "/themes/vuexy/vendor/libs/datatables-sk/plugins/i18n/es-ES.json"
|
||||
},
|
||||
rowGroup: {
|
||||
dataSrc: 'cajas',
|
||||
dataSrc: 'cajasRaw',
|
||||
startRender: function (rows, group) {
|
||||
const nombreGrupo = group === null || group === '' || group === 'null' || group.toUpperCase() === 'NO GROUP'
|
||||
? 'SIN ASIGNAR'
|
||||
: group;
|
||||
// Forzar el grupo a string (DataTables puede pasarlo como undefined o null de forma interna)
|
||||
const groupStr = group == null || group === 0 || group === '0' ? '__SIN__ASIGNAR__' : group.toString();
|
||||
const nombreGrupo = groupStr === '__SIN__ASIGNAR__' ? 'SIN ASIGNAR' : groupStr;
|
||||
|
||||
let totalUnidades = 0;
|
||||
let totalPeso = 0;
|
||||
rows.data().each(function (row) {
|
||||
const unidades = parseInt(row.unidadesEnvio) || 0;
|
||||
const unidades = parseInt(row.unidadesEnvioRaw) || 0;
|
||||
totalUnidades += unidades;
|
||||
totalPeso += parseFloat(row.pesoUnidad) * unidades || 0;
|
||||
totalPeso += (parseFloat(row.pesoUnidad) || 0) * unidades;
|
||||
});
|
||||
|
||||
return `
|
||||
<label class="switch switch-square">
|
||||
<input type="checkbox" class="switch-input switch-grupo" data-grupo="${nombreGrupo}" id="switch-grupo-${nombreGrupo}">
|
||||
<input type="checkbox" class="switch-input switch-grupo" data-grupo="${groupStr}" id="switch-grupo-${groupStr}">
|
||||
<span class="switch-toggle-slider"></span>
|
||||
<span class="switch-label">
|
||||
CAJA: ${nombreGrupo} [unidades: ${totalUnidades}, peso: <span class="peso-grupo" data-valor="${totalPeso}">${totalPeso.toFixed(1)}</span> kg]
|
||||
CAJA: ${nombreGrupo} [unidades: ${totalUnidades}, peso: <span class="peso-grupo" data-valor="${totalPeso}">${totalPeso.toFixed(1)}</span> kg]
|
||||
</span>
|
||||
</label>
|
||||
`;
|
||||
`;
|
||||
}
|
||||
|
||||
},
|
||||
"columnDefs": [
|
||||
{
|
||||
"targets": [0, 8],
|
||||
"targets": [0],
|
||||
"className": "text-center",
|
||||
"orderable": false,
|
||||
"searchable": false,
|
||||
},
|
||||
{
|
||||
"targets": [2, 3, 5, 6, 7],
|
||||
"targets": [3, 4, 6, 7, 8],
|
||||
"className": "text-center",
|
||||
},
|
||||
{
|
||||
targets: [1, 9, 10],
|
||||
targets: [1, 9, 10, 11],
|
||||
visible: false
|
||||
}
|
||||
],
|
||||
@ -105,9 +111,10 @@ class EnvioEdit {
|
||||
const rowData = this.data();
|
||||
const node = $(this.node());
|
||||
|
||||
const valorCaja = (rowData.cajas === null || rowData.cajas === '' || rowData.cajas === 0 || rowData.cajas === '0')
|
||||
? 'SIN ASIGNAR'
|
||||
: rowData.cajas;
|
||||
const valorCaja = (rowData.cajasRaw === null || rowData.cajasRaw === '' ||
|
||||
rowData.cajasRaw === 0 || rowData.cajasRaw === '0' || rowData.cajasRaw === undefined)
|
||||
? '__SIN__ASIGNAR__'
|
||||
: rowData.cajasRaw.toString();
|
||||
|
||||
if (valorCaja == grupo) {
|
||||
node.find('.checkbox-linea-envio').prop('checked', checked);
|
||||
@ -152,9 +159,9 @@ class EnvioEdit {
|
||||
const table = $('#tableLineasEnvio').DataTable();
|
||||
const rowData = table.row($row).data();
|
||||
|
||||
const valorCaja = (rowData.cajas === null || rowData.cajas === '' || rowData.cajas === 0 || rowData.cajas === '0')
|
||||
? 'SIN ASIGNAR'
|
||||
: rowData.cajas;
|
||||
const valorCaja = (rowData.cajasRaw === null || rowData.cajasRaw === '' || rowData.cajasRaw === 0 || rowData.cajasRaw === '0')
|
||||
? '__SIN__ASIGNAR__'
|
||||
: rowData.cajasRaw;
|
||||
|
||||
// 1. Filtrar todas las filas del mismo grupo
|
||||
let total = 0;
|
||||
@ -162,9 +169,9 @@ class EnvioEdit {
|
||||
|
||||
table.rows().every(function () {
|
||||
const data = this.data();
|
||||
const grupo = (data.cajas === null || data.cajas === '' || data.cajas === 0 || data.cajas === '0')
|
||||
const grupo = (data.cajasRaw === null || data.cajasRaw === '' || data.cajasRaw === 0 || data.cajasRaw === '0')
|
||||
? 'SIN ASIGNAR'
|
||||
: data.cajas;
|
||||
: data.cajasRaw;
|
||||
|
||||
if (grupo == valorCaja) {
|
||||
total++;
|
||||
@ -184,72 +191,49 @@ class EnvioEdit {
|
||||
});
|
||||
});
|
||||
|
||||
$(document).on('click', '.btn-edit', (e) => {
|
||||
$(document).on('change', '.input-lineas', (e) => {
|
||||
const table = this.table;
|
||||
const id = $(e.currentTarget).data('id');
|
||||
const row = table.row($(e.currentTarget).closest('tr'));
|
||||
const rowData = row.data();
|
||||
const fieldName = $(e.currentTarget).data('name');
|
||||
const fieldValue = $(e.currentTarget).val();
|
||||
|
||||
Swal.fire({
|
||||
title: 'Editar unidades a enviar',
|
||||
input: 'number',
|
||||
inputLabel: `Unidades actuales: ${rowData.unidadesEnvio}`,
|
||||
inputValue: rowData.unidadesEnvio,
|
||||
inputAttributes: {
|
||||
min: 1
|
||||
},
|
||||
showCancelButton: true,
|
||||
confirmButtonText: 'Guardar',
|
||||
cancelButtonText: 'Cancelar',
|
||||
inputValidator: (value) => {
|
||||
if (!value || parseInt(value) <= 0) {
|
||||
return 'Debes ingresar un valor mayor a 0';
|
||||
}
|
||||
},
|
||||
customClass: {
|
||||
confirmButton: 'btn btn-primary me-1',
|
||||
cancelButton: 'btn btn-secondary'
|
||||
},
|
||||
buttonsStyling: false
|
||||
}).then((result) => {
|
||||
if (result.isConfirmed) {
|
||||
const nuevasUnidades = parseInt(result.value);
|
||||
|
||||
$.post('/logistica/updateUnidadesEnvio', {
|
||||
id: rowData.id,
|
||||
unidades_envio: nuevasUnidades
|
||||
}, function (response) {
|
||||
if (response.status) {
|
||||
table.draw(false);
|
||||
} else {
|
||||
Swal.fire({
|
||||
title: 'Error',
|
||||
text: response.message,
|
||||
icon: 'error',
|
||||
showCancelButton: false,
|
||||
confirmButtonColor: '#3085d6',
|
||||
confirmButtonText: 'Ok',
|
||||
customClass: {
|
||||
confirmButton: 'btn btn-primary me-1',
|
||||
},
|
||||
buttonsStyling: false
|
||||
});
|
||||
}
|
||||
}).fail(() => {
|
||||
Swal.fire({
|
||||
title: 'Error',
|
||||
text: 'No se pudo actualizar el dato.',
|
||||
icon: 'error',
|
||||
showCancelButton: false,
|
||||
confirmButtonColor: '#3085d6',
|
||||
confirmButtonText: 'Ok',
|
||||
customClass: {
|
||||
confirmButton: 'btn btn-primary me-1',
|
||||
},
|
||||
buttonsStyling: false
|
||||
});
|
||||
$.post('/logistica/updateLineaEnvio', {
|
||||
id: rowData.id,
|
||||
"name": fieldName,
|
||||
'value': fieldValue
|
||||
}, function (response) {
|
||||
if (response.status) {
|
||||
table.draw(false);
|
||||
} else {
|
||||
Swal.fire({
|
||||
title: 'Error',
|
||||
text: response.message,
|
||||
icon: 'error',
|
||||
showCancelButton: false,
|
||||
confirmButtonColor: '#3085d6',
|
||||
confirmButtonText: 'Ok',
|
||||
customClass: {
|
||||
confirmButton: 'btn btn-primary me-1',
|
||||
},
|
||||
buttonsStyling: false
|
||||
});
|
||||
table.draw(false);
|
||||
}
|
||||
}).fail(() => {
|
||||
Swal.fire({
|
||||
title: 'Error',
|
||||
text: 'No se pudo actualizar el dato.',
|
||||
icon: 'error',
|
||||
showCancelButton: false,
|
||||
confirmButtonColor: '#3085d6',
|
||||
confirmButtonText: 'Ok',
|
||||
customClass: {
|
||||
confirmButton: 'btn btn-primary me-1',
|
||||
},
|
||||
buttonsStyling: false
|
||||
});
|
||||
table.draw(false);
|
||||
});
|
||||
});
|
||||
|
||||
@ -258,6 +242,8 @@ class EnvioEdit {
|
||||
this.btnAddLinea.on('click', this._addEnvioLinea.bind(this));
|
||||
this.btnAddCaja.on('click', this._addCaja.bind(this));
|
||||
this.btnDeleteLinea.on('click', this._deleteLineas.bind(this));
|
||||
this.btnGenerarAlbaran.on('click', this._generarAlbaran.bind(this));
|
||||
|
||||
this.btnGuardarComentarios.on('click', () => {
|
||||
|
||||
$.post('/logistica/updateComentariosEnvio', {
|
||||
@ -274,6 +260,127 @@ class EnvioEdit {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
this.btnSelectAll.on('click', () => {
|
||||
$('.switch-grupo').each(function () {
|
||||
const $switch = $(this);
|
||||
if (!$switch.is(':checked')) {
|
||||
$switch.prop('checked', true).trigger('change');
|
||||
} else {
|
||||
// incluso si ya está marcado, forzamos el evento por seguridad
|
||||
$switch.trigger('change');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
this._getAlbaranes();
|
||||
}
|
||||
|
||||
_getAlbaranes() {
|
||||
$.get('/albaranes/albaranesEnvio', {
|
||||
envio_id: $('#id').val(),
|
||||
}, function (response) {
|
||||
if (response.status && response.data) {
|
||||
|
||||
for (let i = 0; i < response.data.length; i++) {
|
||||
const albaran = response.data[i];
|
||||
new AlbaranComponent(albaran).mount('#contenedorAlbaranes');
|
||||
}
|
||||
} else {
|
||||
Swal.fire({
|
||||
title: 'Error',
|
||||
text: response.message,
|
||||
icon: 'error',
|
||||
showCancelButton: false,
|
||||
confirmButtonColor: '#3085d6',
|
||||
confirmButtonText: 'Ok',
|
||||
customClass: {
|
||||
confirmButton: 'btn btn-primary me-1',
|
||||
},
|
||||
buttonsStyling: false
|
||||
});
|
||||
}
|
||||
}).fail(() => {
|
||||
Swal.fire({
|
||||
title: 'Error',
|
||||
text: 'No se han podido obtener los albaranes.',
|
||||
icon: 'error',
|
||||
showCancelButton: false,
|
||||
confirmButtonColor: '#3085d6',
|
||||
confirmButtonText: 'Ok',
|
||||
customClass: {
|
||||
confirmButton: 'btn btn-primary me-1',
|
||||
},
|
||||
buttonsStyling: false
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
_generarAlbaran() {
|
||||
|
||||
// se seleccionan los id de las lineas de envio con checkbox marcado
|
||||
const table = this.table;
|
||||
const selectedRows = table.rows({ page: 'current' }).nodes().filter((node) => {
|
||||
const checkbox = $(node).find('.checkbox-linea-envio');
|
||||
return checkbox.is(':checked');
|
||||
});
|
||||
const ids = selectedRows.map((node) => {
|
||||
const rowData = table.row(node).data();
|
||||
return rowData.id;
|
||||
}
|
||||
).toArray();
|
||||
if (ids.length <= 0) {
|
||||
Swal.fire({
|
||||
title: 'Atención!',
|
||||
text: 'Debe seleccionar al menos una línea de envío para generar el albarán.',
|
||||
icon: 'info',
|
||||
showCancelButton: false,
|
||||
confirmButtonColor: '#3085d6',
|
||||
confirmButtonText: 'Ok',
|
||||
customClass: {
|
||||
confirmButton: 'btn btn-primary me-1',
|
||||
},
|
||||
buttonsStyling: false
|
||||
});
|
||||
return;
|
||||
}
|
||||
const idEnvio = $('#id').val();
|
||||
$.post('/albaranes/generarAlbaran', {
|
||||
envio_id: idEnvio,
|
||||
envio_lineas: ids
|
||||
}, function (response) {
|
||||
if (response.status && response.albaran) {
|
||||
|
||||
new AlbaranComponent(response.albaran).mount('#contenedorAlbaranes');
|
||||
} else {
|
||||
Swal.fire({
|
||||
title: 'Error',
|
||||
text: response.message,
|
||||
icon: 'error',
|
||||
showCancelButton: false,
|
||||
confirmButtonColor: '#3085d6',
|
||||
confirmButtonText: 'Ok',
|
||||
customClass: {
|
||||
confirmButton: 'btn btn-primary me-1',
|
||||
},
|
||||
buttonsStyling: false
|
||||
});
|
||||
}
|
||||
}).fail(() => {
|
||||
Swal.fire({
|
||||
title: 'Error',
|
||||
text: 'No se pudo generar el albarán.',
|
||||
icon: 'error',
|
||||
showCancelButton: false,
|
||||
confirmButtonColor: '#3085d6',
|
||||
confirmButtonText: 'Ok',
|
||||
customClass: {
|
||||
confirmButton: 'btn btn-primary me-1',
|
||||
},
|
||||
buttonsStyling: false
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
_deleteLineas() {
|
||||
@ -441,8 +548,8 @@ class EnvioEdit {
|
||||
$('.dtrg-group').each(function () {
|
||||
const $grupo = $(this);
|
||||
let nombreGrupo = $grupo.text().toUpperCase().replace('CAJA:', '').trim();
|
||||
if (nombreGrupo === 'SIN ASIGNAR') {
|
||||
nombreGrupo = null;
|
||||
if (nombreGrupo.includes('SIN ASIGNAR')) {
|
||||
nombreGrupo = '__SIN__ASIGNAR__';
|
||||
}
|
||||
|
||||
// Evitar aplicar múltiples veces
|
||||
@ -655,7 +762,23 @@ class EnvioEdit {
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
new EnvioEdit().init();
|
||||
const dropdown = document.querySelector(".dropdown-language");
|
||||
const activeItem = dropdown.querySelector(".dropdown-menu .dropdown-item");
|
||||
let locale = 'es';
|
||||
if (activeItem) {
|
||||
locale = activeItem.getAttribute("data-language");
|
||||
}
|
||||
|
||||
new Ajax('/translate/getTranslation', { locale: locale, translationFile: ['Albaran'] }, {},
|
||||
function (translations) {
|
||||
window.language = JSON.parse(translations);
|
||||
new EnvioEdit().init();
|
||||
},
|
||||
function (error) {
|
||||
console.log("Error getting translations:", error);
|
||||
}
|
||||
).post();
|
||||
|
||||
});
|
||||
|
||||
export default EnvioEdit;
|
||||
Reference in New Issue
Block a user