【シール発行履歴】プルダウン内容修正

This commit is contained in:
你的名字 2025-09-19 17:52:34 +09:00
parent d1277a2b4a
commit c43a16ed79
2 changed files with 87 additions and 80 deletions

View File

@ -10,66 +10,69 @@ class SealsController extends Controller
{
public function list(Request $request)
{
// ソート用パラメータ取得指定がなければnull
$sort = $request->input('sort');
$sort_type = $request->input('sort_type', 'asc');
// sealテーブルを参照し、フィルター
$query = DB::table('seal');
// フィルター:発行日
$periodType = $request->input('period_type');
$recentPeriod = $request->input('recent_period');
$sealDayFrom = $request->input('seal_day_from');
$sealDayTo = $request->input('seal_day_to');
if ($periodType === 'range' && $sealDayFrom && $sealDayTo) {
$query->whereBetween('seal_day', [$sealDayFrom, $sealDayTo]);
} elseif ($periodType === 'recent' && $recentPeriod) {
$now = date('Y-m-d');
switch ($recentPeriod) {
case '12m':
$from = date('Y-m-d', strtotime('-12 months', strtotime($now)));
break;
case '6m':
$from = date('Y-m-d', strtotime('-6 months', strtotime($now)));
break;
case '3m':
$from = date('Y-m-d', strtotime('-3 months', strtotime($now)));
break;
case '2m':
$from = date('Y-m-d', strtotime('-2 months', strtotime($now)));
break;
case '1m':
$from = date('Y-m-d', strtotime('-1 months', strtotime($now)));
break;
case '1w':
$from = date('Y-m-d', strtotime('-1 week', strtotime($now)));
break;
default:
$from = null;
}
if (isset($from)) {
$query->where('seal_day', '>=', $from);
}
} else {
// デフォルト直近3ヶ月
$now = date('Y-m-d');
$from = date('Y-m-d', strtotime('-3 months', strtotime($now)));
$query->where('seal_day', '>=', $from);
}
// ソート指定があればorderBy、なければDB物理順
if ($sort) {
$list = $query->orderBy($sort, $sort_type)->paginate(20);
} else {
$list = $query->paginate(20);
}
return view('admin.seals.list', [
'list' => $list,
'sort' => $sort,
'sort_type' => $sort_type,
$q = \DB::table('seal as s')
->leftJoin('park as p','s.park_id','=','p.park_id')
->leftJoin('regular_contract as rc','s.contract_id','=','rc.contract_id')
->leftJoin('psection as ps','rc.psection_id','=','ps.psection_id')
->select([
's.seal_issueid',
's.park_id',
'p.park_name',
's.contract_id',
'rc.psection_id',
\DB::raw('ps.psection_subject AS psection_subject'),
'rc.contract_seal_issue',
's.seal_day',
's.seal_reason',
]);
// 駐輪場フィルタ
if($request->filled('park_id')){
$q->where('s.park_id',$request->park_id);
}
// 期間フィルタ
$periodType = $request->input('period_type','range');
if($periodType === 'range'){
if($request->filled('seal_day_from')){
$q->whereDate('s.seal_day','>=',$request->seal_day_from);
}
if($request->filled('seal_day_to')){
$q->whereDate('s.seal_day','<=',$request->seal_day_to);
}
} elseif($periodType === 'recent' && $request->filled('recent_period')){
$map = ['12m'=>12,'6m'=>6,'3m'=>3,'2m'=>2,'1m'=>1,'1w'=>'1w'];
$key = $request->recent_period;
if(isset($map[$key])){
$from = $key==='1w' ? now()->subWeek() : now()->subMonths($map[$key]);
$q->where('s.seal_day','>=',$from->toDateString());
}
}
// --- 並び替え: パラメータがある場合のみ適用 ---
$sort = $request->query('sort'); // デフォルト null
$sortType = $request->query('sort_type','asc'); // 指定なければ asc
$allow = [
'seal_issueid' => 's.seal_issueid',
'park_id' => 'p.park_name',
'contract_id' => 's.contract_id',
'seal_day' => 's.seal_day',
'contract_seal_issue' => 'rc.contract_seal_issue',
'psection_subject' => 'ps.psection_subject',
];
if(isset($allow[$sort])){
$sortType = $sortType === 'desc' ? 'desc' : 'asc';
$q->orderBy($allow[$sort], $sortType);
}
// 並び替え指定が無い時は orderBy 不要 → DB の物理(主キー)順
$list = $q->paginate(20)->appends($request->query());
$parks = \DB::table('park')
->select('park_id','park_name')
->orderBy('park_name')
->get();
return view('admin.seals.list', compact('list','parks','sort','sortType'));
}
}

View File

@ -22,38 +22,42 @@
<div class="col-sm-4">
<select name="park_id" class="form-select">
<option value="">全て</option>
{{-- @foreach($parks as $park)
<option value="{{ $park->id }}" {{ request('park_id') == $park->id ? 'selected' : '' }}>{{ $park->name }}</option>
@endforeach --}}
@foreach($parks as $park)
<option value="{{ $park->park_id }}"
{{ (string)request('park_id') === (string)$park->park_id ? 'selected' : '' }}>
{{ $park->park_name }}
</option>
@endforeach
</select>
</div>
</div>
<div class="row mb-3 align-items-center">
<label class="col-sm-2 col-form-label fw-bold py-0">発行日</label>
<div class="col-sm-10">
<div class="row g-2 align-items-center mb-0">
<div class="row align-items-center mb-0">
<div class="col-auto">
<div class="form-check form-check-inline">
<div class="form-check form-check-inline me-2">
<input class="form-check-input" type="radio" name="period_type" id="period_type_range" value="range" {{ request('period_type', 'range') == 'range' ? 'checked' : '' }}>
<label class="form-check-label" for="period_type_range">範囲指定</label>
</div>
</div>
<div class="col-auto ms-3">
<div class="d-flex align-items-center">
<input type="date" name="seal_day_from" id="seal_day_from" class="form-control" style="width:160px;" value="{{ request('seal_day_from') }}">
<span class="mx-3 fw-bold"></span>
<input type="date" name="seal_day_to" id="seal_day_to" class="form-control" style="width:160px;" value="{{ request('seal_day_to') }}">
</div>
</div>
</div>
<div class="row align-items-center mt-2">
<div class="col-auto">
<div class="input-group">
<input type="date" name="seal_day_from" id="seal_day_from" class="form-control" value="{{ request('seal_day_from') }}">
<span class="input-group-text"></span>
<input type="date" name="seal_day_to" id="seal_day_to" class="form-control" value="{{ request('seal_day_to') }}">
</div>
</div>
</div>
<div class="row g-2 align-items-center">
<div class="col-auto">
<div class="form-check form-check-inline">
<div class="form-check form-check-inline me-2">
<input class="form-check-input" type="radio" name="period_type" id="period_type_recent" value="recent" {{ request('period_type') == 'recent' ? 'checked' : '' }}>
<label class="form-check-label" for="period_type_recent">直近Nヶ月等</label>
</div>
</div>
<div class="col-auto">
<div class="col-auto ms-3">
<select name="recent_period" id="recent_period" class="form-select">
<option value="">全て</option>
<option value="12m" {{ request('recent_period') == '12m' ? 'selected' : '' }}>直近12ヶ月</option>
@ -90,20 +94,20 @@
<table class="table table-bordered table-hover table-sm dataTable">
<thead class="thead-light">
<tr>
<th class="sorting {{ ($sort=='seal_issueid') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="seal_issueid" style="cursor:pointer;">
<th class="sorting {{ ($sort=='seal_issueid') ? ($sortType=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="seal_issueid" style="cursor:pointer;">
シール発行履歴ID
</th>
<th class="sorting {{ ($sort=='park_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="park_id" style="cursor:pointer;">
<th class="sorting {{ ($sort=='park_id') ? ($sortType=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="park_id" style="cursor:pointer;">
シール発行駐輪場
</th>
<th class="sorting {{ ($sort=='contract_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="contract_id" style="cursor:pointer;">
<th class="sorting {{ ($sort=='contract_id') ? ($sortType=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="contract_id" style="cursor:pointer;">
定期契約ID
</th>
<th>車種区分ID</th>
<th class="sorting {{ ($sort=='seal_day') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="seal_day" style="cursor:pointer;">
<th class="sorting {{ ($sort=='seal_day') ? ($sortType=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="seal_day" style="cursor:pointer;">
発行日
</th>
<th class="sorting {{ ($sort=='contract_seal_issue') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="contract_seal_issue" style="cursor:pointer;">
<th class="sorting {{ ($sort=='contract_seal_issue') ? ($sortType=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="contract_seal_issue" style="cursor:pointer;">
発行回数
</th>
<th>理由</th>
@ -115,7 +119,7 @@
<td>{{ $item->seal_issueid }}</td>
<td>{{ $item->park_name ?? $item->park_id }}</td>
<td>{{ $item->contract_id }}</td>
<td>{{ $item->psection_name ?? $item->psection_id }}</td>
<td>{{ $item->psection_subject ?? $item->psection_id }}</td>
<td>{{ $item->seal_day }}</td>
<td>{{ $item->contract_seal_issue }}</td>
<td>{{ $item->seal_reason }}</td>