Files
pds/app/Filament/Widgets/ProcessOrderChart.php
dhanabalan e4050c050f
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 14s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 15s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 21s
Laravel Larastan / larastan (pull_request) Failing after 6m17s
Laravel Pint / pint (pull_request) Successful in 4m54s
Added proper alignment for process order rejection chart
2026-11-18 12:03:28 +05:30

286 lines
9.4 KiB
PHP

<?php
namespace App\Filament\Widgets;
use App\Models\CharacteristicValue;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Facades\DB;
use Filament\Support\RawJs;
class ProcessOrderChart extends ChartWidget
{
protected static ?string $heading = 'Process Order - OK vs Not OK';
protected static ?int $sort = 2;
protected $listeners = ['processOrderChart'];
// protected function getData(): array
// {
// $activeFilter = $this->filter;
// $selectedPlant = session('selected_plant');
// $selectedLine = session('selected_line');
// // dd($selectedPlant, $selectedLine);
// if (!$selectedPlant || !$selectedLine) {
// return [
// 'datasets' => [],
// 'labels' => [],
// ];
// }
// if ($activeFilter == 'yesterday')
// {
// $startDate = now()->subDay()->setTime(8, 0, 0);
// $endDate = now()->setTime(8, 0, 0);
// $groupBy = "EXTRACT(HOUR FROM created_at)";
// }
// else if ($activeFilter == 'this_week')
// {
// $startDate = now()->startOfWeek()->setTime(8, 0, 0);
// $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0);
// $groupBy = "EXTRACT(DOW FROM created_at)";
// }
// else if ($activeFilter == 'this_month')
// {
// $startDate = now()->startOfMonth();
// $endDate = now()->endOfMonth();
// $groupBy = "FLOOR((EXTRACT(DAY FROM created_at) - 1) / 7) + 1";
// }
// else
// {
// $startDate = now()->setTime(8, 0, 0);
// $endDate = now()->copy()->addDay()->setTime(8, 0, 0);
// $groupBy = "EXTRACT(HOUR FROM created_at)";
// }
// // Get process orders for selected plant and line
// $processOrders = DB::table('process_orders')
// ->select('process_order')
// ->where('plant_id', $selectedPlant)
// ->where('line_id', $selectedLine)
// ->distinct();
// // Get OK and Not OK counts against each process order
// $records = CharacteristicValue::query()
// ->where('plant_id', $selectedPlant)
// ->whereIn('process_order', $processOrders)
// ->where('created_at', '>=', $startDate)
// ->where('created_at', '<', $endDate)
// ->selectRaw("
// process_order,
// SUM(CASE WHEN status = 'Ok' THEN 1 ELSE 0 END) as ok_count,
// SUM(CASE WHEN status = 'NotOk' THEN 1 ELSE 0 END) as not_ok_count
// ")
// ->groupBy('process_order')
// ->orderBy('process_order')
// ->get();
// return [
// 'datasets' => [
// [
// 'label' => 'OK',
// 'data' => $records->pluck('ok_count')
// ->map(fn ($v) => (int) $v)
// ->toArray(),
// 'backgroundColor' => '#16A34A',
// ],
// [
// 'label' => 'Not OK',
// 'data' => $records->pluck('not_ok_count')
// ->map(fn ($v) => (int) $v)
// ->toArray(),
// 'backgroundColor' => '#DC2626',
// ],
// ],
// 'labels' => $records->pluck('process_order')->toArray(),
// ];
// $records = CharacteristicValue::query()
// ->selectRaw("
// process_order,
// SUM(CASE WHEN status = 'Ok' THEN 1 ELSE 0 END) as ok_count,
// SUM(CASE WHEN status = 'NotOk' THEN 1 ELSE 0 END) as not_ok_count
// ")
// ->groupBy('process_order')
// ->orderBy('process_order')
// ->get();
// return [
// 'datasets' => [
// [
// 'label' => 'OK',
// 'data' => $records->pluck('ok_count')->map(fn ($v) => (int) $v)->toArray(),
// 'backgroundColor' => '#16A34A',
// ],
// [
// 'label' => 'Not OK',
// 'data' => $records->pluck('not_ok_count')->map(fn ($v) => (int) $v)->toArray(),
// 'backgroundColor' => '#DC2626',
// ],
// ],
// 'labels' => $records->pluck('process_order')->toArray(),
// ];
// }
protected function getData(): array
{
$activeFilter = $this->filter;
$selectedPlant = session('selected_plant');
$selectedLine = session('selected_line');
if (!$selectedPlant || !$selectedLine) {
return [
'datasets' => [],
'labels' => [],
];
}
if ($activeFilter == 'yesterday') {
$startDate = now()->subDay()->setTime(8, 0, 0);
$endDate = now()->setTime(8, 0, 0);
} else if ($activeFilter == 'this_week') {
$startDate = now()->startOfWeek()->setTime(8, 0, 0);
$endDate = now()->startOfWeek()->addWeek()->setTime(8, 0, 0);
} else if ($activeFilter == 'this_month') {
$startDate = now()->startOfMonth()->setTime(8, 0, 0);
$endDate = now()->addMonthNoOverflow()->startOfMonth()->setTime(8, 0, 0);
} else {
$startDate = now()->setTime(8, 0, 0);
$endDate = now()->copy()->addDay()->setTime(8, 0, 0);
}
$records = CharacteristicValue::query()
->where('plant_id', $selectedPlant)
->whereIn('process_order', function ($query) use ($selectedPlant, $selectedLine) {
$query->select('process_order')
->from('process_orders')
->where('plant_id', $selectedPlant)
->where('line_id', $selectedLine)
->distinct();
})
->where('created_at', '>=', $startDate)
->where('created_at', '<', $endDate)
->selectRaw("
SUM(CASE WHEN status = 'Ok' THEN 1 ELSE 0 END) as ok_count,
SUM(CASE WHEN status = 'NotOk' THEN 1 ELSE 0 END) as not_ok_count
")
->first();
$totalOrderQuantity = DB::table('process_orders')
->where('plant_id', $selectedPlant)
->where('line_id', $selectedLine)
->whereIn('process_order', function ($query) use ($selectedPlant, $selectedLine) {
$query->select('process_order')
->from('characteristic_values')
->where('plant_id', $selectedPlant)
->where('line_id', $selectedLine)
->distinct();
})
->where('created_at', '>=', $startDate)
->where('created_at', '<', $endDate)
->sum('order_quantity');
return [
'datasets' => [
[
'label' => 'Quantity',
'data' => [
(int) ($totalOrderQuantity ?? 0),
(int) ($records->ok_count ?? 0),
(int) ($records->not_ok_count ?? 0),
],
'backgroundColor' => [
'#3B82F6',
'#16A34A',
'#DC2626',
],
],
],
'labels' => ['Order Quantity', 'OK', 'Not OK'],
];
}
protected function getType(): string
{
return 'bar';
}
// protected function getOptions(): array
// {
// return [
// 'scales' => [
// 'x' => [
// 'stacked' => true,
// ],
// 'y' => [
// 'stacked' => true,
// 'beginAtZero' => true,
// ],
// ],
// 'plugins' => [
// 'legend' => [
// 'display' => true,
// 'position' => 'top',
// ],
// ],
// ];
// }
protected function getOptions(): array
{
return [
// 'plugins' => [
// 'datalabels' => false,
// ],
'plugins' => [
'datalabels' => [
'anchor' => 'start', // Positions the label relative to the top of the bar
'align' => 'start', // Aligns the label above the bar
'offset' => -15, // Adjust if needed (positive moves up, negative moves down)
'color' => '#000',
'font' => [
'weight' => 'bold',
],
'formatter' => RawJs::make('function(value) {
return value;
}'),
],
],
'scales' => [
'y' => [
'beginAtZero' => true, //Start Y-axis from 0
'ticks' => [
'stepSize' => 0.5,
],
],
],
];
}
protected function getFilters(): ?array
{
return [
'today' => 'Today',
'yesterday' => 'Yesterday',
'week' => 'This Week',
'month' => 'This Month',
];
}
}