check()) { return auth()->user(); } // Fallback: session-based user (registration auto-login flow) $sess = session('user'); $sessId = is_array($sess) ? ($sess['id'] ?? null) : ($sess->id ?? null); return $sessId ? User::find($sessId) : null; } public function index(Request $request) { $user = $this->resolveCurrentUser(); if (!$user) { return redirect()->route('employer.login'); } // Payments are stored in the subscriptions table during registration $subscriptions = DB::table('subscriptions') ->where('user_id', $user->id) ->latest('id') ->get(); $payments = $subscriptions->map(function ($sub) { $planLabel = ucfirst($sub->plan_id) . ' Sponsor Pass Subscription'; $date = \Carbon\Carbon::parse($sub->starts_at ?? $sub->created_at); return [ 'id' => $sub->id, 'amount' => (float) $sub->amount_aed, 'currency' => 'AED', 'description' => $planLabel, 'status' => $sub->status === 'active' ? 'success' : $sub->status, 'date' => $date->format('M d, Y'), 'time' => $date->format('h:i A'), 'invoice_no' => 'INV-' . str_pad($sub->id, 6, '0', STR_PAD_LEFT), 'transaction_id' => $sub->paytabs_transaction_id, 'expires_at' => $sub->expires_at ? \Carbon\Carbon::parse($sub->expires_at)->format('M d, Y') : null, ]; })->toArray(); // Latest active subscription for the summary cards $activeSub = $subscriptions->where('status', 'active')->first(); $expiresAt = $activeSub && $activeSub->expires_at ? \Carbon\Carbon::parse($activeSub->expires_at)->format('M d, Y') : null; $currentPlan = $activeSub ? ucfirst($activeSub->plan_id) . ' Sponsor Pass' : null; $subStatus = ucfirst($user->subscription_status ?? 'none'); return Inertia::render('Employer/PaymentHistory', [ 'payments' => $payments, 'currentPlan' => $currentPlan, 'expiresAt' => $expiresAt, 'subscriptionStatus' => $subStatus, ]); } public function purchase(Request $request) { $user = $this->resolveCurrentUser(); if (!$user) { return response()->json(['error' => 'Unauthorized'], 401); } $request->validate([ 'plan_id' => 'required|string', ]); $planId = $request->plan_id; $plan = \App\Models\Plan::find($planId); if (!$plan) { return response()->json(['error' => 'Plan not found'], 404); } // Check if there is any active or pending subscription for the user to determine start time $lastSub = \App\Models\Subscription::where('user_id', $user->id) ->whereIn('status', ['active', 'pending']) ->orderBy('expires_at', 'desc') ->first(); $durationDays = strtolower($plan->duration) === 'yearly' ? 365 : 30; if ($lastSub) { // Queue it to start when the current subscription expires $startsAt = \Carbon\Carbon::parse($lastSub->expires_at); $expiresAt = $startsAt->copy()->addDays($durationDays); $status = 'pending'; } else { // Activate immediately $startsAt = now(); $expiresAt = now()->addDays($durationDays); $status = 'active'; } // Store subscription in DB $sub = \App\Models\Subscription::create([ 'user_id' => $user->id, 'plan_id' => $plan->id, 'amount_aed' => $plan->price, 'starts_at' => $startsAt, 'expires_at' => $expiresAt, 'paytabs_transaction_id' => 'PT-' . strtoupper(uniqid()), 'status' => $status ]); // If active, sync user subscription fields if ($status === 'active') { $user->subscription_status = 'active'; $user->subscription_expires_at = $expiresAt; $user->save(); } // Build complete dynamic subscription/invoice history list $invoices = \App\Models\Subscription::leftJoin('plans', 'subscriptions.plan_id', '=', 'plans.id') ->where('subscriptions.user_id', $user->id) ->orderBy('subscriptions.created_at', 'desc') ->select('subscriptions.*', 'plans.name as plan_name') ->get() ->map(function($s) { $planLabel = $s->plan_name ?: (ucfirst($s->plan_id) . ' Pass'); $subStatus = strtolower($s->status); if ($subStatus !== 'active' && $subStatus !== 'pending' && $subStatus !== 'expired') { $subStatus = 'expired'; } return [ 'id' => 'INV-' . date('Y', strtotime($s->created_at)) . '-' . str_pad($s->id, 3, '0', STR_PAD_LEFT), 'plan_name' => $planLabel, 'purchase_date' => date('M d, Y', strtotime($s->created_at)), 'activation_date' => $s->starts_at ? date('M d, Y', strtotime($s->starts_at)) : 'N/A', 'expiry_date' => $s->expires_at ? date('M d, Y', strtotime($s->expires_at)) : 'N/A', 'amount' => round($s->amount_aed) . ' AED', 'payment_status' => 'Paid', 'status' => ucfirst($subStatus), ]; })->toArray(); return response()->json([ 'success' => true, 'message' => 'Subscription purchased successfully!', 'invoices' => $invoices, 'currentPlan' => $status === 'active' ? $plan->name : null, 'expiresAt' => $status === 'active' ? date('Y-m-d', strtotime($expiresAt)) : null, ]); } }