schneespur/app/Http/Controllers/Admin/CustomerReportEmailController.php
Michael 2c63440ed8 Revert: move code back to project root from schneespur/ subdirectory
- Reverts the schneespur/ subdirectory restructure (b8e426b)
- Restores package.json and vite.config.js (needed for npm build, were
  removed in an earlier cleanup before the restructure)
- Updates public/build/ assets with current Vite output (new content hashes)
2026-05-17 18:24:26 +00:00

49 lines
1.8 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Jobs\SendCustomerReportEmail;
use App\Models\Customer;
use App\Models\CustomerObject;
use App\Services\NotificationLogService;
use Carbon\Carbon;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
class CustomerReportEmailController extends Controller
{
public function send(Request $request, NotificationLogService $notificationLogService): RedirectResponse
{
$validated = $request->validate([
'customer_id' => ['required', 'exists:customers,id'],
'customer_object_id' => ['nullable', 'exists:customer_objects,id'],
'from' => ['required', 'date'],
'to' => ['required', 'date', 'after_or_equal:from'],
]);
$customer = Customer::findOrFail($validated['customer_id']);
$object = isset($validated['customer_object_id'])
? CustomerObject::findOrFail($validated['customer_object_id'])
: null;
$recipient = $object
? ($object->contact_email ?? $customer->notification_email ?? $customer->email)
: ($customer->notification_email ?? $customer->email);
if (empty($recipient)) {
return redirect()->back()->with('error', __('notification.customer_report_email_no_email'));
}
$from = Carbon::parse($validated['from']);
$to = Carbon::parse($validated['to']);
if ($notificationLogService->wasRecentlySentToCustomer($customer, 'customer_report_email', $from, $to)) {
return redirect()->back()->with('error', __('notification.customer_report_email_duplicate'));
}
SendCustomerReportEmail::dispatch($customer, $from, $to, $object);
return redirect()->back()->with('success', __('notification.customer_report_email_sent'));
}
}