131 lines
4.5 KiB
PHP
131 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\admin\user\CreateUserRequest;
|
|
use App\Http\Requests\admin\user\UpdateUserRequest;
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
class UsersController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$query = User::select('id', 'name', 'email', 'mobile', 'is_admin')
|
|
->with(['userSubscribers' => function ($q) {
|
|
$q->where('expired_at', '>=', now());
|
|
}]);
|
|
|
|
if ($request->filled('q')) {
|
|
$q = $request->q;
|
|
$query->where(function ($qry) use ($q) {
|
|
$qry->where('name', 'like', "%{$q}%")
|
|
->orWhere('email', 'like', "%{$q}%")
|
|
->orWhere('mobile', 'like', "%{$q}%");
|
|
});
|
|
}
|
|
if ($request->filled('type') && in_array($request->type, ['0', '1'])) {
|
|
$query->where('is_admin', (bool) $request->type);
|
|
}
|
|
|
|
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
|
|
$users = $query->paginate($perPage)->withQueryString();
|
|
|
|
return view('admin.users.index', compact('users'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
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();
|
|
$validated['password'] = Hash::make(fake()->password);
|
|
$validated['is_admin'] = $validated['is_admin'] == 'on' ? true : false;
|
|
User::query()->create($validated);
|
|
|
|
return redirect(route('users.index'));
|
|
}
|
|
|
|
public function edit(User $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)
|
|
{
|
|
$validated = $request->validated();
|
|
$validated['is_admin'] = $validated['is_admin'] == 'on' ? true : false;
|
|
$user->update($validated);
|
|
|
|
return redirect(route('users.index'));
|
|
}
|
|
|
|
public function destroy(User $user)
|
|
{
|
|
$user->delete();
|
|
|
|
return back();
|
|
}
|
|
}
|