From d1a9aaac77459915879d0a72a9a7364c42a58199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Jim=C3=A9nez?= Date: Sat, 29 Mar 2025 11:31:03 +0100 Subject: [PATCH] =?UTF-8?q?a=C3=B1adida=20tabla=20de=20credito=20al=20pres?= =?UTF-8?q?upuesto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Presupuestos/Presupuestoadmin.php | 2 + ci4/app/Controllers/Test.php | 13 +-- ci4/app/Language/es/Presupuestos.php | 2 + ci4/app/Models/Clientes/ClienteModel.php | 93 +++++++++++++++++ .../form/facturas/_pagosFacturasItems.php | 39 ++++++-- .../admin/_resumenCreditoItems.php | 99 +++++++++++++++++++ .../admin/_resumenPresupuestoItems.php | 2 +- .../admin/_tiradasAlternativasItems.php | 2 +- .../admin/viewPresupuestoadminForm.php | 14 ++- .../presupuestoAdmin/presupuestoAdminEdit.js | 4 + 10 files changed, 250 insertions(+), 20 deletions(-) create mode 100644 ci4/app/Views/themes/vuexy/form/presupuestos/admin/_resumenCreditoItems.php diff --git a/ci4/app/Controllers/Presupuestos/Presupuestoadmin.php b/ci4/app/Controllers/Presupuestos/Presupuestoadmin.php index 1814caca..35b372c2 100644 --- a/ci4/app/Controllers/Presupuestos/Presupuestoadmin.php +++ b/ci4/app/Controllers/Presupuestos/Presupuestoadmin.php @@ -418,6 +418,8 @@ class Presupuestoadmin extends \App\Controllers\BaseResourceController $this->model->removeIsDuplicado($presupuestoEntity->id); } + $this->viewData['credito'] = model('App\Models\Clientes\ClienteModel')->getResumenPagos($presupuestoEntity->cliente_id); + $this->viewData['boxTitle'] = lang('Basic.global.edit2') . ' ' . $this->viewData['pageTitle'] . ' ' . lang('Basic.global.edit3'); return $this->displayForm(__METHOD__, $id); diff --git a/ci4/app/Controllers/Test.php b/ci4/app/Controllers/Test.php index dfb723d3..42c01781 100755 --- a/ci4/app/Controllers/Test.php +++ b/ci4/app/Controllers/Test.php @@ -29,13 +29,14 @@ class Test extends BaseController } - private function index() + public function index() { - $this->emailService = service('emailService'); - - $a= $this->emailService->send('prueba', 'Esto es una prueba', ['imnavajas@coit.es','imnavajas@gmail.com']); - - echo var_dump($a); + $clienteModel = model('App\Models\Clientes\ClienteModel'); + $datos = $clienteModel->getResumenPagos(1870); + echo '
';
+        var_dump($datos);
+        echo '
'; + } diff --git a/ci4/app/Language/es/Presupuestos.php b/ci4/app/Language/es/Presupuestos.php index 3f73d4f5..b347eebe 100755 --- a/ci4/app/Language/es/Presupuestos.php +++ b/ci4/app/Language/es/Presupuestos.php @@ -332,6 +332,8 @@ return [ 'tiradaImpresion' => 'Coste Impresión', 'duplicado' => 'DUPLICADO', + 'credito' => 'Crédito', + 'duplicarConTipologias' => 'El presupuesto contiene lineas de presupuesto con datos de tipologías. Se va a duplicar el presupuesto con las mismas tipologías', 'presupuestoDuplicadoActualizacion' => 'El presupuesto ha sido creado duplicando un presupuesto existente. Se han actualizado los precios y las líneas de presupuesto con las tarifas actuales. Por favor, revíse la información.', diff --git a/ci4/app/Models/Clientes/ClienteModel.php b/ci4/app/Models/Clientes/ClienteModel.php index a09c6cd6..6a542e87 100755 --- a/ci4/app/Models/Clientes/ClienteModel.php +++ b/ci4/app/Models/Clientes/ClienteModel.php @@ -498,4 +498,97 @@ class ClienteModel extends \App\Models\BaseModel return 0; } } + + public function getResumenPagos($cliente_id = -1){ + + $result = []; + $data = $this->db->table('facturas f') + ->select('sum(f.total)-sum(f.pendiente) as total') + ->where('f.cliente_id', $cliente_id) + ->where('f.deleted_at IS NULL') + ->where('f.estado_pago', 'pendiente') + ->get() + ->getResultObject(); + $result['total_facturas_sin_pagar'] = + round(floatval(($data && $data[0]->total != null) ? $data[0]->total : 0), 2); + + $data = $this->db->table('facturas_pagos fp') + ->select('sum(fp.total) as total') + ->join('facturas f', 'fp.factura_id = f.id', 'left') + ->where('f.cliente_id', $cliente_id) + ->where('f.estado_pago', 'pendiente') + ->where('fp.fecha_pago_at IS NOT NULL') + ->where('fp.deleted_at IS NULL') + ->where('f.deleted_at IS NULL') + ->get() + ->getResultObject(); + $result['total_facturas_pagadas'] = + round(floatval(($data && $data[0]->total != null) ? $data[0]->total : 0), 2); + + $data = $this->db->table('facturas f') + ->select('sum(fp.total) as total') + ->join('facturas_pagos fp', 'fp.factura_id = f.id', 'left') + ->where('f.cliente_id', $cliente_id) + ->where('f.estado_pago', 'pendiente') + ->where('fp.fecha_vencimiento_at <', date('Y-m-d')) + ->where('fp.fecha_pago_at IS NULL') + ->where('f.deleted_at IS NULL') + ->where('fp.deleted_at IS NULL') + ->get() + ->getResultObject(); + $result['total_facturas_vencidas'] = + round(floatval(($data && $data[0]->total != null) ? $data[0]->total : 0), 2); + + // Subconsulta para verificar la existencia en facturas_lineas + $subquery_facturas = "(SELECT 1 + FROM facturas_lineas fl + JOIN facturas f2 ON fl.factura_id = f2.id + WHERE (fl.pedido_linea_impresion_id = p.id OR fl.pedido_maquetacion_id = p.id) + AND f2.estado = 'validada')"; + + + // Subconsulta para calcular el total de pedidos en produccion + $data = $this->db->table('pedidos p') + ->select('SUM(p.total_precio) as total', false) + ->join('pedidos_linea pl', 'p.id = pl.pedido_id', 'left') + ->join('presupuestos pr', 'pl.presupuesto_id = pr.id', 'left') + ->where('pr.cliente_id', $cliente_id) + ->whereIn('p.estado', ['produccion']) + ->where("NOT EXISTS $subquery_facturas", null, false) // Implementación manual de NOT EXISTS + ->get() + ->getResultObject(); + $result['total_pedidos_produccion'] = + round(floatval(($data && $data[0]->total != null) ? $data[0]->total : 0), 2); + + // Subconsulta para calcular el total de pedidos finalizados + $data = $this->db->table('pedidos p') + ->select('SUM(p.total_precio) as total', false) + ->join('pedidos_linea pl', 'p.id = pl.pedido_id', 'left') + ->join('presupuestos pr', 'pl.presupuesto_id = pr.id', 'left') + ->where('pr.cliente_id', $cliente_id) + ->whereIn('p.estado', ['finalizado', 'enviado']) + ->where("NOT EXISTS $subquery_facturas", null, false) // Implementación manual de NOT EXISTS + ->get() + ->getResultObject(); + $result['total_pedidos_finalizados'] = + round(floatval(($data && $data[0]->total != null) ? $data[0]->total : 0), 2); + + $result['total_pendiente'] = + $result['total_facturas_sin_pagar'] + - $result['total_facturas_pagadas'] + + $result['total_pedidos_produccion'] + + $result['total_pedidos_finalizados']; + $result['total_pendiente'] = round(floatval($result['total_pendiente']), 2); + + $result['limite_credito'] = $this->db->table('clientes') + ->select('limite_credito') + ->where('id', $cliente_id) + ->where('is_deleted', 0) + ->get() + ->getResultObject()[0]->limite_credito; + $result['limite_credito'] = round(floatval($result['limite_credito']), 2); + + $result['margen_disponible'] = round(floatval( $result['limite_credito']-$result['total_pendiente']), 2); + return $result; + } } diff --git a/ci4/app/Views/themes/vuexy/form/facturas/_pagosFacturasItems.php b/ci4/app/Views/themes/vuexy/form/facturas/_pagosFacturasItems.php index ec668aae..136d4181 100644 --- a/ci4/app/Views/themes/vuexy/form/facturas/_pagosFacturasItems.php +++ b/ci4/app/Views/themes/vuexy/form/facturas/_pagosFacturasItems.php @@ -22,8 +22,12 @@ - Total: - + Total cobrado: + + + + Total pendiente: + @@ -225,21 +229,38 @@ var tablePagos = $('#tableOfLineasPagos').DataTable({ footerCallback: function (row, data, start, end, display) { var api = this.api(); - // Remove the formatting to get integer data for summation var intVal = function (i) { + // Convertir a número o devolver 0 si no es válido return typeof i === 'string' ? i.replace(/[\$,]/g, '')*1 : typeof i === 'number' ? i : 0; }; - // Total over all pages - var totalPagos = api - .column(6) - .data() - .reduce(function (a, b) { - return intVal(a) + intVal(b); + var filas = []; + api.rows().every(function() { + var data = this.data(); + if (data['fecha_pago_at'] !== undefined && data['fecha_pago_at'] !== null && data['fecha_pago_at'] !== '') { + filas.push(data); // Agregar la fila a la variable + } + }); + var totalPagos = filas + .reduce(function(a, data) { + return a + intVal(data['total']); // Aquí asumimos que la columna 6 está en el índice 5 + }, 0); + + var filas = []; + api.rows().every(function() { + var data = this.data(); + if (data['fecha_pago_at'] === undefined || data['fecha_pago_at'] === null || data['fecha_pago_at'] === '') { + filas.push(data); // Agregar la fila a la variable + } + }); + var totalPendiente = filas + .reduce(function(a, data) { + return a + intVal(data['total']); // Aquí asumimos que la columna 6 está en el índice 5 }, 0); // Update footer $('#totalCobrado-sum').html(totalPagos.toFixed(2)); + $('#pendienteCobro-sum').html(totalPendiente.toFixed(2)); // call footerCallback of the other table if (typeof tableLineas !== 'undefined') { diff --git a/ci4/app/Views/themes/vuexy/form/presupuestos/admin/_resumenCreditoItems.php b/ci4/app/Views/themes/vuexy/form/presupuestos/admin/_resumenCreditoItems.php new file mode 100644 index 00000000..68df4c3f --- /dev/null +++ b/ci4/app/Views/themes/vuexy/form/presupuestos/admin/_resumenCreditoItems.php @@ -0,0 +1,99 @@ +
+
+

+ +

+ +
+
+ +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
Facturas sin pagar: + + + +
Facturas pagadas: + + + +
Pagos vencidos: + + + +
Pedidos produccion: + + + +
Pedidos finalizados: + + + +
Total pendiente: + + + +
Límite crédito: + + + +
Margen disponible: + + + +
+ +
+
+ +
+
+
+
\ No newline at end of file diff --git a/ci4/app/Views/themes/vuexy/form/presupuestos/admin/_resumenPresupuestoItems.php b/ci4/app/Views/themes/vuexy/form/presupuestos/admin/_resumenPresupuestoItems.php index 028d0fe5..240670f1 100644 --- a/ci4/app/Views/themes/vuexy/form/presupuestos/admin/_resumenPresupuestoItems.php +++ b/ci4/app/Views/themes/vuexy/form/presupuestos/admin/_resumenPresupuestoItems.php @@ -1,4 +1,4 @@ -
+