diff --git a/app/Services/Pdf/PdfConfiguration.php b/app/Services/Pdf/PdfConfiguration.php index 3f58f6611c94..e0fb873f8e21 100644 --- a/app/Services/Pdf/PdfConfiguration.php +++ b/app/Services/Pdf/PdfConfiguration.php @@ -11,74 +11,88 @@ namespace App\Services\Pdf; -use App\DataMapper\CompanySettings; -use App\Models\Client; -use App\Models\ClientContact; -use App\Models\CreditInvitation; -use App\Models\Currency; -use App\Models\Design; -use App\Models\InvoiceInvitation; -use App\Models\PurchaseOrderInvitation; -use App\Models\QuoteInvitation; -use App\Models\RecurringInvoiceInvitation; -use App\Models\Vendor; -use App\Models\VendorContact; use App\Utils\Ninja; +use App\Models\Client; +use App\Models\Design; +use App\Models\Vendor; +use App\Models\Country; +use App\Models\Currency; +use App\Models\ClientContact; +use App\Models\VendorContact; +use App\Models\QuoteInvitation; use App\Utils\Traits\MakesHash; +use App\Models\CreditInvitation; +use App\Models\InvoiceInvitation; +use App\DataMapper\CompanySettings; use Illuminate\Support\Facades\App; +use App\Models\PurchaseOrderInvitation; +use App\Models\RecurringInvoiceInvitation; class PdfConfiguration { use MakesHash; - public Design $design; - public ?Client $client; public ?ClientContact $contact; - - public ?Vendor $vendor; - - public ?VendorContact $vendor_contact; - - public object $settings; - - public $settings_object; - - public $entity; - - public string $entity_string; - - public array $pdf_variables; - + + public Country $country; + public Currency $currency; - public ?string $path; - - public string $entity_design_id; - /** - * The parent object of the currency - * - * @var App\Models\Client | App\Models\Vendor - * - */ public Client | Vendor $currency_entity; - + + public Design $design; + + public $entity; + + public string $entity_design_id; + + public string $entity_string; + + public ?string $path; + + public array $pdf_variables; + + public object $settings; + + public $settings_object; + + public ?Vendor $vendor; + + public ?VendorContact $vendor_contact; + + /** + * __construct + * + * @param PdfService $service + * @return void + */ public function __construct(public PdfService $service) { } - + + /** + * init + * + * @return self + */ public function init(): self { $this->setEntityType() ->setPdfVariables() ->setDesign() - ->setCurrency() + ->setCurrencyForPdf() ->setLocale(); return $this; } - + + /** + * setLocale + * + * @return self + */ private function setLocale(): self { App::forgetInstance('translator'); @@ -91,8 +105,13 @@ class PdfConfiguration return $this; } - - private function setCurrency(): self + + /** + * setCurrency + * + * @return self + */ + private function setCurrencyForPdf(): self { $this->currency = $this->client ? $this->client->currency() : $this->vendor->currency(); @@ -100,12 +119,18 @@ class PdfConfiguration return $this; } - + + /** + * setPdfVariables + * + * @return self + */ private function setPdfVariables() :self { $default = (array) CompanySettings::getEntityVariableDefaults(); - $variables = (array)$this->service->company->settings->pdf_variables; + // $variables = (array)$this->service->company->settings->pdf_variables; + $variables = (array)$this->settings->pdf_variables; foreach ($default as $property => $value) { if (array_key_exists($property, $variables)) { @@ -119,8 +144,13 @@ class PdfConfiguration return $this; } - - private function setEntityType() + + /** + * setEntityType + * + * @return self + */ + private function setEntityType(): self { $entity_design_id = ''; @@ -179,8 +209,27 @@ class PdfConfiguration return $this; } + + public function setCurrency(Currency $currency): self + { + $this->currency = $currency; - private function setDesign() + return $this; + } + + public function setCountry(Country $country): self + { + $this->country = $country; + + return $this; + } + + /** + * setDesign + * + * @return self + */ + private function setDesign(): self { $design_id = $this->entity->design_id ? : $this->decodePrimaryKey($this->settings_object->getSetting($this->entity_design_id)); @@ -188,4 +237,50 @@ class PdfConfiguration return $this; } + + public function formatMoney($value): string + { + $value = floatval($value); + + $thousand = $this->currency->thousand_separator; + $decimal = $this->currency->decimal_separator; + $precision = $this->currency->precision; + $code = $this->currency->code; + $swapSymbol = $this->currency->swap_currency_symbol; + + /* Country settings override client settings */ + if (isset($this->country->thousand_separator) && strlen($this->country->thousand_separator) >= 1) { + $thousand = $this->country->thousand_separator; + } + + if (isset($this->country->decimal_separator) && strlen($this->country->decimal_separator) >= 1) { + $decimal = $this->country->decimal_separator; + } + + if (isset($this->country->swap_currency_symbol) && strlen($this->country->swap_currency_symbol) >= 1) { + $swapSymbol = $this->country->swap_currency_symbol; + } + + $value = number_format($value, $precision, $decimal, $thousand); + $symbol = $this->currency->symbol; + + if ($this->settings->show_currency_code === true && $this->currency->code == 'CHF') { + return "{$code} {$value}"; + } elseif ($this->settings->show_currency_code === true) { + return "{$value} {$code}"; + } elseif ($swapSymbol) { + return "{$value} ".trim($symbol); + } elseif ($this->settings->show_currency_code === false) { + return "{$symbol}{$value}"; + } else { + + $value = floatval($value); + $thousand = $this->currency->thousand_separator; + $decimal = $this->currency->decimal_separator; + $precision = $this->currency->precision; + + return number_format($value, $precision, $decimal, $thousand); + } + + } }