|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Web; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Models\Line; |
| 7 | +use App\Models\ProcessTemplate; |
| 8 | +use App\Models\ProductType; |
| 9 | +use App\Models\TemplateStep; |
| 10 | +use App\Services\WorkOrder\WorkOrderService; |
| 11 | +use Illuminate\Http\Request; |
| 12 | +use Illuminate\Support\Facades\DB; |
| 13 | + |
| 14 | +class OnboardingController extends Controller |
| 15 | +{ |
| 16 | + public function index() |
| 17 | + { |
| 18 | + if ($this->isCompleted()) { |
| 19 | + return redirect()->route('admin.dashboard'); |
| 20 | + } |
| 21 | + |
| 22 | + return redirect()->route('onboarding.step1'); |
| 23 | + } |
| 24 | + |
| 25 | + public function step1() |
| 26 | + { |
| 27 | + return view('onboarding.step1-line'); |
| 28 | + } |
| 29 | + |
| 30 | + public function storeStep1(Request $request) |
| 31 | + { |
| 32 | + $validated = $request->validate([ |
| 33 | + 'code' => 'required|string|max:50|unique:lines,code', |
| 34 | + 'name' => 'required|string|max:255', |
| 35 | + 'description' => 'nullable|string', |
| 36 | + ]); |
| 37 | + |
| 38 | + $line = Line::create([...$validated, 'is_active' => true]); |
| 39 | + $line->users()->attach(auth()->id()); |
| 40 | + |
| 41 | + $request->session()->put('onboarding.line_id', $line->id); |
| 42 | + |
| 43 | + return redirect()->route('onboarding.step2'); |
| 44 | + } |
| 45 | + |
| 46 | + public function step2(Request $request) |
| 47 | + { |
| 48 | + if (! $request->session()->has('onboarding.line_id')) { |
| 49 | + return redirect()->route('onboarding.step1'); |
| 50 | + } |
| 51 | + |
| 52 | + return view('onboarding.step2-product-type'); |
| 53 | + } |
| 54 | + |
| 55 | + public function storeStep2(Request $request) |
| 56 | + { |
| 57 | + $validated = $request->validate([ |
| 58 | + 'code' => 'required|string|max:50|unique:product_types,code', |
| 59 | + 'name' => 'required|string|max:255', |
| 60 | + 'unit_of_measure' => 'nullable|string|max:20', |
| 61 | + ]); |
| 62 | + |
| 63 | + $validated['unit_of_measure'] = $validated['unit_of_measure'] ?? 'pcs'; |
| 64 | + $validated['is_active'] = true; |
| 65 | + |
| 66 | + $productType = ProductType::create($validated); |
| 67 | + |
| 68 | + $lineId = $request->session()->get('onboarding.line_id'); |
| 69 | + if ($lineId) { |
| 70 | + Line::find($lineId)?->productTypes()->attach($productType->id); |
| 71 | + } |
| 72 | + |
| 73 | + $request->session()->put('onboarding.product_type_id', $productType->id); |
| 74 | + |
| 75 | + return redirect()->route('onboarding.step3'); |
| 76 | + } |
| 77 | + |
| 78 | + public function step3(Request $request) |
| 79 | + { |
| 80 | + if (! $request->session()->has('onboarding.product_type_id')) { |
| 81 | + return redirect()->route('onboarding.step1'); |
| 82 | + } |
| 83 | + |
| 84 | + return view('onboarding.step3-process-template'); |
| 85 | + } |
| 86 | + |
| 87 | + public function storeStep3(Request $request) |
| 88 | + { |
| 89 | + $validated = $request->validate([ |
| 90 | + 'name' => 'required|string|max:255', |
| 91 | + 'steps' => 'required|array|min:1', |
| 92 | + 'steps.*.name' => 'required|string|max:255', |
| 93 | + 'steps.*.estimated_duration_minutes' => 'nullable|integer|min:0', |
| 94 | + ]); |
| 95 | + |
| 96 | + $productTypeId = $request->session()->get('onboarding.product_type_id'); |
| 97 | + |
| 98 | + $template = ProcessTemplate::create([ |
| 99 | + 'product_type_id' => $productTypeId, |
| 100 | + 'name' => $validated['name'], |
| 101 | + 'version' => 1, |
| 102 | + 'is_active' => true, |
| 103 | + ]); |
| 104 | + |
| 105 | + foreach ($validated['steps'] as $i => $stepData) { |
| 106 | + TemplateStep::create([ |
| 107 | + 'process_template_id' => $template->id, |
| 108 | + 'step_number' => $i + 1, |
| 109 | + 'name' => $stepData['name'], |
| 110 | + 'estimated_duration_minutes' => $stepData['estimated_duration_minutes'] ?? null, |
| 111 | + ]); |
| 112 | + } |
| 113 | + |
| 114 | + $request->session()->put('onboarding.template_id', $template->id); |
| 115 | + |
| 116 | + return redirect()->route('onboarding.step4'); |
| 117 | + } |
| 118 | + |
| 119 | + public function step4(Request $request) |
| 120 | + { |
| 121 | + if (! $request->session()->has('onboarding.template_id')) { |
| 122 | + return redirect()->route('onboarding.step1'); |
| 123 | + } |
| 124 | + |
| 125 | + return view('onboarding.step4-work-order'); |
| 126 | + } |
| 127 | + |
| 128 | + public function storeStep4(Request $request, WorkOrderService $workOrderService) |
| 129 | + { |
| 130 | + $validated = $request->validate([ |
| 131 | + 'order_no' => 'required|string|max:100|unique:work_orders,order_no', |
| 132 | + 'planned_qty' => 'required|numeric|min:0.01', |
| 133 | + 'description' => 'nullable|string', |
| 134 | + ]); |
| 135 | + |
| 136 | + $workOrderService->createWorkOrder([ |
| 137 | + 'order_no' => $validated['order_no'], |
| 138 | + 'line_id' => $request->session()->get('onboarding.line_id'), |
| 139 | + 'product_type_id' => $request->session()->get('onboarding.product_type_id'), |
| 140 | + 'planned_qty' => $validated['planned_qty'], |
| 141 | + 'description' => $validated['description'] ?? null, |
| 142 | + ]); |
| 143 | + |
| 144 | + return redirect()->route('onboarding.complete'); |
| 145 | + } |
| 146 | + |
| 147 | + public function complete(Request $request) |
| 148 | + { |
| 149 | + $this->markCompleted(); |
| 150 | + $request->session()->forget('onboarding'); |
| 151 | + |
| 152 | + return view('onboarding.complete'); |
| 153 | + } |
| 154 | + |
| 155 | + public function skip(Request $request) |
| 156 | + { |
| 157 | + $this->markCompleted(); |
| 158 | + $request->session()->forget('onboarding'); |
| 159 | + |
| 160 | + return redirect()->route('admin.dashboard')->with('success', 'Onboarding skipped. You can re-launch it from Settings.'); |
| 161 | + } |
| 162 | + |
| 163 | + public static function shouldShowWizard(): bool |
| 164 | + { |
| 165 | + $completed = json_decode( |
| 166 | + DB::table('system_settings')->where('key', 'onboarding_completed')->value('value') ?? 'true', |
| 167 | + true |
| 168 | + ); |
| 169 | + |
| 170 | + return ! $completed && Line::count() === 0; |
| 171 | + } |
| 172 | + |
| 173 | + private function isCompleted(): bool |
| 174 | + { |
| 175 | + return ! self::shouldShowWizard(); |
| 176 | + } |
| 177 | + |
| 178 | + private function markCompleted(): void |
| 179 | + { |
| 180 | + DB::table('system_settings') |
| 181 | + ->where('key', 'onboarding_completed') |
| 182 | + ->update(['value' => json_encode(true)]); |
| 183 | + } |
| 184 | +} |
0 commit comments