Files
pds/app/Filament/Widgets/ItemOverview.php
2025-03-25 15:02:25 +05:30

83 lines
2.4 KiB
PHP

<?php
namespace App\Filament\Widgets;
use Filament\Widgets\ChartWidget;
class ItemOverview extends ChartWidget
{
protected static ?string $heading = 'Chart';
protected int|string|array $columnSpan = 'full';
protected function getData(): array
{
$activeFilter = $this->filter;
// Get selected values from the plant and line filter form inputs
$selectedPlant = request()->input('plant'); // Assuming form input name is 'plant'
$selectedLine = request()->input('line'); // Assuming form input name is 'line'
$query = \DB::table('production_quantities')
->selectRaw('EXTRACT(HOUR FROM created_at) AS hour, COUNT(*) AS total_quantity')
->whereBetween('created_at', [now()->startOfDay(), now()->endOfDay()]);
// Apply filters only if values are selected
if (!empty($selectedPlant)) {
$query->where('plant', $selectedPlant);
}
if (!empty($selectedLine)) {
$query->where('line', $selectedLine);
}
$query = $query->groupByRaw('EXTRACT(HOUR FROM created_at)')
->orderByRaw('EXTRACT(HOUR FROM created_at)')
->pluck('total_quantity', 'hour')
->toArray();
$data = array_fill(8, 12, 0);
// Populate actual values
// foreach ($query as $record) {
// $hour = (int) $record->hour;
// if ($hour >= 8 && $hour <= 19) {
// $data[$hour] = $record->total_quantity; // Assign only the hourly production
// }
// }
// Convert data to chart format
return [
'datasets' => [
[
'label' => 'Hourly Production',
'data' => array_values($data), // Values only
'borderColor' => 'rgba(75, 192, 192, 1)',
'backgroundColor' => 'rgba(75, 192, 192, 0.2)',
'fill' => false, // No area fill, just the line
'tension' => 0.3, // Smooth curve
],
],
'labels' => array_map(fn($h) => ($h <= 11 ? "$h AM" : ($h == 12 ? "12 PM" : ($h - 12) . " PM")), array_keys($data)),
];
}
protected function getType(): string
{
return 'line';
}
protected function getFilters(): ?array
{
return [
'today' => 'Today',
'week' => 'Last week',
'month' => 'Last month',
'year' => 'This year',
];
}
}