Akaunting/app/Jobs/Document/SendDocumentAsCustomMail.php

68 lines
1.8 KiB
PHP
Raw Normal View History

2022-06-01 07:15:55 +00:00
<?php
namespace App\Jobs\Document;
use App\Abstracts\Job;
use App\Events\Document\DocumentSending;
2022-06-01 07:15:55 +00:00
use App\Events\Document\DocumentSent;
2023-03-21 22:33:33 +00:00
use App\Http\Requests\Common\CustomMail as Request;
2022-06-01 07:15:55 +00:00
use App\Models\Document\Document;
class SendDocumentAsCustomMail extends Job
{
2023-03-21 22:33:33 +00:00
public string $template_alias;
public function __construct(Request $request, string $template_alias)
2022-06-01 07:15:55 +00:00
{
$this->request = $request;
$this->template_alias = $template_alias;
}
public function handle(): void
{
$document = Document::find($this->request->get('document_id'));
event(new DocumentSending($document));
2023-10-03 08:06:08 +00:00
$mail_request = $this->request->only(['to', 'subject', 'body']);
2022-06-01 07:15:55 +00:00
if ($this->request->get('user_email', false)) {
2023-10-03 08:06:08 +00:00
$mail_request['cc'] = user()->email;
2022-06-01 07:15:55 +00:00
}
$attachments = collect($this->request->get('attachments', []))
->filter(fn($value) => $value == true)
->keys()
->all();
2023-10-03 08:06:08 +00:00
$attach_pdf = in_array('pdf', $attachments);
2022-06-01 07:15:55 +00:00
$notification = config('type.document.' . $document->type . '.notification.class');
2023-10-03 08:06:08 +00:00
$contacts = $document->contact->withPersons();
$counter = 1;
foreach ($contacts as $contact) {
if (! in_array($contact->email, $mail_request['to'])) {
continue;
}
$custom_mail = [
'subject' => $mail_request['subject'],
'body' => $mail_request['body'],
];
if (($counter == 1) && ! empty($mail_request['cc'])) {
$custom_mail['cc'] = $mail_request['cc'];
}
$contact->notify(new $notification($document, $this->template_alias, $attach_pdf, $custom_mail, $attachments));
$counter++;
}
2022-06-01 07:15:55 +00:00
event(new DocumentSent($document));
}
}