mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
387 lines
14 KiB
JavaScript
387 lines
14 KiB
JavaScript
import { getToken } from "../../../common/common.js";
|
|
|
|
class TiradasAlernativas {
|
|
|
|
constructor(functions = {}) {
|
|
|
|
this.getDimensionLibro = functions.getDimensionLibro;
|
|
|
|
this.csrf_token = getToken();
|
|
this.csrf_hash = $('#mainContainer').find('input[name="' + this.csrf_token + '"]').val();
|
|
|
|
this.addTirada = $('#add_tirada_alt');
|
|
this.tirada = $('#tirada_alt');
|
|
|
|
this.table = null;
|
|
}
|
|
|
|
init() {
|
|
|
|
const self = this;
|
|
|
|
this.addTirada.on('click', this.addTiradaAlt.bind(this));
|
|
|
|
this.table = $('#tableTiradas').DataTable({
|
|
|
|
"language": {
|
|
"url": "/themes/vuexy/vendor/libs/datatables-sk/plugins/i18n/es-ES.json"
|
|
},
|
|
"paging": false,
|
|
"info": false,
|
|
"searching": false,
|
|
"ordering": true,
|
|
"autoWidth": false,
|
|
"responsive": true,
|
|
"columnDefs": [
|
|
{
|
|
orderable: false,
|
|
searchable: false,
|
|
targets: [$('#tableTiradas').find("tr:first th").length - 1]
|
|
},
|
|
],
|
|
"columns": [
|
|
{ data: "tirada" },
|
|
{ data: 'coste_impresion' },
|
|
{
|
|
data: 'coste_envio',
|
|
render: function (data, type, row) {
|
|
return parseFloat(data).toFixed(2);
|
|
},
|
|
},
|
|
{
|
|
data: 'margen',
|
|
render: function (data, type, row) {
|
|
return data + '%';
|
|
}
|
|
},
|
|
{
|
|
data: 'total_pedido',
|
|
render: function (data, type, row) {
|
|
return parseFloat(data).toFixed(2);
|
|
}
|
|
},
|
|
{
|
|
data: 'precio_unidad',
|
|
render: function (data, type, row) {
|
|
return parseFloat(data).toFixed(4);
|
|
}
|
|
},
|
|
{
|
|
data: function (row, type, set, meta) {
|
|
return `
|
|
<a href="javascript:void(0);"><i class="ti ti-check ti-sm comfirm-tirada-alt mx-2" ></i></a>
|
|
<a href="javascript:void(0);"><i class="ti ti-trash ti-sm btn-delete-tirada mx-2" F></i></a>
|
|
`;
|
|
},
|
|
className: 'row-edit dt-center'
|
|
}
|
|
],
|
|
order: [[0, 'asc']]
|
|
});
|
|
|
|
// Evento de borrado de una fila de la tabla de tiradas
|
|
$(document).on('click', '.btn-delete-tirada', function (e) {
|
|
|
|
const row = $(this).closest('tr');
|
|
$('#tableTiradas').DataTable().row(row)
|
|
.remove()
|
|
.draw();
|
|
showBreadCrumbSaveButton(true);
|
|
})
|
|
|
|
$(document).on('click', '.comfirm-tirada-alt', function (e) {
|
|
|
|
const row = $(this).closest('tr');
|
|
const data = self.table.row(row).data();
|
|
|
|
self.table.row(row).remove().draw();
|
|
|
|
$('#tirada').val(data.tirada).trigger('change');
|
|
|
|
$(document).trigger('update-envios', { tirada_inicial: $('#tirada').val(), tirada: data.tirada });
|
|
});
|
|
|
|
$(document).on('update-tiradas-alternativas', async function () {
|
|
|
|
await self.updateTiradasAlternativas();
|
|
|
|
$(document).trigger('update-tiradas-alternativas-completed');
|
|
|
|
});
|
|
}
|
|
|
|
async updateTiradasAlternativas() {
|
|
|
|
const self = this;
|
|
|
|
if ($('#tableTiradas').DataTable().rows().count() > 0) {
|
|
// se obtiene la primera columna de la tabla de tiradas
|
|
const data = $('#tableTiradas').DataTable().rows().data().toArray();
|
|
// se borra la tabla de tiradas
|
|
$('#tableTiradas').DataTable().clear().draw();
|
|
const tiradas = data.map(row => row.tirada);
|
|
// por cada tirada, se añade una fila a la tabla de tiradas
|
|
tiradas.forEach(function (tirada) {
|
|
self.addTiradaAlt(tirada);
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
cargar(datos) {
|
|
|
|
if (datos != null && datos != undefined && datos.length > 0) {
|
|
this.table.clear().draw();
|
|
this.table.rows.add(datos).draw();
|
|
}
|
|
}
|
|
|
|
generate_json_tiradas() {
|
|
|
|
const data = this.table.rows().data().toArray();
|
|
return JSON.stringify(data);
|
|
}
|
|
|
|
|
|
addTiradaAlt(tiradaExterna = null) {
|
|
|
|
const self = this;
|
|
const id = window.location.href.split('/').pop();
|
|
|
|
const tirada = tiradaExterna === null || isNaN(tiradaExterna) ? parseInt(this.tirada.val()) : tiradaExterna;
|
|
const tirada_ejemplo = parseInt($('#tirada').val());
|
|
const POD = parseInt($('#POD').val());
|
|
|
|
if ((tirada_ejemplo > POD && tirada <= POD) ||
|
|
(tirada_ejemplo <= POD && tirada > POD)) {
|
|
popErrorAlert('No se pueden mezclar tiradas POD y no POD', 'alert-tiradas_alt');
|
|
return;
|
|
}
|
|
|
|
if (this.table.rows().count() > 0) {
|
|
let tiradas = this.table.rows().data().toArray();
|
|
if (tiradas.find(t => parseInt(t.tirada) == tirada)) {
|
|
popErrorAlert('La tirada ya existe', 'alert-tiradas_alt');
|
|
return;
|
|
}
|
|
}
|
|
|
|
const dimension = this.getDimensionLibro();
|
|
|
|
const envios = JSON.stringify($('#tableOfDireccionesEnvio').DataTable().rows().data().toArray());
|
|
|
|
let datos = {
|
|
id: id,
|
|
tirada: this.tirada.val(),
|
|
peso_libro: this.get_peso_libro(),
|
|
tirada_base: $('#tirada').val(),
|
|
paginas: $('#paginas').val(),
|
|
merma: this.getMerma(tirada),
|
|
tipo_impresion_id: $('#tipo_impresion_id').val(),
|
|
json_tiradas: this.generate_json_lp(),
|
|
json_acabados: this.generate_json_servicios('acabados'),
|
|
json_encuadernaciones: this.generate_json_servicios('encuadernaciones'),
|
|
json_manipulado: this.generate_json_servicios('manipulado'),
|
|
json_presimpresion: this.generate_json_servicios('presimpresion'),
|
|
json_extra: this.generate_json_servicios('extra'),
|
|
json_envios: envios,
|
|
ancho: dimension.ancho,
|
|
alto: dimension.alto,
|
|
cliente_id: $('#clienteId').find(":selected").val(),
|
|
solapas_cubierta: $('#solapas').is(':checked') ? 1 : 0,
|
|
solapas_cubierta_ancho: $('#solapas').is(':checked') ? parseInt($('#solapas_ancho').val()) : 0,
|
|
solapas_sobrecubierta: $('#solapas_sobrecubierta').is(':checked') ? 1 : 0,
|
|
solapas_sobrecubierta_ancho: $('#solapas_sobrecubierta').is(':checked') ? parseInt($('#solapas_ancho_sobrecubierta').val()) : 0,
|
|
lomo: $('#lomo_cubierta').val(),
|
|
faja_alto: $('#faja_alto').val(),
|
|
solapas_faja: $('#faja_solapas_ancho').val(),
|
|
};
|
|
|
|
$.ajax({
|
|
type: "POST",
|
|
url: '/presupuestotiradasalternativas/datatable_2',
|
|
data: datos,
|
|
success: async function (data) {
|
|
|
|
var coste_total = 0.0;
|
|
var margen_total = 0.0;
|
|
|
|
var costes_servicios = 0.0;
|
|
var margen_servicios = 0.0;
|
|
|
|
var coste_envio = 0.0;
|
|
var margen_envio = 0.0;
|
|
|
|
if (data.lineas.length > 0) {
|
|
// Se cogen los valores de la linea con los datos correspondientes
|
|
data.lineas.forEach(function (value) {
|
|
coste_total += value.total_coste;
|
|
margen_total += value.total_margen;
|
|
});
|
|
|
|
if (data.servicios) {
|
|
costes_servicios += data.servicios.coste;
|
|
margen_servicios += data.servicios.margen;
|
|
|
|
}
|
|
|
|
$('#tableOfServiciosPreimpresion').DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
var rowData = this.data();
|
|
costes_servicios += parseFloat(rowData.coste);
|
|
margen_servicios += parseFloat(rowData.precio) - parseFloat(rowData.coste);
|
|
});
|
|
|
|
$('#tableOfServiciosExtra').DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
var rowData = this.data();
|
|
costes_servicios += parseFloat(rowData.coste);
|
|
margen_servicios += parseFloat(rowData.precio) - parseFloat(rowData.coste);
|
|
});
|
|
|
|
if (data.envios) {
|
|
coste_envio += data.envios.coste;
|
|
margen_envio += data.envios.margen;
|
|
|
|
}
|
|
|
|
// Se añade la fila a la tabla de tiradas
|
|
self.table.row.add({
|
|
tirada: datos.tirada,
|
|
coste_impresion: coste_total.toFixed(2),
|
|
coste_envio: coste_envio.toFixed(2),
|
|
margen: ((margen_total + margen_servicios + margen_envio) / (coste_total + coste_envio) * 100.0).toFixed(2),
|
|
total_pedido: (coste_total + costes_servicios + coste_envio).toFixed(2),
|
|
precio_unidad: ((coste_total + costes_servicios + coste_envio) / datos.tirada).toFixed(4),
|
|
|
|
}).draw(false);
|
|
}
|
|
|
|
|
|
},
|
|
error: function (e) { }
|
|
});
|
|
}
|
|
|
|
generate_json_lp() {
|
|
|
|
const data = $('#tableLineasPresupuesto').DataTable().data().toArray();
|
|
|
|
let json = this.filterData(data,
|
|
['paginas',
|
|
'numPagColor',
|
|
'paginas_impresion',
|
|
'papel', // papel generico
|
|
'papel_impresion_id',
|
|
'gramaje',
|
|
'maquina_id',
|
|
'aFavorFibra',
|
|
'row_id',
|
|
'check_papel_total',
|
|
'check_impresion_total',
|
|
'cobAmarillo', // tipologia de papel
|
|
'cobCyan',
|
|
'cobMagenta',
|
|
'cobNegro',
|
|
'cobCG',
|
|
'gotaColor',
|
|
'gotaNegro']);
|
|
|
|
json.forEach(element => {
|
|
element['check_papel_total'] = $('#' + element['row_id'] + '_checkPapel').is(':checked') ? 1 : 0;
|
|
element['check_impresion_total'] = $('#' + element['row_id'] + '_checkClicks').is(':checked') ? 1 : 0;
|
|
});
|
|
return JSON.stringify(json);
|
|
}
|
|
|
|
generate_json_servicios(servicios) {
|
|
|
|
let json = [];
|
|
switch (servicios) {
|
|
case 'acabados':
|
|
$('#tableOfServiciosAcabado').DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
var rowData = this.data();
|
|
json.push(rowData);
|
|
});
|
|
break;
|
|
case 'encuadernaciones':
|
|
$('#tableOfServiciosEncuadernacion').DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
var rowData = this.data();
|
|
json.push(rowData);
|
|
});
|
|
break;
|
|
case 'presimpresion':
|
|
$('#tableOfServiciosPreimpresion').DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
var rowData = this.data();
|
|
json.push(rowData);
|
|
});
|
|
break;
|
|
case 'manipulado':
|
|
$('#tableOfServiciosManipulado').DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
var rowData = this.data();
|
|
json.push(rowData);
|
|
});
|
|
break;
|
|
case 'extra':
|
|
$('#tableOfServiciosExtra').DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
var rowData = this.data();
|
|
json.push(rowData);
|
|
});
|
|
break;
|
|
}
|
|
return JSON.stringify(json);
|
|
}
|
|
|
|
get_peso_libro() {
|
|
|
|
var peso_total_libro = 0.0
|
|
$('#tableLineasPresupuesto').DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
var rowData = this.data();
|
|
peso_total_libro += parseFloat(rowData.peso)
|
|
})
|
|
return peso_total_libro;
|
|
}
|
|
|
|
getMerma(tirada) {
|
|
|
|
let merma = 0;
|
|
|
|
if (tirada > parseInt($('#POD').val())) {
|
|
|
|
merma = tirada * 0.1 <= 30 ? tirada * 0.1 : 30
|
|
}
|
|
else {
|
|
let merma_lineas = []
|
|
$('#tableLineasPresupuesto').DataTable().rows().every(function (rowIdx, tableLoop, rowLoop) {
|
|
var rowData = this.data();
|
|
if (rowData.row_id != 'lp_guardas' && rowData.row_id != 'lp_cubierta' && rowData.row_id != 'lp_sobrecubierta') {
|
|
const formas_linea = $('#isCosido').val() == 0 ? parseInt(rowData.formas) : parseInt(rowData.formas) / 2
|
|
if (formas_linea > tirada)
|
|
merma_lineas.push(formas_linea - tirada)
|
|
else
|
|
merma_lineas.push(tirada % formas_linea)
|
|
}
|
|
})
|
|
|
|
if (merma_lineas.length > 0)
|
|
merma = Math.max(...merma_lineas)
|
|
else
|
|
merma = 0
|
|
}
|
|
return merma;
|
|
}
|
|
|
|
filterData(data, keys) {
|
|
|
|
return data.map(obj => {
|
|
|
|
const newObj = {};
|
|
keys.forEach(key => {
|
|
if (obj.hasOwnProperty(key)) {
|
|
newObj[key] = obj[key];
|
|
}
|
|
});
|
|
return newObj;
|
|
});
|
|
}
|
|
}
|
|
|
|
export default TiradasAlernativas; |