Merge pull request '駐輪場空き台数計算処理修正' (#37) from main_higashide into main
All checks were successful
Deploy so-manager (auto) / deploy (push) Successful in 23s

Reviewed-on: #37
This commit is contained in:
y.higashide 2025-10-02 16:29:09 +09:00
commit d2b631bbab
3 changed files with 99 additions and 70 deletions

View File

@ -100,13 +100,13 @@ class RegularContractCreateController extends Controller
// zoneテーブルデータを取得psectionテーブルとJOINしてpsection_subjectも取得 // zoneテーブルデータを取得psectionテーブルとJOINしてpsection_subjectも取得
$zones = DB::table('zone') $zones = DB::table('zone')
->leftJoin('psection', 'zone.psection_id', '=', 'psection.psection_id') ->leftJoin('psection', 'zone.psection_id', '=', 'psection.psection_id')
->select('zone.zone_id', 'zone.park_id', 'zone.psection_id', 'zone.zone_number', 'zone.zone_tolerance', 'psection.psection_subject') ->select('zone.zone_id', 'zone.park_id', 'zone.ptype_id', 'zone.psection_id', 'zone.zone_number', 'zone.zone_tolerance', 'psection.psection_subject')
->get() ->get()
->groupBy('park_id'); ->groupBy('park_id');
// 空き予約マスタデータを取得 // 空き予約マスタデータを取得
$reserve = DB::table('reserve') $reserve = DB::table('reserve')
->select('reserve_id', 'park_id', 'psection_id') ->select('reserve_id', 'park_id', 'ptype_id', 'psection_id')
->where('valid_flag', 1) ->where('valid_flag', 1)
->get() ->get()
->groupBy('park_id'); ->groupBy('park_id');

View File

@ -89,18 +89,31 @@
<td> <td>
@php @php
$zonesForType = ($zones[$row->park_id] ?? collect())->where('psection_subject', $vehicle); $zonesForType = ($zones[$row->park_id] ?? collect())->where('psection_subject', $vehicle);
@endphp // 空き台数計算
@forelse ($zonesForType as $zone) $hasVacancy = false;
@php foreach ($zonesForType as $zone) {
$reserveCount = ($reserve[$row->park_id] ?? collect()) $reserveCount = ($reserve[$row->park_id] ?? collect())
->where('psection_id', $zone->psection_id) ->where('psection_id', $zone->psection_id)
->where('ptype_id', $zone->ptype_id)
->count(); ->count();
$vacancy = $zone->zone_tolerance - $zone->zone_number - $reserveCount; $vacancy = $zone->zone_tolerance - $zone->zone_number - $reserveCount;
if ($vacancy > 0) {
$hasVacancy = true;
break;
}
}
// 猶予期間判定 // 猶予期間判定
$grace = $city_grace_periods[$row->city_id] ?? null; $grace = $city_grace_periods[$row->city_id] ?? null;
$now = \Carbon\Carbon::now(); $gracePeriodValid =
$grace &&
is_numeric($grace->update_grace_period_start_date) &&
preg_match('/^\d{1,2}:\d{2}$/', $grace->update_grace_period_start_time) &&
is_numeric($grace->update_grace_period_end_date) &&
preg_match('/^\d{1,2}:\d{2}$/', $grace->update_grace_period_end_time);
$inGrace = false; $inGrace = false;
if ($grace && $grace->update_grace_period_start_date && $grace->update_grace_period_start_time && $grace->update_grace_period_end_date && $grace->update_grace_period_end_time) { if ($gracePeriodValid) {
$now = \Carbon\Carbon::now();
$year = $now->year; $year = $now->year;
$month = $now->month; $month = $now->month;
$startDay = (int)$grace->update_grace_period_start_date; $startDay = (int)$grace->update_grace_period_start_date;
@ -125,16 +138,17 @@
} }
} }
@endphp @endphp
@if ($vacancy > 0 && $inGrace) @if ($zonesForType->isNotEmpty() && $gracePeriodValid)
@if ($hasVacancy && $inGrace)
<button class="btn btn-block btn-sm btn-outline-success btn_82-table btn-popup" data-park-id="{{ $row->park_id }}">定期契約</button> <button class="btn btn-block btn-sm btn-outline-success btn_82-table btn-popup" data-park-id="{{ $row->park_id }}">定期契約</button>
@elseif (!$inGrace) @elseif (!$inGrace)
<button class="btn btn-block btn-sm btn-outline-danger btn_103-table btn-popup" data-park-id="{{ $row->park_id }}">販売期間外</button> <button class="btn btn-block btn-sm btn-outline-danger btn_103-table btn-popup" data-park-id="{{ $row->park_id }}">販売期間外</button>
@elseif ($vacancy <= 0 && $inGrace) @elseif (!$hasVacancy && $inGrace)
<button class="btn btn-block btn-sm btn-outline-danger btn_103-table btn-popup" data-park-id="{{ $row->park_id }}">空き待ち申込</button> <button class="btn btn-block btn-sm btn-outline-danger btn_103-table btn-popup" data-park-id="{{ $row->park_id }}">空き待ち申込</button>
@endif @endif
@empty @else
<span class="text-muted"></span> <span class="text-muted"></span>
@endforelse @endif
</td> </td>
@endforeach @endforeach
</tr> </tr>

View File

