1
0
forked from poc/pds

updated where between query logic in hourly chart

This commit is contained in:
dhanabalan
2025-04-27 17:06:48 +05:30
parent b5ede7af73
commit 79db0f03e5

View File

@@ -2,6 +2,7 @@
namespace App\Filament\Widgets;
use Filament\Notifications\Notification;
use Filament\Widgets\ChartWidget;
class ItemOverview extends ChartWidget
@@ -12,8 +13,21 @@ class ItemOverview extends ChartWidget
protected static ?string $maxHeight = '350px';
// protected $listeners = ['$refresh'];
// protected $listeners = ['filtersUpdated'];protected $listeners = ['plantUpdated'];
// protected $listeners = ['plantUpdated' => 'updatedPlantId'];
// public function updatedPlantId($plantId)
// {
// // This will automatically trigger getData() to rebuild the chart
// session()->put('select_plant', $plantId);
// dd($plantId);
// }
protected $listeners = ['filtersUpdated' => '$refresh'];
protected function getData(): array
{
$activeFilter = $this->filter;
@@ -21,7 +35,9 @@ class ItemOverview extends ChartWidget
$selectedPlant = session('selected_plant') ?? session('select_plant');
$selectedLine = session('selected_line') ?? session('select_line');
// dd($selectedPlant);
// dd($selectedPlant);
if (!$selectedPlant || !$selectedLine)
{
return [
@@ -30,6 +46,9 @@ class ItemOverview extends ChartWidget
];
}
//dd($selectedPlant, $selectedLine);
if ($activeFilter === 'yesterday') {
$startDate = now()->subDay()->setTime(8, 0, 0); // yesterday 8:00 AM
$endDate = now()->setTime(8, 0, 0); // today 8:00 AM
@@ -53,13 +72,16 @@ class ItemOverview extends ChartWidget
$startDate = now()->setTime(8, 0, 0); // today at 8:00 AM
$endDate = now()->copy()->addDay()->setTime(8, 0, 0); // tomorrow at 8:00 AM
$groupBy = 'EXTRACT(HOUR FROM production_quantities.created_at)';
}
$query = \DB::table('production_quantities')
->join('plants', 'production_quantities.plant_id', '=', 'plants.id') //inner join
->join('lines', 'production_quantities.line_id', '=', 'lines.id')
->selectRaw("$groupBy AS time_unit, count(*) AS total_quantity")
->whereBetween('production_quantities.created_at', [$startDate, $endDate])
//->whereBetween('production_quantities.created_at', [$startDate, $endDate])
->where('production_quantities.created_at', '>=', $startDate) // $startDate should be >=
->where('production_quantities.created_at', '<', $endDate) // $endDate should be <
->when($selectedPlant, function ($q) use ($selectedPlant) {
return $q->where('plants.id', $selectedPlant);
})
@@ -71,6 +93,8 @@ class ItemOverview extends ChartWidget
->orderByRaw($groupBy)
->pluck('total_quantity', 'time_unit')
->toArray();
if ($activeFilter === 'this_month') {
$weeksCount = ceil($endDate->day / 7); // Calculate total weeks dynamically
$allWeeks = array_fill(1, $weeksCount, 0); // Initialize all weeks with 0
@@ -111,6 +135,7 @@ class ItemOverview extends ChartWidget
}
else
{
// Hourly data (same as before)
$allHours = array_fill(0, 24, 0);
$data = array_replace($allHours, $query);
@@ -123,6 +148,7 @@ class ItemOverview extends ChartWidget
$labels = array_map(fn ($hour) => date("g A", strtotime("$hour:00")), $shiftedKeys);
}
return [
'datasets' => [
[