1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Jobs/Quote/EmailQuote.php
2020-09-14 15:14:37 +10:00

88 lines
2.3 KiB
PHP

<?php
namespace App\Jobs\Quote;
use App\Events\Invoice\InvoiceWasEmailed;
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Events\Quote\QuoteWasEmailed;
use App\Events\Quote\QuoteWasEmailedAndFailed;
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\Quote;
use App\Models\QuoteInvitation;
use App\Models\SystemLog;
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;
class EmailQuote extends BaseMailerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $quote_invitation;
public $email_builder;
public $company;
public $settings;
/**
* EmailQuote constructor.
* @param BuildEmail $email_builder
* @param QuoteInvitation $quote_invitation
*/
public function __construct($email_builder, QuoteInvitation $quote_invitation, Company $company)
{
$this->quote_invitation = $quote_invitation;
$this->email_builder = $email_builder;
$this->company = $company;
}
/**
* Execute the job.
*
*
* @return void
*/
public function handle()
{
MultiDB::setDb($this->company->db);
$this->settings = $this->quote_invitation->contact->client->getMergedSettings();
$this->setMailDriver();
Mail::to($this->quote_invitation->contact->email, $this->quote_invitation->contact->present()->name())
->send(
new TemplateEmail(
$this->email_builder,
$this->quote_invitation->contact->user,
$this->quote_invitation->contact->client
)
);
if (count(Mail::failures()) > 0) {
return $this->logMailError(Mail::failures());
}
$this->quote_invitation->quote->service()->markSent()->save();
}
private function logMailError($errors)
{
SystemLogger::dispatch(
$errors,
SystemLog::CATEGORY_MAIL,
SystemLog::EVENT_MAIL_SEND,
SystemLog::TYPE_FAILURE,
$this->quote->client
);
}
}