146 lines
5.2 KiB
PHP
146 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Facades\Log;
|
|
use App\Mail\WithdrawCompleteMail;
|
|
|
|
class UserWithdrawController extends Controller
|
|
{
|
|
// 退会確認画面表示
|
|
public function showConfirm(Request $request)
|
|
{
|
|
$user_id = session('user_id');
|
|
if (!$user_id) {
|
|
return redirect('/login');
|
|
}
|
|
$user_name = DB::table('user')->where('user_id', $user_id)->value('user_name');
|
|
|
|
\Log::info('退会確認画面にアクセス', [
|
|
'user_id' => $user_id,
|
|
]);
|
|
|
|
return view('user.withdraw_confirm')
|
|
->with([
|
|
'active_menu' => 'SWC-1-1', // マイページメニューの選択状態用
|
|
'user_name' => $user_name ?: '', // ユーザー名(ヘッダー用)
|
|
]);
|
|
}
|
|
|
|
// 退会処理
|
|
public function withdraw(Request $request)
|
|
{
|
|
$user_id = session('user_id');
|
|
if (!$user_id) {
|
|
return redirect('/login');
|
|
}
|
|
|
|
$user = DB::table('user')->where('user_id', $user_id)->first();
|
|
|
|
$now = now();
|
|
|
|
// 利用者マスタの退会フラグを立てる
|
|
DB::table('user')
|
|
->where('user_id', $user_id)
|
|
->update([
|
|
'user_quit_flag' => 1,
|
|
'user_quitday' => $now->format('Y-m-d'),
|
|
'updated_at' => $now,
|
|
]);
|
|
|
|
// 定期空き予約マスタ(reserve)の予約を無効に更新
|
|
DB::table('reserve')
|
|
->where('user_id', $user_id)
|
|
->update([
|
|
'updated_at' => $now,
|
|
'reserve_cancelday' => $now->format('Y-m-d'),
|
|
'valid_flag' => 0,
|
|
]);
|
|
|
|
// 退会完了メール送信
|
|
try {
|
|
Mail::to($user->user_primemail)->send(
|
|
new WithdrawCompleteMail(
|
|
$user->user_name,
|
|
$user->user_primemail,
|
|
$now->format('Y-m-d')
|
|
)
|
|
);
|
|
} catch (\Exception $e) {
|
|
\Log::error('退会完了メール送信失敗: ' . $e->getMessage());
|
|
}
|
|
|
|
// 退会契約チェック
|
|
$contracts = DB::table('regular_contract')
|
|
->where('user_id', $user_id)
|
|
->orderByDesc('contract_id')
|
|
->get();
|
|
|
|
if ($contracts->isEmpty()) {
|
|
// 契約なし→退会完了画面
|
|
\Log::info('退会完了画面にアクセス', [
|
|
'user_id' => $user_id,
|
|
]);
|
|
session()->forget('user_id');
|
|
return view('user.withdraw_complete')->with([
|
|
'active_menu' => 'SWC-1-1', // マイページメニューの選択状態用
|
|
'user_name' => $user ? $user->user_name : '', // ユーザー名(ヘッダー用)
|
|
]);
|
|
}
|
|
|
|
foreach ($contracts as $contract) {
|
|
// 有効性判定
|
|
if ($contract->contract_cancel_flag == 1 || $contract->contract_cancel_flag == 2) {
|
|
continue; // この契約はスキップして次へ
|
|
}
|
|
|
|
// 契約期間のyear/month取得
|
|
$contract_year = $contract->contract_periode ? date('Y', strtotime($contract->contract_periode)) : null;
|
|
$contract_month = $contract->contract_periode ? date('m', strtotime($contract->contract_periode)) : null;
|
|
$today_year = $now->year;
|
|
$today_month = $now->month;
|
|
|
|
// 年月が空欄の場合は次の契約へ
|
|
if (empty($contract_year) || empty($contract_month)) {
|
|
continue;
|
|
}
|
|
|
|
// 今月までなら次の契約へ
|
|
if ($contract_year == $today_year && $contract_month == $today_month) {
|
|
continue;
|
|
}
|
|
|
|
// 翌月以降なら返金処理キュー登録
|
|
if ($contract_year > $today_year || ($contract_year == $today_year && $contract_month > $today_month)) {
|
|
DB::table('operator_que')->insert([
|
|
'que_class' => 13,
|
|
'user_id' => $user_id,
|
|
'contract_id' => $contract->contract_id,
|
|
'park_id' => $contract->park_id,
|
|
'que_comment' => null,
|
|
'que_status' => 1,
|
|
'que_status_comment' => '返金処理が必要な契約があります',
|
|
'work_instructions' => 'お客様にの契約を確認し、退会後の返金処理をおこなってください。',
|
|
'created_at' => $now,
|
|
'updated_at' => $now,
|
|
'operator_id' => null,
|
|
]);
|
|
}
|
|
}
|
|
// 全件確認後に退会完了画面へ
|
|
\Log::info('退会完了画面にアクセス', [
|
|
'user_id' => $user_id,
|
|
]);
|
|
session()->forget('user_id');
|
|
return view('user.withdraw_complete')->with([
|
|
'active_menu' => 'SWC-1-1', // マイページメニューの選択状態用
|
|
'user_name' => $user ? $user->user_name : '', // ユーザー名(ヘッダー用)
|
|
]);
|
|
}
|
|
}
|