up
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?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-m-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-m-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-m-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-m-cog-6-tooth')
|
||||
->color('warning')
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
protected function getColumns(): int
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user