2020-03-03 10:44:26 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Credit;
|
|
|
|
|
|
|
|
use App\Designs\Custom;
|
|
|
|
use App\Designs\Designer;
|
|
|
|
use App\Designs\Modern;
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Design;
|
|
|
|
use App\Models\Invoice;
|
2020-03-06 14:41:15 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-03-03 10:44:26 +01:00
|
|
|
use App\Utils\Traits\MakesInvoiceHtml;
|
|
|
|
use App\Utils\Traits\NumberFormatter;
|
2020-03-06 14:41:15 +01:00
|
|
|
use App\Utils\Traits\Pdf\PdfMaker;
|
2020-03-03 10:44:26 +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\App;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Spatie\Browsershot\Browsershot;
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
class CreateCreditPdf implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public $credit;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public $company;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public $contact;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
private $disk;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct($credit, Company $company, ClientContact $contact = null)
|
|
|
|
{
|
|
|
|
$this->credit = $credit;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->company = $company;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->contact = $contact;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
|
|
|
$this->disk = $disk ?? config('filesystems.default');
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
MultiDB::setDB($this->company->db);
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->credit->load('client');
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (!$this->contact) {
|
|
|
|
$this->contact = $this->credit->client->primary_contact()->first();
|
|
|
|
}
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
App::setLocale($this->contact->preferredLocale());
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$path = $this->credit->client->credit_filepath();
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$file_path = $path . $this->credit->number . '.pdf';
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$design = Design::find($this->decodePrimaryKey($this->credit->client->getSetting('credit_design_id')));
|
|
|
|
|
|
|
|
$designer = new Designer($this->credit, $design, $this->credit->client->getSetting('pdf_variables'), 'credit');
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
//get invoice design
|
|
|
|
// $html = $this->generateInvoiceHtml($designer->build()->getHtml(), $this->credit, $this->contact);
|
|
|
|
$html = $this->generateEntityHtml($designer, $this->credit, $this->contact);
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
//todo - move this to the client creation stage so we don't keep hitting this unnecessarily
|
|
|
|
Storage::makeDirectory($path, 0755);
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-04-11 13:19:05 +02:00
|
|
|
$pdf = $this->makePdf(null, null, $html);
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$instance = Storage::disk($this->disk)->put($file_path, $pdf);
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
return $file_path;
|
|
|
|
}
|
2020-03-03 10:44:26 +01:00
|
|
|
}
|