mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
Merge branch 'main' into fix/sk-7/bugs
This commit is contained in:
@ -498,4 +498,98 @@ class ClienteModel extends \App\Models\BaseModel
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public function getResumenPagos($cliente_id = -1){
|
||||
|
||||
$result = [];
|
||||
$data = $this->db->table('facturas f')
|
||||
->select('sum(f.pendiente) as total')
|
||||
->where('f.cliente_id', $cliente_id)
|
||||
->where('f.deleted_at IS NULL')
|
||||
->where('f.estado', 'validada')
|
||||
->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();
|
||||
$query = $this->db->getLastQuery();
|
||||
$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_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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user