mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-15 04:24:36 -04:00
Refactor PDF Service
This commit is contained in:
parent
19313df73c
commit
08b1b8768f
@ -11,74 +11,88 @@
|
|||||||
|
|
||||||
namespace App\Services\Pdf;
|
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\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\Utils\Traits\MakesHash;
|
||||||
|
use App\Models\CreditInvitation;
|
||||||
|
use App\Models\InvoiceInvitation;
|
||||||
|
use App\DataMapper\CompanySettings;
|
||||||
use Illuminate\Support\Facades\App;
|
use Illuminate\Support\Facades\App;
|
||||||
|
use App\Models\PurchaseOrderInvitation;
|
||||||
|
use App\Models\RecurringInvoiceInvitation;
|
||||||
|
|
||||||
class PdfConfiguration
|
class PdfConfiguration
|
||||||
{
|
{
|
||||||
use MakesHash;
|
use MakesHash;
|
||||||
|
|
||||||
public Design $design;
|
|
||||||
|
|
||||||
public ?Client $client;
|
public ?Client $client;
|
||||||
|
|
||||||
public ?ClientContact $contact;
|
public ?ClientContact $contact;
|
||||||
|
|
||||||
public ?Vendor $vendor;
|
public Country $country;
|
||||||
|
|
||||||
public ?VendorContact $vendor_contact;
|
public Currency $currency;
|
||||||
|
|
||||||
|
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 object $settings;
|
||||||
|
|
||||||
public $settings_object;
|
public $settings_object;
|
||||||
|
|
||||||
public $entity;
|
public ?Vendor $vendor;
|
||||||
|
|
||||||
public string $entity_string;
|
public ?VendorContact $vendor_contact;
|
||||||
|
|
||||||
public array $pdf_variables;
|
|
||||||
|
|
||||||
public Currency $currency;
|
|
||||||
|
|
||||||
public ?string $path;
|
|
||||||
|
|
||||||
public string $entity_design_id;
|
|
||||||
/**
|
/**
|
||||||
* The parent object of the currency
|
* __construct
|
||||||
*
|
|
||||||
* @var App\Models\Client | App\Models\Vendor
|
|
||||||
*
|
*
|
||||||
|
* @param PdfService $service
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public Client | Vendor $currency_entity;
|
|
||||||
|
|
||||||
public function __construct(public PdfService $service)
|
public function __construct(public PdfService $service)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* init
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
public function init(): self
|
public function init(): self
|
||||||
{
|
{
|
||||||
$this->setEntityType()
|
$this->setEntityType()
|
||||||
->setPdfVariables()
|
->setPdfVariables()
|
||||||
->setDesign()
|
->setDesign()
|
||||||
->setCurrency()
|
->setCurrencyForPdf()
|
||||||
->setLocale();
|
->setLocale();
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* setLocale
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
private function setLocale(): self
|
private function setLocale(): self
|
||||||
{
|
{
|
||||||
App::forgetInstance('translator');
|
App::forgetInstance('translator');
|
||||||
@ -92,7 +106,12 @@ class PdfConfiguration
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function setCurrency(): self
|
/**
|
||||||
|
* setCurrency
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
private function setCurrencyForPdf(): self
|
||||||
{
|
{
|
||||||
$this->currency = $this->client ? $this->client->currency() : $this->vendor->currency();
|
$this->currency = $this->client ? $this->client->currency() : $this->vendor->currency();
|
||||||
|
|
||||||
@ -101,11 +120,17 @@ class PdfConfiguration
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* setPdfVariables
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
private function setPdfVariables() :self
|
private function setPdfVariables() :self
|
||||||
{
|
{
|
||||||
$default = (array) CompanySettings::getEntityVariableDefaults();
|
$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) {
|
foreach ($default as $property => $value) {
|
||||||
if (array_key_exists($property, $variables)) {
|
if (array_key_exists($property, $variables)) {
|
||||||
@ -120,7 +145,12 @@ class PdfConfiguration
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function setEntityType()
|
/**
|
||||||
|
* setEntityType
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
private function setEntityType(): self
|
||||||
{
|
{
|
||||||
$entity_design_id = '';
|
$entity_design_id = '';
|
||||||
|
|
||||||
@ -180,7 +210,26 @@ class PdfConfiguration
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function setDesign()
|
public function setCurrency(Currency $currency): self
|
||||||
|
{
|
||||||
|
$this->currency = $currency;
|
||||||
|
|
||||||
|
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));
|
$design_id = $this->entity->design_id ? : $this->decodePrimaryKey($this->settings_object->getSetting($this->entity_design_id));
|
||||||
|
|
||||||
@ -188,4 +237,50 @@ class PdfConfiguration
|
|||||||
|
|
||||||
return $this;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user