updated where between query logic in hourly chart
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Widgets\ChartWidget;
|
||||
|
||||
class ItemOverview extends ChartWidget
|
||||
@@ -12,16 +13,31 @@ 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
|
||||
|
||||
protected function getData(): array
|
||||
{
|
||||
$activeFilter = $this->filter;
|
||||
|
||||
$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' => [
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user