authorizeTmsAccess('tms.carriers.view'); $business_id = $this->tmsUtil->getBusinessId(); if ($request->ajax()) { $query = Carrier::where('business_id', $business_id) ->with('contact:id,name') ->orderBy('name'); if ($request->boolean('active_only')) { $query->where('is_active', true); } $types = $this->tmsUtil->carrierTypes(); return DataTables::of($query) ->addColumn('contact_name', fn ($row) => $row->contact?->name ?? '—') ->editColumn('carrier_type', fn ($row) => $types[$row->carrier_type] ?? $row->carrier_type) ->editColumn('is_active', fn ($row) => $row->is_active ? __('tms::lang.active') : __('tms::lang.inactive')) ->addColumn('action', function ($row) { $html = '
'; if (auth()->user()->can('tms.carriers.update')) { $html .= ''; } if (auth()->user()->can('tms.carriers.delete')) { $html .= ' '; } $html .= '
'; return $html; }) ->rawColumns(['action']) ->make(true); } return view('tms::carriers.index'); } public function create() { $this->authorizeTmsAccess('tms.carriers.create'); $business_id = $this->tmsUtil->getBusinessId(); return view('tms::carriers.create', [ 'carrier_types' => $this->tmsUtil->carrierTypes(), 'suppliers' => $this->tmsUtil->getSuppliersDropdown($business_id), ]); } public function store(Request $request) { $this->authorizeTmsAccess('tms.carriers.create'); $business_id = $this->tmsUtil->getBusinessId(); $data = $request->validate([ 'name' => 'required|string|max:255', 'contact_id' => 'nullable|exists:contacts,id', 'phone' => 'nullable|string|max:50', 'email' => 'nullable|email|max:255', 'carrier_type' => 'required|string|max:50', 'notes' => 'nullable|string', 'is_active' => 'sometimes|boolean', ]); Carrier::create(array_merge($data, [ 'business_id' => $business_id, 'is_active' => $request->boolean('is_active', true), ])); return $this->tmsUtil->jsonOrRedirect($request, true, __('tms::lang.carrier_created')); } public function edit($id) { $this->authorizeTmsAccess('tms.carriers.update'); $business_id = $this->tmsUtil->getBusinessId(); $carrier = Carrier::where('business_id', $business_id)->findOrFail($id); return view('tms::carriers.edit', [ 'carrier' => $carrier, 'carrier_types' => $this->tmsUtil->carrierTypes(), 'suppliers' => $this->tmsUtil->getSuppliersDropdown($business_id), ]); } public function update(Request $request, $id) { $this->authorizeTmsAccess('tms.carriers.update'); $carrier = Carrier::where('business_id', $this->tmsUtil->getBusinessId())->findOrFail($id); $data = $request->validate([ 'name' => 'required|string|max:255', 'contact_id' => 'nullable|exists:contacts,id', 'phone' => 'nullable|string|max:50', 'email' => 'nullable|email|max:255', 'carrier_type' => 'required|string|max:50', 'notes' => 'nullable|string', 'is_active' => 'sometimes|boolean', ]); $carrier->update(array_merge($data, ['is_active' => $request->boolean('is_active')])); return $this->tmsUtil->jsonOrRedirect($request, true, __('tms::lang.carrier_updated')); } public function destroy(Request $request, $id) { $this->authorizeTmsAccess('tms.carriers.delete'); $carrier = Carrier::where('business_id', $this->tmsUtil->getBusinessId())->findOrFail($id); $carrier->delete(); return $this->tmsUtil->jsonOrRedirect($request, true, __('tms::lang.carrier_deleted')); } }