Add customer contact inquiry system, automated receipt emails, admin communication inbox, and customer profile management
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Communication;
|
||||
use App\Models\Order;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class CustomerController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of registered customers and guest buyers.
|
||||
*/
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$search = trim($request->input('q', ''));
|
||||
|
||||
// Query registered non-admin users
|
||||
$usersQuery = User::where('is_admin', false);
|
||||
if ($search !== '') {
|
||||
$usersQuery->where(function ($q) use ($search) {
|
||||
$q->where('name', 'like', "%{$search}%")
|
||||
->orWhere('email', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
$registeredUsers = $usersQuery->withCount(['orders', 'communications'])->latest()->get();
|
||||
|
||||
// Also discover guest customers who placed orders or sent inquiries without an account
|
||||
$registeredEmails = User::pluck('email')->map(fn ($e) => strtolower($e))->toArray();
|
||||
|
||||
$guestOrderEmails = Order::whereNull('user_id')
|
||||
->select('customer_email', 'customer_name', 'customer_phone', 'created_at')
|
||||
->latest()
|
||||
->get()
|
||||
->groupBy(fn ($item) => strtolower($item->customer_email));
|
||||
|
||||
$guestInquiryEmails = Communication::whereNull('user_id')
|
||||
->select('email', 'name', 'phone', 'created_at')
|
||||
->latest()
|
||||
->get()
|
||||
->groupBy(fn ($item) => strtolower($item->email));
|
||||
|
||||
$guestCustomers = collect();
|
||||
$allGuestEmails = $guestOrderEmails->keys()->merge($guestInquiryEmails->keys())->unique();
|
||||
|
||||
foreach ($allGuestEmails as $email) {
|
||||
if (in_array(strtolower($email), $registeredEmails)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($search !== '' && ! str_contains(strtolower($email), strtolower($search))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$orderGroup = $guestOrderEmails->get($email, collect());
|
||||
$inquiryGroup = $guestInquiryEmails->get($email, collect());
|
||||
|
||||
$latestOrder = $orderGroup->first();
|
||||
$latestInquiry = $inquiryGroup->first();
|
||||
|
||||
$name = $latestOrder ? $latestOrder->customer_name : ($latestInquiry ? $latestInquiry->name : 'Gast');
|
||||
$phone = $latestOrder ? $latestOrder->customer_phone : ($latestInquiry ? $latestInquiry->phone : null);
|
||||
$createdAt = $latestOrder ? $latestOrder->created_at : ($latestInquiry ? $latestInquiry->created_at : now());
|
||||
|
||||
$guestCustomers->push((object) [
|
||||
'id' => null,
|
||||
'is_registered' => false,
|
||||
'name' => $name,
|
||||
'email' => $email,
|
||||
'phone' => $phone,
|
||||
'orders_count' => $orderGroup->count(),
|
||||
'communications_count' => $inquiryGroup->count(),
|
||||
'created_at' => $createdAt,
|
||||
]);
|
||||
}
|
||||
|
||||
// Map registered users into unified customer object
|
||||
$registeredCustomers = $registeredUsers->map(function ($u) {
|
||||
return (object) [
|
||||
'id' => $u->id,
|
||||
'is_registered' => true,
|
||||
'name' => $u->name,
|
||||
'email' => $u->email,
|
||||
'phone' => null,
|
||||
'orders_count' => $u->orders_count,
|
||||
'communications_count' => $u->communications_count,
|
||||
'created_at' => $u->created_at,
|
||||
];
|
||||
});
|
||||
|
||||
$allCustomers = $registeredCustomers->concat($guestCustomers)->sortByDesc('created_at')->values();
|
||||
|
||||
// Manual pagination for the unified list
|
||||
$perPage = 20;
|
||||
$page = LengthAwarePaginator::resolveCurrentPage('page');
|
||||
$paginatedCustomers = new LengthAwarePaginator(
|
||||
$allCustomers->forPage($page, $perPage),
|
||||
$allCustomers->count(),
|
||||
$perPage,
|
||||
$page,
|
||||
['path' => route('admin.customers.index'), 'query' => $request->query()]
|
||||
);
|
||||
|
||||
return view('admin.customers.index', [
|
||||
'customers' => $paginatedCustomers,
|
||||
'totalRegistered' => $registeredUsers->count(),
|
||||
'totalGuests' => $guestCustomers->count(),
|
||||
'search' => $search,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a specific customer's profile, including their full communication dossier and orders.
|
||||
*/
|
||||
public function show(string $identifier): View
|
||||
{
|
||||
$user = null;
|
||||
$email = null;
|
||||
|
||||
if (is_numeric($identifier)) {
|
||||
$user = User::find($identifier);
|
||||
if ($user) {
|
||||
$email = strtolower($user->email);
|
||||
}
|
||||
} else {
|
||||
$email = strtolower(urldecode($identifier));
|
||||
$user = User::where('email', $email)->first();
|
||||
}
|
||||
|
||||
if (! $user && ! $email) {
|
||||
abort(404, 'Klant niet gevonden.');
|
||||
}
|
||||
|
||||
// Fetch all communications for this customer (by user_id OR by email)
|
||||
$communicationsQuery = Communication::with(['replies', 'order'])->latest();
|
||||
if ($user) {
|
||||
$communicationsQuery->where(function ($q) use ($user, $email) {
|
||||
$q->where('user_id', $user->id)
|
||||
->orWhere('email', $email);
|
||||
});
|
||||
} else {
|
||||
$communicationsQuery->where('email', $email);
|
||||
}
|
||||
$communications = $communicationsQuery->get();
|
||||
|
||||
// Fetch all orders for this customer (by user_id OR by email)
|
||||
$ordersQuery = Order::with('items')->latest();
|
||||
if ($user) {
|
||||
$ordersQuery->where(function ($q) use ($user, $email) {
|
||||
$q->where('user_id', $user->id)
|
||||
->orWhere('customer_email', $email);
|
||||
});
|
||||
} else {
|
||||
$ordersQuery->where('customer_email', $email);
|
||||
}
|
||||
$orders = $ordersQuery->get();
|
||||
|
||||
$customerName = $user ? $user->name : ($orders->first()?->customer_name ?? $communications->first()?->name ?? 'Klant');
|
||||
$customerPhone = $orders->first()?->customer_phone ?? $communications->first()?->phone ?? null;
|
||||
$totalSpent = $orders->sum('total');
|
||||
|
||||
return view('admin.customers.show', [
|
||||
'user' => $user,
|
||||
'email' => $email,
|
||||
'customerName' => $customerName,
|
||||
'customerPhone' => $customerPhone,
|
||||
'totalSpent' => $totalSpent,
|
||||
'communications' => $communications,
|
||||
'orders' => $orders,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user