331 lines
12 KiB
PHP
331 lines
12 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Admin;
|
||
|
||
use App\Http\Requests\PriceRequest;
|
||
use App\Models\Park;
|
||
use App\Models\Price;
|
||
use App\Models\Psection;
|
||
use App\Models\Ptype;
|
||
use App\Models\Usertype;
|
||
use App\Models\Utils;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Http\Request;
|
||
use App\Http\Controllers\Controller;
|
||
use Illuminate\Support\Facades\Validator;
|
||
use Response;
|
||
|
||
class PriceController extends Controller
|
||
{
|
||
public function list(Request $request)
|
||
{
|
||
$inputs = [
|
||
'isExport' => 0,
|
||
'sort' => $request->input('sort', ''), // ソート対象カラム
|
||
'sort_type' => $request->input('sort_type', ''), // 昇順/降順
|
||
'page' => $request->get('page', 1),
|
||
];
|
||
|
||
// Price::search 内で orderBy を反映させる
|
||
$inputs['list'] = Price::search($inputs);
|
||
|
||
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);
|
||
}
|
||
|
||
|
||
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', __('新しい作成に失敗しました。'));
|
||
}
|
||
}
|
||
|
||
// GET の場合 → 画面表示
|
||
$dataList = $this->getDataDropList();
|
||
return view('admin.prices.add', $dataList);
|
||
}
|
||
|
||
|
||
public function edit(Request $request, $pk ,$view='')
|
||
{
|
||
$price = Price::getByPk($pk);
|
||
if (empty($pk) || empty($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 ($view != '') {
|
||
return view($view, $data);
|
||
}
|
||
|
||
return view('admin.prices.edit', $data);
|
||
}
|
||
|
||
|
||
public function delete(Request $request)
|
||
{
|
||
$arr_pk = $request->get('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', __('削除に失敗しました。'));
|
||
}
|
||
}
|
||
|
||
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 = [
|
||
"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"
|
||
];
|
||
|
||
$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, [
|
||
$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)) {
|
||
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) {
|
||
$type = false;
|
||
$msg = "行:{$record} 予期せぬエラー: ".$e->getMessage();
|
||
}
|
||
|
||
if ($type) {
|
||
DB::commit();
|
||
return redirect()->route('prices')->with('success', __('インポートが正常に完了しました。'));
|
||
} else {
|
||
DB::rollBack();
|
||
return redirect()->route('prices')->with('error', $msg);
|
||
}
|
||
}
|
||
|
||
|
||
public function info(Request $request, $id)
|
||
{
|
||
return $this->edit($request, $id, 'admin.prices.info');
|
||
}
|
||
|
||
public function getDataDropList()
|
||
{
|
||
$data['parks'] = Park::getList() ;
|
||
$data['psections'] = Psection::getList() ;
|
||
$data['ptypes'] = Ptype::getList() ;
|
||
$data['userTypes'] = Usertype::getList() ;
|
||
return $data;
|
||
}
|
||
|
||
|
||
} |