From f5c23e007c9f7ad2a1f64429b8f9dc67ad5a01ed Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Tue, 3 Nov 2026 11:44:41 +0530 Subject: [PATCH] Added production plan target page --- app/Filament/Pages/ProductionPlanTarget.php | 383 ++++++++++++++++++ .../pages/production-plan-target.blade.php | 48 +++ 2 files changed, 431 insertions(+) create mode 100644 app/Filament/Pages/ProductionPlanTarget.php create mode 100644 resources/views/filament/pages/production-plan-target.blade.php diff --git a/app/Filament/Pages/ProductionPlanTarget.php b/app/Filament/Pages/ProductionPlanTarget.php new file mode 100644 index 00000000..a9ac305d --- /dev/null +++ b/app/Filament/Pages/ProductionPlanTarget.php @@ -0,0 +1,383 @@ +statePath('filters') + ->schema([ + Section::make('') + ->schema([ + Select::make('year') + ->label('Year') + ->reactive() + // ->searchable() + ->options([ + '2025' => '2025', + '2026' => '2026', + '2027' => '2027', + '2028' => '2028', + '2029' => '2029', + '2030' => '2030', + '2031' => '2031', + '2032' => '2032', + '2033' => '2033', + '2034' => '2034', + '2035' => '2035', + '2036' => '2036', + '2037' => '2037', + '2038' => '2038', + '2039' => '2039', + '2040' => '2040', + ]) + ->required() + ->afterStateUpdated(function ($state, callable $get, $set) { + $set('month', null); + $set('plant_id', null); + $set('line_id', null); + $set('category', null); + $plantId = $get('plant_id'); + $lineId = $get('line_id'); + // $this->dispatch('loadData',$plantId, $lineId, $state, ''); + $this->dispatch('loadData',$state, '', '', '', ''); + }), + + Select::make('month') + ->label('Month') + ->reactive() + // ->searchable() + ->options([ + '01' => 'January', + '02' => 'February', + '03' => 'March', + '04' => 'April', + '05' => 'May', + '06' => 'June', + '07' => 'July', + '08' => 'August', + '09' => 'September', + '10' => 'October', + '11' => 'November', + '12' => 'December', + ]) + ->required() + ->afterStateUpdated(function ($state, callable $get, $set) { + + $set('plant_id', null); + $set('line_id', null); + $set('category', null); + + $year = $get('year'); + + $this->dispatch('loadData',$year, $state, '', '', ''); + + // $plantId = $get('plant_id'); + // $lineId = $get('line_id'); + // // $month = $get('month'); + // $year = $get('year'); + + // $month = (int) $get('month'); + + // if (!$month) { + // return; + // } + // $this->dispatch('loadData', $plantId, $lineId, $month, $year); + }), + Select::make('plant_id') + ->label('Plant') + ->relationship('plant', 'name') + ->reactive() + // ->searchable() + ->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(); + }) + ->required() + ->afterStateUpdated(function ($state, callable $get, $set) { + // dd($state); + $set('line_id', null); + $set('category', null); + $this->dispatch('loadData',$state, '', '', '', ''); + }), + Select::make('line_id') + ->label('Line') + ->required() + // ->relationship('line', 'name') + // ->searchable() + ->columnSpan(1) + ->options(function (callable $get) { + if (!$get('plant_id')) { + return []; + } + + return \App\Models\Line::where('plant_id', $get('plant_id')) + ->pluck('name', 'id') + ->toArray(); + }) + ->reactive() + ->afterStateUpdated(function ($state, callable $get, $set) { + $this->dispatch('loadData',$state, '', '', '', ''); + }), + // ->afterStateUpdated(function ($state, callable $get, $set) { + // $plantId = $get('plant_id'); + // $lineId = $state; + // // $month = $get('month'); + // $year = $get('year'); + + // $month = (int) $get('month'); + // $this->dispatch('loadData',$year, $month, $plantId, $lineId, ''); + // }), + + Select::make('week') + ->label('Choose Week') + ->reactive() + // ->searchable() + ->options([ + 'all_weeks' => 'All Weeks', + 'Week_1' => 'Week 1', + 'Week_2' => 'Week 2', + 'Week_3' => 'Week 3', + 'Week_4' => 'Week 4', + 'Week_5' => 'Week 5', + ]) + ->required() + ->afterStateUpdated(function ($state, callable $get, $set) { + $set('category', null); + + $year = $get('year'); + + $this->dispatch('loadData',$year, $state, '', '', ''); + + // $plantId = $get('plant_id'); + // $lineId = $get('line_id'); + // // $month = $get('month'); + // $year = $get('year'); + + // $month = (int) $get('month'); + + // if (!$month) { + // return; + // } + // $this->dispatch('loadData', $plantId, $lineId, $month, $year); + }), + + TextInput::make('category') + ->label('Category') + ->reactive() + ->afterStateUpdated(function ($state, callable $get, $set) { + $plantId = $get('plant_id'); + $lineId = $get('line_id'); + // $month = $get('month'); + $year = $get('year'); + + $month = (int) $get('month'); + $category = $get('category'); + + }), + + ]) + ->columns(6) + ]); + } + + public function export(){ + + $plantId = $this->filters['plant_id'] ?? null; + $lineId = $this->filters['line_id'] ?? null; + $year = $this->filters['year'] ?? null; + $month = $this->filters['month'] ?? null; + + if (! $plantId) { + Notification::make() + ->title('Plant') + ->body("please select plant to export data..!") + ->danger() + ->send(); + return; + } + else if (! $lineId) { + Notification::make() + ->title('Line') + ->body("please select line to export data..!") + ->danger() + ->send(); + return; + } + else if (! $year) { + Notification::make() + ->title('Year') + ->body("please select year to export data..!") + ->danger() + ->send(); + return; + } + else if (! $month) { + Notification::make() + ->title('Month') + ->body("please select month to export data..!") + ->danger() + ->send(); + return; + } + + $this->dispatch('loadData1' ,$plantId, $lineId, $year, $month); + + // $this->dispatch('loadDataMonthly' ,$plantId, $lineId, $year, $month); + } + + public function importDayPlan($filePath) + { + $plantId = $this->filters['plant_id'] ?? null; + $lineId = $this->filters['line_id'] ?? null; + $year = $this->filters['year'] ?? null; + $month = $this->filters['month'] ?? null; + $week = $this->filters['week'] ?? null; + + if (!$plantId || !$lineId || !$year || !$month || !$week) { + Notification::make() + ->title('Missing Filters') + ->body('Please select Plant, Line, Year, Month and Week.') + ->danger() + ->send(); + + return; + } + + try { + + $fullPath = Storage::disk('local')->path($filePath); + + Excel::import( + new DayPlanTargetImport( + $plantId, + $lineId, + $year, + $month, + $week + ), + $fullPath + ); + + Notification::make() + ->title('Import Successful') + ->body('Selected week daily targets imported successfully.') + ->success() + ->send(); + + $this->dispatch('loadWeekData',$year, $month, $plantId, $lineId, $week); + + } catch (\Throwable $e) { + + Notification::make() + ->title('Import Failed') + ->body($e->getMessage()) + ->danger() + ->persistent() + ->send(); + } + } + + public function downloadSampleFormat() + { + $plantId = $this->filters['plant_id'] ?? null; + $lineId = $this->filters['line_id'] ?? null; + $year = $this->filters['year'] ?? null; + $month = $this->filters['month'] ?? null; + $week = $this->filters['week'] ?? null; + + if (!$plantId || !$lineId || !$year || !$month || !$week) { + + Notification::make() + ->title('Missing Filters') + ->body('Please select Plant, Line, Year, Month and Week.') + ->danger() + ->send(); + + return; + } + + return Excel::download( + new DayPlanSampleExport( + $plantId, + $lineId, + $year, + $month, + $week + ), + "Day_Plan_Sample_{$week}.xlsx" + ); + } + + public function generate(){ + $plantId = $this->filters['plant_id'] ?? null; + $lineId = $this->filters['line_id'] ?? null; + $year = $this->filters['year'] ?? null; + $month = (int) $this->filters['month'] ?? null; + $week = $this->filters['week'] ?? null; + $category = $this->filters['category'] ?? null; + + + if($week != '' && $week != null){ + $this->dispatch('loadWeekData',$year, $month, $plantId, $lineId, $week); + } + else if($category == '' || $category == null){ + $this->dispatch('loadData',$year, $month, $plantId, $lineId, ''); + } + else{ + $this->dispatch('loadDataCategory',$year, $month, $plantId, $lineId, $category); + } + + } + + public function importDayPlanAction(): Action + { + return Action::make('importDayPlan') + ->label('Import') + ->form([ + FileUpload::make('file') + ->label('Choose Excel File') + ->acceptedFileTypes([ + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + ]) + ->required() + ->disk('local') + ->directory('day-plan-imports'), + ]) + ->action(function (array $data) { + $this->importDayPlan($data['file']); + }); + } + + public static function canAccess(): bool + { + return Auth::check() && Auth::user()->can('view production plan target page'); + } +} diff --git a/resources/views/filament/pages/production-plan-target.blade.php b/resources/views/filament/pages/production-plan-target.blade.php new file mode 100644 index 00000000..993640f0 --- /dev/null +++ b/resources/views/filament/pages/production-plan-target.blade.php @@ -0,0 +1,48 @@ + +
+ + {{-- Render the Select form fields --}} +
+ {{ $this->form }} +
+ {{-- + Import + --}} + + + Download Sample Format + + + + Import + + + Export + + + Generate + +
+ +
+
+