Updated user management
This commit is contained in:
@@ -41,6 +41,58 @@ class UsersController extends Controller
|
||||
return view('admin.users.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Manually assign a paid subscription to a user from the Admin Panel.
|
||||
*/
|
||||
public function addManualSubscription(Request $request)
|
||||
{
|
||||
// 1. Validate the input
|
||||
$request->validate([
|
||||
'user_id' => 'required|exists:users,id',
|
||||
'subscribe_plan_id' => 'required|exists:subscribe_plans,id',
|
||||
]);
|
||||
|
||||
$user = \App\Models\User::findOrFail($request->user_id);
|
||||
$plan = \App\Models\SubscribePlan::findOrFail($request->subscribe_plan_id);
|
||||
|
||||
// Prevent admins from accidentally adding free plans through this paid-only route
|
||||
if ($plan->is_free) {
|
||||
return back()->with('error', 'برای این عملیات باید یک اشتراک پولی انتخاب کنید.');
|
||||
}
|
||||
|
||||
// 2. Find the latest active PAID subscription to correctly stack the days
|
||||
$latestPaidSub = $user->userSubscribers()
|
||||
->where('expired_at', '>', now())
|
||||
->whereHas('subscribe', function ($query) {
|
||||
$query->where('is_free', false);
|
||||
})
|
||||
->latest('expired_at')
|
||||
->first();
|
||||
|
||||
// If they have an active paid plan, start from its expiration. Otherwise, start from right now.
|
||||
$startDate = $latestPaidSub ? $latestPaidSub->expired_at : now();
|
||||
$expiredAt = $startDate->copy()->addDays($plan->expired_day);
|
||||
|
||||
// 3. CLEANUP: Instantly expire any active FREE plans so they don't pollute the database
|
||||
$user->userSubscribers()
|
||||
->where('expired_at', '>', now())
|
||||
->whereHas('subscribe', function ($query) {
|
||||
$query->where('is_free', true);
|
||||
})
|
||||
->update(['expired_at' => now()]);
|
||||
|
||||
// 4. Create the manual paid subscription
|
||||
$user->userSubscribers()->create([
|
||||
'subscribe_plan_id' => $plan->id,
|
||||
'expired_at' => $expiredAt,
|
||||
'market_provider' => 'admin', // This will cleanly display as "(Admin)" in your dashboard charts!
|
||||
'is_free' => false,
|
||||
]);
|
||||
|
||||
// Redirect back with a success message for your blade view
|
||||
return back()->with('success', 'اشتراک پولی با موفقیت برای کاربر ثبت شد.');
|
||||
}
|
||||
|
||||
public function store(CreateUserRequest $request)
|
||||
{
|
||||
$validated = $request->validated();
|
||||
@@ -53,7 +105,11 @@ class UsersController extends Controller
|
||||
|
||||
public function edit(User $user)
|
||||
{
|
||||
return view('admin.users.update', compact('user'));
|
||||
$paidPlans = \App\Models\SubscribePlan::where('is_free', false)
|
||||
->where('is_active', true)
|
||||
->get();
|
||||
|
||||
return view('admin.users.update', compact('user', 'paidPlans'));
|
||||
}
|
||||
|
||||
public function update(UpdateUserRequest $request, User $user)
|
||||
|
||||
Reference in New Issue
Block a user