140 lines
4.8 KiB
PHP
140 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Mail\ContactInquiryReplyMail;
|
|
use App\Models\Communication;
|
|
use App\Models\CommunicationReply;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\View\View;
|
|
|
|
class CommunicationController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of all customer communications / inquiries.
|
|
*/
|
|
public function index(Request $request): View
|
|
{
|
|
$query = Communication::with(['user', 'order', 'replies'])->latest();
|
|
|
|
// Status filter
|
|
if ($request->filled('status') && $request->input('status') !== 'all') {
|
|
$query->where('status', $request->input('status'));
|
|
}
|
|
|
|
// Search query
|
|
if ($request->filled('q')) {
|
|
$search = trim($request->input('q'));
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('name', 'like', "%{$search}%")
|
|
->orWhere('email', 'like', "%{$search}%")
|
|
->orWhere('subject', 'like', "%{$search}%")
|
|
->orWhere('message', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
$communications = $query->paginate(20)->withQueryString();
|
|
|
|
$counts = [
|
|
'all' => Communication::count(),
|
|
'new' => Communication::where('status', 'new')->count(),
|
|
'in_progress' => Communication::where('status', 'in_progress')->count(),
|
|
'resolved' => Communication::where('status', 'resolved')->count(),
|
|
'closed' => Communication::where('status', 'closed')->count(),
|
|
];
|
|
|
|
return view('admin.communications.index', [
|
|
'communications' => $communications,
|
|
'counts' => $counts,
|
|
'currentStatus' => $request->input('status', 'all'),
|
|
'search' => $request->input('q', ''),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Display the specified communication thread.
|
|
*/
|
|
public function show(Communication $communication): View
|
|
{
|
|
$communication->load(['user.orders', 'order.items', 'replies.user']);
|
|
|
|
// Mark as in_progress if still new when viewed by admin
|
|
if ($communication->status === 'new') {
|
|
$communication->update(['status' => 'in_progress']);
|
|
}
|
|
|
|
return view('admin.communications.show', [
|
|
'communication' => $communication,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the status and/or admin notes for this communication.
|
|
*/
|
|
public function updateStatus(Request $request, Communication $communication): RedirectResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'status' => 'required|string|in:new,in_progress,resolved,closed',
|
|
'admin_notes' => 'nullable|string|max:5000',
|
|
]);
|
|
|
|
$communication->update($validated);
|
|
|
|
return back()->with('success', 'Status van het bericht is bijgewerkt.');
|
|
}
|
|
|
|
/**
|
|
* Send a reply to the customer from the admin dashboard.
|
|
*/
|
|
public function sendReply(Request $request, Communication $communication): RedirectResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'message' => 'required|string|min:5|max:5000',
|
|
], [
|
|
'message.required' => 'Vul alstublieft een antwoordbericht in.',
|
|
'message.min' => 'Het antwoordbericht moet minimaal 5 tekens bevatten.',
|
|
]);
|
|
|
|
$reply = CommunicationReply::create([
|
|
'communication_id' => $communication->id,
|
|
'user_id' => Auth::id(),
|
|
'is_admin_reply' => true,
|
|
'message' => trim($validated['message']),
|
|
]);
|
|
|
|
$communication->update([
|
|
'status' => 'resolved',
|
|
'last_replied_at' => now(),
|
|
]);
|
|
|
|
// Send email to customer from info@jouwdriedee.nl
|
|
try {
|
|
Mail::to($communication->email)->send(new ContactInquiryReplyMail($communication, $reply));
|
|
} catch (\Throwable $e) {
|
|
Log::error('Fout bij verzenden van admin reactie naar klant: '.$e->getMessage(), [
|
|
'communication_id' => $communication->id,
|
|
'email' => $communication->email,
|
|
]);
|
|
|
|
return back()->with('warning', 'Het antwoord is opgeslagen in de database, maar er trad een fout op bij het verzenden van de e-mail: '.$e->getMessage());
|
|
}
|
|
|
|
return back()->with('success', 'Jouw antwoord is verzonden naar '.$communication->email.' en toegevoegd aan het communicatiedossier.');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified communication from storage.
|
|
*/
|
|
public function destroy(Communication $communication): RedirectResponse
|
|
{
|
|
$communication->delete();
|
|
|
|
return redirect()->route('admin.communications.index')->with('success', 'Klantbericht is verwijderd.');
|
|
}
|
|
}
|