2019-12-22 11:28:41 +01:00
|
|
|
<?php
|
2020-08-06 05:04:09 +02: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
|
|
|
|
*/
|
2019-12-22 11:28:41 +01:00
|
|
|
|
|
|
|
namespace App\Jobs\Invoice;
|
|
|
|
|
|
|
|
use App\Events\Invoice\InvoiceWasEmailed;
|
|
|
|
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
|
2020-02-15 10:01:15 +01:00
|
|
|
use App\Helpers\Email\InvoiceEmail;
|
2020-08-06 05:04:09 +02:00
|
|
|
use App\Jobs\Mail\BaseMailerJob;
|
2020-02-15 10:01:15 +01:00
|
|
|
use App\Jobs\Utils\SystemLogger;
|
2020-02-15 13:27:24 +01:00
|
|
|
use App\Libraries\MultiDB;
|
2019-12-22 11:28:41 +01:00
|
|
|
use App\Mail\TemplateEmail;
|
2019-12-27 01:28:36 +01:00
|
|
|
use App\Models\Company;
|
2019-12-22 11:28:41 +01:00
|
|
|
use App\Models\Invoice;
|
2020-02-15 10:01:15 +01:00
|
|
|
use App\Models\InvoiceInvitation;
|
2020-02-15 10:06:30 +01:00
|
|
|
use App\Models\SystemLog;
|
2020-08-12 04:16:44 +02:00
|
|
|
use App\Utils\Ninja;
|
2019-12-22 11:28:41 +01:00
|
|
|
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;
|
2020-02-15 10:01:15 +01:00
|
|
|
use Symfony\Component\Mime\Test\Constraint\EmailTextBodyContains;
|
2019-12-22 11:28:41 +01:00
|
|
|
|
2020-08-06 05:04:09 +02:00
|
|
|
/*Multi Mailer implemented*/
|
|
|
|
|
|
|
|
class EmailInvoice extends BaseMailerJob implements ShouldQueue
|
2019-12-22 11:28:41 +01:00
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
public $invoice_invitation;
|
2019-12-22 11:28:41 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
public $email_builder;
|
2019-12-27 01:28:36 +01:00
|
|
|
|
2020-02-15 13:27:24 +01:00
|
|
|
public $company;
|
2020-08-10 06:55:44 +02:00
|
|
|
|
|
|
|
public $settings;
|
2019-12-22 11:28:41 +01:00
|
|
|
/**
|
2020-03-21 06:37:30 +01:00
|
|
|
*
|
2020-04-13 03:49:58 +02:00
|
|
|
* EmailInvoice constructor.
|
2020-04-15 02:30:52 +02:00
|
|
|
* @param InvoiceEmail $email_builder
|
2020-04-13 03:49:58 +02:00
|
|
|
* @param InvoiceInvitation $quote_invitation
|
2019-12-22 11:28:41 +01:00
|
|
|
*/
|
2020-02-15 10:06:30 +01:00
|
|
|
|
2020-02-15 13:27:24 +01:00
|
|
|
public function __construct(InvoiceEmail $email_builder, InvoiceInvitation $invoice_invitation, Company $company)
|
2019-12-22 11:28:41 +01:00
|
|
|
{
|
2020-02-15 13:27:24 +01:00
|
|
|
$this->company = $company;
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
$this->invoice_invitation = $invoice_invitation;
|
2020-02-15 12:49:31 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
$this->email_builder = $email_builder;
|
2020-08-10 06:55:44 +02:00
|
|
|
|
|
|
|
$this->settings = $invoice_invitation->contact->client->getMergedSettings();
|
|
|
|
|
2019-12-22 11:28:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-12-22 11:28:41 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2020-02-15 10:01:15 +01:00
|
|
|
|
2019-12-22 11:28:41 +01:00
|
|
|
public function handle()
|
2020-04-30 13:45:47 +02:00
|
|
|
{
|
2020-04-15 02:30:52 +02:00
|
|
|
MultiDB::setDB($this->company->db);
|
2020-04-19 12:29:58 +02:00
|
|
|
|
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) {
|
|
|
|
|
|
|
|
event(new InvoiceWasEmailedAndFailed($this->invoice_invitation->invoice, $this->company, $e->getMessage(), Ninja::eventVars()));
|
2020-08-27 14:12:39 +02:00
|
|
|
|
2020-08-12 04:16:44 +02:00
|
|
|
}
|
2020-02-15 10:01:15 +01:00
|
|
|
|
|
|
|
if (count(Mail::failures()) > 0) {
|
2020-08-14 00:08:10 +02:00
|
|
|
$this->logMailError(Mail::failures(), $this->invoice->client);
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
2020-08-12 04:16:44 +02:00
|
|
|
else{
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
2019-12-22 11:28:41 +01:00
|
|
|
}
|
|
|
|
|
2020-08-08 06:38:02 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|