mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
probando todo
This commit is contained in:
@ -309,9 +309,26 @@ class ClienteModel extends \App\Models\BaseModel
|
||||
/*
|
||||
TO-DO: Implementar la lógica de negocio para el crédito disponible
|
||||
*/
|
||||
public function creditoDisponible($cliente_id)
|
||||
public function creditoDisponible($cliente_id, $total_pedido = 0)
|
||||
{
|
||||
return true;
|
||||
$builder = $this->db
|
||||
->table($this->table . " t1")
|
||||
->select(
|
||||
"t1.limite_credito AS limite_credito"
|
||||
)
|
||||
->where("t1.is_deleted", 0)
|
||||
->where("t1.id", $cliente_id);
|
||||
$limite = $builder->get()->getResultObject();
|
||||
|
||||
if($limite){
|
||||
|
||||
$pendiente = $this->getPendienteCobro($cliente_id);
|
||||
$credito_disponible = floatval($limite[0]->limite_credito) - $pendiente[0] - $pendiente[1] - floatval($total_pedido);
|
||||
if($credito_disponible < 0)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getClienteDataFacturas($cliente_id)
|
||||
@ -403,34 +420,81 @@ class ClienteModel extends \App\Models\BaseModel
|
||||
|
||||
}
|
||||
|
||||
public function getPendienteCobro($cliente_id = -1){
|
||||
public function getPendienteCobro($cliente_id = -1)
|
||||
{
|
||||
|
||||
$pendiente_old = $this->getTotalPendienteOldERP($cliente_id);
|
||||
$db = \Config\Database::connect('default'); // Conectar a olderp
|
||||
$builder = $db->table('vista_importe_pendiente_cliente t1')
|
||||
->select('*')
|
||||
->where('t1.cliente_id', $cliente_id);
|
||||
|
||||
// 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 pendientes
|
||||
$subquery_total_pedidos = $this->db->table('pedidos p')
|
||||
->select('SUM(p.total_precio)', 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', 'finalizado', 'enviado'])
|
||||
->where("NOT EXISTS $subquery_facturas", null, false) // Implementación manual de NOT EXISTS
|
||||
->getCompiledSelect();
|
||||
|
||||
// Construcción de la consulta principal
|
||||
$builder = $this->db->table('(SELECT DISTINCT cliente_id FROM presupuestos UNION SELECT DISTINCT cliente_id FROM facturas) c');
|
||||
$builder->select('c.cliente_id');
|
||||
$builder->select('COALESCE(SUM(f.pendiente), 0) AS total_facturas');
|
||||
$builder->select("COALESCE(($subquery_total_pedidos), 0) AS total_pedidos", false);
|
||||
$builder->select("COALESCE(SUM(f.pendiente), 0) + COALESCE(($subquery_total_pedidos), 0) AS total_pendiente", false);
|
||||
$builder->join('facturas f', 'c.cliente_id = f.cliente_id AND f.deleted_at IS NULL AND f.estado = "validada"', 'left');
|
||||
$builder->where('c.cliente_id', $cliente_id);
|
||||
$builder->groupBy('c.cliente_id');
|
||||
|
||||
$query = $builder->get();
|
||||
$valor = $query->getRow();
|
||||
if($valor){
|
||||
return [floatval($valor->total_pendiente) , floatval($pendiente_old)];
|
||||
}else{
|
||||
return [0 , floatval($pendiente_old)];
|
||||
|
||||
if ($valor) {
|
||||
return [round(floatval($valor->total_pendiente), 2), round(floatval($pendiente_old), 2)];
|
||||
} else {
|
||||
return [0, round(floatval($pendiente_old), 2)];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function getTotalPendienteOldERP($customer_id = -1){
|
||||
private function getTotalPendienteOldERP($customer_id = -1)
|
||||
{
|
||||
|
||||
$db = \Config\Database::connect('old_erp'); // Conectar a olderp
|
||||
$builder = $db->table('safekat.facturas f');
|
||||
$builder->select('f.customer_id');
|
||||
$builder->select('SUM(f.pendiente) + COALESCE((
|
||||
SELECT SUM(pl.total_raw)
|
||||
FROM safekat.pedido_libro pl
|
||||
WHERE pl.factura_id IS NULL
|
||||
AND pl.estado IN ("aceptado", "finalizado", "validado")
|
||||
AND pl.total_raw IS NOT NULL
|
||||
AND pl.total_raw != 0
|
||||
AND pl.inc_rei IS NULL
|
||||
AND pl.customer_id = f.customer_id
|
||||
), 0) AS total_pendiente');
|
||||
$builder->where('f.deleted_at IS NULL');
|
||||
$builder->where('f.estado', 'open');
|
||||
$builder->where('f.customer_id', $customer_id);
|
||||
$builder->groupBy('f.customer_id');
|
||||
|
||||
$query = $builder->get();
|
||||
$valor = $query->getRow();
|
||||
/*
|
||||
$builder = $db->table('vista_importe_pendiente_cliente t1')
|
||||
->select('*')
|
||||
->where('t1.customer_id', $customer_id);
|
||||
$query = $builder->get();
|
||||
$valor = $query->getRow();
|
||||
if($valor){
|
||||
$valor = $query->getRow();*/
|
||||
if ($valor) {
|
||||
return $valor->total_pendiente;
|
||||
}else{
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user