1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/Jobs/Invoice/EmailInvoice.php

99 lines
2.9 KiB
PHP
Raw Normal View History

<?php
2020-08-06 05:04:09 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2020-08-06 05:04:09 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Invoice;
use App\Events\Invoice\InvoiceWasEmailed;
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Helpers\Email\InvoiceEmail;
2020-08-06 05:04:09 +02:00
use App\Jobs\Mail\BaseMailerJob;
use App\Jobs\Utils\SystemLogger;
use App\Libraries\MultiDB;
use App\Mail\TemplateEmail;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
use App\Models\SystemLog;
2020-08-12 04:16:44 +02:00
use App\Utils\Ninja;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
use Symfony\Component\Mime\Test\Constraint\EmailTextBodyContains;
2020-08-06 05:04:09 +02:00
/*Multi Mailer implemented*/
class EmailInvoice extends BaseMailerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $invoice_invitation;
public $email_builder;
public $company;
2020-08-10 06:55:44 +02:00
public $settings;
/**
2020-04-13 03:49:58 +02:00
* EmailInvoice constructor.
* @param InvoiceEmail $email_builder
2020-04-13 03:49:58 +02:00
* @param InvoiceInvitation $quote_invitation
*/
public function __construct(InvoiceEmail $email_builder, InvoiceInvitation $invoice_invitation, Company $company)
{
$this->company = $company;
$this->invoice_invitation = $invoice_invitation;
2020-02-15 12:49:31 +01:00
$this->email_builder = $email_builder;
2020-08-10 06:55:44 +02:00
$this->settings = $invoice_invitation->contact->client->getMergedSettings();
}
/**
* Execute the job.
*
*
* @return void
*/
public function handle()
{
MultiDB::setDB($this->company->db);
2020-08-10 06:55:44 +02:00
$this->setMailDriver();
2020-08-06 05:04:09 +02:00
2020-08-12 04:16:44 +02:00
try {
Mail::to($this->invoice_invitation->contact->email, $this->invoice_invitation->contact->present()->name())
->send(
new TemplateEmail(
$this->email_builder,
$this->invoice_invitation->contact->user,
$this->invoice_invitation->contact->client
)
);
} catch (\Swift_TransportException $e) {
2020-08-12 04:16:44 +02:00
event(new InvoiceWasEmailedAndFailed($this->invoice_invitation->invoice, $this->company, $e->getMessage(), Ninja::eventVars()));
}
if (count(Mail::failures()) > 0) {
2020-08-14 00:08:10 +02:00
$this->logMailError(Mail::failures(), $this->invoice->client);
} else {
2020-08-12 04:16:44 +02:00
event(new InvoiceWasEmailed($this->invoice_invitation, $this->company, Ninja::eventVars()));
}
2020-08-14 00:08:10 +02:00
/* Mark invoice sent */
$this->invoice_invitation->invoice->service()->markSent()->save();
}
}