This commit is contained in:
Lizandro Guarnizo
2026-01-06 15:35:59 -05:00
commit a768146f65
602 changed files with 42505 additions and 0 deletions
@@ -0,0 +1,127 @@
<?php
namespace App\Filament\Resources\InformeResource\Pages;
use App\Filament\Resources\InformeResource;
use App\Models\DetalleVenta;
use App\Models\Producto;
use Filament\Resources\Pages\ListRecords;
use Filament\Actions;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
class ListInformes extends ListRecords
{
protected static string $resource = InformeResource::class;
protected function getHeaderActions(): array
{
return [
Actions\Action::make('resumen')
->label('Ver Resumen')
->icon('heroicon-o-chart-pie')
->color('info')
->modalHeading('Resumen Ejecutivo - Promedio Ponderado')
->modalContent(function () {
return view('filament.pages.informe-resumen', [
'resumen' => $this->getResumenEjecutivo()
]);
})
->modalWidth('6xl'),
];
}
protected function getHeaderWidgets(): array
{
return [
InformeResource\Widgets\ResumenPromedioWidget::class,
];
}
/**
* Calcular resumen ejecutivo del período
*/
protected function getResumenEjecutivo(): array
{
$filtros = request()->get('tableFilters', []);
$fechaDesde = $filtros['fecha_rango']['fecha_desde'] ?? Carbon::now()->startOfMonth()->format('Y-m-d');
$fechaHasta = $filtros['fecha_rango']['fecha_hasta'] ?? Carbon::now()->endOfMonth()->format('Y-m-d');
$query = DetalleVenta::query()
->join('productos', 'detalle_ventas.producto_id', '=', 'productos.id')
->join('ventas', 'detalle_ventas.venta_id', '=', 'ventas.id')
->whereDate('ventas.created_at', '>=', $fechaDesde)
->whereDate('ventas.created_at', '<=', $fechaHasta);
// Cálculos principales
$totalProductosVendidos = $query->sum('detalle_ventas.cantidad');
$valorTotalCompra = $query->sum(DB::raw('detalle_ventas.cantidad * CAST(productos.precio_compra AS DECIMAL)'));
$valorTotalVenta = $query->sum(DB::raw('detalle_ventas.cantidad * detalle_ventas.precio_unitario'));
$promedioCompra = $totalProductosVendidos > 0
? $valorTotalCompra / $totalProductosVendidos
: 0;
$promedioVenta = $totalProductosVendidos > 0
? $valorTotalVenta / $totalProductosVendidos
: 0;
$margenTotal = $valorTotalCompra > 0
? (($valorTotalVenta - $valorTotalCompra) / $valorTotalCompra) * 100
: 0;
// Top productos por valor de compra
$topProductos = DetalleVenta::query()
->select([
'productos.nombre',
DB::raw('SUM(detalle_ventas.cantidad) as cantidad_vendida'),
DB::raw('SUM(detalle_ventas.cantidad * CAST(productos.precio_compra AS DECIMAL)) as valor_compra_total'),
'productos.precio_compra'
])
->join('productos', 'detalle_ventas.producto_id', '=', 'productos.id')
->join('ventas', 'detalle_ventas.venta_id', '=', 'ventas.id')
->whereDate('ventas.created_at', '>=', $fechaDesde)
->whereDate('ventas.created_at', '<=', $fechaHasta)
->groupBy('productos.id', 'productos.nombre', 'productos.precio_compra')
->orderByRaw('SUM(detalle_ventas.cantidad * CAST(productos.precio_compra AS DECIMAL)) desc')
->limit(10)
->get();
// Análisis por categoría
$categorias = DetalleVenta::query()
->select([
'categorias.nombre as categoria',
DB::raw('SUM(detalle_ventas.cantidad) as cantidad_vendida'),
DB::raw('SUM(detalle_ventas.cantidad * CAST(productos.precio_compra AS DECIMAL)) as valor_compra_total'),
DB::raw('AVG(CAST(productos.precio_compra AS DECIMAL)) as precio_compra_promedio'),
DB::raw('COUNT(DISTINCT productos.id) as productos_diferentes'),
])
->join('productos', 'detalle_ventas.producto_id', '=', 'productos.id')
->join('categorias', 'productos.categoria_id', '=', 'categorias.id')
->join('ventas', 'detalle_ventas.venta_id', '=', 'ventas.id')
->whereDate('ventas.created_at', '>=', $fechaDesde)
->whereDate('ventas.created_at', '<=', $fechaHasta)
->groupBy('categorias.id', 'categorias.nombre')
->orderByRaw('SUM(detalle_ventas.cantidad * CAST(productos.precio_compra AS DECIMAL)) desc')
->get();
return [
'periodo' => [
'fecha_desde' => Carbon::parse($fechaDesde)->format('d/m/Y'),
'fecha_hasta' => Carbon::parse($fechaHasta)->format('d/m/Y'),
'dias' => Carbon::parse($fechaDesde)->diffInDays(Carbon::parse($fechaHasta)) + 1,
],
'totales' => [
'productos_vendidos' => $totalProductosVendidos,
'valor_total_compra' => $valorTotalCompra,
'valor_total_venta' => $valorTotalVenta,
'promedio_compra' => $promedioCompra,
'promedio_venta' => $promedioVenta,
'margen_total' => $margenTotal,
'beneficio_total' => $valorTotalVenta - $valorTotalCompra,
],
'top_productos' => $topProductos,
'categorias' => $categorias,
];
}
}