You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
5.7 KiB
PHTML
172 lines
5.7 KiB
PHTML
1 year ago
|
<?php
|
||
|
|
||
1 year ago
|
namespace App\Http\Controllers\Admin;
|
||
1 year ago
|
|
||
1 year ago
|
use App\Http\Controllers\Controller;
|
||
1 year ago
|
use App\Models\Customer;
|
||
1 year ago
|
use App\Models\CustomerLevel;
|
||
1 year ago
|
use Illuminate\Http\Request;
|
||
1 year ago
|
use Illuminate\Support\Facades\Hash;
|
||
1 year ago
|
|
||
|
class CustomerController extends Controller
|
||
|
{
|
||
|
public function index(Request $request)
|
||
|
{
|
||
1 year ago
|
$stats = [
|
||
|
'basic_count' => Customer::whereHas('level', fn ($q) => $q->where('key', CustomerLevel::BASIC))->count(),
|
||
|
'silver_count' => Customer::whereHas('level', fn ($q) => $q->where('key', CustomerLevel::SILVER))->count(),
|
||
|
'gold_count' => Customer::whereHas('level', fn ($q) => $q->where('key', CustomerLevel::GOLD))->count(),
|
||
|
'platinum_count' => Customer::whereHas('level', fn ($q) => $q->where('key', CustomerLevel::PLATINUM))->count(),
|
||
|
];
|
||
|
|
||
|
$query = Customer::query()->with(['level', 'paylater', 'locationFavorites']);
|
||
1 year ago
|
|
||
|
if ($request->q != '') {
|
||
1 year ago
|
$query->where(function ($query) use ($request) {
|
||
|
$query->where('name', 'like', "%$request->q%")
|
||
|
->orWhere('fullname', 'like', "%$request->q%")
|
||
|
->orWhere('email', 'like', "%$request->q%")
|
||
|
->orWhere('phone', 'like', "%$request->q%");
|
||
|
});
|
||
|
}
|
||
|
|
||
|
if ($request->location_id != '') {
|
||
|
$query->whereHas('locationFavorites', fn ($q) => $q->where('id', $request->location_id));
|
||
|
}
|
||
|
|
||
|
if ($request->level_id != '') {
|
||
|
$query->where('customer_level_id', $request->level_id);
|
||
|
}
|
||
|
|
||
|
if ($request->sortBy != '' && $request->sortRule != '') {
|
||
|
$query->orderBy($request->sortBy, $request->sortRule);
|
||
|
} else {
|
||
|
$query->orderBy('updated_at', 'desc');
|
||
1 year ago
|
}
|
||
|
|
||
|
return inertia('Customer/Index', [
|
||
|
'query' => $query->paginate(),
|
||
1 year ago
|
'stats' => $stats,
|
||
1 year ago
|
]);
|
||
|
}
|
||
|
|
||
|
public function create()
|
||
|
{
|
||
1 year ago
|
return inertia('Customer/Form', [
|
||
|
'levels' => CustomerLevel::all(),
|
||
|
'statuses' => Customer::STATUS
|
||
|
]);
|
||
1 year ago
|
}
|
||
|
|
||
|
public function store(Request $request)
|
||
|
{
|
||
|
$request->validate([
|
||
|
'username' => 'required|string|min:5|alpha_dash|unique:customers,username',
|
||
|
'password' => 'required|string|min:8',
|
||
|
'name' => 'required|string',
|
||
|
'fullname' => 'required|string',
|
||
|
'address' => 'required|string',
|
||
|
'phone' => 'required|string',
|
||
1 year ago
|
'image' => 'nullable|image',
|
||
|
'status' => 'required|numeric',
|
||
1 year ago
|
]);
|
||
|
|
||
1 year ago
|
$customer = Customer::make([
|
||
1 year ago
|
'username' => $request->username,
|
||
|
'password' => bcrypt($request->password),
|
||
|
'name' => $request->name,
|
||
|
'fullname' => $request->fullname,
|
||
|
'address' => $request->address,
|
||
|
'phone' => $request->phone,
|
||
1 year ago
|
'status' => $request->status,
|
||
1 year ago
|
]);
|
||
|
|
||
1 year ago
|
if ($request->hasFile('image')) {
|
||
|
$file = $request->file('image');
|
||
|
$file->store('uploads', 'public');
|
||
|
$customer->image = $file->hashName('uploads');
|
||
|
}
|
||
|
|
||
|
$customer->save();
|
||
|
|
||
1 year ago
|
return redirect()->route('customer.index')
|
||
|
->with('message', ['type' => 'success', 'message' => 'Item has beed saved']);
|
||
|
}
|
||
|
|
||
|
public function edit(Customer $customer)
|
||
|
{
|
||
|
return inertia('Customer/Form', [
|
||
1 year ago
|
'customer' => $customer->load(['level']),
|
||
1 year ago
|
'levels' => CustomerLevel::all(),
|
||
1 year ago
|
'statuses' => Customer::STATUS
|
||
1 year ago
|
]);
|
||
|
}
|
||
|
|
||
|
public function update(Request $request, Customer $customer)
|
||
|
{
|
||
|
$request->validate([
|
||
1 year ago
|
'username' => 'required|string|min:5|alpha_dash|unique:customers,username,' . $customer->id,
|
||
1 year ago
|
'password' => 'nullable|string|min:8',
|
||
|
'name' => 'required|string',
|
||
|
'fullname' => 'required|string',
|
||
|
'address' => 'required|string',
|
||
|
'phone' => 'required|string',
|
||
|
'image' => 'nullable|image',
|
||
1 year ago
|
'status' => 'required|numeric'
|
||
1 year ago
|
]);
|
||
|
|
||
|
if ($request->password != '') {
|
||
1 year ago
|
$customer->password = Hash::make($request->password);
|
||
1 year ago
|
}
|
||
|
|
||
|
if ($request->hasFile('image')) {
|
||
|
$file = $request->file('image');
|
||
|
$file->store('uploads', 'public');
|
||
|
$customer->image = $file->hashName('uploads');
|
||
|
}
|
||
|
|
||
|
$customer->update([
|
||
|
'username' => $request->username,
|
||
|
'password' => $customer->password,
|
||
|
'name' => $request->name,
|
||
|
'fullname' => $request->fullname,
|
||
|
'address' => $request->address,
|
||
|
'phone' => $request->phone,
|
||
|
'image' => $customer->image,
|
||
1 year ago
|
'status' => $request->status
|
||
1 year ago
|
]);
|
||
|
|
||
|
return redirect()->route('customer.index')
|
||
|
->with('message', ['type' => 'success', 'message' => 'Item has beed updated']);
|
||
|
}
|
||
|
|
||
|
public function destroy(Customer $customer)
|
||
|
{
|
||
|
$customer->delete();
|
||
|
|
||
|
return redirect()->route('customer.index')
|
||
|
->with('message', ['type' => 'success', 'message' => 'Item has beed deleted']);
|
||
|
}
|
||
1 year ago
|
|
||
|
public function update_level(Request $request, Customer $customer)
|
||
|
{
|
||
|
$request->validate([
|
||
|
'level' => 'required|exists:customer_levels,key',
|
||
|
'paylater_limit' => 'required|numeric',
|
||
|
]);
|
||
|
|
||
|
$level = CustomerLevel::where('key', $request->level)->first();
|
||
|
|
||
|
$customer->update(['customer_level_id' => $level->id]);
|
||
|
|
||
|
$customer->paylater()->updateOrCreate([
|
||
|
'customer_id' => $customer->id,
|
||
|
], [
|
||
1 year ago
|
'limit' => $request->paylater_limit,
|
||
1 year ago
|
]);
|
||
|
|
||
|
return redirect()->route('customer.index')
|
||
|
->with('message', ['type' => 'success', 'message' => 'Item has beed updated']);
|
||
|
}
|
||
1 year ago
|
}
|