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;
|
|
|
|
|
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
|
|
|
*/
|
2022-06-01 10:15:55 +03:00
|
|
|
public function __construct(Document $invoice = null, string $template_alias = null, bool $attach_pdf = false, array $custom_mail = [])
|
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;
|
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) {
|
2021-04-30 14:57:44 +03:00
|
|
|
$message->attach($this->storeDocumentPdfAndGetPath($this->invoice), [
|
2017-11-16 20:27:30 +03:00
|
|
|
'mime' => 'application/pdf',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
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}',
|
2020-08-10 20:31:00 +02:00
|
|
|
'{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
|
|
|
{
|
|
|
|
return [
|
2021-01-22 18:33:25 +09:00
|
|
|
$this->invoice->document_number,
|
2019-11-16 10:21:14 +03:00
|
|
|
money($this->invoice->amount, $this->invoice->currency_code, true),
|
2021-01-22 14:24:53 +03:00
|
|
|
money($this->invoice->amount_due, $this->invoice->currency_code, true),
|
2021-07-07 10:07:30 +03:00
|
|
|
company_date($this->invoice->issued_at),
|
2020-02-01 00:14:36 +03:00
|
|
|
company_date($this->invoice->due_at),
|
2021-04-16 00:59:43 +03:00
|
|
|
URL::signedRoute('signed.invoices.show', [$this->invoice->id]),
|
2019-11-16 10:21:14 +03:00
|
|
|
route('invoices.show', $this->invoice->id),
|
|
|
|
route('portal.invoices.show', $this->invoice->id),
|
|
|
|
$this->invoice->contact_name,
|
2020-08-10 20:31:00 +02:00
|
|
|
$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
|
|
|
}
|