From 0b2f2a446e9689720782f80d6b72a37fbaffbe80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Jim=C3=A9nez?= Date: Sat, 11 Jan 2025 18:17:41 +0100 Subject: [PATCH] =?UTF-8?q?a=C3=B1adido=20fichero=20servicios.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presupuestoAdmin/sections/servicios.js | 356 ++++++++++++++++++ 1 file changed, 356 insertions(+) create mode 100644 httpdocs/assets/js/safekat/pages/presupuestoAdmin/sections/servicios.js diff --git a/httpdocs/assets/js/safekat/pages/presupuestoAdmin/sections/servicios.js b/httpdocs/assets/js/safekat/pages/presupuestoAdmin/sections/servicios.js new file mode 100644 index 00000000..6c318376 --- /dev/null +++ b/httpdocs/assets/js/safekat/pages/presupuestoAdmin/sections/servicios.js @@ -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 ` + + `; + } + }, + { + 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 ` + + `; + } + } + ], + + 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 = ` + `; + } + }); + + $('#serv-acabado-error').html(htmlString) + } +} + +export default Servicios; \ No newline at end of file