mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-07 16:04:32 -04:00
Merge branch 'support_for_custom_statement_designs' of https://github.com/turbo124/invoiceninja into support_for_custom_statement_designs
This commit is contained in:
commit
8c2f94a1c0
@ -23,6 +23,7 @@ use App\Models\QuoteInvitation;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use App\Models\CreditInvitation;
|
||||
use App\Models\RecurringInvoice;
|
||||
use App\Services\Pdf\PdfService;
|
||||
use App\Utils\PhantomJS\Phantom;
|
||||
use App\Models\InvoiceInvitation;
|
||||
use App\Utils\HostedPDF\NinjaPdf;
|
||||
@ -89,6 +90,17 @@ class CreateRawPdf implements ShouldQueue
|
||||
|
||||
public function handle()
|
||||
{
|
||||
/** Testing this override to improve PDF generation performance */
|
||||
$ps = new PdfService($this->invitation, 'product', [
|
||||
'client' => $this->entity->client,
|
||||
"{$this->entity_string}s" => [$this->entity],
|
||||
]);
|
||||
|
||||
$pdf = $ps->boot()->getPdf();
|
||||
nlog("pdf timer = ". $ps->execution_time);
|
||||
|
||||
|
||||
|
||||
/* Forget the singleton*/
|
||||
App::forgetInstance('translator');
|
||||
|
||||
|
@ -144,8 +144,8 @@ class Statement
|
||||
'variables' => collect([$variables]),
|
||||
'invoices' => $this->getInvoices()->get(),
|
||||
'payments' => $this->options['show_payments_table'] ? $this->getPayments()->get() : collect([]),
|
||||
'credits' => $this->options['show_credits_table'] ? $this->getCredits()->get() : [],
|
||||
'aging' => $this->options['show_aging_table'] ? $this->getAging() : [],
|
||||
'credits' => $this->options['show_credits_table'] ? $this->getCredits()->get() : collect([]),
|
||||
'aging' => $this->options['show_aging_table'] ? $this->getAging() : collect([]),
|
||||
]);
|
||||
|
||||
$html = $ts->getHtml();
|
||||
|
@ -11,13 +11,14 @@
|
||||
|
||||
namespace App\Services\Pdf;
|
||||
|
||||
use App\Models\Credit;
|
||||
use App\Models\Quote;
|
||||
use App\Utils\Helpers;
|
||||
use App\Utils\Traits\MakesDates;
|
||||
use DOMDocument;
|
||||
use Illuminate\Support\Carbon;
|
||||
use App\Models\Quote;
|
||||
use App\Models\Credit;
|
||||
use App\Utils\Helpers;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Carbon;
|
||||
use App\Utils\Traits\MakesDates;
|
||||
use App\Services\Template\TemplateService;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
|
||||
class PdfBuilder
|
||||
@ -67,6 +68,7 @@ class PdfBuilder
|
||||
->buildSections()
|
||||
->getEmptyElements()
|
||||
->updateElementProperties()
|
||||
->parseTwigElements()
|
||||
->updateVariables();
|
||||
|
||||
return $this;
|
||||
@ -104,6 +106,40 @@ class PdfBuilder
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function parseTwigElements()
|
||||
{
|
||||
|
||||
$replacements = [];
|
||||
$contents = $this->document->getElementsByTagName('ninja');
|
||||
|
||||
$template_service = new TemplateService();
|
||||
$data = $template_service->processData($this->service->options)->getData();
|
||||
|
||||
$twig = $template_service->twig;
|
||||
|
||||
foreach ($contents as $content) {
|
||||
|
||||
$template = $content->ownerDocument->saveHTML($content);
|
||||
|
||||
$template = $twig->createTemplate(html_entity_decode($template));
|
||||
$template = $template->render($data);
|
||||
|
||||
$f = $this->document->createDocumentFragment();
|
||||
$f->appendXML($template);
|
||||
$replacements[] = $f;
|
||||
|
||||
}
|
||||
|
||||
foreach($contents as $key => $content) {
|
||||
$content->parentNode->replaceChild($replacements[$key], $content);
|
||||
}
|
||||
|
||||
$contents = null;
|
||||
|
||||
return $this;
|
||||
|
||||
}
|
||||
|
||||
public function setDocument($document): self
|
||||
{
|
||||
$this->document = $document;
|
||||
@ -1091,7 +1127,8 @@ class PdfBuilder
|
||||
} elseif (Str::startsWith($variable, '$custom_surcharge')) {
|
||||
$_variable = ltrim($variable, '$'); // $custom_surcharge1 -> custom_surcharge1
|
||||
|
||||
$visible = intval($this->service->config->entity->{$_variable}) != 0;
|
||||
// $visible = intval($this->service->config->entity->{$_variable}) != 0;
|
||||
$visible = intval(str_replace(['0','.'], '', $this->service->config->entity->{$_variable})) != 0;
|
||||
|
||||
$elements[1]['elements'][] = ['element' => 'div', 'elements' => [
|
||||
['element' => 'span', 'content' => $variable . '_label', 'properties' => ['hidden' => !$visible, 'data-ref' => 'totals_table-' . substr($variable, 1) . '-label']],
|
||||
@ -1546,12 +1583,6 @@ class PdfBuilder
|
||||
// Dom Traversal
|
||||
///////////////////////////////////////
|
||||
|
||||
|
||||
public function getSectionNode(string $selector)
|
||||
{
|
||||
return $this->document->getElementById($selector);
|
||||
}
|
||||
|
||||
public function updateElementProperties() :self
|
||||
{
|
||||
foreach ($this->sections as $element) {
|
||||
|
@ -195,7 +195,6 @@ class PdfMock
|
||||
{
|
||||
return ['values' =>
|
||||
[
|
||||
|
||||
'$client.shipping_postal_code' => '46420',
|
||||
'$client.billing_postal_code' => '11243',
|
||||
'$company.city_state_postal' => 'Beveley Hills, CA, 90210',
|
||||
@ -496,6 +495,8 @@ class PdfMock
|
||||
'$show_shipping_address' => $this->settings->show_shipping_address ? 'flex' : 'none',
|
||||
'$show_shipping_address_block' => $this->settings->show_shipping_address ? 'block' : 'none',
|
||||
'$show_shipping_address_visibility' => $this->settings->show_shipping_address ? 'visible' : 'hidden',
|
||||
'$start_date' => '31/01/2023',
|
||||
'$end_date' => '31/12/2023',
|
||||
],
|
||||
'labels' => $this->mockTranslatedLabels(),
|
||||
];
|
||||
@ -811,6 +812,8 @@ class PdfMock
|
||||
'$tax_label' => ctrans('texts.tax'),
|
||||
'$dir_label' => '',
|
||||
'$to_label' => ctrans('texts.to'),
|
||||
'$start_date_label' => ctrans('texts.start_date'),
|
||||
'$end_date_label' => ctrans('texts.end_date'),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -44,6 +44,10 @@ class PdfService
|
||||
|
||||
public array $options;
|
||||
|
||||
private float $start_time;
|
||||
|
||||
public float $execution_time;
|
||||
|
||||
const DELIVERY_NOTE = 'delivery_note';
|
||||
const STATEMENT = 'statement';
|
||||
const PURCHASE_ORDER = 'purchase_order';
|
||||
@ -58,10 +62,14 @@ class PdfService
|
||||
$this->document_type = $document_type;
|
||||
|
||||
$this->options = $options;
|
||||
|
||||
$this->start_time = microtime(true);
|
||||
|
||||
}
|
||||
|
||||
public function boot(): self
|
||||
{
|
||||
|
||||
$this->init();
|
||||
|
||||
return $this;
|
||||
@ -90,6 +98,8 @@ class PdfService
|
||||
throw new \Exception($e->getMessage(), $e->getCode());
|
||||
}
|
||||
|
||||
$this->execution_time = microtime(true) - $this->start_time;
|
||||
|
||||
return $pdf;
|
||||
}
|
||||
|
||||
@ -104,9 +114,11 @@ class PdfService
|
||||
$html = $this->builder->getCompiledHTML();
|
||||
|
||||
if (config('ninja.log_pdf_html')) {
|
||||
info($html);
|
||||
nlog($html);
|
||||
}
|
||||
|
||||
$this->execution_time = microtime(true) - $this->start_time;
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
|
@ -11,32 +11,31 @@
|
||||
|
||||
namespace App\Services\Template;
|
||||
|
||||
use App\Utils\Number;
|
||||
use App\Models\Client;
|
||||
use App\Models\Company;
|
||||
use App\Models\Credit;
|
||||
use App\Models\Design;
|
||||
use App\Models\Company;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Payment;
|
||||
use App\Models\Project;
|
||||
use App\Models\Activity;
|
||||
use App\Utils\HtmlEngine;
|
||||
use League\Fractal\Manager;
|
||||
use App\Models\PurchaseOrder;
|
||||
use App\Utils\VendorHtmlEngine;
|
||||
use App\Utils\PaymentHtmlEngine;
|
||||
use App\Utils\Traits\MakesDates;
|
||||
use App\Utils\HostedPDF\NinjaPdf;
|
||||
use App\Utils\Traits\Pdf\PdfMaker;
|
||||
use Twig\Extra\Intl\IntlExtension;
|
||||
use App\Transformers\TaskTransformer;
|
||||
use App\Transformers\QuoteTransformer;
|
||||
use App\Services\Template\TemplateMock;
|
||||
use App\Transformers\CreditTransformer;
|
||||
use App\Transformers\InvoiceTransformer;
|
||||
use App\Models\Quote;
|
||||
use App\Models\RecurringInvoice;
|
||||
use App\Models\Vendor;
|
||||
use App\Transformers\ProjectTransformer;
|
||||
use App\Transformers\PurchaseOrderTransformer;
|
||||
use App\Transformers\QuoteTransformer;
|
||||
use App\Transformers\TaskTransformer;
|
||||
use App\Utils\HostedPDF\NinjaPdf;
|
||||
use App\Utils\HtmlEngine;
|
||||
use App\Utils\Number;
|
||||
use App\Utils\PaymentHtmlEngine;
|
||||
use App\Utils\Traits\MakesDates;
|
||||
use App\Utils\Traits\Pdf\PdfMaker;
|
||||
use App\Utils\VendorHtmlEngine;
|
||||
use League\Fractal\Manager;
|
||||
use League\Fractal\Serializer\ArraySerializer;
|
||||
use Twig\Extra\Intl\IntlExtension;
|
||||
|
||||
class TemplateService
|
||||
{
|
||||
@ -54,6 +53,14 @@ class TemplateService
|
||||
|
||||
public ?Company $company;
|
||||
|
||||
private ?Client $client;
|
||||
|
||||
private ?Vendor $vendor;
|
||||
|
||||
private Invoice | Quote | Credit | PurchaseOrder | RecurringInvoice $entity;
|
||||
|
||||
private Payment $payment;
|
||||
|
||||
public function __construct(public ?Design $template = null)
|
||||
{
|
||||
$this->template = $template;
|
||||
@ -84,6 +91,12 @@ class TemplateService
|
||||
});
|
||||
$this->twig->addFunction($function);
|
||||
|
||||
$filter = new \Twig\TwigFilter('sum', function (array $array, string $column) {
|
||||
return array_sum(array_column($array, $column));
|
||||
});
|
||||
|
||||
$this->twig->addFilter($filter);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -137,6 +150,11 @@ class TemplateService
|
||||
return $this->compiled_html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the PDF string
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPdf(): mixed
|
||||
{
|
||||
|
||||
@ -150,7 +168,23 @@ class TemplateService
|
||||
|
||||
}
|
||||
|
||||
private function processData($data): self
|
||||
/**
|
||||
* Get the parsed data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getData(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process data variables
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return self
|
||||
*/
|
||||
public function processData($data): self
|
||||
{
|
||||
|
||||
$this->data = $this->preProcessDataBlocks($data);
|
||||
@ -175,24 +209,19 @@ class TemplateService
|
||||
|
||||
try {
|
||||
$template = $this->twig->createTemplate(html_entity_decode($template));
|
||||
}
|
||||
catch(\Twig\Error\SyntaxError $e) {
|
||||
} catch(\Twig\Error\SyntaxError $e) {
|
||||
nlog($e->getMessage());
|
||||
throw ($e);
|
||||
}
|
||||
catch(\Twig\Error\Error $e) {
|
||||
} catch(\Twig\Error\Error $e) {
|
||||
nlog("error = " .$e->getMessage());
|
||||
throw ($e);
|
||||
}
|
||||
catch(\Twig\Error\RuntimeError $e) {
|
||||
} catch(\Twig\Error\RuntimeError $e) {
|
||||
nlog("runtime = " .$e->getMessage());
|
||||
throw ($e);
|
||||
}
|
||||
catch(\Twig\Error\LoaderError $e) {
|
||||
} catch(\Twig\Error\LoaderError $e) {
|
||||
nlog("loader = " . $e->getMessage());
|
||||
throw ($e);
|
||||
}
|
||||
catch(\Twig\Error\SecurityError $e) {
|
||||
} catch(\Twig\Error\SecurityError $e) {
|
||||
nlog("security = " . $e->getMessage());
|
||||
throw ($e);
|
||||
}
|
||||
@ -228,8 +257,7 @@ class TemplateService
|
||||
|
||||
foreach($this->variables as $key => $variable) {
|
||||
|
||||
if(isset($variable['labels']) && isset($variable['values']))
|
||||
{
|
||||
if(isset($variable['labels']) && isset($variable['values'])) {
|
||||
$html = strtr($html, $variable['labels']);
|
||||
$html = strtr($html, $variable['values']);
|
||||
}
|
||||
@ -260,8 +288,9 @@ class TemplateService
|
||||
*/
|
||||
private function compose(): self
|
||||
{
|
||||
if(!$this->template)
|
||||
if(!$this->template) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$html = '';
|
||||
$html .= $this->template->design->includes;
|
||||
@ -308,8 +337,9 @@ class TemplateService
|
||||
|
||||
$processed = [];
|
||||
|
||||
if(in_array($key, ['tasks','projects','aging']) || !$value->first() )
|
||||
if(in_array($key, ['tasks','projects','aging']) || !$value->first()) {
|
||||
return $processed;
|
||||
}
|
||||
|
||||
match ($key) {
|
||||
'variables' => $processed = $value->first() ?? [],
|
||||
@ -359,6 +389,7 @@ class TemplateService
|
||||
->map(function ($invoice) {
|
||||
|
||||
$payments = [];
|
||||
$this->entity = $invoice;
|
||||
|
||||
if($invoice->payments ?? false) {
|
||||
$payments = $invoice->payments->map(function ($payment) {
|
||||
@ -443,8 +474,9 @@ class TemplateService
|
||||
|
||||
$item->cost = Number::formatMoney($item->cost_raw, $client);
|
||||
|
||||
if($item->is_amount_discount)
|
||||
if($item->is_amount_discount) {
|
||||
$item->discount = Number::formatMoney($item->discount_raw, $client);
|
||||
}
|
||||
|
||||
$item->line_total = Number::formatMoney($item->line_total_raw, $client);
|
||||
$item->gross_line_total = Number::formatMoney($item->gross_line_total_raw, $client);
|
||||
@ -461,6 +493,8 @@ class TemplateService
|
||||
|
||||
$data = [];
|
||||
|
||||
$this->payment = $payment;
|
||||
|
||||
$credits = $payment->credits->map(function ($credit) use ($payment) {
|
||||
return [
|
||||
'credit' => $credit->number,
|
||||
@ -614,6 +648,8 @@ class TemplateService
|
||||
$credits = collect($credits)
|
||||
->map(function ($credit) {
|
||||
|
||||
$this->entity = $credit;
|
||||
|
||||
return [
|
||||
'amount' => Number::formatMoney($credit->amount, $credit->client),
|
||||
'balance' => Number::formatMoney($credit->balance, $credit->client),
|
||||
@ -765,4 +801,124 @@ class TemplateService
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses and finds any stacks to replace
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
private function parseGlobalStacks(): self
|
||||
{
|
||||
$stacks = [
|
||||
'entity-details',
|
||||
'client-details',
|
||||
'vendor-details',
|
||||
'company-details',
|
||||
'company-address',
|
||||
'shipping-details',
|
||||
];
|
||||
|
||||
collect($stacks)->filter(function ($stack) {
|
||||
$this->document->getElementById($stack);
|
||||
|
||||
})->each(function ($stack){
|
||||
$this->parseStack($stack);
|
||||
});
|
||||
|
||||
return $this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Injects field stacks into Template
|
||||
*
|
||||
* @param string $stack
|
||||
* @return self
|
||||
*/
|
||||
private function parseStack(string $stack): self
|
||||
{
|
||||
match($stack){
|
||||
'entity-details' => $this->entityDetails(),
|
||||
'client-details' => $this->clientDetails(),
|
||||
'vendor-details' => $this->vendorDetails(),
|
||||
'company-details' => $this->companyDetails(),
|
||||
'company-address' => $this->companyAddress(),
|
||||
'shipping-details' => $this->shippingDetails(),
|
||||
};
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function companyDetails(): self
|
||||
{
|
||||
|
||||
collect($this->company->settings->pdf_variables['company_details'])
|
||||
->filter(function ($variable) {
|
||||
return isset($this->variables['values'][$variable]) && !empty($this->variables['values'][$variable]);
|
||||
})
|
||||
->map(function ($variable) {
|
||||
return ['element' => 'p', 'content' => $variable, 'show_empty' => false, 'properties' => ['data-ref' => 'company_details-' . substr($variable, 1)]];
|
||||
});
|
||||
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function companyAddress(): self
|
||||
{
|
||||
|
||||
$variables = $this->company->settings->pdf_variables['company_address'];
|
||||
|
||||
$elements = [];
|
||||
|
||||
foreach ($variables as $variable) {
|
||||
$elements[] = ['element' => 'p', 'content' => $variable, 'show_empty' => false, 'properties' => ['data-ref' => 'company_address-' . substr($variable, 1)]];
|
||||
}
|
||||
|
||||
return $elements;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function shippingDetails(): self
|
||||
{
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function clientDetails(): self
|
||||
{
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function entityDetails(): self
|
||||
{
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function vendorDetails(): self
|
||||
{
|
||||
|
||||
|
||||
$elements = [];
|
||||
|
||||
if (!$this->vendor) {
|
||||
return $elements;
|
||||
}
|
||||
|
||||
$variables = $this->context['pdf_variables']['vendor_details'];
|
||||
|
||||
foreach ($variables as $variable) {
|
||||
$elements[] = ['element' => 'p', 'content' => $variable, 'show_empty' => false, 'properties' => ['data-ref' => 'vendor_details-' . substr($variable, 1)]];
|
||||
}
|
||||
|
||||
return $elements;
|
||||
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -5163,6 +5163,8 @@ $LANG = array(
|
||||
'click_or_drop_files_here' => 'Click or drop files here',
|
||||
'payment_refund_receipt' => 'Payment Refund Receipt # :number',
|
||||
'payment_receipt' => 'Payment Receipt # :number',
|
||||
'load_template_description' => 'The template will be applied to following:',
|
||||
'run_template' => 'Run template',
|
||||
);
|
||||
|
||||
return $LANG;
|
||||
|
Loading…
x
Reference in New Issue
Block a user