2020-11-09 14:30:50 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-11-09 14:30:50 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\Design;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Services\PdfMaker\Design as PdfMakerDesign;
|
|
|
|
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
|
2021-04-02 13:44:44 +02:00
|
|
|
use App\Utils\HostedPDF\NinjaPdf;
|
2020-11-09 14:30:50 +01:00
|
|
|
use App\Utils\HtmlEngine;
|
2021-04-02 13:44:44 +02:00
|
|
|
use App\Utils\PhantomJS\Phantom;
|
2020-11-09 14:30:50 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use App\Utils\Traits\Pdf\PdfMaker;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
|
|
class GenerateDeliveryNote
|
|
|
|
{
|
|
|
|
use MakesHash, PdfMaker;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \App\Models\Invoice
|
|
|
|
*/
|
|
|
|
private $invoice;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \App\Models\ClientContact
|
|
|
|
*/
|
|
|
|
private $contact;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
private $disk;
|
|
|
|
|
|
|
|
public function __construct(Invoice $invoice, ClientContact $contact = null, $disk = null)
|
|
|
|
{
|
|
|
|
$this->invoice = $invoice;
|
|
|
|
|
|
|
|
$this->contact = $contact;
|
|
|
|
|
2021-05-15 04:19:36 +02:00
|
|
|
$this->disk = 'public';
|
|
|
|
|
|
|
|
// $this->disk = $disk ?? config('filesystems.default');
|
2020-11-09 14:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$design_id = $this->invoice->design_id
|
|
|
|
? $this->invoice->design_id
|
|
|
|
: $this->decodePrimaryKey($this->invoice->client->getSetting('invoice_design_id'));
|
|
|
|
|
2021-05-15 04:19:36 +02:00
|
|
|
$file_path = sprintf('%s%s_delivery_note.pdf', $this->invoice->client->invoice_filepath(), $this->invoice->number);
|
2020-11-09 14:30:50 +01:00
|
|
|
|
2021-05-09 13:30:31 +02:00
|
|
|
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
2021-04-02 13:44:44 +02:00
|
|
|
return (new Phantom)->generate($this->invoice->invitations->first());
|
|
|
|
}
|
|
|
|
|
2020-11-09 14:30:50 +01:00
|
|
|
$design = Design::find($design_id);
|
|
|
|
$html = new HtmlEngine($this->invoice->invitations->first());
|
|
|
|
|
|
|
|
if ($design->is_custom) {
|
|
|
|
$options = ['custom_partials' => json_decode(json_encode($design->design), true)];
|
|
|
|
$template = new PdfMakerDesign(PdfMakerDesign::CUSTOM, $options);
|
|
|
|
} else {
|
|
|
|
$template = new PdfMakerDesign(strtolower($design->name));
|
|
|
|
}
|
|
|
|
|
|
|
|
$state = [
|
|
|
|
'template' => $template->elements([
|
|
|
|
'client' => $this->invoice->client,
|
|
|
|
'entity' => $this->invoice,
|
|
|
|
'pdf_variables' => (array) $this->invoice->company->settings->pdf_variables,
|
|
|
|
'contact' => $this->contact,
|
|
|
|
], 'delivery_note'),
|
|
|
|
'variables' => $html->generateLabelsAndValues(),
|
|
|
|
];
|
|
|
|
|
|
|
|
$maker = new PdfMakerService($state);
|
|
|
|
|
|
|
|
$maker
|
|
|
|
->design($template)
|
|
|
|
->build();
|
|
|
|
|
2021-05-15 04:19:36 +02:00
|
|
|
// Storage::makeDirectory($this->invoice->client->invoice_filepath(), 0775);
|
|
|
|
|
2021-05-09 13:30:31 +02:00
|
|
|
if(config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja'){
|
2021-04-02 13:44:44 +02:00
|
|
|
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML());
|
|
|
|
}
|
2020-11-09 14:30:50 +01:00
|
|
|
|
2020-12-17 15:44:01 +01:00
|
|
|
if (config('ninja.log_pdf_html')) {
|
|
|
|
info($maker->getCompiledHTML());
|
|
|
|
}
|
|
|
|
|
2021-05-15 04:19:36 +02:00
|
|
|
Storage::disk($this->disk)->put($file_path, $pdf);
|
|
|
|
|
|
|
|
return Storage::disk($this->disk)->path($file_path);
|
|
|
|
|
2020-11-09 14:30:50 +01:00
|
|
|
}
|
|
|
|
}
|