1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00
invoiceninja/app/Services/Pdf/PdfConfiguration.php

205 lines
6.2 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;
2022-12-23 10:51:24 +01:00
use App\DataMapper\CompanySettings;
2022-12-23 03:22:01 +01:00
use App\Models\Client;
2022-12-27 16:44:12 +01:00
use App\Models\ClientContact;
2022-12-23 03:22:01 +01:00
use App\Models\Credit;
use App\Models\CreditInvitation;
2022-12-28 09:31:43 +01:00
use App\Models\Currency;
2022-12-23 03:22:01 +01:00
use App\Models\Design;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
use App\Models\PurchaseOrder;
use App\Models\PurchaseOrderInvitation;
use App\Models\Quote;
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoice;
use App\Models\RecurringInvoiceInvitation;
use App\Models\Vendor;
2022-12-27 16:44:12 +01:00
use App\Models\VendorContact;
2022-12-23 03:22:01 +01:00
use App\Services\Pdf\PdfService;
2022-12-28 15:50:11 +01:00
use App\Utils\Ninja;
2022-12-23 03:22:01 +01:00
use App\Utils\Traits\MakesHash;
2022-12-28 15:50:11 +01:00
use Illuminate\Support\Facades\App;
2022-12-23 03:22:01 +01:00
class PdfConfiguration
{
use MakesHash;
public Design $design;
public ?Client $client;
2022-12-27 16:44:12 +01:00
public ?ClientContact $contact;
2022-12-23 03:22:01 +01:00
public ?Vendor $vendor;
2022-12-27 16:44:12 +01:00
public ?VendorContact $vendor_contact;
2022-12-23 03:22:01 +01:00
public object $settings;
public $settings_object;
public $entity;
public string $entity_string;
2022-12-23 10:51:24 +01:00
public array $pdf_variables;
2022-12-28 09:31:43 +01:00
public Currency $currency;
/**
* The parent object of the currency
2022-12-28 15:50:11 +01:00
*
2022-12-28 09:31:43 +01:00
* @var App\Models\Client | App\Models\Vendor
2022-12-28 15:50:11 +01:00
*
2022-12-28 09:31:43 +01:00
*/
public Client | Vendor $currency_entity;
2022-12-23 03:22:01 +01:00
public function __construct(public PdfService $service){}
2022-12-23 03:22:01 +01:00
2022-12-28 15:50:11 +01:00
public function init(): self
2022-12-23 03:22:01 +01:00
{
$this->setEntityType()
2022-12-23 10:51:24 +01:00
->setPdfVariables()
2022-12-28 09:31:43 +01:00
->setDesign()
2022-12-28 15:50:11 +01:00
->setCurrency()
->setLocale();
2022-12-28 09:31:43 +01:00
return $this;
}
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));
return $this;
}
private function setCurrency(): 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;
}
2022-12-23 10:51:24 +01:00
private function setPdfVariables() :self
{
$default = (array) CompanySettings::getEntityVariableDefaults();
2022-12-28 15:50:11 +01:00
2022-12-28 10:29:51 +01:00
$variables = (array)$this->service->company->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;
}
2022-12-23 03:22:01 +01:00
private function setEntityType()
{
$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;
} 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;
} 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;
} 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;
} 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;
} else {
throw new \Exception('Unable to resolve entity', 500);
}
2022-12-23 03:22:01 +01:00
$this->path = $this->path.$this->entity->numberFormatter().'.pdf';
return $this;
2022-12-23 03:22:01 +01:00
}
private function setDesign()
{
$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;
}
}