Refactor PDF Service

This commit is contained in:
David Bomba 2023-02-23 23:01:18 +11:00
parent 08b1b8768f
commit 0efa84a070
2 changed files with 49 additions and 9 deletions

View File

@ -17,7 +17,6 @@ use App\Utils\Helpers;
use App\Utils\Number; use App\Utils\Number;
use App\Utils\Traits\MakesDates; use App\Utils\Traits\MakesDates;
use DOMDocument; use DOMDocument;
use DOMXPath;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use League\CommonMark\CommonMarkConverter; use League\CommonMark\CommonMarkConverter;
@ -245,9 +244,9 @@ class PdfBuilder
$element = ['element' => 'tr', 'elements' => []]; $element = ['element' => 'tr', 'elements' => []];
$element['elements'][] = ['element' => 'td', 'content' => $invoice->number]; $element['elements'][] = ['element' => 'td', 'content' => $invoice->number];
$element['elements'][] = ['element' => 'td', 'content' => $this->translateDate($payment->date, $this->service->config->client->date_format(), $this->service->config->client->locale()) ?: ' ']; $element['elements'][] = ['element' => 'td', 'content' => $this->translateDate($payment->date, $this->service->config->date_format, $this->service->config->locale) ?: ' '];
$element['elements'][] = ['element' => 'td', 'content' => $payment->type ? $payment->type->name : ctrans('texts.manual_entry')]; $element['elements'][] = ['element' => 'td', 'content' => $payment->type ? $payment->type->name : ctrans('texts.manual_entry')];
$element['elements'][] = ['element' => 'td', 'content' => Number::formatMoney($payment->pivot->amount, $this->service->config->client) ?: ' ']; $element['elements'][] = ['element' => 'td', 'content' => $this->service->config->formatMoney($payment->pivot->amount) ?: ' '];
$tbody[] = $element; $tbody[] = $element;
@ -382,8 +381,8 @@ class PdfBuilder
$element = ['element' => 'tr', 'elements' => []]; $element = ['element' => 'tr', 'elements' => []];
$element['elements'][] = ['element' => 'td', 'content' => $invoice->number]; $element['elements'][] = ['element' => 'td', 'content' => $invoice->number];
$element['elements'][] = ['element' => 'td', 'content' => $this->translateDate($invoice->date, $this->service->config->client->date_format(), $this->service->config->client->locale()) ?: ' ']; $element['elements'][] = ['element' => 'td', 'content' => $this->translateDate($invoice->date, $this->service->config->client->date_format(), $this->service->config->locale) ?: ' '];
$element['elements'][] = ['element' => 'td', 'content' => $this->translateDate($invoice->due_date, $this->service->config->client->date_format(), $this->service->config->client->locale()) ?: ' ']; $element['elements'][] = ['element' => 'td', 'content' => $this->translateDate($invoice->due_date, $this->service->config->client->date_format(), $this->service->config->locale) ?: ' '];
$element['elements'][] = ['element' => 'td', 'content' => Number::formatMoney($invoice->amount, $this->service->config->client) ?: ' ']; $element['elements'][] = ['element' => 'td', 'content' => Number::formatMoney($invoice->amount, $this->service->config->client) ?: ' '];
$element['elements'][] = ['element' => 'td', 'content' => Number::formatMoney($invoice->balance, $this->service->config->client) ?: ' ']; $element['elements'][] = ['element' => 'td', 'content' => Number::formatMoney($invoice->balance, $this->service->config->client) ?: ' '];
@ -1104,7 +1103,7 @@ class PdfBuilder
*/ */
public function statementDetails(): array public function statementDetails(): array
{ {
$s_date = $this->translateDate(now(), $this->service->config->client->date_format(), $this->service->config->client->locale()); $s_date = $this->translateDate(now(), $this->service->config->client->date_format(), $this->service->config->locale);
return [ return [
['element' => 'tr', 'properties' => ['data-ref' => 'statement-label'], 'elements' => [ ['element' => 'tr', 'properties' => ['data-ref' => 'statement-label'], 'elements' => [

View File

@ -25,12 +25,14 @@ use App\Models\CreditInvitation;
use App\Models\InvoiceInvitation; use App\Models\InvoiceInvitation;
use App\DataMapper\CompanySettings; use App\DataMapper\CompanySettings;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use App\Models\PurchaseOrderInvitation; use App\Models\PurchaseOrderInvitation;
use App\Models\RecurringInvoiceInvitation; use App\Models\RecurringInvoiceInvitation;
use App\Utils\Traits\AppSetup;
class PdfConfiguration class PdfConfiguration
{ {
use MakesHash; use MakesHash, AppSetup;
public ?Client $client; public ?Client $client;
@ -62,6 +64,10 @@ class PdfConfiguration
public ?VendorContact $vendor_contact; public ?VendorContact $vendor_contact;
public string $date_format;
public string $locale;
/** /**
* __construct * __construct
* *
@ -80,6 +86,7 @@ class PdfConfiguration
public function init(): self public function init(): self
{ {
$this->setEntityType() $this->setEntityType()
->setDateFormat()
->setPdfVariables() ->setPdfVariables()
->setDesign() ->setDesign()
->setCurrencyForPdf() ->setCurrencyForPdf()
@ -103,6 +110,8 @@ class PdfConfiguration
$t->replace(Ninja::transformTranslations($this->settings)); $t->replace(Ninja::transformTranslations($this->settings));
$this->locale = $this->settings_object->locale();
return $this; return $this;
} }
@ -163,6 +172,7 @@ class PdfConfiguration
$this->entity_design_id = 'invoice_design_id'; $this->entity_design_id = 'invoice_design_id';
$this->settings = $this->client->getMergedSettings(); $this->settings = $this->client->getMergedSettings();
$this->settings_object = $this->client; $this->settings_object = $this->client;
$this->country = $this->client->country;
} elseif ($this->service->invitation instanceof QuoteInvitation) { } elseif ($this->service->invitation instanceof QuoteInvitation) {
$this->entity = $this->service->invitation->quote; $this->entity = $this->service->invitation->quote;
$this->entity_string = 'quote'; $this->entity_string = 'quote';
@ -172,6 +182,7 @@ class PdfConfiguration
$this->entity_design_id = 'quote_design_id'; $this->entity_design_id = 'quote_design_id';
$this->settings = $this->client->getMergedSettings(); $this->settings = $this->client->getMergedSettings();
$this->settings_object = $this->client; $this->settings_object = $this->client;
$this->country = $this->client->country;
} elseif ($this->service->invitation instanceof CreditInvitation) { } elseif ($this->service->invitation instanceof CreditInvitation) {
$this->entity = $this->service->invitation->credit; $this->entity = $this->service->invitation->credit;
$this->entity_string = 'credit'; $this->entity_string = 'credit';
@ -181,6 +192,7 @@ class PdfConfiguration
$this->entity_design_id = 'credit_design_id'; $this->entity_design_id = 'credit_design_id';
$this->settings = $this->client->getMergedSettings(); $this->settings = $this->client->getMergedSettings();
$this->settings_object = $this->client; $this->settings_object = $this->client;
$this->country = $this->client->country;
} elseif ($this->service->invitation instanceof RecurringInvoiceInvitation) { } elseif ($this->service->invitation instanceof RecurringInvoiceInvitation) {
$this->entity = $this->service->invitation->recurring_invoice; $this->entity = $this->service->invitation->recurring_invoice;
$this->entity_string = 'recurring_invoice'; $this->entity_string = 'recurring_invoice';
@ -190,6 +202,7 @@ class PdfConfiguration
$this->entity_design_id = 'invoice_design_id'; $this->entity_design_id = 'invoice_design_id';
$this->settings = $this->client->getMergedSettings(); $this->settings = $this->client->getMergedSettings();
$this->settings_object = $this->client; $this->settings_object = $this->client;
$this->country = $this->client->country;
} elseif ($this->service->invitation instanceof PurchaseOrderInvitation) { } elseif ($this->service->invitation instanceof PurchaseOrderInvitation) {
$this->entity = $this->service->invitation->purchase_order; $this->entity = $this->service->invitation->purchase_order;
$this->entity_string = 'purchase_order'; $this->entity_string = 'purchase_order';
@ -201,6 +214,7 @@ class PdfConfiguration
$this->settings = $this->vendor->company->settings; $this->settings = $this->vendor->company->settings;
$this->settings_object = $this->vendor; $this->settings_object = $this->vendor;
$this->client = null; $this->client = null;
$this->country = $this->vendor->country ?: $this->vendor->company->country();
} else { } else {
throw new \Exception('Unable to resolve entity', 500); throw new \Exception('Unable to resolve entity', 500);
} }
@ -237,7 +251,13 @@ class PdfConfiguration
return $this; return $this;
} }
/**
* formatMoney
*
* @param float $value
* @return string
*/
public function formatMoney($value): string public function formatMoney($value): string
{ {
$value = floatval($value); $value = floatval($value);
@ -248,7 +268,6 @@ class PdfConfiguration
$code = $this->currency->code; $code = $this->currency->code;
$swapSymbol = $this->currency->swap_currency_symbol; $swapSymbol = $this->currency->swap_currency_symbol;
/* Country settings override client settings */
if (isset($this->country->thousand_separator) && strlen($this->country->thousand_separator) >= 1) { if (isset($this->country->thousand_separator) && strlen($this->country->thousand_separator) >= 1) {
$thousand = $this->country->thousand_separator; $thousand = $this->country->thousand_separator;
} }
@ -283,4 +302,26 @@ class PdfConfiguration
} }
} }
/**
* date_format
*
* @return self
*/
public function setDateFormat(): self
{
$date_formats = Cache::get('date_formats');
if (! $date_formats) {
$this->buildCache(true);
}
$this->date_format = $date_formats->filter(function ($item) {
return $item->id == $this->settings->date_format_id;
})->first()->format;
return $this;
}
} }