Fix Pint formatting
This commit is contained in:
@@ -11,9 +11,7 @@ use ZipArchive;
|
||||
|
||||
class ReimbursementEmailService
|
||||
{
|
||||
public function __construct(private ReimbursementRecipientService $recipients)
|
||||
{
|
||||
}
|
||||
public function __construct(private ReimbursementRecipientService $recipients) {}
|
||||
|
||||
public function sendBatchEmail(
|
||||
int $batchId,
|
||||
@@ -23,14 +21,14 @@ class ReimbursementEmailService
|
||||
array $checkIds
|
||||
): bool {
|
||||
$batch = ReimbursementBatch::query()->find($batchId);
|
||||
if (!$batch) {
|
||||
if (! $batch) {
|
||||
throw new \RuntimeException('Requested batch was not found.');
|
||||
}
|
||||
|
||||
$receiptRows = !empty($receiptIds) ? $this->fetchBatchReceiptRows($batchId, $receiptIds) : [];
|
||||
$receiptRows = ! empty($receiptIds) ? $this->fetchBatchReceiptRows($batchId, $receiptIds) : [];
|
||||
|
||||
$checkRows = [];
|
||||
if (!empty($checkIds)) {
|
||||
if (! empty($checkIds)) {
|
||||
$checkRows = ReimbursementBatchAdminFile::query()
|
||||
->select('id as file_id', 'admin_id', 'filename', 'original_filename', 'uploaded_at')
|
||||
->where('batch_id', $batchId)
|
||||
@@ -40,29 +38,29 @@ class ReimbursementEmailService
|
||||
|
||||
$adminLookup = [];
|
||||
foreach ($this->recipients->adminRoster() as $admin) {
|
||||
$adminLookup[(int) ($admin['id'] ?? 0)] = trim((string) (($admin['firstname'] ?? '') . ' ' . ($admin['lastname'] ?? '')));
|
||||
$adminLookup[(int) ($admin['id'] ?? 0)] = trim((string) (($admin['firstname'] ?? '').' '.($admin['lastname'] ?? '')));
|
||||
}
|
||||
foreach ($checkRows as &$row) {
|
||||
$aid = (int) ($row['admin_id'] ?? 0);
|
||||
$row['admin'] = $adminLookup[$aid] ?? ($aid > 0 ? 'Admin #' . $aid : 'Admin');
|
||||
$row['admin'] = $adminLookup[$aid] ?? ($aid > 0 ? 'Admin #'.$aid : 'Admin');
|
||||
}
|
||||
unset($row);
|
||||
}
|
||||
|
||||
$tempDir = storage_path('tmp');
|
||||
if (!is_dir($tempDir)) {
|
||||
if (! is_dir($tempDir)) {
|
||||
mkdir($tempDir, 0755, true);
|
||||
}
|
||||
|
||||
$cleanupPaths = [];
|
||||
|
||||
$batchLabel = trim((string) ($batch->title ?? '')) ?: ('Batch #' . ((int) ($batch->yearly_batch_number ?? $batchId)));
|
||||
$batchLabel = trim((string) ($batch->title ?? '')) ?: ('Batch #'.((int) ($batch->yearly_batch_number ?? $batchId)));
|
||||
$batchFileLabel = $this->sanitizeAttachmentComponent($batchLabel);
|
||||
|
||||
$csvPath = tempnam($tempDir, 'batch_csv_');
|
||||
$cleanupPaths[] = $csvPath;
|
||||
$csvHandle = fopen($csvPath, 'w');
|
||||
if (!$csvHandle) {
|
||||
if (! $csvHandle) {
|
||||
if (is_file($csvPath)) {
|
||||
@unlink($csvPath);
|
||||
}
|
||||
@@ -74,31 +72,31 @@ class ReimbursementEmailService
|
||||
$this->writeBatchCsvSummary($csvHandle, $receiptRows, $batchClosedDate);
|
||||
fclose($csvHandle);
|
||||
|
||||
$csvAttachmentName = $batchFileLabel . '_summary.csv';
|
||||
$csvAttachmentName = $batchFileLabel.'_summary.csv';
|
||||
|
||||
$receiptsArchivePath = null;
|
||||
$receiptZipCreated = false;
|
||||
if (!empty($receiptRows)) {
|
||||
$archivePath = $tempDir . '/batch_receipts_' . $batchId . '_' . date('Ymd_His') . '_' . bin2hex(random_bytes(4)) . '.zip';
|
||||
$zip = new ZipArchive();
|
||||
if (! empty($receiptRows)) {
|
||||
$archivePath = $tempDir.'/batch_receipts_'.$batchId.'_'.date('Ymd_His').'_'.bin2hex(random_bytes(4)).'.zip';
|
||||
$zip = new ZipArchive;
|
||||
$added = 0;
|
||||
$usedNames = [];
|
||||
if ($zip->open($archivePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
|
||||
foreach ($receiptRows as $row) {
|
||||
$sourcePath = storage_path('uploads/receipts/' . basename((string) ($row['receipt_filename'] ?? '')));
|
||||
if (!is_file($sourcePath)) {
|
||||
$sourcePath = storage_path('uploads/receipts/'.basename((string) ($row['receipt_filename'] ?? '')));
|
||||
if (! is_file($sourcePath)) {
|
||||
continue;
|
||||
}
|
||||
$firstName = $row['purchaser_firstname'] ?? '';
|
||||
if (trim($firstName) === '' && !empty($row['purchaser_lastname'] ?? '')) {
|
||||
if (trim($firstName) === '' && ! empty($row['purchaser_lastname'] ?? '')) {
|
||||
$firstName = $row['purchaser_lastname'];
|
||||
}
|
||||
$firstName = $this->sanitizeAttachmentComponent($firstName);
|
||||
$vendorPart = $this->sanitizeAttachmentComponent($row['retailor'] ?? '');
|
||||
$datePart = $this->formatAttachmentDate($row['purchase_date'] ?? '');
|
||||
$extension = pathinfo($sourcePath, PATHINFO_EXTENSION);
|
||||
$extPart = $extension !== '' ? ('.' . strtolower($extension)) : '';
|
||||
$baseName = $firstName . '_' . $vendorPart . '_' . $datePart;
|
||||
$extPart = $extension !== '' ? ('.'.strtolower($extension)) : '';
|
||||
$baseName = $firstName.'_'.$vendorPart.'_'.$datePart;
|
||||
$entryName = $this->uniqueZipEntryName($baseName, $extPart, $usedNames);
|
||||
if ($zip->addFile($sourcePath, $entryName)) {
|
||||
$added++;
|
||||
@@ -117,21 +115,21 @@ class ReimbursementEmailService
|
||||
|
||||
$checksArchivePath = null;
|
||||
$checksAdded = 0;
|
||||
if (!empty($checkRows)) {
|
||||
$archivePath = $tempDir . '/batch_checks_' . $batchId . '_' . date('Ymd_His') . '_' . bin2hex(random_bytes(4)) . '.zip';
|
||||
$zip = new ZipArchive();
|
||||
if (! empty($checkRows)) {
|
||||
$archivePath = $tempDir.'/batch_checks_'.$batchId.'_'.date('Ymd_His').'_'.bin2hex(random_bytes(4)).'.zip';
|
||||
$zip = new ZipArchive;
|
||||
$usedNames = [];
|
||||
if ($zip->open($archivePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
|
||||
foreach ($checkRows as $row) {
|
||||
$sourcePath = storage_path('uploads/reimbursements/' . basename((string) ($row['filename'] ?? '')));
|
||||
if (!is_file($sourcePath)) {
|
||||
$sourcePath = storage_path('uploads/reimbursements/'.basename((string) ($row['filename'] ?? '')));
|
||||
if (! is_file($sourcePath)) {
|
||||
continue;
|
||||
}
|
||||
$adminLabel = $row['admin'] ?? 'Admin';
|
||||
$firstToken = strtok($adminLabel, ' ');
|
||||
$namePart = $this->sanitizeAttachmentComponent($firstToken ?: $adminLabel);
|
||||
$extension = pathinfo($sourcePath, PATHINFO_EXTENSION);
|
||||
$extPart = $extension !== '' ? ('.' . strtolower($extension)) : '';
|
||||
$extPart = $extension !== '' ? ('.'.strtolower($extension)) : '';
|
||||
$entryName = $this->uniqueZipEntryName($namePart, $extPart, $usedNames);
|
||||
if ($zip->addFile($sourcePath, $entryName)) {
|
||||
$checksAdded++;
|
||||
@@ -155,14 +153,14 @@ class ReimbursementEmailService
|
||||
$attachmentSummary[] = 'Check Scans';
|
||||
}
|
||||
|
||||
$subject = 'Reimbursement Batch Export: ' . $batchLabel;
|
||||
$subject = 'Reimbursement Batch Export: '.$batchLabel;
|
||||
$bodyParts = [
|
||||
'<p>Please find attached the export for <strong>' . htmlspecialchars($batchLabel, ENT_QUOTES, 'UTF-8') . '</strong>.</p>',
|
||||
'<p>Please find attached the export for <strong>'.htmlspecialchars($batchLabel, ENT_QUOTES, 'UTF-8').'</strong>.</p>',
|
||||
];
|
||||
if (trim($message) !== '') {
|
||||
$bodyParts[] = '<p><strong>Message:</strong><br>' . nl2br(htmlspecialchars($message, ENT_QUOTES, 'UTF-8')) . '</p>';
|
||||
$bodyParts[] = '<p><strong>Message:</strong><br>'.nl2br(htmlspecialchars($message, ENT_QUOTES, 'UTF-8')).'</p>';
|
||||
}
|
||||
$bodyParts[] = '<p>Attachments: ' . htmlspecialchars(implode(', ', $attachmentSummary), ENT_QUOTES, 'UTF-8') . '.</p>';
|
||||
$bodyParts[] = '<p>Attachments: '.htmlspecialchars(implode(', ', $attachmentSummary), ENT_QUOTES, 'UTF-8').'.</p>';
|
||||
$bodyParts[] = '<p>Regards,<br>Al Rahma Sunday School</p>';
|
||||
$htmlBody = implode("\n", $bodyParts);
|
||||
|
||||
@@ -170,10 +168,10 @@ class ReimbursementEmailService
|
||||
['path' => $csvPath, 'name' => $csvAttachmentName],
|
||||
];
|
||||
if ($receiptsArchivePath) {
|
||||
$attachments[] = ['path' => $receiptsArchivePath, 'name' => $batchFileLabel . '_receipts.zip'];
|
||||
$attachments[] = ['path' => $receiptsArchivePath, 'name' => $batchFileLabel.'_receipts.zip'];
|
||||
}
|
||||
if ($checksArchivePath) {
|
||||
$attachments[] = ['path' => $checksArchivePath, 'name' => $batchFileLabel . '_checks.zip'];
|
||||
$attachments[] = ['path' => $checksArchivePath, 'name' => $batchFileLabel.'_checks.zip'];
|
||||
}
|
||||
|
||||
$sender = $this->resolveSender('general');
|
||||
@@ -181,11 +179,11 @@ class ReimbursementEmailService
|
||||
try {
|
||||
Mail::html($htmlBody, function ($message) use ($recipientEmail, $subject, $sender, $attachments) {
|
||||
$message->to($recipientEmail)->subject($subject);
|
||||
if (!empty($sender['email'])) {
|
||||
if (! empty($sender['email'])) {
|
||||
$message->from($sender['email'], $sender['name']);
|
||||
}
|
||||
foreach ($attachments as $file) {
|
||||
if (!empty($file['path']) && is_file($file['path'])) {
|
||||
if (! empty($file['path']) && is_file($file['path'])) {
|
||||
$message->attach($file['path'], ['as' => $file['name'] ?? basename($file['path'])]);
|
||||
}
|
||||
}
|
||||
@@ -193,7 +191,8 @@ class ReimbursementEmailService
|
||||
|
||||
return true;
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Failed to send batch export email: ' . $e->getMessage());
|
||||
Log::error('Failed to send batch export email: '.$e->getMessage());
|
||||
|
||||
return false;
|
||||
} finally {
|
||||
foreach ($cleanupPaths as $path) {
|
||||
@@ -212,7 +211,7 @@ class ReimbursementEmailService
|
||||
->leftJoin('users as u', 'u.id', '=', 'e.purchased_by')
|
||||
->where('bi.batch_id', $batchId);
|
||||
|
||||
if (!empty($receiptIds)) {
|
||||
if (! empty($receiptIds)) {
|
||||
$builder->whereIn('e.id', $receiptIds);
|
||||
}
|
||||
|
||||
@@ -230,7 +229,7 @@ class ReimbursementEmailService
|
||||
'purchaser_lastname' => $row->purchaser_lastname ?? null,
|
||||
];
|
||||
}, $rows), static function ($row) {
|
||||
return !empty($row['receipt_filename']);
|
||||
return ! empty($row['receipt_filename']);
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -247,7 +246,7 @@ class ReimbursementEmailService
|
||||
{
|
||||
$receiptTotalsByRecipient = [];
|
||||
foreach ($receiptRows as $row) {
|
||||
$recipientName = trim(($row['purchaser_firstname'] ?? '') . ' ' . ($row['purchaser_lastname'] ?? ''));
|
||||
$recipientName = trim(($row['purchaser_firstname'] ?? '').' '.($row['purchaser_lastname'] ?? ''));
|
||||
if ($recipientName === '') {
|
||||
$recipientName = 'Unknown';
|
||||
}
|
||||
@@ -271,7 +270,7 @@ class ReimbursementEmailService
|
||||
implode(' | ', $descriptionParts),
|
||||
]);
|
||||
}
|
||||
if (!empty($receiptTotalsByRecipient)) {
|
||||
if (! empty($receiptTotalsByRecipient)) {
|
||||
fputcsv($handle, []);
|
||||
fputcsv($handle, ['Totals by Recipient', '', '', '', '', '', '', '']);
|
||||
foreach ($receiptTotalsByRecipient as $recipient => $total) {
|
||||
@@ -294,42 +293,46 @@ class ReimbursementEmailService
|
||||
{
|
||||
$clean = preg_replace('/[^A-Za-z0-9]+/', '_', trim((string) $value));
|
||||
$clean = trim($clean, '_');
|
||||
|
||||
return $clean === '' ? 'unknown' : $clean;
|
||||
}
|
||||
|
||||
private function formatAttachmentDate(?string $value): string
|
||||
{
|
||||
if (!$value) {
|
||||
if (! $value) {
|
||||
return 'unknown';
|
||||
}
|
||||
$timestamp = strtotime($value);
|
||||
if ($timestamp === false) {
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
return date('Ymd', $timestamp);
|
||||
}
|
||||
|
||||
private function uniqueZipEntryName(string $base, string $extension, array &$used): string
|
||||
{
|
||||
$candidate = $base . $extension;
|
||||
$candidate = $base.$extension;
|
||||
$counter = 1;
|
||||
while (isset($used[$candidate])) {
|
||||
$candidate = $base . '_' . $counter . $extension;
|
||||
$candidate = $base.'_'.$counter.$extension;
|
||||
$counter++;
|
||||
}
|
||||
$used[$candidate] = true;
|
||||
|
||||
return $candidate;
|
||||
}
|
||||
|
||||
private function formatCsvDate(?string $value): string
|
||||
{
|
||||
if (!$value) {
|
||||
if (! $value) {
|
||||
return '';
|
||||
}
|
||||
$timestamp = strtotime($value);
|
||||
if ($timestamp === false) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return date('m-d-Y', $timestamp);
|
||||
}
|
||||
|
||||
@@ -341,6 +344,7 @@ class ReimbursementEmailService
|
||||
|
||||
if (is_array($senders) && isset($senders[$fromKey])) {
|
||||
$info = $senders[$fromKey];
|
||||
|
||||
return [
|
||||
'name' => (string) ($info['name'] ?? 'Sender'),
|
||||
'email' => (string) ($info['email'] ?? ''),
|
||||
|
||||
Reference in New Issue
Block a user