121 lines
4.6 KiB
PHP
121 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Park;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ReductionConfirmMasterController extends Controller
|
|
{
|
|
/**
|
|
* 減免確認マスタ画面を表示
|
|
*
|
|
* @param Request $request park_id をクエリパラメータで受け取る
|
|
* @return \Illuminate\View\View
|
|
*/
|
|
public function list(Request $request)
|
|
{
|
|
// park_id の検証
|
|
$request->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.list', [
|
|
'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',
|
|
'row_user_categoryid' => 'array',
|
|
'row_user_categoryid.*' => 'integer',
|
|
'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'];
|
|
|
|
// ログイン中のオペレータID取得
|
|
$opeId = auth()->user()->ope_id ?? null;
|
|
|
|
// POST された配列は index ベースで来るため、row_user_categoryid のインデックスに合わせてマッピングする
|
|
$rowUserCategory = $request->input('row_user_categoryid', []);
|
|
$types = $request->input('reduction_confirm_type', []);
|
|
|
|
try {
|
|
DB::transaction(function () use ($parkId, $rowUserCategory, $types, $opeId) {
|
|
foreach ($rowUserCategory as $idx => $userCategoryId) {
|
|
if (!isset($types[$idx])) {
|
|
continue;
|
|
}
|
|
$type = (int) $types[$idx];
|
|
|
|
DB::table('reduction_confirm')
|
|
->where('park_id', $parkId)
|
|
->where('user_categoryid', (int) $userCategoryId)
|
|
->update([
|
|
'reduction_confirm_type' => $type,
|
|
'updated_at' => now(),
|
|
'ope_id' => $opeId,
|
|
]);
|
|
}
|
|
});
|
|
|
|
return redirect()->route('reduction_confirm.list', ['park_id' => $parkId])
|
|
->with('success', '減免確認マスタを更新しました。');
|
|
} catch (\Exception $e) {
|
|
\Log::error('ReductionConfirm update failed', [
|
|
'park_id' => $parkId,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
|
|
return back()->withErrors(['error' => '更新に失敗しました。管理者にお問い合わせください。'])
|
|
->withInput();
|
|
}
|
|
}
|
|
}
|
|
|