2019-11-05 23:52:57 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-11-05 23:52:57 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Invoice;
|
|
|
|
|
2020-02-05 11:28:56 +01:00
|
|
|
use App\Designs\Designer;
|
|
|
|
use App\Designs\Modern;
|
2019-12-27 01:28:36 +01:00
|
|
|
use App\Libraries\MultiDB;
|
2020-02-10 10:53:02 +01:00
|
|
|
use App\Models\ClientContact;
|
2019-12-27 01:28:36 +01:00
|
|
|
use App\Models\Company;
|
2019-11-05 23:52:57 +01:00
|
|
|
use App\Models\Invoice;
|
2020-02-10 10:53:02 +01:00
|
|
|
|
2019-11-20 06:41:49 +01:00
|
|
|
use App\Utils\Traits\MakesInvoiceHtml;
|
2019-12-27 01:28:36 +01:00
|
|
|
use App\Utils\Traits\NumberFormatter;
|
2019-11-05 23:52:57 +01:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2020-02-10 10:53:02 +01:00
|
|
|
|
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
|
2019-11-05 23:52:57 +01:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Spatie\Browsershot\Browsershot;
|
2020-02-10 10:53:02 +01:00
|
|
|
|
|
|
|
class CreateInvoicePdf implements ShouldQueue {
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml;
|
|
|
|
|
|
|
|
public $invoice;
|
|
|
|
|
|
|
|
public $company;
|
|
|
|
|
|
|
|
public $contact;
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(Invoice $invoice, Company $company, ClientContact $contact) {
|
|
|
|
$this->invoice = $invoice;
|
|
|
|
|
|
|
|
$this->company = $company;
|
|
|
|
|
|
|
|
$this->contact = $contact;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle() {
|
|
|
|
MultiDB::setDB($this->company->db);
|
|
|
|
|
|
|
|
App::setLocale($this->contact->preferredLocale());
|
|
|
|
|
|
|
|
$this->invoice->load('client');
|
|
|
|
$path = 'public/'.$this->invoice->client->client_hash.'/invoices/';
|
|
|
|
$file_path = $path.$this->invoice->number.'.pdf';
|
|
|
|
|
|
|
|
$modern = new Modern();
|
|
|
|
$designer = new Designer($modern, $this->invoice->client->getSetting('invoice_variables'));
|
|
|
|
|
|
|
|
//get invoice design
|
|
|
|
$html = $this->generateInvoiceHtml($designer->build($this->invoice)->getHtml(), $this->invoice, $this->contact);
|
|
|
|
|
|
|
|
//todo - move this to the client creation stage so we don't keep hitting this unnecessarily
|
|
|
|
Storage::makeDirectory($path, 0755);
|
|
|
|
|
|
|
|
//\Log::error($html);
|
|
|
|
//create pdf
|
|
|
|
$pdf = $this->makePdf(null, null, $html);
|
|
|
|
|
|
|
|
$path = Storage::put($file_path, $pdf);
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a PDF stream
|
|
|
|
*
|
|
|
|
* @param string $header Header to be included in PDF
|
|
|
|
* @param string $footer Footer to be included in PDF
|
|
|
|
* @param string $html The HTML object to be converted into PDF
|
|
|
|
*
|
|
|
|
* @return string The PDF string
|
|
|
|
*/
|
|
|
|
private function makePdf($header, $footer, $html) {
|
|
|
|
return Browsershot::html($html)
|
|
|
|
//->showBrowserHeaderAndFooter()
|
|
|
|
//->headerHtml($header)
|
|
|
|
//->footerHtml($footer)
|
|
|
|
->deviceScaleFactor(1)
|
|
|
|
->showBackground()
|
|
|
|
->waitUntilNetworkIdle(true) ->pdf();
|
|
|
|
//->margins(10,10,10,10)
|
|
|
|
//->savePdf('test.pdf');
|
|
|
|
}
|
2019-11-05 23:52:57 +01:00
|
|
|
}
|