mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
añadido fichero servicios.js
This commit is contained in:
@ -0,0 +1,356 @@
|
||||
import Ajax from "../../../components/ajax.js";
|
||||
import { getToken } from "../../../common/common.js";
|
||||
import ClassSelect from "../../../components/select2.js";
|
||||
|
||||
class Servicios {
|
||||
|
||||
constructor() {
|
||||
|
||||
this.csrf_token = getToken();
|
||||
this.csrf_hash = $('#mainContainer').find('input[name="' + this.csrf_token + '"]').val();
|
||||
|
||||
this.serviciosAcabado = new ServiciosAcabado(this.csrf_token, this.csrf_hash);
|
||||
}
|
||||
|
||||
init() {
|
||||
|
||||
this.serviciosAcabado.init();
|
||||
|
||||
$('.nav-servicios button').on('shown.bs.tab', function () {
|
||||
this.serviciosAcabado.table.columns.adjust();
|
||||
/*$("#tableOfServiciosEncuadernacion").DataTable().columns.adjust();
|
||||
$("#tableOfServiciosPreimpresion").DataTable().columns.adjust();
|
||||
$("#tableOfServiciosManipulado").DataTable().columns.adjust();
|
||||
$("#tableOfServiciosAcabado").DataTable().columns.adjust();
|
||||
$("#tableOfServiciosExtra").DataTable().columns.adjust();*/
|
||||
})
|
||||
|
||||
$(document).on('add-servicio-lineas', this.addServicio.bind(this));
|
||||
$(document).on('remove-servicio-lineas', this.addServicio.bind(this));
|
||||
|
||||
}
|
||||
|
||||
cargar() {
|
||||
this.serviciosAcabado.cargarServiciosAcabado();
|
||||
}
|
||||
|
||||
addServicio(event, servicio) {
|
||||
|
||||
if (servicio == 'acabadoCubierta') {
|
||||
this.serviciosAcabado.updateAcabadosExteriores(1, 0);
|
||||
}
|
||||
else if (servicio == 'acabadoSobrecubierta') {
|
||||
this.serviciosAcabado.updateAcabadosExteriores(0, 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
removeServicio(servicio) {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
class ServiciosAcabado {
|
||||
|
||||
constructor(token, hash) {
|
||||
|
||||
this.csrf_token = token;
|
||||
this.csrf_hash = hash;
|
||||
|
||||
this.table = null;
|
||||
|
||||
this.selectorServicios = new ClassSelect($('#add_servicio_acabado_list'), '/tarifas/acabados/gettarifas', window.language.Presupuestos.servicioAcabadoList, false, {});
|
||||
this.addServicio = $('#insertar_serv_acabado');
|
||||
}
|
||||
|
||||
init() {
|
||||
|
||||
const self = this;
|
||||
|
||||
this.selectorServicios.init();
|
||||
|
||||
this.table = new DataTable('#tableOfServiciosAcabado', {
|
||||
scrollX: true,
|
||||
searching: false,
|
||||
paging: false,
|
||||
info: false,
|
||||
ordering: false,
|
||||
responsive: true,
|
||||
select: false,
|
||||
language: {
|
||||
url: "/themes/vuexy/vendor/libs/datatables-sk/plugins/i18n/es-ES.json"
|
||||
},
|
||||
columns: [
|
||||
{ data: 'tarifa_id' },
|
||||
{
|
||||
data: 'nombre', render: function (data, type, row) {
|
||||
if (row.cubierta == 1) {
|
||||
return row.nombre + ' (' + window.language.Presupuestos.cubierta + ')';
|
||||
}
|
||||
else if (row.sobrecubierta == 1) {
|
||||
return row.nombre + ' (' + window.language.Presupuestos.sobrecubierta + ')';
|
||||
}
|
||||
else {
|
||||
return row.nombre;
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
data: 'proveedor', render: function (data, type, row) {
|
||||
return `
|
||||
<select id="proveedor_acabado_${row.tarifa_id}" class="form-control select2bs proveedor_acabado" style="width: 100%;">
|
||||
<option selected value="${row.proveedor_id}">${row.proveedor}</option>
|
||||
</select>
|
||||
`;
|
||||
}
|
||||
},
|
||||
{
|
||||
data: 'precio_unidad', render: function (data, type, row) {
|
||||
let precio_unidad_coste = (row.precio_unidad / (1 + row.margen / 100)).toFixed(2);
|
||||
precio_unidad_coste = parseFloat(precio_unidad_coste).toLocaleString('es-ES', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
|
||||
const precio_unidad = parseFloat(row.precio_unidad).toLocaleString('es-ES', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
|
||||
return precio_unidad_coste + '/' + precio_unidad;
|
||||
}
|
||||
},
|
||||
{
|
||||
data: 'precio_total', render: function (data, type, row) {
|
||||
return parseFloat(data).toLocaleString('es-ES', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
|
||||
}
|
||||
},
|
||||
{ data: 'margen' },
|
||||
{ data: 'cubierta', visible: false },
|
||||
{ data: 'sobrecubierta', visible: false },
|
||||
{
|
||||
data: function (row) {
|
||||
return `
|
||||
<a href="javascript:void(0);"><i class="ti ti-trash ti-sm btn-delete-servacabados mx-2" data-id="${row.id}"></i></a>
|
||||
`;
|
||||
}
|
||||
}
|
||||
],
|
||||
|
||||
drawCallback: function (settings) {
|
||||
|
||||
$('.proveedor_acabado').select2({
|
||||
allowClear: false,
|
||||
minimumResultsForSearch: -1,
|
||||
ajax: {
|
||||
url: '/serviciosacabados/menuitems',
|
||||
type: 'post',
|
||||
dataType: 'json',
|
||||
|
||||
data: function (params) {
|
||||
|
||||
if (parseInt($('#tirada').val()) > 0) {
|
||||
var tirada = parseInt($('#tirada').val())
|
||||
}
|
||||
else {
|
||||
var tirada = 0
|
||||
}
|
||||
|
||||
var row = self.table.row($(this).closest('tr')).data();
|
||||
var return_data = {
|
||||
tarifa_id: row.tarifa_id,
|
||||
tirada: tirada,
|
||||
};
|
||||
return_data = Object.assign(return_data, window.token_ajax);
|
||||
|
||||
return return_data;
|
||||
},
|
||||
delay: 60,
|
||||
processResults: function (response) {
|
||||
yeniden(response[window.csrf_token]);
|
||||
return {
|
||||
results: response.menu
|
||||
};
|
||||
},
|
||||
cache: true
|
||||
}
|
||||
});
|
||||
|
||||
$('.proveedor_acabado').on('change.select2', function () {
|
||||
self.getPresupuestoAcabado(null, null, $(this));
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
$(document).on('click', '.btn-delete-servacabados', function () {
|
||||
const rowId = $(this).closest('td').parent()[0].sectionRowIndex;
|
||||
self.table.row(rowId).remove().draw();
|
||||
});
|
||||
|
||||
|
||||
|
||||
this.addServicio.on('click', this.addServicioAcabado.bind(this));
|
||||
|
||||
/*this.table.on('draw.dt', function () {
|
||||
|
||||
/* TO-DO
|
||||
updatePresupuesto({
|
||||
update_resumen: true,
|
||||
update_tiradas_alternativas: true})
|
||||
|
||||
}); */
|
||||
}
|
||||
|
||||
updateAcabadosExteriores(cubierta, sobrecubierta) {
|
||||
|
||||
this.table.rows().every(function () {
|
||||
var data = this.data();
|
||||
if (data.cubierta == cubierta && data.sobrecubierta == sobrecubierta) {
|
||||
|
||||
this.remove().draw();
|
||||
}
|
||||
});
|
||||
|
||||
let tarifa_id = 0;
|
||||
if (cubierta == 1) {
|
||||
tarifa_id = $('#acabado_cubierta_id').val();
|
||||
}
|
||||
else if (sobrecubierta == 1) {
|
||||
tarifa_id = $('#acabado_sobrecubierta_id').val();
|
||||
}
|
||||
|
||||
this.getPresupuestoAcabado(tarifa_id, cubierta == 1 ? 'cubierta' : 'sobrecubierta', null);
|
||||
}
|
||||
|
||||
getPresupuestoAcabado(tarifa_id = -1, uso = null, updateSelect = null) {
|
||||
|
||||
const self = this;
|
||||
|
||||
let tirada = 0
|
||||
if (parseInt($('#tirada').val()) > 0) {
|
||||
tirada = parseInt($('#tirada').val())
|
||||
}
|
||||
let datos = {
|
||||
tirada: tirada,
|
||||
};
|
||||
|
||||
if (updateSelect != null) {
|
||||
datos.tarifa_acabado_id = self.table.row(updateSelect.closest('tr')).data().tarifa_id;
|
||||
datos.proveedor_id = parseInt(updateSelect.val())
|
||||
}
|
||||
else {
|
||||
datos.tarifa_acabado_id = tarifa_id;
|
||||
}
|
||||
|
||||
if (datos.tarifa_acabado_id > 0) {
|
||||
new Ajax('/serviciosacabados/getvalues', datos, {},
|
||||
function (response) {
|
||||
if (response.values) {
|
||||
response.values[0].cubierta = 0;
|
||||
response.values[0].sobrecubierta = 0;
|
||||
if (uso == 'cubierta') {
|
||||
response.values[0].cubierta = 1;
|
||||
}
|
||||
else if (uso == 'sobrecubierta') {
|
||||
response.values[0].sobrecubierta = 1;
|
||||
}
|
||||
if (updateSelect != null) {
|
||||
|
||||
self.table.row(updateSelect.closest('tr')).data(response.values[0]).draw();
|
||||
}
|
||||
else {
|
||||
self.table.rows.add([response.values[0]]).draw();
|
||||
}
|
||||
}
|
||||
},
|
||||
function (error) {
|
||||
console.error(error);
|
||||
}
|
||||
).post();
|
||||
}
|
||||
}
|
||||
|
||||
addServicioAcabado() {
|
||||
|
||||
const tarifa_text = this.selectorServicios.getText();
|
||||
|
||||
if (tarifa_text.length > 0) {
|
||||
|
||||
var rows = this.table.rows().data().toArray();
|
||||
var found = rows.some(row => {
|
||||
const renderedText = row.nombre;
|
||||
if (row.cubierta == 1) {
|
||||
return renderedText === tarifa_text + ' (' + window.language.Presupuestos.cubierta + ')';
|
||||
} else if (row.sobrecubierta == 1) {
|
||||
return renderedText === tarifa_text + ' (' + window.language.Presupuestos.sobrecubierta + ')';
|
||||
} else {
|
||||
return renderedText === tarifa_text;
|
||||
}
|
||||
});
|
||||
|
||||
if (!found)
|
||||
this.getPresupuestoAcabado(this.selectorServicios.getVal());
|
||||
else
|
||||
popErrorAlert(window.language.Presupuestos.errores.error_servicios_duplicados, 'serv-acabado-alert');
|
||||
}
|
||||
|
||||
this.check_serv_acabado_error();
|
||||
|
||||
/* TO-DO
|
||||
|
||||
showBreadCrumbSaveButton(true);
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
cargarServiciosAcabado() {
|
||||
|
||||
const self = this;
|
||||
const id = window.location.href.split('/').pop();
|
||||
|
||||
new Ajax(
|
||||
'/serviciosacabados/cargar',
|
||||
{
|
||||
[this.csrf_token]: this.csrf_token,
|
||||
presupuesto_id: id
|
||||
},
|
||||
{},
|
||||
function (response) {
|
||||
if (response.rows.length > 0) {
|
||||
|
||||
self.table.clear().draw();
|
||||
self.table.rows.add(response.rows).draw();
|
||||
}
|
||||
},
|
||||
function (response) {
|
||||
console.error(response);
|
||||
}
|
||||
).get();
|
||||
}
|
||||
|
||||
guardarServiciosAcabado() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
check_serv_acabado_error() {
|
||||
|
||||
var htmlString = '';
|
||||
|
||||
// recorre las filas de la tabla de servicios acabado
|
||||
this.table.rows().every(function () {
|
||||
var data = this.data();
|
||||
if (data.precio_total == 0) {
|
||||
htmlString = `
|
||||
<div class="alert alert-danger d-flex align-items-baseline" role="alert">
|
||||
<span class="alert-icon alert-icon-lg text-primary me-2">
|
||||
<i class="ti ti-ban ti-sm"></i>
|
||||
</span>
|
||||
<div class="d-flex flex-column ps-1">
|
||||
<h5 class="alert-heading mb-2">` +
|
||||
window.language.Presupuestos.errores.error_servicios_anadidos +
|
||||
`</h5>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
});
|
||||
|
||||
$('#serv-acabado-error').html(htmlString)
|
||||
}
|
||||
}
|
||||
|
||||
export default Servicios;
|
||||
Reference in New Issue
Block a user