1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Services/Pdf/PdfConfiguration.php

349 lines
10 KiB
PHP
Raw Normal View History

2022-12-23 03:22:01 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Pdf;
2023-02-25 06:11:12 +01:00
use App\DataMapper\CompanySettings;
2022-12-23 03:22:01 +01:00
use App\Models\Client;
2023-02-25 06:11:12 +01:00
use App\Models\ClientContact;
use App\Models\Country;
2023-02-25 01:11:09 +01:00
use App\Models\Credit;
2023-02-25 06:11:12 +01:00
use App\Models\CreditInvitation;
use App\Models\Currency;
2022-12-23 03:22:01 +01:00
use App\Models\Design;
2023-02-25 01:11:09 +01:00
use App\Models\Invoice;
2023-02-25 06:11:12 +01:00
use App\Models\InvoiceInvitation;
2023-02-25 01:11:09 +01:00
use App\Models\PurchaseOrder;
2023-02-25 06:11:12 +01:00
use App\Models\PurchaseOrderInvitation;
use App\Models\Quote;
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoiceInvitation;
use App\Models\Vendor;
2022-12-27 16:44:12 +01:00
use App\Models\VendorContact;
2023-02-25 06:11:12 +01:00
use App\Utils\Ninja;
2023-02-25 01:11:09 +01:00
use App\Utils\Traits\AppSetup;
2022-12-23 03:22:01 +01:00
use App\Utils\Traits\MakesHash;
2023-02-25 06:11:12 +01:00
use Illuminate\Support\Collection;
2022-12-28 15:50:11 +01:00
use Illuminate\Support\Facades\App;
2023-02-23 13:01:18 +01:00
use Illuminate\Support\Facades\Cache;
2022-12-23 03:22:01 +01:00
class PdfConfiguration
{
2023-02-23 13:01:18 +01:00
use MakesHash, AppSetup;
2022-12-23 03:22:01 +01:00
public ?Client $client;
2022-12-27 16:44:12 +01:00
public ?ClientContact $contact;
2023-02-23 12:39:20 +01:00
public Country $country;
public Currency $currency;
2022-12-27 16:44:12 +01:00
2023-02-23 12:39:20 +01:00
public Client | Vendor $currency_entity;
public Design $design;
2023-02-25 01:11:09 +01:00
public Invoice | Credit | Quote | PurchaseOrder $entity;
2023-02-23 12:39:20 +01:00
public string $entity_design_id;
2022-12-23 03:22:01 +01:00
public string $entity_string;
2023-02-23 12:39:20 +01:00
2023-02-21 08:39:07 +01:00
public ?string $path;
2023-02-23 12:39:20 +01:00
public array $pdf_variables;
public object $settings;
public $settings_object;
public ?Vendor $vendor;
public ?VendorContact $vendor_contact;
2023-02-23 13:01:18 +01:00
public string $date_format;
public string $locale;
2023-02-25 01:11:09 +01:00
public Collection $tax_map;
public ?array $total_tax_map;
2022-12-28 09:31:43 +01:00
/**
2023-02-23 12:39:20 +01:00
* __construct
2023-02-21 08:39:07 +01:00
*
2023-02-23 12:39:20 +01:00
* @param PdfService $service
* @return void
2022-12-28 09:31:43 +01:00
*/
2023-02-21 08:39:07 +01:00
public function __construct(public PdfService $service)
{
}
2023-02-23 12:39:20 +01:00
/**
* init
*
* @return self
*/
2022-12-28 15:50:11 +01:00
public function init(): self
2022-12-23 03:22:01 +01:00
{
$this->setEntityType()
2023-02-23 13:01:18 +01:00
->setDateFormat()
2022-12-23 10:51:24 +01:00
->setPdfVariables()
2022-12-28 09:31:43 +01:00
->setDesign()
2023-02-23 12:39:20 +01:00
->setCurrencyForPdf()
2022-12-28 15:50:11 +01:00
->setLocale();
2022-12-28 09:31:43 +01:00
return $this;
}
2023-02-23 12:39:20 +01:00
/**
* setLocale
*
* @return self
*/
2022-12-28 15:50:11 +01:00
private function setLocale(): self
{
App::forgetInstance('translator');
$t = app('translator');
App::setLocale($this->settings_object->locale());
$t->replace(Ninja::transformTranslations($this->settings));
2023-02-23 13:01:18 +01:00
$this->locale = $this->settings_object->locale();
2022-12-28 15:50:11 +01:00
return $this;
}
2023-02-23 12:39:20 +01:00
/**
* setCurrency
*
* @return self
*/
private function setCurrencyForPdf(): self
2022-12-28 09:31:43 +01:00
{
$this->currency = $this->client ? $this->client->currency() : $this->vendor->currency();
$this->currency_entity = $this->client ? $this->client : $this->vendor;
2022-12-23 03:22:01 +01:00
return $this;
}
2023-02-23 12:39:20 +01:00
/**
* setPdfVariables
*
* @return self
*/
2023-02-25 02:08:34 +01:00
public function setPdfVariables() :self
2022-12-23 10:51:24 +01:00
{
$default = (array) CompanySettings::getEntityVariableDefaults();
2022-12-28 15:50:11 +01:00
2023-02-23 12:39:20 +01:00
// $variables = (array)$this->service->company->settings->pdf_variables;
$variables = (array)$this->settings->pdf_variables;
2022-12-23 10:51:24 +01:00
foreach ($default as $property => $value) {
if (array_key_exists($property, $variables)) {
continue;
}
$variables[$property] = $value;
}
$this->pdf_variables = $variables;
return $this;
}
2023-02-23 12:39:20 +01:00
/**
* setEntityType
*
* @return self
*/
private function setEntityType(): self
2022-12-23 03:22:01 +01:00
{
$entity_design_id = '';
2022-12-23 03:22:01 +01:00
if ($this->service->invitation instanceof InvoiceInvitation) {
$this->entity = $this->service->invitation->invoice;
$this->entity_string = 'invoice';
$this->client = $this->entity->client;
2022-12-27 16:44:12 +01:00
$this->contact = $this->service->invitation->contact;
2022-12-23 03:22:01 +01:00
$this->path = $this->client->invoice_filepath($this->service->invitation);
$this->entity_design_id = 'invoice_design_id';
$this->settings = $this->client->getMergedSettings();
$this->settings_object = $this->client;
2023-02-23 13:01:18 +01:00
$this->country = $this->client->country;
} elseif ($this->service->invitation instanceof QuoteInvitation) {
$this->entity = $this->service->invitation->quote;
$this->entity_string = 'quote';
2022-12-23 03:22:01 +01:00
$this->client = $this->entity->client;
2022-12-27 16:44:12 +01:00
$this->contact = $this->service->invitation->contact;
2022-12-23 03:22:01 +01:00
$this->path = $this->client->quote_filepath($this->service->invitation);
$this->entity_design_id = 'quote_design_id';
$this->settings = $this->client->getMergedSettings();
$this->settings_object = $this->client;
2023-02-23 13:01:18 +01:00
$this->country = $this->client->country;
} elseif ($this->service->invitation instanceof CreditInvitation) {
$this->entity = $this->service->invitation->credit;
$this->entity_string = 'credit';
2022-12-23 03:22:01 +01:00
$this->client = $this->entity->client;
2022-12-27 16:44:12 +01:00
$this->contact = $this->service->invitation->contact;
2022-12-23 03:22:01 +01:00
$this->path = $this->client->credit_filepath($this->service->invitation);
$this->entity_design_id = 'credit_design_id';
$this->settings = $this->client->getMergedSettings();
$this->settings_object = $this->client;
2023-02-23 13:01:18 +01:00
$this->country = $this->client->country;
} elseif ($this->service->invitation instanceof RecurringInvoiceInvitation) {
$this->entity = $this->service->invitation->recurring_invoice;
$this->entity_string = 'recurring_invoice';
2022-12-23 03:22:01 +01:00
$this->client = $this->entity->client;
2022-12-27 16:44:12 +01:00
$this->contact = $this->service->invitation->contact;
2022-12-23 03:22:01 +01:00
$this->path = $this->client->recurring_invoice_filepath($this->service->invitation);
$this->entity_design_id = 'invoice_design_id';
$this->settings = $this->client->getMergedSettings();
$this->settings_object = $this->client;
2023-02-23 13:01:18 +01:00
$this->country = $this->client->country;
} elseif ($this->service->invitation instanceof PurchaseOrderInvitation) {
$this->entity = $this->service->invitation->purchase_order;
$this->entity_string = 'purchase_order';
2022-12-23 03:22:01 +01:00
$this->vendor = $this->entity->vendor;
2022-12-27 16:44:12 +01:00
$this->vendor_contact = $this->service->invitation->contact;
2022-12-23 03:22:01 +01:00
$this->path = $this->vendor->purchase_order_filepath($this->service->invitation);
$this->entity_design_id = 'invoice_design_id';
$this->entity_design_id = 'purchase_order_design_id';
$this->settings = $this->vendor->company->settings;
$this->settings_object = $this->vendor;
$this->client = null;
2023-02-23 13:01:18 +01:00
$this->country = $this->vendor->country ?: $this->vendor->company->country();
} else {
throw new \Exception('Unable to resolve entity', 500);
}
2022-12-23 03:22:01 +01:00
2023-02-25 01:11:09 +01:00
$this->setTaxMap($this->entity->calc()->getTaxMap());
$this->setTotalTaxMap($this->entity->calc()->getTotalTaxMap());
2022-12-23 03:22:01 +01:00
$this->path = $this->path.$this->entity->numberFormatter().'.pdf';
return $this;
}
2023-02-23 12:39:20 +01:00
2023-02-25 01:11:09 +01:00
public function setTaxMap($map): self
{
$this->tax_map = $map;
return $this;
}
public function setTotalTaxMap($map): self
{
$this->total_tax_map = $map;
return $this;
}
2023-02-23 12:39:20 +01:00
public function setCurrency(Currency $currency): self
{
$this->currency = $currency;
return $this;
}
2022-12-23 03:22:01 +01:00
2023-02-23 12:39:20 +01:00
public function setCountry(Country $country): self
{
$this->country = $country;
return $this;
}
/**
* setDesign
*
* @return self
*/
private function setDesign(): self
2022-12-23 03:22:01 +01:00
{
$design_id = $this->entity->design_id ? : $this->decodePrimaryKey($this->settings_object->getSetting($this->entity_design_id));
$this->design = Design::find($design_id ?: 2);
return $this;
}
2023-02-23 13:01:18 +01:00
/**
* formatMoney
*
* @param float $value
* @return string
*/
2023-02-23 12:39:20 +01:00
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;
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);
}
}
2023-02-23 13:01:18 +01:00
/**
* 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) {
2023-02-25 06:11:12 +01:00
return $item->id == $this->settings->date_format_id;
})->first()->format;
2023-02-23 13:01:18 +01:00
return $this;
}
2023-02-21 08:39:07 +01:00
}