2019-12-22 11:28:41 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. 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\Libraries\MultiDB;
|
|
|
|
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;
|
|
|
|
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 EmailInvoice implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public $invoice;
|
|
|
|
|
2019-12-24 22:55:29 +01:00
|
|
|
public $message_array = [];
|
|
|
|
|
2019-12-27 01:28:36 +01:00
|
|
|
private $company;
|
|
|
|
|
2019-12-22 11:28:41 +01:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-12-27 01:28:36 +01:00
|
|
|
public function __construct(Invoice $invoice, Company $company)
|
2019-12-22 11:28:41 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
$this->invoice = $invoice;
|
|
|
|
|
2019-12-27 01:28:36 +01:00
|
|
|
$this->company = $company;
|
|
|
|
|
2019-12-22 11:28:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
/*Jobs are not multi-db aware, need to set! */
|
2019-12-27 01:28:36 +01:00
|
|
|
MultiDB::setDB($this->company->db);
|
2019-12-22 11:28:41 +01:00
|
|
|
|
2019-12-24 22:55:29 +01:00
|
|
|
//todo - change runtime config of mail driver if necessary
|
2019-12-22 11:28:41 +01:00
|
|
|
|
|
|
|
$template_style = $this->invoice->client->getSetting('email_style');
|
|
|
|
|
2019-12-24 22:55:29 +01:00
|
|
|
$this->invoice->invitations->each(function ($invitation) use($template_style){
|
2019-12-22 11:28:41 +01:00
|
|
|
|
|
|
|
if($invitation->contact->send_invoice && $invitation->contact->email)
|
|
|
|
{
|
|
|
|
|
2019-12-24 22:55:29 +01:00
|
|
|
$message_array = $this->invoice->getEmailData('', $invitation->contact);
|
|
|
|
$message_array['title'] = &$message_array['subject'];
|
|
|
|
$message_array['footer'] = "Sent to ".$invitation->contact->present()->name();
|
|
|
|
|
2019-12-22 11:28:41 +01:00
|
|
|
//change the runtime config of the mail provider here:
|
|
|
|
|
|
|
|
//send message
|
2019-12-24 22:55:29 +01:00
|
|
|
Mail::to($invitation->contact->email, $invitation->contact->present()->name())
|
2019-12-22 11:28:41 +01:00
|
|
|
->send(new TemplateEmail($message_array, $template_style, $invitation->contact->user, $invitation->contact->client));
|
|
|
|
|
|
|
|
if( count(Mail::failures()) > 0 ) {
|
|
|
|
|
|
|
|
event(new InvoiceWasEmailedAndFailed($this->invoice, Mail::failures()));
|
|
|
|
|
|
|
|
return $this->logMailError($errors);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//fire any events
|
|
|
|
event(new InvoiceWasEmailed($this->invoice));
|
|
|
|
|
2019-12-24 22:55:29 +01:00
|
|
|
//sleep(5);
|
2019-12-22 11:28:41 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function logMailError($errors)
|
|
|
|
{
|
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
$errors,
|
|
|
|
SystemLog::CATEGORY_MAIL,
|
|
|
|
SystemLog::EVENT_MAIL_SEND,
|
|
|
|
SystemLog::TYPE_FAILURE,
|
|
|
|
$this->invoice->client
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|