diff --git a/app/Http/Controllers/Admin/ReductionConfirmMasterController.php b/app/Http/Controllers/Admin/ReductionConfirmMasterController.php new file mode 100644 index 0000000..90dacf7 --- /dev/null +++ b/app/Http/Controllers/Admin/ReductionConfirmMasterController.php @@ -0,0 +1,111 @@ +validate([ + 'park_id' => 'required|integer|exists:park,park_id', + ], [ + 'park_id.required' => '駐輪場IDは必須です。', + 'park_id.integer' => '駐輪場IDは整数である必要があります。', + 'park_id.exists' => '指定された駐輪場が見つかりません。', + ]); + + $parkId = (int) $request->input('park_id'); + + // 駐輪場情報を取得 + $park = Park::where('park_id', $parkId)->firstOrFail(); + + // reduction_confirm を主テーブルとして、usertype と JOIN して一覧を取得 + // WHERE park_id = ? で対象駐輪場のレコードのみ取得 + $reductionData = DB::table('reduction_confirm') + ->leftJoin('usertype', 'reduction_confirm.user_categoryid', '=', 'usertype.user_categoryid') + ->where('reduction_confirm.park_id', $parkId) + ->orderBy('reduction_confirm.user_categoryid', 'asc') + ->select( + 'reduction_confirm.park_id', + 'reduction_confirm.user_categoryid', + 'reduction_confirm.reduction_confirm_type', + 'usertype.usertype_subject1', + 'usertype.usertype_subject2', + 'usertype.usertype_subject3' + ) + ->paginate(50); + + return view('admin.reduction_confirm.index', [ + 'park' => $park, + 'parkId' => $parkId, + 'reductionData' => $reductionData, + ]); + } + + /** + * 減免確認情報を一括更新 + * + * @param Request $request + * @return \Illuminate\Http\RedirectResponse + */ + public function store(Request $request) + { + // バリデーション + $validated = $request->validate([ + 'park_id' => 'required|integer|exists:park,park_id', + 'reduction_confirm_type' => 'array', + 'reduction_confirm_type.*' => 'in:0,1,2', + ], [ + 'park_id.required' => '駐輪場IDは必須です。', + 'park_id.integer' => '駐輪場IDは整数である必要があります。', + 'park_id.exists' => '指定された駐輪場が見つかりません。', + 'reduction_confirm_type.*.in' => '減免確認種別は0, 1, 2 のいずれかである必要があります。', + ]); + + $parkId = (int) $validated['park_id']; + $types = $validated['reduction_confirm_type'] ?? []; + + // ログイン中のオペレータID取得 + $opeId = auth()->user()->ope_id ?? null; + + try { + // トランザクションで更新処理を実行 + DB::transaction(function () use ($parkId, $types, $opeId) { + foreach ($types as $userCategoryId => $type) { + DB::table('reduction_confirm') + ->where('park_id', $parkId) + ->where('user_categoryid', (int) $userCategoryId) + ->update([ + 'reduction_confirm_type' => (int) $type, + 'updated_at' => now(), + 'ope_id' => $opeId, + ]); + } + }); + + return redirect()->route('reduction_confirm.index', ['park_id' => $parkId]) + ->with('success', '減免確認マスタを更新しました。'); + } catch (\Exception $e) { + \Log::error('ReductionConfirm update failed', [ + 'park_id' => $parkId, + 'error' => $e->getMessage(), + ]); + + return back()->withErrors(['error' => '更新に失敗しました。管理者にお問い合わせください。']) + ->withInput(); + } + } +} + diff --git a/app/Models/ReductionMaster.php b/app/Models/ReductionMaster.php new file mode 100644 index 0000000..e199364 --- /dev/null +++ b/app/Models/ReductionMaster.php @@ -0,0 +1,48 @@ +where('user_categoryid', $userCategoryId) + ->first(); + } + + /** + * レコード保存時に operator_id を自動設定 + */ + public static function boot() + { + parent::boot(); + self::saving(function (ReductionMaster $model) { + if (!isset($model->operator_id) || $model->operator_id === null) { + $model->operator_id = Auth::user()->ope_id ?? null; + } + }); + } +} diff --git a/resources/views/admin/parks/edit.blade.php b/resources/views/admin/parks/edit.blade.php index eb0718b..33efd9a 100644 --- a/resources/views/admin/parks/edit.blade.php +++ b/resources/views/admin/parks/edit.blade.php @@ -2,76 +2,79 @@ @section('title', '駐輪場マスタ 編集') @section('content') -{{-- ▼ パンくず --}} -