Added FG Line for Cumulative Chart for production line count

This commit is contained in:
dhanabalan
2025-05-28 17:26:20 +05:30
parent 3c70cd0260
commit 5e2ecd2460

View File

@@ -3,6 +3,8 @@
namespace App\Filament\Widgets; namespace App\Filament\Widgets;
use App\Models\Line; use App\Models\Line;
use App\Models\ProductionQuantity;
use App\Models\QualityValidation;
use Filament\Widgets\ChartWidget; use Filament\Widgets\ChartWidget;
class CumulativeChart extends ChartWidget class CumulativeChart extends ChartWidget
@@ -15,13 +17,77 @@ class CumulativeChart extends ChartWidget
protected int|string|array $columnSpan = 12; protected int|string|array $columnSpan = 12;
// protected function getData(): array
// {
// $selectedPlant = session('selected_plant');
// $activeFilter = $this->filter;
// // Define date range based on filter
// switch ($activeFilter) {
// case 'yesterday':
// $startDate = now()->subDay()->startOfDay();
// $endDate = now()->subDay()->endOfDay();
// break;
// case 'this_week':
// $startDate = now()->startOfWeek();
// $endDate = now()->endOfWeek();
// break;
// case 'this_month':
// $startDate = now()->startOfMonth();
// $endDate = now()->endOfMonth();
// break;
// default: // today
// $startDate = now()->startOfDay();
// $endDate = now()->endOfDay();
// break;
// }
// // Get all lines for selected plant
// $lines = Line::where('plant_id', $selectedPlant)
// ->pluck('name', 'id')
// ->toArray();
// // Get total production per line in the date range
// $production = \DB::table('production_quantities')
// ->select('line_id', \DB::raw('COUNT(*) as total_quantity'))
// ->whereBetween('created_at', [$startDate, $endDate])
// ->whereIn('line_id', array_keys($lines))
// ->groupBy('line_id')
// ->pluck('total_quantity', 'line_id')
// ->toArray();
// // Match quantities with lines (fill 0 if missing)
// $labels = [];
// $data = [];
// foreach ($lines as $lineId => $lineName) {
// $labels[] = $lineName;
// $data[] = $production[$lineId] ?? 0;
// }
// return [
// 'labels' => $labels,
// 'datasets' => [
// [
// 'label' => match ($activeFilter) {
// 'yesterday' => "Production Quantity (Yesterday)",
// 'this_week' => "Daily Production This Week",
// 'this_month' => "Weekly Production This Month",
// default => "Today's Production",
// },
// 'data' => $data,
// ],
// ],
// ];
// }
protected function getData(): array protected function getData(): array
{ {
$selectedPlant = session('selected_plant'); $selectedPlant = session('selected_plant');
$activeFilter = $this->filter; $activeFilter = $this->filter;
// Define date range based on filter // Define date range
switch ($activeFilter) { switch ($activeFilter)
{
case 'yesterday': case 'yesterday':
$startDate = now()->subDay()->startOfDay(); $startDate = now()->subDay()->startOfDay();
$endDate = now()->subDay()->endOfDay(); $endDate = now()->subDay()->endOfDay();
@@ -40,46 +106,90 @@ class CumulativeChart extends ChartWidget
break; break;
} }
// Get all lines for selected plant // Get lines with names and types
$lines = Line::where('plant_id', $selectedPlant) $lines = Line::where('plant_id', $selectedPlant)->get(['id', 'name', 'type']);
->pluck('name', 'id')
->toArray();
// Get total production per line in the date range $lineNames = [];
$production = \DB::table('production_quantities') $fgLineIds = [];
->select('line_id', \DB::raw('COUNT(*) as total_quantity')) $nonFgLineIds = [];
foreach ($lines as $line)
{
$lineNames[$line->id] = $line->name;
if ($line->type == 'FG Line')
{
$fgLineIds[] = $line->id;
}
else
{
$nonFgLineIds[] = $line->id;
}
}
//FG production from quality_validations
$fgProduction = QualityValidation::select('line_id', \DB::raw('COUNT(*) as total_quantity'))
->whereBetween('created_at', [$startDate, $endDate]) ->whereBetween('created_at', [$startDate, $endDate])
->whereIn('line_id', array_keys($lines)) ->whereIn('line_id', $fgLineIds)
->groupBy('line_id') ->groupBy('line_id')
->pluck('total_quantity', 'line_id') ->pluck('total_quantity', 'line_id')
->toArray(); ->toArray();
// Match quantities with lines (fill 0 if missing) //Non-FG production from production_quantities
$labels = []; $nonFgProduction = ProductionQuantity::select('line_id', \DB::raw('COUNT(*) as total_quantity'))
$data = []; ->whereBetween('created_at', [$startDate, $endDate])
->whereIn('line_id', $nonFgLineIds)
->groupBy('line_id')
->pluck('total_quantity', 'line_id')
->toArray();
foreach ($lines as $lineId => $lineName) { //Separate FG and non-FG into different datasets
$labels = [];
$fgData = [];
$nonFgData = [];
foreach ($lineNames as $lineId => $lineName) {
$labels[] = $lineName; $labels[] = $lineName;
$data[] = $production[$lineId] ?? 0;
if (in_array($lineId, $fgLineIds))
{
$fgData[] = $fgProduction[$lineId] ?? 0;
$nonFgData[] = null;
}
else
{
$nonFgData[] = $nonFgProduction[$lineId] ?? 0;
$fgData[] = null;
}
} }
return [ return [
'labels' => $labels, 'labels' => $labels,
'datasets' => [ 'datasets' => array_filter([
[ [
'label' => match ($activeFilter) { 'label' => match ($activeFilter) {
'yesterday' => "Production Quantity (Yesterday)", 'yesterday' => "Production Quantity (Yesterday)",
'this_week' => "Daily Production This Week", 'this_week' => "Daily Production This Week",
'this_month' => "Weekly Production This Month", 'this_month' => "Weekly Production This Month",
default => "Today's Production", default => "Today's Production",
}, },
'data' => $data, 'data' => $nonFgData,
//'backgroundColor' => '#3b82f6', // Blue for non-FG
], ],
], [
'label' => match ($activeFilter) {
'yesterday' => "FG Count (Yesterday)",
'this_week' => "FG Count This Week",
'this_month' => "FG Count This Month",
default => "Today's FG Count",
},
'data' => $fgData,
// 'backgroundColor' => '#ef4444', // Red for FG
],
]),
]; ];
} }
protected function getType(): string protected function getType(): string
{ {
return 'bar'; return 'bar';
@@ -106,7 +216,6 @@ class CumulativeChart extends ChartWidget
], ],
], ],
], ],
]; ];
} }