First Commit
This commit is contained in:
+140
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\SubscriptionPlan;
|
||||
use App\Models\UserNotification;
|
||||
use App\Models\UserSubscription;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class BillingController extends Controller
|
||||
{
|
||||
public function index(): View
|
||||
{
|
||||
$user = Auth::user();
|
||||
$activeSubscription = UserSubscription::where('user_id', $user->id)
|
||||
->where('status', 'active')
|
||||
->with('plan')
|
||||
->first();
|
||||
|
||||
$subscriptionHistory = UserSubscription::where('user_id', $user->id)
|
||||
->with('plan')
|
||||
->orderByDesc('created_at')
|
||||
->get();
|
||||
|
||||
$invoices = Invoice::where('user_id', $user->id)
|
||||
->with('subscription.plan')
|
||||
->orderByDesc('created_at')
|
||||
->get();
|
||||
|
||||
$plans = SubscriptionPlan::where('is_active', true)->get();
|
||||
|
||||
$notifications = UserNotification::where('user_id', $user->id)
|
||||
->where('is_read', false)
|
||||
->orderByDesc('created_at')
|
||||
->get();
|
||||
|
||||
return view('admin.billing.index', compact(
|
||||
'activeSubscription', 'subscriptionHistory', 'invoices', 'plans', 'notifications'
|
||||
));
|
||||
}
|
||||
|
||||
public function subscribe(Request $request, SubscriptionPlan $plan): \Illuminate\Http\RedirectResponse
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
// Check if user already has active subscription
|
||||
$existing = UserSubscription::where('user_id', $user->id)
|
||||
->where('status', 'active')
|
||||
->first();
|
||||
|
||||
if ($existing) {
|
||||
return back()->with('error', __('You already have an active subscription.'));
|
||||
}
|
||||
|
||||
$subscription = UserSubscription::create([
|
||||
'user_id' => $user->id,
|
||||
'subscription_plan_id' => $plan->id,
|
||||
'start_date' => now(),
|
||||
'end_date' => now()->addDays($plan->duration_days),
|
||||
'status' => 'active',
|
||||
'payment_approved' => false,
|
||||
]);
|
||||
|
||||
// Create pending invoice
|
||||
$lastInvoice = Invoice::orderByDesc('id')->first();
|
||||
$nextNumber = $lastInvoice ? (int)filter_var($lastInvoice->invoice_number, FILTER_SANITIZE_NUMBER_INT) + 1 : 1001;
|
||||
|
||||
Invoice::create([
|
||||
'user_id' => $user->id,
|
||||
'user_subscription_id' => $subscription->id,
|
||||
'invoice_number' => 'INV-' . $nextNumber . '/' . now()->year,
|
||||
'amount' => $plan->price,
|
||||
'currency' => $plan->currency,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
// Notify SuperAdmin
|
||||
UserNotification::create([
|
||||
'user_id' => 1, // SuperAdmin
|
||||
'type' => 'admin_message',
|
||||
'title' => __('New Subscription'),
|
||||
'message' => __('User :name subscribed to plan :plan. Payment pending approval.', [
|
||||
'name' => $user->name,
|
||||
'plan' => $plan->name,
|
||||
]),
|
||||
]);
|
||||
|
||||
return back()->with('success', __('Subscription created successfully. Your payment is pending approval.'));
|
||||
}
|
||||
|
||||
public function downloadInvoice(Invoice $invoice)
|
||||
{
|
||||
if ($invoice->user_id !== Auth::id()) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
if ($invoice->pdf_path && Storage::exists($invoice->pdf_path)) {
|
||||
return Storage::download($invoice->pdf_path, "invoice-{$invoice->invoice_number}.pdf");
|
||||
}
|
||||
|
||||
return back()->with('error', __('Invoice PDF not available yet.'));
|
||||
}
|
||||
|
||||
public function notifications(): View
|
||||
{
|
||||
$user = Auth::user();
|
||||
$notifications = UserNotification::where('user_id', $user->id)
|
||||
->orderByDesc('created_at')
|
||||
->paginate(20);
|
||||
|
||||
return view('admin.billing.notifications', compact('notifications'));
|
||||
}
|
||||
|
||||
public function markNotificationRead(UserNotification $notification): \Illuminate\Http\RedirectResponse
|
||||
{
|
||||
if ($notification->user_id !== Auth::id()) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$notification->markAsRead();
|
||||
|
||||
return back();
|
||||
}
|
||||
|
||||
public function markAllNotificationsRead(): \Illuminate\Http\RedirectResponse
|
||||
{
|
||||
UserNotification::where('user_id', Auth::id())
|
||||
->where('is_read', false)
|
||||
->update(['is_read' => true, 'read_at' => now()]);
|
||||
|
||||
return back()->with('success', __('All notifications marked as read.'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user