diff --git a/app/Http/Controllers/Admin/PriceController.php b/app/Http/Controllers/Admin/PriceController.php index 27a1404..86110bd 100644 --- a/app/Http/Controllers/Admin/PriceController.php +++ b/app/Http/Controllers/Admin/PriceController.php @@ -32,6 +32,8 @@ class PriceController extends Controller if ($inputs['list']->total() > 0 && $inputs['page'] > $inputs['list']->lastPage()) { return redirect()->route('prices'); } + $dataList = $this->getDataDropList(); + $inputs = array_merge($inputs, $dataList); return view('admin.prices.list', $inputs); } @@ -39,204 +41,278 @@ class PriceController extends Controller public function add(Request $request) { - $inputs = [ - 'price_parkplaceid' => $request->input('price_parkplaceid'), // 駐車場所ID - 'park_id' => $request->input('park_id'), // 駐輪場ID - 'prine_name' => $request->input('prine_name'), // 商品名 - 'price_month' => $request->input('price_month',''), // 期間 - 'user_categoryid' => $request->input('user_categoryid'), // 利用者分類ID - 'price' => $request->input('price'), // 駐輪料金(税込) - 'psection_id' => $request->input('psection_id'), // 車種区分ID - 'price_ptypeid' => $request->input('price_ptypeid'), // 駐輪分類ID - 'pplace_id' => $request->input('pplace_id'), // 駐車車室ID - ]; - - $dataList = $this->getDataDropList(); - $inputs = array_merge($inputs, $dataList); + // POST の場合のみバリデーション + 保存 if ($request->isMethod('POST')) { + + // ★ 1. 全角数字を半角に変換(例:123 → 123) + $request->merge([ + 'pplace_id' => mb_convert_kana($request->input('pplace_id'), 'n'), + ]); + + // バリデーション + $validated = $request->validate([ + 'price_parkplaceid' => 'required|integer', // 駐車場所ID + 'park_id' => 'required|integer', // 駐輪場ID + 'prine_name' => 'required|string|max:255', // 商品名 + 'price_month' => 'nullable|integer', // 期間(月) + 'user_categoryid' => 'required|integer', // 利用者分類ID + 'price' => 'required|numeric|min:0', // 駐輪料金(税込) + 'psection_id' => 'required|integer', // 車種区分ID + 'price_ptypeid' => 'required|integer', // 駐輪分類ID + 'pplace_id' => 'required|integer', // 駐車車室ID + ], [ + 'pplace_id.integer' => '駐車車室ID は整数で入力してください。', + ]); + + // DB 登録 $type = false; - \DB::transaction(function () use ($inputs, &$type) { + \DB::transaction(function () use ($validated, &$type) { $new = new Price(); - $new->fill($inputs); - if( $new->save()){ + $new->fill($validated); + if ($new->save()) { $type = true; } }); + + // 結果 if ($type) { - $request->session()->flash('success', __('新しい成功を創造する。')); - return redirect()->route('prices'); + return redirect()->route('prices') + ->with('success', __('新しい成功を創造する。')); } else { - $request->session()->flash('error', __('新しい作成に失敗しました')); + return redirect()->route('prices') + ->with('error', __('新しい作成に失敗しました。')); } } - return view('admin.prices.add', $inputs); + // GET の場合 → 画面表示 + $dataList = $this->getDataDropList(); + return view('admin.prices.add', $dataList); } - public function edit(Request $request, $pk ,$view=''){ + + public function edit(Request $request, $pk ,$view='') + { $price = Price::getByPk($pk); if (empty($pk) || empty($price)) { - abort('404'); + abort(404); } $data = $price->getAttributes(); $dataList = $this->getDataDropList(); $data = array_merge($data, $dataList); + if ($request->isMethod('POST')) { $type = false; $requestAll = [ 'price_parkplaceid' => $request->input('price_parkplaceid'), - 'park_id' => $request->input('park_id'), - 'prine_name' => $request->input('prine_name'), - 'price_month' => $request->input('price_month',''), - 'user_categoryid' => $request->input('user_categoryid'), - 'price' => $request->input('price'), - 'psection_id' => $request->input('psection_id'), - 'price_ptypeid' => $request->input('price_ptypeid'), - 'pplace_id' => $request->input('pplace_id'), + 'park_id' => $request->input('park_id'), + 'prine_name' => $request->input('prine_name'), + 'price_month' => $request->input('price_month',''), + 'user_categoryid' => $request->input('user_categoryid'), + 'price' => $request->input('price'), + 'psection_id' => $request->input('psection_id'), + 'price_ptypeid' => $request->input('price_ptypeid'), + 'pplace_id' => $request->input('pplace_id'), ]; $data = array_merge($data, $requestAll); - \DB::transaction(function () use ($data, &$type,$price) { + + \DB::transaction(function () use ($data, &$type, $price) { $price->fill($data); $price->save(); $type = true; }); + if ($type) { - $request->session()->flash('success', __('更新に成功しました')); - return redirect()->route('prices'); + return redirect()->route('prices')->with('success', __('更新に成功しました。')); } else { - $request->session()->flash('error', __('更新に失敗しました')); + return redirect()->route('prices')->with('error', __('更新に失敗しました。')); } } + if ($view != '') { return view($view, $data); } + return view('admin.prices.edit', $data); } + public function delete(Request $request) { $arr_pk = $request->get('pk'); + if ($arr_pk) { - if (Price::deleteByPk($arr_pk)) { - return redirect()->route('prices')->with('success', __("削除成功。")); + $ids = is_array($arr_pk) ? $arr_pk : [$arr_pk]; + + if (Price::deleteByPk($ids)) { + return redirect()->route('prices')->with('success', __("削除成功しました。")); } else { return redirect()->route('prices')->with('error', __('削除に失敗しました。')); } } + return redirect()->route('prices')->with('error', __('削除するユーザーを選択してください。')); } + public static function deleteByPk($ids) + { + if (!is_array($ids)) { + $ids = [$ids]; + } + return self::whereIn('price_parkplaceid', $ids)->delete(); + } public function export(Request $request) { - - $headers = array( + $headers = [ "Content-type" => "text/csv;charset=UTF-8", 'Content-Encoding: UTF-8', "Content-Disposition" => "attachment; filename=file.csv", "Pragma" => "no-cache", "Cache-Control" => "must-revalidate, post-check=0, pre-check=0", "Expires" => "0" - ); - $inputs = [ - 'isMethodPost' => 0, - 'isExport' => 1, - 'sort' => $request->input('sort', ''), - 'sort_type' => $request->input('sort_type', ''), - ]; - $dataExport = Price::search($inputs); - $columns = array( - __('駐車場所ID'),// 0 - __('商品名'),// 1 - __('期間'),// 2 - __('駐輪場ID'),// 3 - __('駐輪場名'),// 3 - __('車種区分ID'),// 5 - __('車種区分'),// 6 - __('駐輪分類ID'),// 7 - __('駐輪分類'),// 8 - __('利用者分類ID'),// 9 - __('利用者分類'),// 10 - __('駐車車室ID'),//11 - __('駐輪料金(税込)'),// 12 - ); - $filename = "駐輪場所、料金マスタ.csv"; + $query = Price::query(); + + // 🚩 条件付きエクスポート(park_id) + if ($request->filled('park_id')) { + $query->where('park_id', $request->input('park_id')); + } + + // ソート + if ($request->filled('sort')) { + $query->orderBy($request->input('sort'), $request->input('sort_type', 'asc')); + } + + $dataExport = $query->get(); + + $columns = [ + __('駐車場所ID'), + __('商品名'), + __('期間'), + __('駐輪場ID'), + __('駐輪場名'), + __('車種区分ID'), + __('車種区分'), + __('駐輪分類ID'), + __('駐輪分類'), + __('利用者分類ID'), + __('利用者分類'), + __('駐車車室ID'), + __('駐輪料金(税込)'), + ]; + + $filename = "駐輪場所、料金マスタ.csv"; $file = fopen($filename, 'w+'); fputcsv($file, $columns); + foreach ($dataExport as $items) { - fputcsv($file, array( - $items->price_parkplaceid,// 0 - $items->prine_name, // 1 - $items->price_month, // 2 - $items->park_id, // 3 - !empty($items->getPark())? $items->getPark()->park_name:'' ,// 4 - $items->psection_id, // 5 - !empty($items->getPSection())? $items->getPSection()->psection_subject:'',// 6 - $items->price_ptypeid, // 7 - !empty($items->getPType())? $items->getPType()->ptype_subject:'' ,// 8 - $items->user_categoryid, //9 - !empty($items->getUserType())? $items->getUserType()->print_name:'' ,//10 - $items->pplace_id,// 11 - $items->price, // 12 - )); + fputcsv($file, [ + $items->price_parkplaceid, + $items->prine_name, + $items->price_month, + $items->park_id, + optional($items->getPark())->park_name, + $items->psection_id, + optional($items->getPSection())->psection_subject, + $items->price_ptypeid, + optional($items->getPType())->ptype_subject, + $items->user_categoryid, + optional($items->getUserType())->print_name, + $items->pplace_id, + $items->price, + ]); } fclose($file); + return Response::download($filename, $filename, $headers); } + public function import(Request $request) { $file = $request->file('file'); - if(!empty($file)){ - $data = Utils::csvToArray($file); - $type = 1; - $msg = ''; - $record = 0; - DB::beginTransaction(); - try { - Price::query()->delete(); - $col = 13; - foreach ($data as $key => $items) { - $record = $key + 2; - if (count($items) == $col) { - $row = new Price(); - $row->price_parkplaceid = $items[0]; - $row->prine_name = $items[1]; - $row->price_month = $items[2]; - $row->park_id = $items[3]; - $row->psection_id = $items[5]; - $row->price_ptypeid = $items[7]; - $row->user_categoryid = $items[9]; - $row->pplace_id = $items[11]; - $row->price = $items[12]; - if (!$row->save()) { - $type = 0; - $msg = '行:record型が一致しません。'; - break; - } - } else { - $type = 0; - $msg = '行:record列数が一致しません。'; - break; - } + if (empty($file)) { + return redirect()->route('prices')->with('error', __('CSVファイルを選択してください。')); + } + + $data = Utils::csvToArray($file); + $type = true; + $msg = ''; + $record = 0; + + DB::beginTransaction(); + try { + // 先清空数据(全置換仕様なら残す) + Price::query()->delete(); + + $col = 13; // CSV 項目数 + foreach ($data as $key => $items) { + $record = $key + 2; // エラー行番号(ヘッダ行を考慮) + + // 項目数チェック + if (count($items) != $col) { + $type = false; + $msg = "行:{$record} 列数が一致しません。"; + break; + } + + // 必須チェック + if (empty($items[0])) { $type = false; $msg = "行:{$record} 駐車場所IDが未設定です。"; break; } + if (empty($items[1])) { $type = false; $msg = "行:{$record} 商品名が未設定です。"; break; } + if (empty($items[2])) { $type = false; $msg = "行:{$record} 期間が未設定です。"; break; } + if (empty($items[3])) { $type = false; $msg = "行:{$record} 駐輪場IDが未設定です。"; break; } + if (empty($items[5])) { $type = false; $msg = "行:{$record} 車種区分IDが未設定です。"; break; } + if (empty($items[7])) { $type = false; $msg = "行:{$record} 駐輪分類IDが未設定です。"; break; } + if (empty($items[9])) { $type = false; $msg = "行:{$record} 利用者分類IDが未設定です。"; break; } + if (empty($items[11])) { $type = false; $msg = "行:{$record} 駐車車室IDが未設定です。"; break; } + if (empty($items[12])) { $type = false; $msg = "行:{$record} 駐輪料金が未設定です。"; break; } + + // マスタ存在チェック + if (!Park::where('park_id', $items[3])->exists()) { + $type = false; $msg = "行:{$record} 駐輪場IDが存在しません。"; break; + } + if (!Psection::where('psection_id', $items[5])->exists()) { + $type = false; $msg = "行:{$record} 車種区分IDが存在しません。"; break; + } + if (!Ptype::where('ptype_id', $items[7])->exists()) { + $type = false; $msg = "行:{$record} 駐輪分類IDが存在しません。"; break; + } + if (!Usertype::where('user_categoryid', $items[9])->exists()) { + $type = false; $msg = "行:{$record} 利用者分類IDが存在しません。"; break; + } + // TODO: 駐車車室ID チェック(pplace_id) + + // 保存 + $row = new Price(); + $row->price_parkplaceid = $items[0]; + $row->prine_name = $items[1]; + $row->price_month = $items[2]; + $row->park_id = $items[3]; + $row->psection_id = $items[5]; + $row->price_ptypeid = $items[7]; + $row->user_categoryid = $items[9]; + $row->pplace_id = $items[11]; + $row->price = $items[12]; + + if (!$row->save()) { + $type = false; $msg = "行:{$record} データ保存に失敗しました。"; break; } - } catch (\Exception $e) { - $msg = '行:record型が一致しません。'; - $type = 0; - } - if ($type) { - DB::commit(); - return redirect()->route('prices')->with('success', __('輸入成功')); - } else { - DB::rollBack(); - return redirect()->route('prices')->with('error', __($msg, ['record' => $record])); } + } catch (\Exception $e) { + $type = false; + $msg = "行:{$record} 予期せぬエラー: ".$e->getMessage(); + } + + if ($type) { + DB::commit(); + return redirect()->route('prices')->with('success', __('インポートが正常に完了しました。')); } else { - return redirect()->route('prices')->with('error', __('あなたはcsvファイルを選択していません。')); + DB::rollBack(); + return redirect()->route('prices')->with('error', $msg); } } + public function info(Request $request, $id) { return $this->edit($request, $id, 'admin.prices.info'); diff --git a/app/Http/Controllers/Admin/PsectionController.php b/app/Http/Controllers/Admin/PsectionController.php index 9351d2a..66c287e 100644 --- a/app/Http/Controllers/Admin/PsectionController.php +++ b/app/Http/Controllers/Admin/PsectionController.php @@ -9,22 +9,37 @@ use App\Models\Psection; class PsectionController extends Controller { // 一覧画面 - public function list(Request $request) + public function list(Request $request) { - // ソート順 - $sort = $request->input('sort', 'psection_id'); - $sort_type = $request->input('sort_type', 'asc'); + $inputs = $request->all(); + // ソート可能なカラム + $allowedSortColumns = ['psection_id', 'psection_subject']; - $query = Psection::query(); - if (in_array($sort, ['psection_id', 'psection_subject'])) { - $query->orderBy($sort, $sort_type); + // ソート情報の取得 + $sortColumn = $inputs['sort'] ?? 'psection_id'; + $sortType = strtolower($inputs['sort_type'] ?? 'asc'); + + $query = \App\Models\Psection::query(); + + if (in_array($sortColumn, $allowedSortColumns, true)) { + if (!in_array($sortType, ['asc', 'desc'], true)) { + $sortType = 'asc'; + } + $query->orderBy($sortColumn, $sortType); } - $list = $query->get(); + // ページネーション(20件) + $list = $query->paginate(20); - return view('admin.psection.list', compact('list', 'sort', 'sort_type')); + return view('admin.psection.list', [ + 'list' => $list, + 'sort' => $sortColumn, + 'sort_type' => $sortType, + ]); } + + // 新規追加 public function add(Request $request) { @@ -42,16 +57,26 @@ class PsectionController extends Controller // 編集 public function edit(Request $request, $id) { + // 主キーで検索(見つからない場合は 404) $psection = Psection::findOrFail($id); + if ($request->isMethod('post')) { + // バリデーション $validated = $request->validate([ 'psection_subject' => 'required|string|max:255', ]); + + // データ更新 $psection->update($validated); + + // 成功メッセージ & リダイレクト return redirect()->route('psection')->with('success', '車種区分を更新しました'); } - return view('admin.psection.edit', compact('psection')); - } + + // 編集画面を表示 + return view('admin.psection.edit', compact('psection')); +} + // 詳細(info) public function info(Request $request, $id) diff --git a/app/Models/Price.php b/app/Models/Price.php index cfd2e5d..3ad4daf 100644 --- a/app/Models/Price.php +++ b/app/Models/Price.php @@ -21,6 +21,7 @@ class Price extends Model protected $primaryKey = 'price_parkplaceid'; protected $fillable = [ + 'price_parkplaceid', 'prine_name', 'price_month', 'park_id', diff --git a/resources/views/admin/prices/_form.blade.php b/resources/views/admin/prices/_form.blade.php index 56f1139..6e1da0a 100644 --- a/resources/views/admin/prices/_form.blade.php +++ b/resources/views/admin/prices/_form.blade.php @@ -18,20 +18,33 @@ @endif