mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-07 12:54:31 -04:00
Refactor PDF Service
This commit is contained in:
parent
08b1b8768f
commit
0efa84a070
@ -17,7 +17,6 @@ use App\Utils\Helpers;
|
||||
use App\Utils\Number;
|
||||
use App\Utils\Traits\MakesDates;
|
||||
use DOMDocument;
|
||||
use DOMXPath;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Str;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
@ -245,9 +244,9 @@ class PdfBuilder
|
||||
$element = ['element' => 'tr', 'elements' => []];
|
||||
|
||||
$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' => Number::formatMoney($payment->pivot->amount, $this->service->config->client) ?: ' '];
|
||||
$element['elements'][] = ['element' => 'td', 'content' => $this->service->config->formatMoney($payment->pivot->amount) ?: ' '];
|
||||
|
||||
$tbody[] = $element;
|
||||
|
||||
@ -382,8 +381,8 @@ class PdfBuilder
|
||||
$element = ['element' => 'tr', 'elements' => []];
|
||||
|
||||
$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->due_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->locale) ?: ' '];
|
||||
$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) ?: ' '];
|
||||
|
||||
@ -1104,7 +1103,7 @@ class PdfBuilder
|
||||
*/
|
||||
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 [
|
||||
['element' => 'tr', 'properties' => ['data-ref' => 'statement-label'], 'elements' => [
|
||||
|
@ -25,12 +25,14 @@ use App\Models\CreditInvitation;
|
||||
use App\Models\InvoiceInvitation;
|
||||
use App\DataMapper\CompanySettings;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use App\Models\PurchaseOrderInvitation;
|
||||
use App\Models\RecurringInvoiceInvitation;
|
||||
use App\Utils\Traits\AppSetup;
|
||||
|
||||
class PdfConfiguration
|
||||
{
|
||||
use MakesHash;
|
||||
use MakesHash, AppSetup;
|
||||
|
||||
public ?Client $client;
|
||||
|
||||
@ -62,6 +64,10 @@ class PdfConfiguration
|
||||
|
||||
public ?VendorContact $vendor_contact;
|
||||
|
||||
public string $date_format;
|
||||
|
||||
public string $locale;
|
||||
|
||||
/**
|
||||
* __construct
|
||||
*
|
||||
@ -80,6 +86,7 @@ class PdfConfiguration
|
||||
public function init(): self
|
||||
{
|
||||
$this->setEntityType()
|
||||
->setDateFormat()
|
||||
->setPdfVariables()
|
||||
->setDesign()
|
||||
->setCurrencyForPdf()
|
||||
@ -103,6 +110,8 @@ class PdfConfiguration
|
||||
|
||||
$t->replace(Ninja::transformTranslations($this->settings));
|
||||
|
||||
$this->locale = $this->settings_object->locale();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -163,6 +172,7 @@ class PdfConfiguration
|
||||
$this->entity_design_id = 'invoice_design_id';
|
||||
$this->settings = $this->client->getMergedSettings();
|
||||
$this->settings_object = $this->client;
|
||||
$this->country = $this->client->country;
|
||||
} elseif ($this->service->invitation instanceof QuoteInvitation) {
|
||||
$this->entity = $this->service->invitation->quote;
|
||||
$this->entity_string = 'quote';
|
||||
@ -172,6 +182,7 @@ class PdfConfiguration
|
||||
$this->entity_design_id = 'quote_design_id';
|
||||
$this->settings = $this->client->getMergedSettings();
|
||||
$this->settings_object = $this->client;
|
||||
$this->country = $this->client->country;
|
||||
} elseif ($this->service->invitation instanceof CreditInvitation) {
|
||||
$this->entity = $this->service->invitation->credit;
|
||||
$this->entity_string = 'credit';
|
||||
@ -181,6 +192,7 @@ class PdfConfiguration
|
||||
$this->entity_design_id = 'credit_design_id';
|
||||
$this->settings = $this->client->getMergedSettings();
|
||||
$this->settings_object = $this->client;
|
||||
$this->country = $this->client->country;
|
||||
} elseif ($this->service->invitation instanceof RecurringInvoiceInvitation) {
|
||||
$this->entity = $this->service->invitation->recurring_invoice;
|
||||
$this->entity_string = 'recurring_invoice';
|
||||
@ -190,6 +202,7 @@ class PdfConfiguration
|
||||
$this->entity_design_id = 'invoice_design_id';
|
||||
$this->settings = $this->client->getMergedSettings();
|
||||
$this->settings_object = $this->client;
|
||||
$this->country = $this->client->country;
|
||||
} elseif ($this->service->invitation instanceof PurchaseOrderInvitation) {
|
||||
$this->entity = $this->service->invitation->purchase_order;
|
||||
$this->entity_string = 'purchase_order';
|
||||
@ -201,6 +214,7 @@ class PdfConfiguration
|
||||
$this->settings = $this->vendor->company->settings;
|
||||
$this->settings_object = $this->vendor;
|
||||
$this->client = null;
|
||||
$this->country = $this->vendor->country ?: $this->vendor->company->country();
|
||||
} else {
|
||||
throw new \Exception('Unable to resolve entity', 500);
|
||||
}
|
||||
@ -238,6 +252,12 @@ class PdfConfiguration
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* formatMoney
|
||||
*
|
||||
* @param float $value
|
||||
* @return string
|
||||
*/
|
||||
public function formatMoney($value): string
|
||||
{
|
||||
$value = floatval($value);
|
||||
@ -248,7 +268,6 @@ class PdfConfiguration
|
||||
$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;
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user