From 1cf829b137a77507693623d9921ec3b6441fb476 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 12 Oct 2023 12:13:08 +1100 Subject: [PATCH] Working on statement templates --- app/Services/Client/Statement.php | 28 +++-- app/Services/Template/TemplateService.php | 123 +++++++++++++--------- 2 files changed, 95 insertions(+), 56 deletions(-) diff --git a/app/Services/Client/Statement.php b/app/Services/Client/Statement.php index a30df8c714..4cca2b8bb7 100644 --- a/app/Services/Client/Statement.php +++ b/app/Services/Client/Statement.php @@ -30,12 +30,15 @@ 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\MakesDates; use App\Utils\Traits\Pdf\PdfMaker as PdfMakerTrait; class Statement { use PdfMakerTrait; use MakesHash; + use MakesDates; + /** * @var Invoice|Payment|null */ @@ -55,12 +58,20 @@ class Statement $html = new HtmlEngine($this->getInvitation()); - $variables = $html->generateLabelsAndValues(); + $variables = []; if($this->client->getSetting('statement_design_id') != '') { + + $variables['values']['$start_date'] = $this->translateDate($this->options['start_date'], $this->client->date_format(), $this->client->locale()); + $variables['values']['$end_date'] = $this->translateDate($this->options['end_date'], $this->client->date_format(), $this->client->locale()); + $variables['labels']['$start_date_label'] = ctrans('texts.start_date'); + $variables['labels']['$end_date_label'] = ctrans('texts.end_date'); + return $this->templateStatement($variables); } + $variables = $html->generateLabelsAndValues(); + if ($this->getDesign()->is_custom) { $this->options['custom_partials'] = \json_decode(\json_encode($this->getDesign()->design), true); @@ -119,7 +130,7 @@ class Statement } private function templateStatement($variables) - {nlog($this->options); + { if(isset($this->options['template'])) $statement_design_id = $this->options['template']; else @@ -130,11 +141,11 @@ class Statement ->first(); $ts = $template->service()->build([ - 'variables' => $variables, + 'variables' => collect([$variables]), 'invoices' => $this->getInvoices()->get(), - 'payments' => $this->getPayments()->get(), - 'credits' => $this->getCredits()->get(), - 'aging' => $this->getAging(), + '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() : [], ]); $html = $ts->getHtml(); @@ -305,7 +316,6 @@ class Statement case 'unpaid': return [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]; - default: return [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID]; @@ -337,7 +347,7 @@ class Statement protected function getCredits(): Builder { return Credit::withTrashed() - ->with('client.country', 'invoices') + ->with('client.country','invoice') ->where('is_deleted', false) ->where('company_id', $this->client->company_id) ->where('client_id', $this->client->id) @@ -386,7 +396,7 @@ class Statement * @param mixed $range * @return string */ - private function getAgingAmount($range) + private function getAgingAmount($range): string { $ranges = $this->calculateDateRanges($range); diff --git a/app/Services/Template/TemplateService.php b/app/Services/Template/TemplateService.php index 5fc3605927..d7b0264e92 100644 --- a/app/Services/Template/TemplateService.php +++ b/app/Services/Template/TemplateService.php @@ -308,10 +308,11 @@ class TemplateService $processed = []; - if(in_array($key, ['tasks','projects','aging']) || is_array($value) || !$value->first() ) + if(in_array($key, ['tasks','projects','aging']) || !$value->first() ) return $processed; match ($key) { + 'variables' => $processed = $value->first() ?? [], 'invoices' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [], 'quotes' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [], 'credits' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [], @@ -455,35 +456,6 @@ class TemplateService })->toArray(); } - public function processInvoicesBak($invoices): array - { - $it = new InvoiceTransformer(); - $it->setDefaultIncludes(['client','payments', 'credits']); - $manager = new Manager(); - $manager->parseIncludes(['client','payments','payments.type','credits']); - $resource = new \League\Fractal\Resource\Collection($invoices, $it, null); - $invoices = $manager->createData($resource)->toArray(); - - foreach($invoices['data'] as $key => $invoice) - { - - $invoices['data'][$key]['client'] = $invoice['client']['data'] ?? []; - $invoices['data'][$key]['client']['contacts'] = $invoice['client']['data']['contacts']['data'] ?? []; - $invoices['data'][$key]['payments'] = $invoice['payments']['data'] ?? []; - $invoices['data'][$key]['credits'] = $invoice['credits']['data'] ?? []; - - if($invoice['payments']['data'] ?? false) { - foreach($invoice['payments']['data'] as $keyx => $payment) { - $invoices['data'][$key]['payments'][$keyx]['paymentables'] = $payment['paymentables']['data'] ?? []; - $invoices['data'][$key]['payments'][$keyx]['type'] = $payment['type']['data'] ?? []; - } - } - - } - - return $invoices['data']; - } - private function transformPayment(Payment $payment): array { @@ -558,7 +530,7 @@ class TemplateService ]; nlog($data); - + return $data; } @@ -610,8 +582,6 @@ class TemplateService } - - public function processQuotes($quotes): array { $it = new QuoteTransformer(); @@ -641,24 +611,75 @@ class TemplateService */ public function processCredits($credits): array { - $it = new CreditTransformer(); - $it->setDefaultIncludes(['client']); - $manager = new Manager(); - $resource = new \League\Fractal\Resource\Collection($credits, $it, Credit::class); - $resources = $manager->createData($resource)->toArray(); + $credits = collect($credits) + ->map(function ($credit){ - foreach($resources['data'] as $key => $resource) { + return [ + 'amount' => Number::formatMoney($credit->amount, $credit->client), + 'balance' => Number::formatMoney($credit->balance, $credit->client), + 'balance_raw' => $credit->balance, + 'number' => $credit->number ?: '', + 'discount' => $credit->discount, + 'po_number' => $credit->po_number ?: '', + 'date' => $this->translateDate($credit->date, $credit->client->date_format(), $credit->client->locale()), + 'last_sent_date' => $this->translateDate($credit->last_sent_date, $credit->client->date_format(), $credit->client->locale()), + 'next_send_date' => $this->translateDate($credit->next_send_date, $credit->client->date_format(), $credit->client->locale()), + 'due_date' => $this->translateDate($credit->due_date, $credit->client->date_format(), $credit->client->locale()), + 'terms' => $credit->terms ?: '', + 'public_notes' => $credit->public_notes ?: '', + 'private_notes' => $credit->private_notes ?: '', + 'uses_inclusive_taxes' => (bool) $credit->uses_inclusive_taxes, + 'tax_name1' => $credit->tax_name1 ?? '', + 'tax_rate1' => (float) $credit->tax_rate1, + 'tax_name2' => $credit->tax_name2 ?? '', + 'tax_rate2' => (float) $credit->tax_rate2, + 'tax_name3' => $credit->tax_name3 ?? '', + 'tax_rate3' => (float) $credit->tax_rate3, + 'total_taxes' => Number::formatMoney($credit->total_taxes, $credit->client), + 'total_taxes_raw' => $credit->total_taxes, + 'is_amount_discount' => (bool) $credit->is_amount_discount ?? false, + 'footer' => $credit->footer ?? '', + 'partial' => $credit->partial ?? 0, + 'partial_due_date' => $this->translateDate($credit->partial_due_date, $credit->client->date_format(), $credit->client->locale()), + 'custom_value1' => (string) $credit->custom_value1 ?: '', + 'custom_value2' => (string) $credit->custom_value2 ?: '', + 'custom_value3' => (string) $credit->custom_value3 ?: '', + 'custom_value4' => (string) $credit->custom_value4 ?: '', + 'custom_surcharge1' => (float) $credit->custom_surcharge1, + 'custom_surcharge2' => (float) $credit->custom_surcharge2, + 'custom_surcharge3' => (float) $credit->custom_surcharge3, + 'custom_surcharge4' => (float) $credit->custom_surcharge4, + 'exchange_rate' => (float) $credit->exchange_rate, + 'custom_surcharge_tax1' => (bool) $credit->custom_surcharge_tax1, + 'custom_surcharge_tax2' => (bool) $credit->custom_surcharge_tax2, + 'custom_surcharge_tax3' => (bool) $credit->custom_surcharge_tax3, + 'custom_surcharge_tax4' => (bool) $credit->custom_surcharge_tax4, + 'line_items' => $credit->line_items ? $this->padLineItems($credit->line_items, $credit->client): (array) [], + 'reminder1_sent' => $this->translateDate($credit->reminder1_sent, $credit->client->date_format(), $credit->client->locale()), + 'reminder2_sent' => $this->translateDate($credit->reminder2_sent, $credit->client->date_format(), $credit->client->locale()), + 'reminder3_sent' => $this->translateDate($credit->reminder3_sent, $credit->client->date_format(), $credit->client->locale()), + 'reminder_last_sent' => $this->translateDate($credit->reminder_last_sent, $credit->client->date_format(), $credit->client->locale()), + 'paid_to_date' => Number::formatMoney($credit->paid_to_date, $credit->client), + 'auto_bill_enabled' => (bool) $credit->auto_bill_enabled, + 'client' => [ + 'name' => $credit->client->present()->name(), + 'balance' => $credit->client->balance, + 'payment_balance' => $credit->client->payment_balance, + 'credit_balance' => $credit->client->credit_balance, + ], + 'payments' => [], + 'total_tax_map' => $credit->calc()->getTotalTaxMap(), + 'line_tax_map' => $credit->calc()->getTaxMap(), + ]; - $resources['data'][$key]['client'] = $resource['client']['data'] ?? []; - $resources['data'][$key]['client']['contacts'] = $resource['client']['data']['contacts']['data'] ?? []; - - } - - return $resources['data']; + }); + return $credits->toArray(); } - + + + /** * Pushes payments through the appropriate transformer * @@ -668,7 +689,7 @@ class TemplateService public function processPayments($payments): array { - $payments = $payments->map(function ($payment) { + $payments = collect($payments)->map(function ($payment) { return $this->transformPayment($payment); })->toArray(); @@ -736,4 +757,12 @@ class TemplateService { return $this->company; } + + public function overrideVariables($variables): self + { + $this->variables = $variables; + + return $this; + } + } \ No newline at end of file