añadida tabla de credito al presupuesto

This commit is contained in:
2025-03-29 11:31:03 +01:00
parent 8d289fde15
commit d1a9aaac77
10 changed files with 250 additions and 20 deletions

View File

@ -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;
}
}