2019-11-05 23:52:57 +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\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\PaymentTerm;
|
|
|
|
use App\Repositories\InvoiceRepository;
|
|
|
|
use App\Utils\Traits\NumberFormatter;
|
2019-11-20 06:41:49 +01:00
|
|
|
use App\Utils\Traits\MakesInvoiceHtml;
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(Invoice $invoice)
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->invoice = $invoice;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
$this->invoice->load('client');
|
|
|
|
$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
|
|
|
|
$pdf = $this->makePdf(null,null,$html);
|
|
|
|
|
|
|
|
$path = Storage::put($file_path, $pdf);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
->waitUntilNetworkIdle(false)->pdf();
|
|
|
|
//->margins(10,10,10,10)
|
|
|
|
//->savePdf('test.pdf');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|