2020-02-15 10:01:15 +01:00
|
|
|
<?php
|
2020-02-15 10:06:30 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-02-15 10:06:30 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-02-15 10:06:30 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-02-15 10:06:30 +01:00
|
|
|
*/
|
2020-02-15 10:01:15 +01:00
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
2023-09-07 04:58:24 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-02-15 10:01:15 +01:00
|
|
|
use App\Models\Invoice;
|
2023-09-07 04:58:24 +02:00
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Jobs\Entity\EmailEntity;
|
2020-02-17 10:37:44 +01:00
|
|
|
use App\Services\AbstractService;
|
2023-09-07 04:58:24 +02:00
|
|
|
use App\Events\Invoice\InvoiceWasEmailed;
|
2020-02-15 10:01:15 +01:00
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
class SendEmail extends AbstractService
|
2020-02-15 10:01:15 +01:00
|
|
|
{
|
2023-06-03 14:49:48 +02:00
|
|
|
public function __construct(protected Invoice $invoice, protected $reminder_template = null, protected ?ClientContact $contact = null)
|
2020-02-15 10:01:15 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Builds the correct template to send.
|
2020-02-15 10:01:15 +01:00
|
|
|
*/
|
2020-02-17 10:37:44 +01:00
|
|
|
public function run()
|
2020-02-15 10:01:15 +01:00
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $this->reminder_template) {
|
2020-10-27 12:57:12 +01:00
|
|
|
$this->reminder_template = $this->invoice->calculateTemplate('invoice');
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->invoice->invitations->each(function ($invitation) {
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $invitation->contact->trashed() && $invitation->contact->email) {
|
2022-07-31 23:30:04 +02:00
|
|
|
EmailEntity::dispatch($invitation, $invitation->company, $this->reminder_template)->delay(10);
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
});
|
2023-09-07 04:58:24 +02:00
|
|
|
|
|
|
|
if ($this->invoice->invitations->count() >= 1) {
|
|
|
|
event(new InvoiceWasEmailed($this->invoice->invitations->first(), $this->invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $this->reminder_template ?? 'invoice'));
|
|
|
|
}
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
}
|