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 @@
| = lang('Presupuestos.clienteId') ?> | ++ + | +
| Facturas sin pagar: | ++ + = $credito['total_facturas_sin_pagar'] ?> + + | +
| Facturas pagadas: | ++ + = $credito['total_facturas_pagadas'] ?> + + | +
| Pagos vencidos: | ++ + = $credito['total_facturas_vencidas'] ?> + + | +
| Pedidos produccion: | ++ + = $credito['total_pedidos_produccion'] ?> + + | +
| Pedidos finalizados: | ++ + = $credito['total_pedidos_finalizados'] ?> + + | +
| Total pendiente: | ++ + = $credito['total_pendiente'] ?> + + | +
| Límite crédito: | ++ + = $credito['limite_credito'] ?> + + | +
| Margen disponible: | ++ + = $credito['margen_disponible'] ?> + + | +