2020-02-15 10:01:15 +01:00
|
|
|
<?php
|
2020-02-15 10:06:30 +01:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2020-02-15 10:01:15 +01:00
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
|
|
|
use App\Helpers\Email\InvoiceEmail;
|
|
|
|
use App\Jobs\Invoice\EmailInvoice;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
|
|
|
class SendEmail
|
|
|
|
{
|
|
|
|
|
2020-02-15 10:06:30 +01:00
|
|
|
protected $invoice;
|
2020-02-15 10:01:15 +01:00
|
|
|
|
2020-02-15 10:06:30 +01:00
|
|
|
public function __construct(Invoice $invoice)
|
2020-02-15 10:01:15 +01:00
|
|
|
{
|
|
|
|
$this->invoice = $invoice;
|
|
|
|
}
|
|
|
|
|
2020-02-15 10:06:30 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
/**
|
|
|
|
* Builds the correct template to send
|
|
|
|
* @param string $reminder_template The template name ie reminder1
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function run($reminder_template = null, $contact = null): array
|
|
|
|
{
|
|
|
|
if (!$reminder_template) {
|
|
|
|
$reminder_template = $this->invoice->status_id == Invoice::STATUS_DRAFT || Carbon::parse($this->invoice->due_date) > now() ? 'invoice' : $this->invoice->calculateTemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
$email_builder = (new InvoiceEmail())->build($this->invoice, $reminder_template, $contact);
|
|
|
|
|
|
|
|
$this->invoice->invitations->each(function ($invitation) use ($email_builder) {
|
|
|
|
if ($invitation->contact->send_invoice && $invitation->contact->email) {
|
2020-02-15 12:49:31 +01:00
|
|
|
EmailInvoice::dispatch($email_builder, $invitation);
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|