ranjith-dev #1462
78
app/Filament/Pages/ProcessOrderRejection.php
Normal file
78
app/Filament/Pages/ProcessOrderRejection.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Pages;
|
||||
|
||||
use Filament\Pages\Page;
|
||||
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;
|
||||
use App\Models\Plant;
|
||||
use Filament\Facades\Filament;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Form;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class ProcessOrderRejection extends Page
|
||||
{
|
||||
protected static ?string $navigationIcon = 'heroicon-o-document-text';
|
||||
|
||||
protected static string $view = 'filament.pages.process-order-rejection';
|
||||
|
||||
protected static ?string $navigationGroup = 'Process Order DashBoard';
|
||||
|
||||
use HasFiltersForm;
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
session()->forget(['selected_plant', 'selected_line']);
|
||||
$this->filtersForm->fill([
|
||||
'plant' => null,
|
||||
'line' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function filtersForm(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->statePath('filters') // Explicitly set where to store form data
|
||||
->schema([
|
||||
Select::make('plant')
|
||||
->label('Select Plant')
|
||||
->reactive()
|
||||
->options(function (callable $get) {
|
||||
$userHas = Filament::auth()->user()->plant_id;
|
||||
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray();
|
||||
})
|
||||
->afterStateUpdated(function ($state) {
|
||||
session(['selected_plant' => $state]);
|
||||
$this->triggerChartUpdate();
|
||||
}),
|
||||
// Line Filter
|
||||
Select::make('line')
|
||||
->options(function ($get) {
|
||||
$plantId = $get('plant');
|
||||
return $plantId ? Plant::find($plantId)->getLineNames()->pluck('name', 'id') : [];
|
||||
})
|
||||
->label('Select Line')
|
||||
->reactive()
|
||||
->afterStateUpdated(function ($state) {
|
||||
session(['selected_line' => $state]); // Store in session
|
||||
$this->triggerChartUpdate();
|
||||
}),
|
||||
])
|
||||
->columns(2);
|
||||
}
|
||||
|
||||
public function triggerChartUpdate(): void
|
||||
{
|
||||
$filters = $this->filtersForm->getState();
|
||||
|
||||
if (!empty($filters['plant']) && !empty($filters['line'])) {
|
||||
$this->dispatch('processOrderChart', filters: $filters);
|
||||
}
|
||||
}
|
||||
|
||||
public static function canAccess(): bool
|
||||
{
|
||||
return Auth::check() && Auth::user()->can('view process order rejection dashboard');
|
||||
}
|
||||
}
|
||||
165
app/Filament/Widgets/ProcessOrderChart.php
Normal file
165
app/Filament/Widgets/ProcessOrderChart.php
Normal file
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Models\CharacteristicValue;
|
||||
use Filament\Widgets\ChartWidget;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
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 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 getFilters(): ?array
|
||||
{
|
||||
return [
|
||||
'today' => 'Today',
|
||||
'yesterday' => 'Yesterday',
|
||||
'week' => 'This Week',
|
||||
'month' => 'This Month',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -290,6 +290,7 @@ class PermissionSeeder extends Seeder
|
||||
Permission::updateOrCreate(['name' => 'view export courier outward']);
|
||||
|
||||
Permission::updateOrCreate(['name' => 'view test certificate page']);
|
||||
Permission::updateOrCreate(['name' => 'view process order rejection dashboard']);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<x-filament-panels::page>
|
||||
<div class="space-y-4">
|
||||
{{-- Render the Select form fields --}}
|
||||
<div class="space-y-4">
|
||||
{{ $this->filtersForm($this->form) }}
|
||||
</div>
|
||||
|
||||
{{-- Render the chart widget below the form --}}
|
||||
<div class="mt-6">
|
||||
@livewire(\App\Filament\Widgets\ProcessOrderChart::class)
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</x-filament-panels::page>
|
||||
Reference in New Issue
Block a user