From 149637bb107d3dd6380fd06e5dd2d2fe3d56e9a3 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 12 Oct 2023 09:17:28 +1100 Subject: [PATCH] Working on statement templates --- app/Services/Client/Statement.php | 138 ++++++++++++---------- app/Services/Template/TemplateService.php | 39 +++++- 2 files changed, 110 insertions(+), 67 deletions(-) diff --git a/app/Services/Client/Statement.php b/app/Services/Client/Statement.php index 68d1f29aa531..a30df8c714a8 100644 --- a/app/Services/Client/Statement.php +++ b/app/Services/Client/Statement.php @@ -12,24 +12,25 @@ namespace App\Services\Client; -use App\Factory\InvoiceFactory; -use App\Factory\InvoiceInvitationFactory; -use App\Factory\InvoiceItemFactory; +use App\Utils\Number; use App\Models\Client; +use App\Models\Credit; use App\Models\Design; use App\Models\Invoice; use App\Models\Payment; -use App\Services\PdfMaker\Design as PdfMakerDesign; -use App\Services\PdfMaker\PdfMaker; -use App\Utils\HostedPDF\NinjaPdf; use App\Utils\HtmlEngine; -use App\Utils\Number; -use App\Utils\PhantomJS\Phantom; -use App\Utils\Traits\Pdf\PdfMaker as PdfMakerTrait; use Illuminate\Support\Carbon; -use Illuminate\Support\Facades\DB; -use App\Models\Credit; +use App\Factory\InvoiceFactory; use App\Utils\Traits\MakesHash; +use App\Utils\PhantomJS\Phantom; +use App\Utils\HostedPDF\NinjaPdf; +use Illuminate\Support\Facades\DB; +use App\Factory\InvoiceItemFactory; +use App\Services\PdfMaker\PdfMaker; +use App\Factory\InvoiceInvitationFactory; +use Illuminate\Database\Eloquent\Builder; +use App\Services\PdfMaker\Design as PdfMakerDesign; +use App\Utils\Traits\Pdf\PdfMaker as PdfMakerTrait; class Statement { @@ -54,6 +55,12 @@ class Statement $html = new HtmlEngine($this->getInvitation()); + $variables = $html->generateLabelsAndValues(); + + if($this->client->getSetting('statement_design_id') != '') { + return $this->templateStatement($variables); + } + if ($this->getDesign()->is_custom) { $this->options['custom_partials'] = \json_decode(\json_encode($this->getDesign()->design), true); @@ -62,8 +69,6 @@ class Statement $template = new PdfMakerDesign(strtolower($this->getDesign()->name), $this->options); } - $variables = $html->generateLabelsAndValues(); - $state = [ 'template' => $template->elements([ 'client' => $this->client, @@ -71,9 +76,9 @@ class Statement 'pdf_variables' => (array) $this->entity->company->settings->pdf_variables, '$product' => $this->getDesign()->design->product, 'variables' => $variables, - 'invoices' => $this->getInvoices(), - 'payments' => $this->getPayments(), - 'credits' => $this->getCredits(), + 'invoices' => $this->getInvoices()->cursor(), + 'payments' => $this->getPayments()->cursor(), + 'credits' => $this->getCredits()->cursor(), 'aging' => $this->getAging(), ], \App\Services\PdfMaker\Design::STATEMENT), 'variables' => $variables, @@ -81,44 +86,65 @@ class Statement 'client' => $this->client, 'entity' => $this->entity, 'variables' => $variables, - 'invoices' => $this->getInvoices(), - 'payments' => $this->getPayments(), - 'credits' => $this->getCredits(), + 'invoices' => $this->getInvoices()->cursor(), + 'payments' => $this->getPayments()->cursor(), + 'credits' => $this->getCredits()->cursor(), 'aging' => $this->getAging(), ], 'process_markdown' => $this->entity->client->company->markdown_enabled, ]; - if($this->options['template'] ?? false){ + - $template = Design::where('id', $this->decodePrimaryKey($this->options['template'])) - ->where('company_id', $this->client->company_id) - ->first(); + $maker = new PdfMaker($state); - $ts = $template->service()->build([ - 'client' => $this->client, - 'entity' => $this->entity, - 'variables' => $variables, - 'invoices' => $this->getInvoices(), - 'payments' => $this->getPayments(), - 'credits' => $this->getCredits(), - 'aging' => $this->getAging(), - ]); + $maker + ->design($template) + ->build(); - $html = $ts->getHtml(); - + $pdf = null; + $html = $maker->getCompiledHTML(true); + + + if ($this->rollback) { + \DB::connection(config('database.default'))->rollBack(); } - else { - $maker = new PdfMaker($state); + $pdf = $this->convertToPdf($html); - $maker - ->design($template) - ->build(); + $maker = null; + $state = null; - $pdf = null; - $html = $maker->getCompiledHTML(true); - } + return $pdf; + } + + private function templateStatement($variables) + {nlog($this->options); + if(isset($this->options['template'])) + $statement_design_id = $this->options['template']; + else + $statement_design_id = $this->client->getSetting('statement_design_id'); + + $template = Design::where('id', $this->decodePrimaryKey($statement_design_id)) + ->where('company_id', $this->client->company_id) + ->first(); + + $ts = $template->service()->build([ + 'variables' => $variables, + 'invoices' => $this->getInvoices()->get(), + 'payments' => $this->getPayments()->get(), + 'credits' => $this->getCredits()->get(), + 'aging' => $this->getAging(), + ]); + + $html = $ts->getHtml(); + + return $this->convertToPdf($html); + } + + private function convertToPdf(string $html): mixed + { + $pdf = false; try { if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') { @@ -132,16 +158,9 @@ class Statement nlog(print_r($e->getMessage(), 1)); } - if ($this->rollback) { - \DB::connection(config('database.default'))->rollBack(); - } - - $maker = null; - $state = null; return $pdf; } - /** * Setup correct entity instance. * @@ -253,9 +272,9 @@ class Statement /** * The collection of invoices for the statement. * - * @return Invoice[]|\Illuminate\Support\LazyCollection + * @return Builder */ - public function getInvoices(): \Illuminate\Support\LazyCollection + public function getInvoices(): Builder { return Invoice::withTrashed() ->with('payments.type') @@ -265,8 +284,7 @@ class Statement ->whereIn('status_id', $this->invoiceStatuses()) ->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])]) ->orderBy('due_date', 'ASC') - ->orderBy('date', 'ASC') - ->cursor(); + ->orderBy('date', 'ASC'); } private function invoiceStatuses() :array @@ -297,9 +315,9 @@ class Statement /** * The collection of payments for the statement. * - * @return Payment[]|\Illuminate\Support\LazyCollection + * @return Builder */ - protected function getPayments(): \Illuminate\Support\LazyCollection + protected function getPayments(): Builder { return Payment::withTrashed() ->with('client.country', 'invoices') @@ -308,16 +326,15 @@ class Statement ->where('client_id', $this->client->id) ->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED]) ->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])]) - ->orderBy('date', 'ASC') - ->cursor(); + ->orderBy('date', 'ASC'); } /** * The collection of credits for the statement. * - * @return Credit[]|\Illuminate\Support\LazyCollection + * @return Builder */ - protected function getCredits(): \Illuminate\Support\LazyCollection + protected function getCredits(): Builder { return Credit::withTrashed() ->with('client.country', 'invoices') @@ -330,8 +347,7 @@ class Statement $query->whereDate('due_date', '>=', $this->options['end_date']) ->orWhereNull('due_date'); }) - ->orderBy('date', 'ASC') - ->cursor(); + ->orderBy('date', 'ASC'); } /** diff --git a/app/Services/Template/TemplateService.php b/app/Services/Template/TemplateService.php index 4e3a2ace4564..5fc3605927bd 100644 --- a/app/Services/Template/TemplateService.php +++ b/app/Services/Template/TemplateService.php @@ -71,10 +71,13 @@ 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(); $this->twig->addExtension($string_extension); $this->twig->addExtension(new IntlExtension()); + $this->twig->addExtension(new \Twig\Extension\DebugExtension()); $function = new \Twig\TwigFunction('img', function ($string, $style = '') { return ''; @@ -123,7 +126,12 @@ class TemplateService return $this; } - + + /** + * Returns the HTML as string + * + * @return string + */ public function getHtml(): string { return $this->compiled_html; @@ -172,6 +180,22 @@ class TemplateService nlog($e->getMessage()); throw ($e); } + catch(\Twig\Error\Error $e) { + nlog("error = " .$e->getMessage()); + throw ($e); + } + catch(\Twig\Error\RuntimeError $e) { + nlog("runtime = " .$e->getMessage()); + throw ($e); + } + catch(\Twig\Error\LoaderError $e) { + nlog("loader = " . $e->getMessage()); + throw ($e); + } + catch(\Twig\Error\SecurityError $e) { + nlog("security = " . $e->getMessage()); + throw ($e); + } $template = $template->render($this->data); @@ -284,7 +308,7 @@ class TemplateService $processed = []; - if(in_array($key, ['tasks','projects']) || !$value->first() ) + if(in_array($key, ['tasks','projects','aging']) || is_array($value) || !$value->first() ) return $processed; match ($key) { @@ -295,6 +319,8 @@ class TemplateService 'tasks' => $processed = [], 'projects' => $processed = [], 'purchase_orders' => (new VendorHtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [], + 'aging' => $processed = [], + default => $processed = [], }; return $processed; @@ -317,6 +343,8 @@ class TemplateService 'tasks' => $processed = $this->processTasks($value), 'projects' => $processed = $this->processProjects($value), 'purchase_orders' => $processed = $this->processPurchaseOrders($value), + 'aging' => $processed = $value, + default => $processed = [], }; return $processed; @@ -333,7 +361,6 @@ class TemplateService if($invoice->payments ?? false) { $payments = $invoice->payments->map(function ($payment) { - // nlog(microtime(true)); return $this->transformPayment($payment); })->toArray(); } @@ -530,8 +557,8 @@ class TemplateService 'refund_activity' => $this->getPaymentRefundActivity($payment), ]; - nlog($this->getPaymentRefundActivity($payment)); - + nlog($data); + return $data; }