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;
|
|
|
|
|
2019-12-27 01:28:36 +01:00
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Company;
|
2019-11-05 23:52:57 +01:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\PaymentTerm;
|
|
|
|
use App\Repositories\InvoiceRepository;
|
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;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Spatie\Browsershot\Browsershot;
|
|
|
|
use Symfony\Component\Debug\Exception\FatalThrowableError;
|
|
|
|
|
|
|
|
class CreateInvoicePdf implements ShouldQueue
|
|
|
|
{
|
2019-11-20 06:41:49 +01:00
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml;
|
2019-11-05 23:52:57 +01:00
|
|
|
|
|
|
|
public $invoice;
|
|
|
|
|
2019-12-29 23:06:42 +01:00
|
|
|
public $company;
|
2019-11-05 23:52:57 +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-11-05 23:52:57 +01:00
|
|
|
{
|
|
|
|
$this->invoice = $invoice;
|
|
|
|
|
2019-12-27 01:28:36 +01:00
|
|
|
$this->company = $company;
|
2019-11-05 23:52:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
{
|
2019-12-27 01:28:36 +01:00
|
|
|
MultiDB::setDB($this->company->db);
|
2019-11-05 23:52:57 +01:00
|
|
|
|
|
|
|
$this->invoice->load('client');
|
2019-12-30 22:59:12 +01:00
|
|
|
$path = 'public/' . $this->invoice->client->client_hash . '/invoices/';
|
2019-11-27 11:27:24 +01:00
|
|
|
$file_path = $path . $this->invoice->number . '.pdf';
|
2019-11-05 23:52:57 +01:00
|
|
|
|
|
|
|
//get invoice design
|
|
|
|
$html = $this->generateInvoiceHtml($this->invoice->design(), $this->invoice);
|
|
|
|
|
|
|
|
//todo - move this to the client creation stage so we don't keep hitting this unnecessarily
|
|
|
|
Storage::makeDirectory($path, 0755);
|
|
|
|
|
|
|
|
//create pdf
|
2019-12-30 22:59:12 +01:00
|
|
|
$pdf = $this->makePdf(null, null, $html);
|
2019-11-05 23:52:57 +01:00
|
|
|
|
|
|
|
$path = Storage::put($file_path, $pdf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a PDF stream
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-11-05 23:52:57 +01:00
|
|
|
* @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
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-11-05 23:52:57 +01:00
|
|
|
* @return string The PDF string
|
|
|
|
*/
|
2019-12-30 22:59:12 +01:00
|
|
|
private function makePdf($header, $footer, $html)
|
2019-11-05 23:52:57 +01:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
return Browsershot::html($html)
|
2019-11-05 23:52:57 +01:00
|
|
|
//->showBrowserHeaderAndFooter()
|
|
|
|
//->headerHtml($header)
|
|
|
|
//->footerHtml($footer)
|
|
|
|
->waitUntilNetworkIdle(false)->pdf();
|
2019-12-30 22:59:12 +01:00
|
|
|
//->margins(10,10,10,10)
|
2019-11-05 23:52:57 +01:00
|
|
|
//->savePdf('test.pdf');
|
|
|
|
}
|
|
|
|
}
|