Add customer contact inquiry system, automated receipt emails, admin communication inbox, and customer profile management
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Mail\ContactInquiryConfirmationMail;
|
||||
use App\Mail\ContactInquiryReceivedMail;
|
||||
use App\Models\Communication;
|
||||
use App\Models\Order;
|
||||
use App\Models\Setting;
|
||||
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 ContactController extends Controller
|
||||
{
|
||||
/**
|
||||
* Show the contact / question form.
|
||||
*/
|
||||
public function index(): View
|
||||
{
|
||||
$user = Auth::user();
|
||||
$userOrders = $user ? $user->orders()->latest()->take(10)->get() : collect();
|
||||
|
||||
return view('contact.index', [
|
||||
'user' => $user,
|
||||
'userOrders' => $userOrders,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the submission of a question / contact inquiry.
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:150',
|
||||
'email' => 'required|email|max:150',
|
||||
'phone' => 'nullable|string|max:40',
|
||||
'subject' => 'required|string|max:200',
|
||||
'order_number' => 'nullable|string|max:50',
|
||||
'message' => 'required|string|min:10|max:5000',
|
||||
], [
|
||||
'name.required' => 'Vul alstublieft jouw naam in.',
|
||||
'email.required' => 'Vul alstublieft een geldig e-mailadres in.',
|
||||
'email.email' => 'Het ingevoerde e-mailadres is niet geldig.',
|
||||
'subject.required' => 'Vul alstublieft het onderwerp van jouw vraag in.',
|
||||
'message.required' => 'Vul alstublieft jouw bericht of vraag in.',
|
||||
'message.min' => 'Het bericht moet minimaal 10 tekens bevatten.',
|
||||
]);
|
||||
|
||||
$orderId = null;
|
||||
if (! empty($validated['order_number'])) {
|
||||
$cleanNumber = ltrim(trim($validated['order_number']), '#');
|
||||
$foundOrder = Order::where('order_number', $cleanNumber)->first();
|
||||
if ($foundOrder) {
|
||||
$orderId = $foundOrder->id;
|
||||
}
|
||||
}
|
||||
|
||||
$communication = Communication::create([
|
||||
'user_id' => Auth::id(),
|
||||
'order_id' => $orderId,
|
||||
'name' => trim($validated['name']),
|
||||
'email' => strtolower(trim($validated['email'])),
|
||||
'phone' => ! empty($validated['phone']) ? trim($validated['phone']) : null,
|
||||
'subject' => trim($validated['subject']),
|
||||
'message' => trim($validated['message']),
|
||||
'status' => 'new',
|
||||
'ip_address' => $request->ip(),
|
||||
'user_agent' => substr((string) $request->userAgent(), 0, 500),
|
||||
]);
|
||||
|
||||
// 1. Send incoming mail to webshop management (info@jouwdrieedee.nl / info@jouwdriedee.nl)
|
||||
try {
|
||||
$shopInbox = Setting::get('contact_email', 'info@jouwdrieedee.nl');
|
||||
// Ensure delivery to both info@jouwdrieedee.nl and fallback info@jouwdriedee.nl
|
||||
$recipients = array_unique([$shopInbox, 'info@jouwdrieedee.nl', 'info@jouwdriedee.nl']);
|
||||
Mail::to($recipients)->send(new ContactInquiryReceivedMail($communication));
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Fout bij versturen van klantvraag naar webshop beheer: '.$e->getMessage(), [
|
||||
'communication_id' => $communication->id,
|
||||
]);
|
||||
}
|
||||
|
||||
// 2. Send afschrift receipt from (noreply@jouwdriedee.nl) to the customer
|
||||
try {
|
||||
Mail::to($communication->email)->send(new ContactInquiryConfirmationMail($communication));
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Fout bij versturen van afschrift naar klant: '.$e->getMessage(), [
|
||||
'communication_id' => $communication->id,
|
||||
'email' => $communication->email,
|
||||
]);
|
||||
}
|
||||
|
||||
return redirect()->route('contact.index')->with(
|
||||
'success',
|
||||
'Bedankt voor je vraag! We hebben jouw bericht ontvangen. Er is een afschrift verzonden naar '.$communication->email.'.'
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user