diff --git a/app/Http/Controllers/Admin/PriceController.php b/app/Http/Controllers/Admin/PriceController.php index 86110bd..5f422a2 100644 --- a/app/Http/Controllers/Admin/PriceController.php +++ b/app/Http/Controllers/Admin/PriceController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin; use App\Http\Requests\PriceRequest; use App\Models\Park; use App\Models\Price; +use App\Models\Pplace; use App\Models\Psection; use App\Models\Ptype; use App\Models\Usertype; @@ -41,117 +42,92 @@ class PriceController extends Controller public function add(Request $request) { - // 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 ($validated, &$type) { - $new = new Price(); - $new->fill($validated); - if ($new->save()) { - $type = true; - } - }); - - // 結果 - if ($type) { - return redirect()->route('prices') - ->with('success', __('新しい成功を創造する。')); - } else { - return redirect()->route('prices') - ->with('error', __('新しい作成に失敗しました。')); - } + if ($request->isMethod('get')) { + return view('admin.prices.add', array_merge( + $this->getDataDropList(), + [ + 'record' => new Price(), + 'isEdit' => false, + ] + )); } - // GET の場合 → 画面表示 - $dataList = $this->getDataDropList(); - return view('admin.prices.add', $dataList); + $request->merge([ + 'pplace_id' => mb_convert_kana($request->input('pplace_id'), 'n'), + ]); + + $validated = $this->validateRequest($request); + + $created = false; + \DB::transaction(function () use ($validated, &$created) { + $price = new Price(); + $price->fill($validated); + $created = $price->save(); + }); + + return redirect()->route('prices') + ->with($created ? 'success' : 'error', $created ? '登録しました。' : '登録に失敗しました。'); } - public function edit(Request $request, $pk ,$view='') + public function edit(Request $request, $id) { - $price = Price::getByPk($pk); - if (empty($pk) || empty($price)) { + $price = Price::getByPk($id); + if (!$price) { 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'), - ]; - $data = array_merge($data, $requestAll); - - \DB::transaction(function () use ($data, &$type, $price) { - $price->fill($data); - $price->save(); - $type = true; - }); - - if ($type) { - return redirect()->route('prices')->with('success', __('更新に成功しました。')); - } else { - return redirect()->route('prices')->with('error', __('更新に失敗しました。')); - } + if ($request->isMethod('get')) { + return view('admin.prices.edit', array_merge( + $this->getDataDropList(), + [ + 'record' => $price, + 'isEdit' => true, + ] + )); } - if ($view != '') { - return view($view, $data); - } + $request->merge([ + 'pplace_id' => mb_convert_kana($request->input('pplace_id'), 'n'), + ]); - return view('admin.prices.edit', $data); + $validated = $this->validateRequest($request, $id); + + $updated = false; + \DB::transaction(function () use ($validated, &$updated, $price) { + $price->fill($validated); + $updated = $price->save(); + }); + + return redirect()->route('prices') + ->with($updated ? 'success' : 'error', $updated ? '更新しました。' : '更新に失敗しました。'); } - public function delete(Request $request) + + public function delete(Request $request, $id = null) { - $arr_pk = $request->get('pk'); + // 一覧画面(checkbox で複数削除) + $ids = $request->input('pk'); - if ($arr_pk) { - $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', __('削除に失敗しました。')); - } + // 編集画面(単体削除) + if ($id) { + $ids = [$id]; } - return redirect()->route('prices')->with('error', __('削除するユーザーを選択してください。')); + // 削除対象が空 + if (empty($ids)) { + return redirect()->route('prices')->with('error', '削除対象が選択されていません。'); + } + + // 削除処理 + Price::destroy($ids); + + return redirect()->route('prices')->with('success', '削除しました。'); } + + + public static function deleteByPk($ids) { if (!is_array($ids)) { @@ -324,8 +300,33 @@ class PriceController extends Controller $data['psections'] = Psection::getList() ; $data['ptypes'] = Ptype::getList() ; $data['userTypes'] = Usertype::getList() ; + $data['pplaces'] = Pplace::getList() ; + return $data; } + /** + * Price バリデーション共通 + */ + private function validateRequest(Request $request): array + { + return $request->validate([ + 'prine_name' => 'required|string|max:255', + 'price_month' => 'required|int', + 'park_id' => 'required|int', + 'psection_id' => 'required|int', + 'price_ptypeid' => 'required|int', + 'user_categoryid' => 'required|int', + 'pplace_id' => 'nullable|int', + 'park_number' => 'nullable|int', + 'park_standard' => 'nullable|int', + 'park_limit' => 'nullable|int', + 'price' => 'required|numeric', + 'operator_id' => 'nullable|int', + ]); + } + + + } \ No newline at end of file diff --git a/app/Models/Price.php b/app/Models/Price.php index ec12629..c6571db 100644 --- a/app/Models/Price.php +++ b/app/Models/Price.php @@ -45,9 +45,13 @@ class Price extends Model $query = self::query() ->select( 'price_a.*', - \DB::raw("CONCAT_WS('/', usertype.usertype_subject1, usertype.usertype_subject2, usertype.usertype_subject3) as user_category_name") + \DB::raw("CONCAT_WS('/', usertype.usertype_subject1, usertype.usertype_subject2, usertype.usertype_subject3) as user_category_name"), + 'psection.psection_subject', + 'ptype.ptype_subject' ) - ->leftJoin('usertype', 'price_a.user_categoryid', '=', 'usertype.user_categoryid'); + ->leftJoin('usertype', 'price_a.user_categoryid', '=', 'usertype.user_categoryid') + ->leftJoin('psection', 'price_a.psection_id', '=', 'psection.psection_id') + ->leftJoin('ptype', 'price_a.price_ptypeid', '=', 'ptype.ptype_id'); // ソート対象カラム $allowedSortColumns = [ @@ -111,4 +115,17 @@ class Price extends Model return $this->belongsTo(Usertype::class, 'user_categoryid', 'user_categoryid')->first(); } + public function psection() + { + return $this->belongsTo(Psection::class, 'psection_id'); // 外部キーが psection_id + + } + public function ptype() + { + return $this->belongsTo(Ptype::class, 'price_ptypeid'); // 外部キーが price_ptypeid + + } + + + } \ No newline at end of file diff --git a/resources/lang/ja/validation.php b/resources/lang/ja/validation.php index f7e7073..18b4ef8 100644 --- a/resources/lang/ja/validation.php +++ b/resources/lang/ja/validation.php @@ -419,6 +419,11 @@ return [ 'memo' => '備考', 'subject' => '件名', 'text' => '本文', +//SWA-92 + 'edit_master' => 'マスタ編集', + 'web_master' => 'ウェブ参照マスタ', + 'auto_change_date' => 'ウェブ参照マスタ自動切り替え日時', + diff --git a/resources/views/admin/devices/_form.blade.php b/resources/views/admin/devices/_form.blade.php index 00b0637..c2c95fa 100644 --- a/resources/views/admin/devices/_form.blade.php +++ b/resources/views/admin/devices/_form.blade.php @@ -1,24 +1,4 @@ - - -{{-- アラート --}} +{{-- ===== エラーメッセージ ===== --}} @if(Session::has('success'))