withCount(['users', 'machines']); // 搜尋 if ($search = $request->input('search')) { $query->where(function($q) use ($search) { $q->where('name', 'like', "%{$search}%") ->orWhere('code', 'like', "%{$search}%"); }); } // 狀態篩選 if ($request->filled('status')) { $query->where('status', $request->status); } $limit = $request->input('limit', 10); $companies = $query->latest()->paginate($limit)->withQueryString(); return view('admin.companies.index', compact('companies')); } /** * Store a newly created resource in storage. */ public function store(Request $request) { $validated = $request->validate([ 'name' => 'required|string|max:255', 'code' => 'required|string|max:50|unique:companies,code', 'tax_id' => 'nullable|string|max:50', 'contact_name' => 'nullable|string|max:255', 'contact_phone' => 'nullable|string|max:50', 'contact_email' => 'nullable|email|max:255', 'valid_until' => 'nullable|date', 'status' => 'required|boolean', 'note' => 'nullable|string', // 帳號相關欄位 (可選) 'admin_username' => 'nullable|string|max:255|unique:users,username', 'admin_password' => 'nullable|string|min:8', 'admin_name' => 'nullable|string|max:255', ]); DB::transaction(function () use ($validated) { $company = Company::create([ 'name' => $validated['name'], 'code' => $validated['code'], 'tax_id' => $validated['tax_id'] ?? null, 'contact_name' => $validated['contact_name'] ?? null, 'contact_phone' => $validated['contact_phone'] ?? null, 'contact_email' => $validated['contact_email'] ?? null, 'valid_until' => $validated['valid_until'] ?? null, 'status' => $validated['status'], 'note' => $validated['note'] ?? null, ]); // 如果有填寫帳號資訊,則建立管理員帳號 if (!empty($validated['admin_username']) && !empty($validated['admin_password'])) { $user = \App\Models\System\User::create([ 'company_id' => $company->id, 'username' => $validated['admin_username'], 'password' => \Illuminate\Support\Facades\Hash::make($validated['admin_password']), 'name' => $validated['admin_name'] ?: ($validated['contact_name'] ?: $validated['name']), 'status' => 1, ]); // 綁定客戶管理員角色 $user->assignRole('tenant-admin'); } }); return redirect()->back()->with('success', __('Customer created successfully.')); } /** * Update the specified resource in storage. */ public function update(Request $request, Company $company) { $validated = $request->validate([ 'name' => 'required|string|max:255', 'code' => 'required|string|max:50|unique:companies,code,' . $company->id, 'tax_id' => 'nullable|string|max:50', 'contact_name' => 'required|string|max:255', 'contact_phone' => 'nullable|string|max:50', 'contact_email' => 'nullable|email|max:255', 'valid_until' => 'nullable|date', 'status' => 'required|boolean', 'note' => 'nullable|string', ]); $company->update($validated); return redirect()->back()->with('success', __('Customer updated successfully.')); } /** * Remove the specified resource from storage. */ public function destroy(Company $company) { if ($company->users()->count() > 0) { return redirect()->back()->with('error', __('Cannot delete company with active accounts.')); } $company->delete(); return redirect()->back()->with('success', __('Customer deleted successfully.')); } }