This commit is contained in:
parent
8b85451145
commit
e39fa8a87e
@ -9,6 +9,7 @@ use App\Models\Pplace;
|
|||||||
use App\Models\Psection;
|
use App\Models\Psection;
|
||||||
use App\Models\Ptype;
|
use App\Models\Ptype;
|
||||||
use App\Models\Usertype;
|
use App\Models\Usertype;
|
||||||
|
use App\Models\Station;
|
||||||
use App\Models\Utils;
|
use App\Models\Utils;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@ -183,6 +184,67 @@ class PriceController extends Controller
|
|||||||
return response()->download($filename, $filename, $headers)->deleteFileAfterSend(true);
|
return response()->download($filename, $filename, $headers)->deleteFileAfterSend(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function exportFiltered(Request $request)
|
||||||
|
{
|
||||||
|
$parkId = $request->input('park_id');
|
||||||
|
$filename = 'price_' . $parkId . '_' . now()->format('YmdHis') . '.csv';
|
||||||
|
|
||||||
|
// 一時ファイル作成
|
||||||
|
$file = fopen($filename, 'w+');
|
||||||
|
fwrite($file, "\xEF\xBB\xBF"); // UTF-8 BOM追加
|
||||||
|
|
||||||
|
// CSVヘッダ
|
||||||
|
$columns = [
|
||||||
|
'駐輪場所ID',
|
||||||
|
'商品名',
|
||||||
|
'期間',
|
||||||
|
'駐輪場ID',
|
||||||
|
'駐輪場名',
|
||||||
|
'車種区分ID',
|
||||||
|
'車種区分',
|
||||||
|
'駐輪分類ID',
|
||||||
|
'駐輪分類',
|
||||||
|
'利用者分類ID',
|
||||||
|
'利用者分類',
|
||||||
|
'駐輪車室ID',
|
||||||
|
'駐輪料金(税込)',
|
||||||
|
];
|
||||||
|
fputcsv($file, $columns);
|
||||||
|
|
||||||
|
// データ取得(選択した駐輪場IDに限定)
|
||||||
|
$data = \App\Models\Price::where('price_parkplaceid', $parkId)->get();
|
||||||
|
|
||||||
|
foreach ($data as $item) {
|
||||||
|
fputcsv($file, [
|
||||||
|
$item->price_parkplaceid, // 駐輪場所ID
|
||||||
|
$item->prine_name, // 商品名
|
||||||
|
$item->price_term ?? $item->price_month, // 期間
|
||||||
|
$item->park_id, // 駐輪場ID
|
||||||
|
optional($item->getPark())->park_name, // 駐輪場名
|
||||||
|
$item->psection_id, // 車種区分ID
|
||||||
|
optional($item->getPSection())->psection_subject, // 車種区分
|
||||||
|
$item->price_ptypeid, // 駐輪分類ID
|
||||||
|
optional($item->getPType())->ptype_subject, // 駐輪分類
|
||||||
|
$item->user_categoryid, // 利用者分類ID
|
||||||
|
optional($item->getUserType())->print_name, // 利用者分類
|
||||||
|
$item->pplace_id, // 駐輪車室ID
|
||||||
|
$item->price_taxin ?? $item->price, // 駐輪料金(税込)
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose($file);
|
||||||
|
|
||||||
|
// ヘッダ設定
|
||||||
|
$headers = [
|
||||||
|
"Content-Type" => "text/csv; charset=UTF-8",
|
||||||
|
"Content-Disposition" => "attachment; filename=\"{$filename}\"",
|
||||||
|
];
|
||||||
|
|
||||||
|
// ダウンロード後にファイル削除
|
||||||
|
return response()->download($filename, $filename, $headers)->deleteFileAfterSend(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function import(Request $request)
|
public function import(Request $request)
|
||||||
{
|
{
|
||||||
$file = $request->file('file');
|
$file = $request->file('file');
|
||||||
@ -197,8 +259,6 @@ class PriceController extends Controller
|
|||||||
|
|
||||||
DB::beginTransaction();
|
DB::beginTransaction();
|
||||||
try {
|
try {
|
||||||
// 先清空数据(全置換仕様なら残す)
|
|
||||||
Price::query()->delete();
|
|
||||||
|
|
||||||
$col = 13; // CSV 項目数
|
$col = 13; // CSV 項目数
|
||||||
foreach ($data as $key => $items) {
|
foreach ($data as $key => $items) {
|
||||||
@ -236,22 +296,28 @@ class PriceController extends Controller
|
|||||||
$type = false; $msg = "行:{$record} 利用者分類IDが存在しません。"; break;
|
$type = false; $msg = "行:{$record} 利用者分類IDが存在しません。"; break;
|
||||||
}
|
}
|
||||||
// TODO: 駐車車室ID チェック(pplace_id)
|
// TODO: 駐車車室ID チェック(pplace_id)
|
||||||
|
if (!Pplace::where('pplace_id', $items[11])->exists()) {
|
||||||
// 保存
|
$type = false; $msg = "行:{$record} 駐車車室IDが存在しません。"; break;
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 保存(存在すれば更新、なければ新規作成)
|
||||||
|
Price::updateOrCreate(
|
||||||
|
['price_parkplaceid' => $items[0]], // 主キー条件(存在チェック)
|
||||||
|
|
||||||
|
[
|
||||||
|
'prine_name' => $items[1],
|
||||||
|
'price_month' => $items[2],
|
||||||
|
'park_id' => $items[3],
|
||||||
|
'psection_id' => $items[5],
|
||||||
|
'price_ptypeid' => $items[7],
|
||||||
|
'user_categoryid' => $items[9],
|
||||||
|
'pplace_id' => $items[11],
|
||||||
|
'price' => $items[12],
|
||||||
|
'operator_id' => auth()->user()->operator_id ?? null, // オプション
|
||||||
|
'updated_at' => now(),
|
||||||
|
'created_at' => now(),
|
||||||
|
]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$type = false;
|
$type = false;
|
||||||
|
|||||||
@ -125,6 +125,15 @@ class Price extends Model
|
|||||||
return $this->belongsTo(Ptype::class, 'price_ptypeid'); // 外部キーが price_ptypeid
|
return $this->belongsTo(Ptype::class, 'price_ptypeid'); // 外部キーが price_ptypeid
|
||||||
|
|
||||||
}
|
}
|
||||||
|
public function pplace()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Pplace::class, 'pplace_id'); // 外部キーが pplace_id
|
||||||
|
|
||||||
|
}
|
||||||
|
// public function getStation()
|
||||||
|
// {
|
||||||
|
// return $this->belongsTo(Station::class, 'price_parkplaceid', 'park_id');
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -63,7 +63,7 @@
|
|||||||
<div class="modal fade" id="exportModal" tabindex="-1" role="dialog">
|
<div class="modal fade" id="exportModal" tabindex="-1" role="dialog">
|
||||||
<div class="modal-dialog" role="document">
|
<div class="modal-dialog" role="document">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
<form method="POST" action="{{ route('prices_export') }}">
|
<form method="POST" action="{{ route('prices_export_filtered') }}">
|
||||||
@csrf
|
@csrf
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
<p>エクスポートする駐輪場を選択してください。</p>
|
<p>エクスポートする駐輪場を選択してください。</p>
|
||||||
@ -149,7 +149,6 @@
|
|||||||
|
|
||||||
<!-- ▼ ここから単一テーブル構成 ----------------------------------------- -->
|
<!-- ▼ ここから単一テーブル構成 ----------------------------------------- -->
|
||||||
<div class="col-lg-12 mb20">
|
<div class="col-lg-12 mb20">
|
||||||
<!-- <div class="table-responsive"> -->
|
|
||||||
<div class="table-responsive prices-wrap">
|
<div class="table-responsive prices-wrap">
|
||||||
<table class="table table-bordered dataTable text-nowrap prices-table">
|
<table class="table table-bordered dataTable text-nowrap prices-table">
|
||||||
<thead class="thead-light">
|
<thead class="thead-light">
|
||||||
|
|||||||
@ -185,6 +185,8 @@ Route::middleware('auth')->group(function () {
|
|||||||
Route::match(['get', 'post'], '/admin/prices/delete', [PriceController::class, 'delete'])->name('prices_delete');
|
Route::match(['get', 'post'], '/admin/prices/delete', [PriceController::class, 'delete'])->name('prices_delete');
|
||||||
Route::match(['get', 'post'], '/admin/prices/import', [PriceController::class, 'import'])->name('prices_import');
|
Route::match(['get', 'post'], '/admin/prices/import', [PriceController::class, 'import'])->name('prices_import');
|
||||||
Route::get('/admin/prices/export', [PriceController::class, 'export'])->name('prices_export');
|
Route::get('/admin/prices/export', [PriceController::class, 'export'])->name('prices_export');
|
||||||
|
// POST: 条件付きエクスポート(モーダルで選択)
|
||||||
|
Route::post('/admin/prices/export', [PriceController::class, 'exportFiltered'])->name('prices_export_filtered');
|
||||||
|
|
||||||
|
|
||||||
//車種区分マスタ
|
//車種区分マスタ
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user