161 lines
4.6 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2019-12-31 15:49:09 +03:00
namespace App\Notifications\Sale;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
use App\Abstracts\Notification;
2022-06-01 10:15:55 +03:00
use App\Models\Setting\EmailTemplate;
use App\Models\Document\Document;
2021-04-29 18:12:37 +03:00
use App\Traits\Documents;
2022-06-01 10:15:55 +03:00
use Illuminate\Notifications\Messages\MailMessage;
2019-11-16 10:21:14 +03:00
use Illuminate\Support\Facades\URL;
2022-06-01 10:15:55 +03:00
use Illuminate\Support\Str;
2017-09-14 22:21:00 +03:00
2017-11-17 00:37:26 +03:00
class Invoice extends Notification
2017-09-14 22:21:00 +03:00
{
2021-04-29 18:12:37 +03:00
use Documents;
2017-11-17 00:37:26 +03:00
/**
2019-11-16 10:21:14 +03:00
* The invoice model.
2017-11-17 00:37:26 +03:00
*
* @var object
*/
2017-09-14 22:21:00 +03:00
public $invoice;
/**
2019-11-16 10:21:14 +03:00
* The email template.
2017-09-14 22:21:00 +03:00
*
2022-06-01 10:15:55 +03:00
* @var EmailTemplate
2017-09-14 22:21:00 +03:00
*/
2019-11-16 10:21:14 +03:00
public $template;
2017-09-14 22:21:00 +03:00
2021-04-29 18:12:37 +03:00
/**
* Should attach pdf or not.
*
* @var bool
*/
public $attach_pdf;
/**
* List of document attachments to attach when sending the email.
*
* @var array
*/
public $attachments;
2017-09-14 22:21:00 +03:00
/**
2019-11-16 10:21:14 +03:00
* Create a notification instance.
2017-09-14 22:21:00 +03:00
*/
public function __construct(Document $invoice = null, string $template_alias = null, bool $attach_pdf = false, array $custom_mail = [], $attachments = [])
2017-09-14 22:21:00 +03:00
{
2019-11-16 10:21:14 +03:00
parent::__construct();
$this->invoice = $invoice;
2021-04-29 18:12:37 +03:00
$this->template = EmailTemplate::alias($template_alias)->first();
$this->attach_pdf = $attach_pdf;
2022-06-01 10:15:55 +03:00
$this->custom_mail = $custom_mail;
$this->attachments = $attachments;
2017-09-14 22:21:00 +03:00
}
/**
2019-11-16 10:21:14 +03:00
* Get the mail representation of the notification.
2017-09-14 22:21:00 +03:00
*
* @param mixed $notifiable
*/
2022-06-01 10:15:55 +03:00
public function toMail($notifiable): MailMessage
2017-09-14 22:21:00 +03:00
{
2022-06-01 10:15:55 +03:00
if (!empty($this->custom_mail['to'])) {
$notifiable->email = $this->custom_mail['to'];
}
$message = $this->initMailMessage();
2018-02-25 19:00:31 +03:00
2021-04-29 18:12:37 +03:00
// Attach the PDF file
if ($this->attach_pdf) {
$message->attach($this->storeDocumentPdfAndGetPath($this->invoice), [
2017-11-16 20:27:30 +03:00
'mime' => 'application/pdf',
]);
}
// Attach selected attachments
if (! empty($this->invoice->attachment)) {
foreach ($this->invoice->attachment as $attachment) {
if (in_array($attachment->id, $this->attachments)) {
$message->attach($attachment->getAbsolutePath(), [
'mime' => $attachment->mime_type,
]);
}
}
}
2017-11-16 20:27:30 +03:00
return $message;
2017-09-14 22:21:00 +03:00
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
*/
2022-06-01 10:15:55 +03:00
public function toArray($notifiable): array
2017-09-14 22:21:00 +03:00
{
$this->initArrayMessage();
2017-09-14 22:21:00 +03:00
return [
2021-06-19 18:16:09 +03:00
'template_alias' => $this->template->alias,
2022-06-01 10:15:55 +03:00
'title' => trans('notifications.menu.' . $this->template->alias . '.title'),
'description' => trans('notifications.menu.' . $this->template->alias . '.description', $this->getTagsBinding()),
2017-09-14 22:21:00 +03:00
'invoice_id' => $this->invoice->id,
2021-06-19 18:16:09 +03:00
'invoice_number' => $this->invoice->document_number,
'customer_name' => $this->invoice->contact_name,
2017-09-14 22:21:00 +03:00
'amount' => $this->invoice->amount,
2021-06-19 18:16:09 +03:00
'invoiced_date' => company_date($this->invoice->issued_at),
'invoice_due_date' => company_date($this->invoice->due_at),
'status' => $this->invoice->status,
2017-09-14 22:21:00 +03:00
];
}
2019-11-16 10:21:14 +03:00
2022-06-01 10:15:55 +03:00
public function getTags(): array
2019-11-16 10:21:14 +03:00
{
return [
'{invoice_number}',
'{invoice_total}',
2021-01-22 14:24:53 +03:00
'{invoice_amount_due}',
2021-06-19 18:16:09 +03:00
'{invoiced_date}',
2019-11-16 10:21:14 +03:00
'{invoice_due_date}',
'{invoice_guest_link}',
'{invoice_admin_link}',
'{invoice_portal_link}',
'{customer_name}',
'{company_name}',
'{company_email}',
'{company_tax_number}',
'{company_phone}',
'{company_address}',
2019-11-16 10:21:14 +03:00
];
}
2022-06-01 10:15:55 +03:00
public function getTagsReplacement(): array
2019-11-16 10:21:14 +03:00
{
2023-03-22 01:33:33 +03:00
$route_params = [
'company_id' => $this->invoice->company_id,
'invoice' => $this->invoice->id,
];
2019-11-16 10:21:14 +03:00
return [
$this->invoice->document_number,
money($this->invoice->amount, $this->invoice->currency_code),
money($this->invoice->amount_due, $this->invoice->currency_code),
company_date($this->invoice->issued_at),
2020-02-01 00:14:36 +03:00
company_date($this->invoice->due_at),
2023-03-22 01:33:33 +03:00
URL::signedRoute('signed.invoices.show', $route_params),
route('invoices.show', $route_params),
route('portal.invoices.show', $route_params),
2019-11-16 10:21:14 +03:00
$this->invoice->contact_name,
$this->invoice->company->name,
$this->invoice->company->email,
$this->invoice->company->tax_number,
$this->invoice->company->phone,
nl2br(trim($this->invoice->company->address)),
2019-11-16 10:21:14 +03:00
];
}
2017-09-14 22:21:00 +03:00
}