76 lines
2.7 KiB
PHP
76 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets;
|
|
|
|
use App\Models\Producto;
|
|
use App\Models\Bodega;
|
|
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
|
|
use Filament\Widgets\StatsOverviewWidget\Stat;
|
|
|
|
class AlertasStockBodegasWidget extends BaseWidget
|
|
{
|
|
protected static bool $isLazy = false;
|
|
|
|
protected function getStats(): array
|
|
{
|
|
try {
|
|
$stats = [];
|
|
|
|
// Productos con stock bajo general
|
|
$productosStockBajo = Producto::where('estado', true)
|
|
->get()
|
|
->filter(function ($producto) {
|
|
return $producto->getStockEfectivo() <= $producto->stock_minimo;
|
|
})
|
|
->count();
|
|
|
|
$stats[] = Stat::make('Stock Bajo Global', $productosStockBajo)
|
|
->description('Productos bajo mínimo')
|
|
->descriptionIcon('heroicon-o-exclamation-triangle')
|
|
->color($productosStockBajo > 0 ? 'danger' : 'success');
|
|
|
|
// Productos con stock alto general
|
|
$productosStockAlto = Producto::where('estado', true)
|
|
->whereNotNull('stock_maximo')
|
|
->get()
|
|
->filter(function ($producto) {
|
|
return $producto->getStockEfectivo() > $producto->stock_maximo;
|
|
})
|
|
->count();
|
|
|
|
$stats[] = Stat::make('Stock Excesivo', $productosStockAlto)
|
|
->description('Productos sobre máximo')
|
|
->descriptionIcon('heroicon-o-arrow-trending-up')
|
|
->color($productosStockAlto > 0 ? 'warning' : 'success');
|
|
|
|
// Análisis por bodega crítica (si existe)
|
|
$bodegaPrincipal = Bodega::where('nombre', 'Principal')->first();
|
|
if ($bodegaPrincipal) {
|
|
$productosConStockCero = $bodegaPrincipal->productos()
|
|
->wherePivot('stock', 0)
|
|
->count();
|
|
|
|
$stats[] = Stat::make('Sin Stock en Principal', $productosConStockCero)
|
|
->description('Productos agotados')
|
|
->descriptionIcon('heroicon-o-x-circle')
|
|
->color($productosConStockCero > 0 ? 'danger' : 'success');
|
|
}
|
|
|
|
return $stats;
|
|
|
|
} catch (\Exception $e) {
|
|
// En caso de error, devolver estadísticas básicas
|
|
return [
|
|
Stat::make('Sistema de Alertas', 'Configurando...')
|
|
->description('Ejecute las migraciones necesarias')
|
|
->descriptionIcon('heroicon-o-cog')
|
|
->color('warning')
|
|
];
|
|
}
|
|
}
|
|
|
|
protected function getColumns(): int
|
|
{
|
|
return 3;
|
|
}
|
|
} |