Grafting field stacks into templates

This commit is contained in:
David Bomba 2023-11-06 08:50:36 +11:00
parent d5339d0864
commit 17eaec3186
3 changed files with 319 additions and 178 deletions

View File

@ -1583,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) {

View File

@ -62,11 +62,13 @@ class PdfService
$this->document_type = $document_type;
$this->options = $options;
$this->start_time = microtime(true);
}
public function boot(): self
{
$this->start_time = microtime(true);
$this->init();

View File

@ -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;
@ -71,7 +78,7 @@ class TemplateService
$this->document->validateOnParse = true;
$loader = new \Twig\Loader\FilesystemLoader(storage_path());
$this->twig = new \Twig\Environment($loader,[
$this->twig = new \Twig\Environment($loader, [
'debug' => true,
]);
$string_extension = new \Twig\Extension\StringLoaderExtension();
@ -143,6 +150,11 @@ class TemplateService
return $this->compiled_html;
}
/**
* Returns the PDF string
*
* @return mixed
*/
public function getPdf(): mixed
{
@ -156,11 +168,22 @@ class TemplateService
}
/**
* 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
{
@ -186,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);
}
@ -239,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']);
}
@ -271,8 +288,9 @@ class TemplateService
*/
private function compose(): self
{
if(!$this->template)
if(!$this->template) {
return $this;
}
$html = '';
$html .= $this->template->design->includes;
@ -319,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() ?? [],
@ -343,7 +362,7 @@ class TemplateService
private function preProcessDataBlocks($data): array
{
return collect($data)->map(function ($value, $key){
return collect($data)->map(function ($value, $key) {
$processed = [];
@ -367,9 +386,10 @@ class TemplateService
public function processInvoices($invoices): array
{
$invoices = collect($invoices)
->map(function ($invoice){
->map(function ($invoice) {
$payments = [];
$this->entity = $invoice;
if($invoice->payments ?? false) {
$payments = $invoice->payments->map(function ($payment) {
@ -443,7 +463,7 @@ class TemplateService
public function padLineItems(array $items, Client $client): array
{
return collect($items)->map(function ($item) use ($client){
return collect($items)->map(function ($item) use ($client) {
$item->cost_raw = $item->cost ?? 0;
$item->discount_raw = $item->discount ?? 0;
@ -454,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);
@ -472,6 +493,8 @@ class TemplateService
$data = [];
$this->payment = $payment;
$credits = $payment->credits->map(function ($credit) use ($payment) {
return [
'credit' => $credit->number,
@ -570,7 +593,7 @@ class TemplateService
{
return collect($payment->refund_meta ?? [])
->map(function ($refund) use($payment){
->map(function ($refund) use ($payment) {
$date = \Carbon\Carbon::parse($refund['date'])->addSeconds($payment->client->timezone_offset());
$date = $this->translateDate($date, $payment->client->date_format(), $payment->client->locale());
@ -623,7 +646,9 @@ class TemplateService
public function processCredits($credits): array
{
$credits = collect($credits)
->map(function ($credit){
->map(function ($credit) {
$this->entity = $credit;
return [
'amount' => Number::formatMoney($credit->amount, $credit->client),
@ -776,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;
}
}