Files
cmsi-kchb/app/Http/Controllers/ScreeningAppointmentController.php
2025-06-13 12:29:06 +08:00

218 lines
8.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ScreeningAppointmentRequest;
use App\Models\ScreeningAppointment;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
class ScreeningAppointmentController extends Controller
{
public function info()
{
return view('screeningAppointmentInfo');
}
public function fillForm()
{
$options_limit = [];
for($i = 1; $i <= 8; $i++)
{
$options_limit[$i] = [];
for($j = 14; $j <= 17; $j++)
{
$item = (new ScreeningAppointment())->where('screening_appointment_catalog_id', '=', $i);
$preOrderTimeCount = $item->where('pre_order_time','=', $j)->count();
$options_limit[$i][$j] = $preOrderTimeCount;
switch ($i)
{
case 1:
$options_limit[$i][$j] -= 15;
break;
case 2:
$options_limit[$i][$j] -= 10;
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
$options_limit[$i][$j] -= 60;
break;
case 8:
$options_limit[$i][$j] -= 4;
break;
}
}
}
return view('screeningAppointmentFillForm', ['options_limit' => $options_limit]);
}
public function fillFormPost(ScreeningAppointmentRequest $request)
{
try
{
throw new \Exception('活動預約已截止,當日可至現場參與。');
DB::beginTransaction();
//$behaviors = $request->input('behaviors');
//$behaviorStr = implode(',', $behaviors);
$behaviorStr = "";
$historyStr = "";
// $histories = $request->input('history');
// foreach($histories as $history)
// {
// if ($historyStr != "")
// {
// $historyStr .= ',' . $history;
// }
// else
// {
// $historyStr = $history;
// }
// switch ($history)
// {
// case '乳癌':
// $historyStr .= '-' . $request->input('history_y1');
// break;
// case '子宮頸癌':
// $historyStr .= '-' . $request->input('history_y2');
// break;
// case '大腸癌':
// $historyStr .= '-' . $request->input('history_y3');
// break;
// case '口腔癌':
// $historyStr .= '-' . $request->input('history_y4');
// break;
// }
// }
$cids = $request->input('screening_appointment_catalog_id');
foreach ($cids as $cid)
{
$item = (new ScreeningAppointment())->where('id_number','=', $request->input('id_number'));
$item = $item->where('screening_appointment_catalog_id', '=', $cid);
$item = $item->first();
if ($item)
{
throw new \Exception('您已經登記過這個篩檢項目');
}
$item = (new ScreeningAppointment())->where('screening_appointment_catalog_id', '=', $cid);
switch($cid)
{
case 1:
$preOrderTime = $request->input('pre_order_time_1');
if ($preOrderTime == null)
{
throw new \Exception('請選乳癌篩檢時段');
}
$preOrderTimeCount = $item->where('pre_order_time','=', $preOrderTime)->count();
if ($preOrderTimeCount >= 15)
{
throw new \Exception('乳癌篩檢該時段已額滿');
}
break;
case 2:
$preOrderTime = $request->input('pre_order_time_2');
$preOrderTimeCount = $item->where('pre_order_time','=', $preOrderTime)->count();
if ($preOrderTime == null)
{
throw new \Exception('請選子宮頸癌篩檢時段');
}
if ($preOrderTimeCount >= 10)
{
throw new \Exception('子宮頸癌篩檢該時段已額滿');
}
break;
case 3:
$preOrderTime = $request->input('pre_order_time_3');
if ($preOrderTime == null)
{
throw new \Exception('請選定量免疫糞便潛血檢查時段');
}
break;
case 4:
$preOrderTime = $request->input('pre_order_time_4');
if ($preOrderTime == null)
{
throw new \Exception('請選擇口腔黏膜檢查時段');
}
break;
case 5:
$preOrderTime = $request->input('pre_order_time_5');
if ($preOrderTime == null)
{
throw new \Exception('請選擇B、C肝炎篩檢時段');
}
break;
case 6:
$preOrderTime = $request->input('pre_order_time_6');
break;
case 7:
$preOrderTime = $request->input('pre_order_time_7');
if ($preOrderTime == null)
{
throw new \Exception('請選擇胸部X光及骨質疏鬆症AI篩檢時段');
}
$preOrderTimeCount = $item->where('pre_order_time','=', $preOrderTime)->count();
if ($preOrderTimeCount >= 60)
{
throw new \Exception('胸部X光及骨質疏鬆症AI篩檢該時段已額滿');
}
break;
case 8:
$preOrderTime = $request->input('pre_order_time_8');
$preOrderTimeCount = $item->where('pre_order_time','=', $preOrderTime)->count();
if ($preOrderTimeCount >= 4)
{
throw new \Exception('骨質疏鬆症AI篩檢該時段已額滿');
}
break;
}
ScreeningAppointment::insert([
'email' => $request->input('email'),
'name' => $request->input('name'),
'phone' => $request->input('phone'),
'id_number' => $request->input('id_number'),
'screening_appointment_catalog_id' => $cid,
'birth' => $request->input('birth'),
'address' => $request->input('address'),
'pre_order_time' => $preOrderTime,
'behaviors' => $behaviorStr,
'history' => $historyStr,
'created_at' => Carbon::now(),
]);
}
DB::commit();
}
catch (\Exception $ex)
{
DB::rollBack();
return redirect()->back()->withErrors([$ex->getMessage()])->withInput(Request()->all());
}
return redirect()->back()->with('success', '登記成功!');
}
public function location()
{
return view('screeningAppointmentLocation');
}
}