@ -36,64 +36,79 @@
</div> </div>
<!-- 空き台数・契約情報 --> <!-- 空き台数・契約情報 -->
<div class="mt-3"> <div class="mt-3">
@foreach($zones as $zone)
@php @php
$vacant = $vacancyData[$zone->psection_id . '_' . $zone->ptype_subject] ?? 0; $zonesByPtype = $zones->groupBy('ptype_id');
$grace = $city_grace_periods[$park->city_id] ?? null;
$now = \Carbon\Carbon::now();
// 猶予期間判定
$isGracePeriod = false;
if ($grace && $grace->update_grace_period_start_date && $grace->update_grace_period_start_time && $grace->update_grace_period_end_date && $grace->update_grace_period_end_time) {
$now = \Carbon\Carbon::now();
$year = $now->year;
$month = $now->month;
$startDay = (int)$grace->update_grace_period_start_date;
$endDay = (int)$grace->update_grace_period_end_date;
$startTime = $grace->update_grace_period_start_time;
$endTime = $grace->update_grace_period_end_time;
if ($startDay > $endDay) {
// 月またぎ
$start = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $year, $month, $startDay, $startTime));
$nextMonth = $month == 12 ? 1 : $month + 1;
$nextYear = $month == 12 ? $year + 1 : $year;
$end = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $nextYear, $nextMonth, $endDay, $endTime));
} else {
// 同月
$start = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $year, $month, $startDay, $startTime));
$end = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $year, $month, $endDay, $endTime));
}
$isGracePeriod = $now->between($start, $end);
}
@endphp @endphp
<div class="mb-2"> @foreach($zonesByPtype as $ptypeId => $zonesGroup)
<strong>{{ $zone->ptype_subject }}</strong><br> <div class="mb-3">
{{ $zone->psection_subject }}:空き {{ $vacant }} <strong>{{ $zonesGroup->first()->ptype_subject }}</strong>
@if($isGracePeriod) <div style="display: flex; gap: 1em;">
@if($vacant > 0) @foreach($zonesGroup as $zone)
<button type="button" class="btn btn-success btn-sm btn-contract" @php
data-park-id="{{ $park->park_id }}" $vacant = $vacancyData[$zone->psection_id . '_' . $zone->ptype_subject] ?? 0;
data-psection-id="{{ $zone->psection_id }}" $grace = $city_grace_periods[$park->city_id] ?? null;
data-ptype-id="{{ $zone->ptype_id }}"> $now = \Carbon\Carbon::now();
定期契約
</button> // 猶予期間判定
@else $isGracePeriod = false;
<button type="button" class="btn btn-danger btn_103 btn-reserve" if ($grace && $grace->update_grace_period_start_date && $grace->update_grace_period_start_time && $grace->update_grace_period_end_date && $grace->update_grace_period_end_time) {
data-park-id="{{ $park->park_id }}" $now = \Carbon\Carbon::now();
data-psection-id="{{ $zone->psection_id }}" $year = $now->year;
data-ptype-id="{{ $zone->ptype_id }}"> $month = $now->month;
空き待ち申込 $startDay = (int)$grace->update_grace_period_start_date;
</button> $endDay = (int)$grace->update_grace_period_end_date;
@endif $startTime = $grace->update_grace_period_start_time;
@else $endTime = $grace->update_grace_period_end_time;
<button type="button" class="btn btn-danger btn_103 btn-reserve"
data-park-id="{{ $park->park_id }}" if ($startDay > $endDay) {
data-psection-id="{{ $zone->psection_id }}" // 月またぎ
data-ptype-id="{{ $zone->ptype_id }}"> // 前月の開始日~今月の終了日
販売期間外 $prevMonth = $now->copy()->subMonth();
</button> $startPrev = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $prevMonth->year, $prevMonth->month, $startDay, $grace->update_grace_period_start_time));
@endif $endCurr = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $year, $month, $endDay, $grace->update_grace_period_end_time));
// 今月の開始日~翌月の終了日
$startCurr = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $year, $month, $startDay, $grace->update_grace_period_start_time));
$nextMonth = $month == 12 ? 1 : $month + 1;
$nextYear = $month == 12 ? $year + 1 : $year;
$endNext = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $nextYear, $nextMonth, $endDay, $grace->update_grace_period_end_time));
$isGracePeriod = $now->between($startPrev, $endCurr) || $now->between($startCurr, $endNext);
} else {
// 同月
$start = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $year, $month, $startDay, $grace->update_grace_period_start_time));
$end = \Carbon\Carbon::createFromFormat('Y-m-d H:i', sprintf('%04d-%02d-%02d %s', $year, $month, $endDay, $grace->update_grace_period_end_time));
$isGracePeriod = $now->between($start, $end);
}
}
@endphp
<div class="mb-2">
{{ $zone->psection_subject }}:空き {{ max(0, $vacant) }}
@if($isGracePeriod)
@if($vacant > 0)
<button type="button" class="btn btn-success btn-sm btn-contract"
data-park-id="{{ $park->park_id }}"
data-psection-id="{{ $zone->psection_id }}"
data-ptype-id="{{ $zone->ptype_id }}">
定期契約
</button>
@else
<button type="button" class="btn btn-danger btn_103 btn-reserve"
data-park-id="{{ $park->park_id }}"
data-psection-id="{{ $zone->psection_id }}"
data-ptype-id="{{ $zone->ptype_id }}">
空き待ち申込
</button>
@endif
@else
<button type="button" class="btn btn-danger btn_103 btn-reserve"
data-park-id="{{ $park->park_id }}"
data-psection-id="{{ $zone->psection_id }}"
data-ptype-id="{{ $zone->ptype_id }}">
販売期間外
</button>
@endif
</div>
@endforeach
</div>
</div> </div>
@endforeach @endforeach
</div> </div>