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