2020-11-09 14:30:50 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-11-09 14:30:50 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-11-09 14:30:50 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
2023-10-04 06:29:31 +02:00
|
|
|
use App\Services\Template\TemplateService;
|
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
|
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
use MakesHash;
|
|
|
|
use PdfMaker;
|
2020-11-09 14:30:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
private $disk;
|
|
|
|
|
2023-06-03 14:49:48 +02:00
|
|
|
public function __construct(private Invoice $invoice, private ?ClientContact $contact = null, $disk = null)
|
2020-11-09 14:30:50 +01:00
|
|
|
{
|
2021-07-07 13:39:49 +02:00
|
|
|
$this->disk = $disk ?? config('filesystems.default');
|
2020-11-09 14:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
2023-10-04 06:29:31 +02:00
|
|
|
|
|
|
|
$delivery_note_design_id = $this->invoice->client->getSetting('delivery_note_design_id');
|
2023-10-06 08:47:36 +02:00
|
|
|
$design = Design::withTrashed()->find($this->decodePrimaryKey($delivery_note_design_id));
|
2023-10-04 06:29:31 +02:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if($design && $design->is_template) {
|
2023-10-04 06:29:31 +02:00
|
|
|
|
|
|
|
$ts = new TemplateService($design);
|
2024-04-02 12:50:59 +02:00
|
|
|
|
|
|
|
$pdf = $ts->setCompany($this->invoice->company)
|
|
|
|
->build([
|
2023-10-04 06:29:31 +02:00
|
|
|
'invoices' => collect([$this->invoice]),
|
|
|
|
])->getPdf();
|
|
|
|
|
|
|
|
return $pdf;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-09 14:30:50 +01:00
|
|
|
$design_id = $this->invoice->design_id
|
|
|
|
? $this->invoice->design_id
|
|
|
|
: $this->decodePrimaryKey($this->invoice->client->getSetting('invoice_design_id'));
|
|
|
|
|
2021-06-12 13:50:01 +02:00
|
|
|
$invitation = $this->invoice->invitations->first();
|
2024-04-05 10:14:29 +02:00
|
|
|
|
|
|
|
// return (new \App\Services\Pdf\PdfService($invitation, 'delivery_note'))->boot()->getPdf();
|
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') {
|
2024-01-14 05:05:00 +01:00
|
|
|
return (new Phantom())->generate($this->invoice->invitations->first());
|
2021-04-02 13:44:44 +02:00
|
|
|
}
|
|
|
|
|
2023-04-29 13:01:02 +02:00
|
|
|
$design = Design::withTrashed()->find($design_id);
|
2021-06-12 13:50:01 +02:00
|
|
|
$html = new HtmlEngine($invitation);
|
2020-11-09 14:30:50 +01:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2024-04-05 10:14:29 +02:00
|
|
|
$variables = $html->generateLabelsAndValues();
|
|
|
|
$variables['labels']['$entity_label']= ctrans('texts.delivery_note');
|
|
|
|
|
2020-11-09 14:30:50 +01:00
|
|
|
$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'),
|
2024-04-05 10:14:29 +02:00
|
|
|
'variables' => $variables,
|
2023-09-19 02:05:13 +02:00
|
|
|
'options' => [
|
|
|
|
'client' => $this->invoice->client,
|
|
|
|
'entity' => $this->invoice,
|
|
|
|
'contact' => $this->contact,
|
|
|
|
],
|
2021-08-09 14:31:31 +02:00
|
|
|
'process_markdown' => $this->invoice->client->company->markdown_enabled,
|
2020-11-09 14:30:50 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$maker = new PdfMakerService($state);
|
|
|
|
|
|
|
|
$maker
|
|
|
|
->design($template)
|
|
|
|
->build();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
2022-11-01 11:20:28 +01:00
|
|
|
$maker = null;
|
|
|
|
$state = null;
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2024-04-05 10:14:29 +02:00
|
|
|
return $pdf;
|
|
|
|
|
2020-11-09 14:30:50 +01:00
|
|
|
}
|
|
|
|
}
|