1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Services/Template/TemplateService.php

1563 lines
64 KiB
PHP
Raw Normal View History

2023-09-22 08:14:25 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2023-09-22 08:14:25 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Template;
2023-10-03 07:38:36 +02:00
use App\Models\Client;
2023-11-26 08:41:42 +01:00
use App\Models\Company;
2023-09-22 15:31:12 +02:00
use App\Models\Credit;
2023-09-22 08:14:25 +02:00
use App\Models\Design;
use App\Models\Invoice;
2023-09-22 15:31:12 +02:00
use App\Models\Payment;
use App\Models\Project;
use App\Models\PurchaseOrder;
2023-11-26 08:41:42 +01:00
use App\Models\Quote;
2023-11-05 22:50:36 +01:00
use App\Models\RecurringInvoice;
2023-11-26 08:41:42 +01:00
use App\Models\User;
use App\Models\Vendor;
use App\Utils\HostedPDF\NinjaPdf;
use App\Utils\HtmlEngine;
use App\Utils\Number;
2023-09-22 14:08:57 +02:00
use App\Utils\PaymentHtmlEngine;
use App\Utils\Traits\MakesDates;
2023-10-08 10:46:00 +02:00
use App\Utils\Traits\Pdf\PdfMaker;
2023-11-26 08:41:42 +01:00
use App\Utils\VendorHtmlEngine;
2023-11-20 03:53:39 +01:00
use League\CommonMark\CommonMarkConverter;
2023-11-26 08:41:42 +01:00
use Twig\Error\Error;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Extra\Intl\IntlExtension;
use Twig\Sandbox\SecurityError;
2023-09-22 08:14:25 +02:00
class TemplateService
{
2023-11-06 06:22:45 +01:00
use MakesDates;
use PdfMaker;
2023-09-22 08:14:25 +02:00
private \DomDocument $document;
2023-11-13 22:59:42 +01:00
public \Twig\Environment $twig;
2023-09-25 07:56:32 +02:00
2023-09-22 08:14:25 +02:00
private string $compiled_html = '';
2023-09-27 12:36:08 +02:00
private array $data = [];
2023-09-27 13:44:09 +02:00
private array $variables = [];
2023-11-23 06:13:06 +01:00
private array $global_vars = [];
2023-11-28 14:27:04 +01:00
public ?Company $company = null;
2023-09-27 13:32:17 +02:00
2023-11-28 14:27:04 +01:00
private ?Client $client = null;
2023-11-05 22:50:36 +01:00
2023-11-28 14:27:04 +01:00
private ?Vendor $vendor = null;
2023-11-05 22:50:36 +01:00
private Invoice | Quote | Credit | PurchaseOrder | RecurringInvoice $entity;
private Payment $payment;
2023-11-06 02:54:29 +01:00
private CommonMarkConverter $commonmark;
2023-11-28 14:27:04 +01:00
private ?object $settings = null;
2023-09-25 07:56:32 +02:00
public function __construct(public ?Design $template = null)
2023-09-22 08:14:25 +02:00
{
$this->template = $template;
$this->init();
}
2023-11-06 06:22:45 +01:00
2023-09-22 08:14:25 +02:00
/**
* Boot Dom Document
*
* @return self
*/
private function init(): self
{
2023-11-06 02:54:29 +01:00
$this->commonmark = new CommonMarkConverter([
'allow_unsafe_links' => false,
]);
2023-09-22 08:14:25 +02:00
$this->document = new \DOMDocument();
$this->document->validateOnParse = true;
2023-11-14 00:57:02 +01:00
$loader = new \Twig\Loader\FilesystemLoader(storage_path());
$this->twig = new \Twig\Environment($loader, [
2023-10-12 00:17:28 +02:00
'debug' => true,
]);
2024-06-14 09:09:44 +02:00
2023-11-13 22:59:42 +01:00
$string_extension = new \Twig\Extension\StringLoaderExtension();
2023-09-25 07:56:32 +02:00
$this->twig->addExtension($string_extension);
$this->twig->addExtension(new IntlExtension());
2023-11-13 23:59:20 +01:00
$this->twig->addExtension(new \Twig\Extension\DebugExtension());
2023-11-14 00:57:02 +01:00
2024-05-16 02:20:54 +02:00
2023-11-14 00:57:02 +01:00
$function = new \Twig\TwigFunction('img', function ($string, $style = '') {
return '<img src="' . $string . '" style="' . $style . '"></img>';
2023-10-02 04:43:07 +02:00
});
$this->twig->addFunction($function);
2023-11-27 12:20:58 +01:00
$function = new \Twig\TwigFunction('t', function ($string) {
return ctrans("texts.{$string}");
});
$this->twig->addFunction($function);
2023-11-15 04:57:07 +01:00
$filter = new \Twig\TwigFilter('sum', function (?array $array, ?string $column) {
2023-11-26 08:41:42 +01:00
if(!is_array($array)) {
2023-11-15 04:57:07 +01:00
return 0;
2023-11-26 08:41:42 +01:00
}
2024-01-14 05:05:00 +01:00
2023-10-13 04:43:59 +02:00
return array_sum(array_column($array, $column));
});
2023-11-06 06:22:45 +01:00
2023-10-13 04:43:59 +02:00
$this->twig->addFilter($filter);
2024-05-16 02:20:54 +02:00
$allowedTags = ['if', 'for', 'set', 'filter'];
2024-06-17 02:14:04 +02:00
$allowedFilters = ['escape', 'e', 'upper', 'lower', 'capitalize', 'filter', 'length', 'merge','format_currency', 'format_number','format_percent_number','map', 'join', 'first', 'date','sum'];
2024-05-16 02:20:54 +02:00
$allowedFunctions = ['range', 'cycle', 'constant', 'date',];
2024-05-16 02:34:38 +02:00
$allowedProperties = ['type_id'];
2024-05-16 02:20:54 +02:00
$allowedMethods = ['img','t'];
2024-05-14 07:13:49 +02:00
2024-05-16 02:20:54 +02:00
$policy = new \Twig\Sandbox\SecurityPolicy($allowedTags, $allowedFilters, $allowedFunctions, $allowedProperties, $allowedMethods);
$this->twig->addExtension(new \Twig\Extension\SandboxExtension($policy, true));
2024-05-14 07:13:49 +02:00
2023-09-22 08:14:25 +02:00
return $this;
}
2023-11-06 06:22:45 +01:00
2023-09-22 08:14:25 +02:00
/**
* Iterate through all of the
2023-11-06 06:22:45 +01:00
* ninja nodes, and field stacks
2023-09-22 08:14:25 +02:00
*
* @param array $data - the payload to be passed into the template
* @return self
*/
2023-09-22 14:35:43 +02:00
public function build(array $data): self
2023-11-22 07:52:02 +01:00
{
2023-09-22 08:14:25 +02:00
$this->compose()
2023-09-27 12:36:08 +02:00
->processData($data)
2023-11-23 06:13:06 +01:00
->setGlobals()
2023-09-27 12:36:08 +02:00
->parseNinjaBlocks()
2023-09-27 13:44:09 +02:00
->processVariables($data)
2023-11-15 03:37:06 +01:00
->parseGlobalStacks()
2023-10-25 10:04:27 +02:00
->parseVariables();
2023-09-22 08:14:25 +02:00
return $this;
}
2023-11-06 06:22:45 +01:00
/**
* Initialized a set of HTMLEngine variables
*
2023-11-10 00:09:27 +01:00
* @param array | \Illuminate\Support\Collection $data
2023-11-06 06:22:45 +01:00
* @return self
*/
2023-09-27 13:44:09 +02:00
private function processVariables($data): self
{
$this->variables = $this->resolveHtmlEngine($data);
return $this;
}
2024-03-25 05:05:30 +01:00
public function setGlobals(): self
2023-11-23 06:13:06 +01:00
{
2024-06-14 09:09:44 +02:00
2023-11-23 06:13:06 +01:00
foreach($this->global_vars as $key => $value) {
$this->twig->addGlobal($key, $value);
}
2024-01-14 05:05:00 +01:00
2023-11-23 06:13:06 +01:00
$this->global_vars = [];
return $this;
}
2023-11-28 14:27:04 +01:00
public function setSettings($settings): self
{
$this->settings = $settings;
return $this;
}
private function getSettings(): object
{
2023-11-30 07:59:17 +01:00
if($this->settings) {
2023-11-28 14:27:04 +01:00
return $this->settings;
2023-11-30 07:59:17 +01:00
}
2023-11-28 14:27:04 +01:00
2023-11-30 07:59:17 +01:00
if($this->client) {
2023-11-28 14:27:04 +01:00
return $this->client->getMergedSettings();
2023-11-30 07:59:17 +01:00
}
2023-11-28 14:27:04 +01:00
return $this->company->settings;
}
2023-11-23 06:13:06 +01:00
public function addGlobal(array $var): self
2023-11-26 08:41:42 +01:00
{
2023-11-23 06:13:06 +01:00
$this->global_vars = array_merge($this->global_vars, $var);
2024-01-14 05:05:00 +01:00
2023-11-23 06:13:06 +01:00
return $this;
}
2023-11-06 06:22:45 +01:00
/**
* Returns a Mock Template
*
* @return self
*/
2023-09-27 12:36:08 +02:00
public function mock(): self
{
2023-09-27 13:32:17 +02:00
$tm = new TemplateMock($this->company);
2023-11-28 14:27:04 +01:00
$tm->setSettings($this->getSettings())->init();
2023-09-27 13:44:09 +02:00
$this->entity = $this->company->invoices()->first() ?? $this->company->quotes()->first();
2023-11-06 05:47:16 +01:00
2023-09-27 13:32:17 +02:00
$this->data = $tm->engines;
2023-09-27 13:44:09 +02:00
$this->variables = $tm->variables[0];
2023-11-23 06:13:06 +01:00
$this->twig->addGlobal('currency_code', $this->company->currency()->code);
$this->twig->addGlobal('show_credits', true);
$this->twig->addGlobal('show_aging', true);
$this->twig->addGlobal('show_payments', true);
2023-09-27 13:32:17 +02:00
$this->parseNinjaBlocks()
2023-11-06 02:54:29 +01:00
->parseGlobalStacks()
2023-09-27 13:44:09 +02:00
->parseVariables();
2023-09-27 12:36:08 +02:00
return $this;
}
2023-11-06 06:22:45 +01:00
2023-10-12 00:17:28 +02:00
/**
* Returns the HTML as string
*
* @return string
*/
2023-09-22 08:14:25 +02:00
public function getHtml(): string
{
return $this->compiled_html;
}
2023-09-27 12:36:08 +02:00
2023-11-05 22:50:36 +01:00
/**
* Returns the PDF string
*
2023-11-06 06:22:45 +01:00
* @return string
2023-11-05 22:50:36 +01:00
*/
2023-11-06 06:22:45 +01:00
public function getPdf(): string
2023-10-04 06:29:31 +02:00
{
2024-06-14 09:09:44 +02:00
2023-10-04 06:29:31 +02:00
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
$pdf = (new NinjaPdf())->build($this->compiled_html);
} else {
$pdf = $this->makePdf(null, null, $this->compiled_html);
}
return $pdf;
}
2023-11-05 22:50:36 +01:00
/**
* Get the parsed data
*
* @return array
*/
public function getData(): array
{
return $this->data;
}
2023-11-06 06:22:45 +01:00
2023-11-05 22:50:36 +01:00
/**
* Process data variables
*
2023-11-10 00:09:27 +01:00
* @param array | \Illuminate\Support\Collection $data
2023-11-05 22:50:36 +01:00
* @return self
*/
public function processData($data): self
2023-09-27 12:36:08 +02:00
{
$this->data = $this->preProcessDataBlocks($data);
2024-05-14 07:13:49 +02:00
2023-09-27 12:36:08 +02:00
return $this;
}
2023-09-22 08:14:25 +02:00
/**
* Parses all Ninja tags in the document
2023-10-25 10:04:27 +02:00
*
2023-09-22 08:14:25 +02:00
* @return self
*/
2023-09-27 12:36:08 +02:00
private function parseNinjaBlocks(): self
2023-09-22 08:14:25 +02:00
{
$replacements = [];
2023-09-25 07:56:32 +02:00
2023-09-22 08:14:25 +02:00
$contents = $this->document->getElementsByTagName('ninja');
foreach ($contents as $content) {
2023-11-06 06:22:45 +01:00
2023-09-22 08:14:25 +02:00
$template = $content->ownerDocument->saveHTML($content);
2023-10-02 02:06:24 +02:00
try {
$template = $this->twig->createTemplate(html_entity_decode($template));
2023-10-26 00:54:58 +02:00
} catch(SyntaxError $e) {
2023-10-02 02:06:24 +02:00
nlog($e->getMessage());
2023-11-14 00:57:02 +01:00
throw ($e);
2023-10-26 00:54:58 +02:00
} catch(RuntimeError $e) {
2023-11-14 00:57:02 +01:00
nlog("runtime = " . $e->getMessage());
throw ($e);
2023-10-26 00:54:58 +02:00
} catch(LoaderError $e) {
2023-10-12 00:17:28 +02:00
nlog("loader = " . $e->getMessage());
2023-11-14 00:57:02 +01:00
throw ($e);
2023-10-26 00:54:58 +02:00
} catch(SecurityError $e) {
2023-10-12 00:17:28 +02:00
nlog("security = " . $e->getMessage());
2023-11-13 22:59:42 +01:00
throw ($e);
2024-06-18 02:25:56 +02:00
} catch(Error $e) {
nlog("error = " . $e->getMessage());
throw ($e);
2023-10-12 00:17:28 +02:00
}
2024-06-18 02:25:56 +02:00
2023-09-27 12:36:08 +02:00
$template = $template->render($this->data);
2023-10-02 04:43:07 +02:00
2023-10-03 07:38:36 +02:00
$f = $this->document->createDocumentFragment();
$f->appendXML(html_entity_decode($template));
2023-11-06 06:22:45 +01:00
2023-09-22 08:14:25 +02:00
$replacements[] = $f;
}
foreach($contents as $key => $content) {
$content->parentNode->replaceChild($replacements[$key], $content);
}
$this->save();
return $this;
}
2023-11-06 06:22:45 +01:00
2023-09-22 08:14:25 +02:00
/**
* Parses all variables in the document
2023-10-25 10:04:27 +02:00
*
2023-09-22 08:14:25 +02:00
* @return self
*/
2023-11-06 05:47:16 +01:00
public function parseVariables(): self
2023-09-22 08:14:25 +02:00
{
2023-09-22 14:35:43 +02:00
$html = $this->getHtml();
2023-09-22 08:14:25 +02:00
2023-09-27 13:44:09 +02:00
foreach($this->variables as $key => $variable) {
2023-10-25 10:04:27 +02:00
if(isset($variable['labels']) && isset($variable['values'])) {
2023-09-25 07:56:32 +02:00
$html = strtr($html, $variable['labels']);
$html = strtr($html, $variable['values']);
}
2023-09-22 14:08:57 +02:00
}
2023-09-22 08:14:25 +02:00
@$this->document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$this->save();
return $this;
}
2023-11-06 06:22:45 +01:00
2023-09-22 08:14:25 +02:00
/**
* Saves the document and updates the compiled string.
*
* @return self
*/
private function save(): self
{
$this->compiled_html = str_replace('%24', '$', $this->document->saveHTML());
return $this;
}
/**
* compose
*
* @return self
*/
2023-11-28 14:27:04 +01:00
public function compose(): self
2023-09-22 08:14:25 +02:00
{
2023-10-25 10:04:27 +02:00
if(!$this->template) {
2023-09-25 07:56:32 +02:00
return $this;
2023-10-25 10:04:27 +02:00
}
2023-09-25 07:56:32 +02:00
2023-09-22 08:14:25 +02:00
$html = '';
$html .= $this->template->design->includes;
$html .= $this->template->design->header;
$html .= $this->template->design->body;
$html .= $this->template->design->footer;
2023-10-01 12:21:57 +02:00
@$this->document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
2023-09-22 08:14:25 +02:00
return $this;
}
2023-11-06 06:22:45 +01:00
2023-09-25 07:56:32 +02:00
/**
2023-10-25 10:04:27 +02:00
* Inject the template components
2023-09-25 07:56:32 +02:00
* manually
*
* @return self
*/
public function setTemplate(array $partials): self
2023-09-27 12:36:08 +02:00
{
2023-09-25 07:56:32 +02:00
$html = '';
$html .= $partials['design']['includes'];
$html .= $partials['design']['header'];
$html .= $partials['design']['body'];
$html .= $partials['design']['footer'];
2023-10-01 12:21:57 +02:00
@$this->document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
2023-09-25 07:56:32 +02:00
return $this;
}
2023-09-22 08:14:25 +02:00
/**
* Resolves the labels and values needed to replace the string
* holders in the template.
*
2023-11-06 06:22:45 +01:00
* @param array $data
2023-09-22 08:14:25 +02:00
* @return array
*/
2023-09-22 14:08:57 +02:00
private function resolveHtmlEngine(array $data): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 14:35:43 +02:00
return collect($data)->map(function ($value, $key) {
2023-11-06 06:22:45 +01:00
2023-09-22 14:35:43 +02:00
$processed = [];
2023-09-22 14:08:57 +02:00
2023-11-14 00:57:02 +01:00
if(in_array($key, ['tasks', 'projects', 'aging']) || !$value->first()) {
2023-09-26 13:21:10 +02:00
return $processed;
2023-10-25 10:04:27 +02:00
}
2023-09-26 13:21:10 +02:00
2023-09-22 14:08:57 +02:00
match ($key) {
2023-10-25 10:04:27 +02:00
'variables' => $processed = $value->first() ?? [],
2023-11-28 14:27:04 +01:00
'invoices' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->setSettings($this->getSettings())->generateLabelsAndValues() ?? [],
'quotes' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->setSettings($this->getSettings())->generateLabelsAndValues() ?? [],
'credits' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->setSettings($this->getSettings())->generateLabelsAndValues() ?? [],
'payments' => $processed = (new PaymentHtmlEngine($value->first(), $value->first()->client->contacts()->first()))->setSettings($this->getSettings())->generateLabelsAndValues() ?? [],
2023-09-22 14:35:43 +02:00
'tasks' => $processed = [],
'projects' => $processed = [],
2023-11-28 14:27:04 +01:00
'purchase_orders' => (new VendorHtmlEngine($value->first()->invitations()->first()))->setSettings($this->getSettings())->generateLabelsAndValues() ?? [],
2023-10-12 00:17:28 +02:00
'aging' => $processed = [],
default => $processed = [],
2023-09-22 14:08:57 +02:00
};
2023-11-06 06:22:45 +01:00
2023-11-28 14:27:04 +01:00
// nlog($key);
// nlog($processed);
2024-01-14 05:05:00 +01:00
2023-09-22 14:08:57 +02:00
return $processed;
})->toArray();
2023-09-22 08:14:25 +02:00
}
2023-11-06 06:22:45 +01:00
/**
* Pre Processes the Data Blocks into
* Twig consumables
*
2023-11-10 00:09:27 +01:00
* @param array | \Illuminate\Support\Collection $data
2023-11-06 06:22:45 +01:00
* @return array
*/
2023-09-22 08:14:25 +02:00
private function preProcessDataBlocks($data): array
{
2023-10-25 10:04:27 +02:00
return collect($data)->map(function ($value, $key) {
2023-09-22 08:14:25 +02:00
2023-09-22 14:35:43 +02:00
$processed = [];
2023-09-22 08:14:25 +02:00
match ($key) {
2023-09-22 14:35:43 +02:00
'invoices' => $processed = $this->processInvoices($value),
'quotes' => $processed = $this->processQuotes($value),
'credits' => $processed = $this->processCredits($value),
'payments' => $processed = $this->processPayments($value),
'tasks' => $processed = $this->processTasks($value),
'projects' => $processed = $this->processProjects($value),
'purchase_orders' => $processed = $this->processPurchaseOrders($value),
2023-10-12 00:17:28 +02:00
'aging' => $processed = $value,
default => $processed = [],
2023-09-22 08:14:25 +02:00
};
return $processed;
})->toArray();
}
2023-11-06 06:22:45 +01:00
/**
* Process Invoices into consumable form for Twig templates
*
2023-11-10 00:09:27 +01:00
* @param array | \Illuminate\Support\Collection $invoices
2023-11-06 06:22:45 +01:00
* @return array
*/
2023-09-27 12:36:08 +02:00
public function processInvoices($invoices): array
{
$invoices = collect($invoices)
2023-10-25 10:04:27 +02:00
->map(function ($invoice) {
2023-10-25 10:04:27 +02:00
$payments = [];
2023-11-14 00:57:02 +01:00
$this->entity = $invoice;
2023-10-25 10:04:27 +02:00
if($invoice->payments ?? false) {
$payments = $invoice->payments->map(function ($payment) {
return $this->transformPayment($payment);
})->toArray();
}
return [
'amount' => Number::formatMoney($invoice->amount, $invoice->client),
'balance' => Number::formatMoney($invoice->balance, $invoice->client),
2023-11-14 00:57:02 +01:00
'status_id' => $invoice->status_id,
'status' => Invoice::stringStatus($invoice->status_id),
2023-11-23 13:54:42 +01:00
'amount_raw' => $invoice->amount ,
2023-10-25 10:04:27 +02:00
'balance_raw' => $invoice->balance,
'number' => $invoice->number ?: '',
'discount' => $invoice->discount,
'po_number' => $invoice->po_number ?: '',
'date' => $this->translateDate($invoice->date, $invoice->client->date_format(), $invoice->client->locale()),
'last_sent_date' => $this->translateDate($invoice->last_sent_date, $invoice->client->date_format(), $invoice->client->locale()),
'next_send_date' => $this->translateDate($invoice->next_send_date, $invoice->client->date_format(), $invoice->client->locale()),
'due_date' => $this->translateDate($invoice->due_date, $invoice->client->date_format(), $invoice->client->locale()),
'terms' => $invoice->terms ?: '',
'public_notes' => $invoice->public_notes ?: '',
'private_notes' => $invoice->private_notes ?: '',
'uses_inclusive_taxes' => (bool) $invoice->uses_inclusive_taxes,
'tax_name1' => $invoice->tax_name1 ?? '',
'tax_rate1' => (float) $invoice->tax_rate1,
'tax_name2' => $invoice->tax_name2 ?? '',
'tax_rate2' => (float) $invoice->tax_rate2,
'tax_name3' => $invoice->tax_name3 ?? '',
'tax_rate3' => (float) $invoice->tax_rate3,
'total_taxes' => Number::formatMoney($invoice->total_taxes, $invoice->client),
'total_taxes_raw' => $invoice->total_taxes,
'is_amount_discount' => (bool) $invoice->is_amount_discount ?? false,
'footer' => $invoice->footer ?? '',
'partial' => $invoice->partial ?? 0,
'partial_due_date' => $this->translateDate($invoice->partial_due_date, $invoice->client->date_format(), $invoice->client->locale()),
'custom_value1' => (string) $invoice->custom_value1 ?: '',
'custom_value2' => (string) $invoice->custom_value2 ?: '',
'custom_value3' => (string) $invoice->custom_value3 ?: '',
'custom_value4' => (string) $invoice->custom_value4 ?: '',
'custom_surcharge1' => (float) $invoice->custom_surcharge1,
'custom_surcharge2' => (float) $invoice->custom_surcharge2,
'custom_surcharge3' => (float) $invoice->custom_surcharge3,
'custom_surcharge4' => (float) $invoice->custom_surcharge4,
'exchange_rate' => (float) $invoice->exchange_rate,
'custom_surcharge_tax1' => (bool) $invoice->custom_surcharge_tax1,
'custom_surcharge_tax2' => (bool) $invoice->custom_surcharge_tax2,
'custom_surcharge_tax3' => (bool) $invoice->custom_surcharge_tax3,
'custom_surcharge_tax4' => (bool) $invoice->custom_surcharge_tax4,
2023-11-14 00:57:02 +01:00
'line_items' => $invoice->line_items ? $this->padLineItems($invoice->line_items, $invoice->client) : (array) [],
2023-10-25 10:04:27 +02:00
'reminder1_sent' => $this->translateDate($invoice->reminder1_sent, $invoice->client->date_format(), $invoice->client->locale()),
'reminder2_sent' => $this->translateDate($invoice->reminder2_sent, $invoice->client->date_format(), $invoice->client->locale()),
'reminder3_sent' => $this->translateDate($invoice->reminder3_sent, $invoice->client->date_format(), $invoice->client->locale()),
'reminder_last_sent' => $this->translateDate($invoice->reminder_last_sent, $invoice->client->date_format(), $invoice->client->locale()),
'paid_to_date' => Number::formatMoney($invoice->paid_to_date, $invoice->client),
'auto_bill_enabled' => (bool) $invoice->auto_bill_enabled,
'client' => [
'name' => $invoice->client->present()->name(),
'balance' => $invoice->client->balance,
'payment_balance' => $invoice->client->payment_balance,
'credit_balance' => $invoice->client->credit_balance,
2023-11-27 12:20:58 +01:00
'vat_number' => $invoice->client->vat_number ?? '',
2024-04-04 02:37:21 +02:00
'currency' => $invoice->client->currency()->code ?? 'USD',
2024-06-14 09:09:44 +02:00
'locale' => substr($invoice->client->locale(), 0, 2),
2023-10-25 10:04:27 +02:00
],
'payments' => $payments,
'total_tax_map' => $invoice->calc()->getTotalTaxMap(),
'line_tax_map' => $invoice->calc()->getTaxMap(),
];
});
return $invoices->toArray();
}
2023-11-06 06:22:45 +01:00
/**
* Pads Line Items with raw and formatted content
*
* @param array $items
* @param Vendor | Client $client_or_vendor
2023-11-06 06:22:45 +01:00
* @return array
*/
public function padLineItems(array $items, Vendor | Client $client_or_vendor): array
2023-10-03 07:38:36 +02:00
{
return collect($items)->map(function ($item) use ($client_or_vendor) {
2023-10-03 07:38:36 +02:00
2023-10-04 06:29:31 +02:00
$item->cost_raw = $item->cost ?? 0;
$item->discount_raw = $item->discount ?? 0;
$item->line_total_raw = $item->line_total ?? 0;
$item->gross_line_total_raw = $item->gross_line_total ?? 0;
$item->tax_amount_raw = $item->tax_amount ?? 0;
$item->product_cost_raw = $item->product_cost ?? 0;
2023-10-03 07:38:36 +02:00
$item->cost = Number::formatMoney($item->cost_raw, $client_or_vendor);
2023-11-06 06:22:45 +01:00
2023-11-05 22:50:36 +01:00
if($item->is_amount_discount) {
$item->discount = Number::formatMoney($item->discount_raw, $client_or_vendor);
2023-11-05 22:50:36 +01:00
}
2023-11-06 06:22:45 +01:00
$item->line_total = Number::formatMoney($item->line_total_raw, $client_or_vendor);
$item->gross_line_total = Number::formatMoney($item->gross_line_total_raw, $client_or_vendor);
$item->tax_amount = Number::formatMoney($item->tax_amount_raw, $client_or_vendor);
$item->product_cost = Number::formatMoney($item->product_cost_raw, $client_or_vendor);
2023-10-03 07:38:36 +02:00
return (array)$item;
2023-10-03 07:38:36 +02:00
})->toArray();
}
2023-11-06 06:22:45 +01:00
/**
* Transforms a Payment into consumable for twig
*
* @param Payment $payment
* @return array
*/
2023-10-01 12:21:57 +02:00
private function transformPayment(Payment $payment): array
{
2023-11-05 22:50:36 +01:00
$this->payment = $payment;
$credits = $payment->credits->map(function ($credit) use ($payment) {
return [
'credit' => $credit->number,
'amount_raw' => $credit->pivot->amount,
'refunded_raw' => $credit->pivot->refunded,
'net_raw' => $credit->pivot->amount - $credit->pivot->refunded,
'amount' => Number::formatMoney($credit->pivot->amount, $payment->client),
'refunded' => Number::formatMoney($credit->pivot->refunded, $payment->client),
'net' => Number::formatMoney($credit->pivot->amount - $credit->pivot->refunded, $payment->client),
'is_credit' => true,
2023-10-08 07:06:37 +02:00
'date' => $this->translateDate($credit->date, $payment->client->date_format(), $payment->client->locale()),
'created_at' => $this->translateDate($credit->pivot->created_at, $payment->client->date_format(), $payment->client->locale()),
'updated_at' => $this->translateDate($credit->pivot->updated_at, $payment->client->date_format(), $payment->client->locale()),
'timestamp' => $credit->pivot->created_at->timestamp,
];
});
$pivot = $payment->invoices->map(function ($invoice) use ($payment) {
return [
'invoice' => $invoice->number,
'amount_raw' => $invoice->pivot->amount,
'refunded_raw' => $invoice->pivot->refunded,
'net_raw' => $invoice->pivot->amount - $invoice->pivot->refunded,
'amount' => Number::formatMoney($invoice->pivot->amount, $payment->client),
'refunded' => Number::formatMoney($invoice->pivot->refunded, $payment->client),
'net' => Number::formatMoney($invoice->pivot->amount - $invoice->pivot->refunded, $payment->client),
'is_credit' => false,
2023-10-08 07:06:37 +02:00
'date' => $this->translateDate($invoice->date, $payment->client->date_format(), $payment->client->locale()),
'created_at' => $this->translateDate($invoice->pivot->created_at, $payment->client->date_format(), $payment->client->locale()),
'updated_at' => $this->translateDate($invoice->pivot->updated_at, $payment->client->date_format(), $payment->client->locale()),
'timestamp' => $invoice->pivot->created_at->timestamp,
];
})->merge($credits)->sortBy('timestamp')->toArray();
return [
'status' => $payment->stringStatus($payment->status_id),
2024-01-26 00:20:28 +01:00
'badge' => $payment->badgeForStatus(),
'amount' => Number::formatMoney($payment->amount, $payment->client),
'applied' => Number::formatMoney($payment->applied, $payment->client),
'balance' => Number::formatMoney(($payment->amount - $payment->refunded - $payment->applied), $payment->client),
'refunded' => Number::formatMoney($payment->refunded, $payment->client),
'amount_raw' => $payment->amount,
'applied_raw' => $payment->applied,
'refunded_raw' => $payment->refunded,
2024-04-04 02:32:31 +02:00
'balance_raw' => ($payment->amount - $payment->applied),
'date' => $this->translateDate($payment->date, $payment->client->date_format(), $payment->client->locale()),
'method' => $payment->translatedType(),
2023-11-16 05:57:24 +01:00
'currency' => $payment->currency->code ?? $this->company->currency()->code,
'exchange_rate' => $payment->exchange_rate,
'transaction_reference' => $payment->transaction_reference,
'is_manual' => $payment->is_manual,
'number' => $payment->number,
'custom_value1' => $payment->custom_value1 ?? '',
'custom_value2' => $payment->custom_value2 ?? '',
'custom_value3' => $payment->custom_value3 ?? '',
'custom_value4' => $payment->custom_value4 ?? '',
'created_at' => $this->translateDate($payment->created_at, $payment->client->date_format(), $payment->client->locale()),
'updated_at' => $this->translateDate($payment->updated_at, $payment->client->date_format(), $payment->client->locale()),
'client' => [
'name' => $payment->client->present()->name(),
'balance' => $payment->client->balance,
'payment_balance' => $payment->client->payment_balance,
'credit_balance' => $payment->client->credit_balance,
2023-11-27 12:20:58 +01:00
'vat_number' => $payment->client->vat_number ?? '',
2024-04-04 02:37:21 +02:00
'currency' => $payment->client->currency()->code ?? 'USD',
],
'paymentables' => $pivot,
2023-10-08 10:46:00 +02:00
'refund_activity' => $this->getPaymentRefundActivity($payment),
];
2024-01-14 05:05:00 +01:00
2023-10-08 10:46:00 +02:00
}
/**
* [
"id" => 12,
"date" => "2023-10-08",
"invoices" => [
[
"amount" => 1,
"invoice_id" => 23,
"id" => null,
],
],
"q" => "/api/v1/payments/refund",
"email_receipt" => "true",
"gateway_refund" => false,
"send_email" => false,
],
*
* @param Payment $payment
* @return array
*/
2023-10-08 10:46:00 +02:00
private function getPaymentRefundActivity(Payment $payment): array
{
2024-06-14 09:09:44 +02:00
if(!is_array($payment->refund_meta)) {
2024-02-17 07:44:16 +01:00
return [];
2024-06-14 09:09:44 +02:00
}
2024-02-17 07:44:16 +01:00
return collect($payment->refund_meta)
2023-10-25 10:04:27 +02:00
->map(function ($refund) use ($payment) {
2023-10-08 10:46:00 +02:00
2024-03-05 01:27:53 +01:00
$date = \Carbon\Carbon::parse($refund['date'] ?? $payment->date)->addSeconds($payment->client->timezone_offset());
$date = $this->translateDate($date, $payment->client->date_format(), $payment->client->locale());
$entity = ctrans('texts.invoice');
$map = [];
foreach($refund['invoices'] as $refunded_invoice) {
2023-10-25 10:04:27 +02:00
$invoice = Invoice::withTrashed()->find($refunded_invoice['invoice_id']);
$amount = Number::formatMoney($refunded_invoice['amount'], $payment->client);
2023-10-08 10:46:00 +02:00
$notes = ctrans('texts.status_partially_refunded_amount', ['amount' => $amount]);
array_push($map, "{$date} {$entity} #{$invoice->number} {$notes}\n");
}
2023-10-08 10:46:00 +02:00
return $map;
2023-10-08 10:46:00 +02:00
})->flatten()->toArray();
}
2023-11-06 06:22:45 +01:00
/**
2023-11-13 22:59:42 +01:00
*
2023-11-06 06:22:45 +01:00
*
* @param array | \Illuminate\Support\Collection $quotes
2023-11-06 06:22:45 +01:00
* @return array
*/
2023-09-27 13:11:47 +02:00
public function processQuotes($quotes): array
2023-09-22 08:14:25 +02:00
{
2024-01-14 05:05:00 +01:00
2023-11-13 22:59:42 +01:00
return collect($quotes)->map(function ($quote) {
2023-09-25 07:56:32 +02:00
return [
'amount' => Number::formatMoney($quote->amount, $quote->client),
'balance' => Number::formatMoney($quote->balance, $quote->client),
'balance_raw' => (float) $quote->balance,
'client' => [
'name' => $quote->client->present()->name(),
'balance' => $quote->client->balance,
'payment_balance' => $quote->client->payment_balance,
'credit_balance' => $quote->client->credit_balance,
2023-11-27 12:20:58 +01:00
'vat_number' => $quote->client->vat_number ?? '',
2024-04-04 02:37:21 +02:00
'currency' => $quote->client->currency()->code ?? 'USD',
],
2024-01-14 05:05:00 +01:00
'status_id' => $quote->status_id,
'status' => Quote::stringStatus($quote->status_id),
'number' => $quote->number ?: '',
'discount' => (float) $quote->discount,
'po_number' => $quote->po_number ?: '',
'date' => $quote->date ? $this->translateDate($quote->date, $quote->client->date_format(), $quote->client->locale()) : '',
'last_sent_date' => $quote->last_sent_date ? $this->translateDate($quote->last_sent_date, $quote->client->date_format(), $quote->client->locale()) : '',
// 'next_send_date' => $quote->next_send_date ?: '',
// 'reminder1_sent' => $quote->reminder1_sent ?: '',
// 'reminder2_sent' => $quote->reminder2_sent ?: '',
// 'reminder3_sent' => $quote->reminder3_sent ?: '',
// 'reminder_last_sent' => $quote->reminder_last_sent ?: '',
'due_date' => $quote->due_date ? $this->translateDate($quote->due_date, $quote->client->date_format(), $quote->client->locale()) : '',
'terms' => $quote->terms ?: '',
'public_notes' => $quote->public_notes ?: '',
'private_notes' => $quote->private_notes ?: '',
'is_deleted' => (bool) $quote->is_deleted,
'uses_inclusive_taxes' => (bool) $quote->uses_inclusive_taxes,
'tax_name1' => $quote->tax_name1 ? $quote->tax_name1 : '',
'tax_rate1' => (float) $quote->tax_rate1,
'tax_name2' => $quote->tax_name2 ? $quote->tax_name2 : '',
'tax_rate2' => (float) $quote->tax_rate2,
'tax_name3' => $quote->tax_name3 ? $quote->tax_name3 : '',
'tax_rate3' => (float) $quote->tax_rate3,
'total_taxes' => (float) $quote->total_taxes,
'is_amount_discount' => (bool) ($quote->is_amount_discount ?: false),
'footer' => $quote->footer ?: '',
'partial' => (float) ($quote->partial ?: 0.0),
'partial_due_date' => $quote->partial_due_date ? $this->translateDate($quote->partial_due_date, $quote->client->date_format(), $quote->client->locale()) : '',
'custom_value1' => (string) $quote->custom_value1 ?: '',
'custom_value2' => (string) $quote->custom_value2 ?: '',
'custom_value3' => (string) $quote->custom_value3 ?: '',
'custom_value4' => (string) $quote->custom_value4 ?: '',
'has_expenses' => (bool) $quote->has_expenses,
'custom_surcharge1' => (float) $quote->custom_surcharge1,
'custom_surcharge2' => (float) $quote->custom_surcharge2,
'custom_surcharge3' => (float) $quote->custom_surcharge3,
'custom_surcharge4' => (float) $quote->custom_surcharge4,
'custom_surcharge_tax1' => (bool) $quote->custom_surcharge_tax1,
'custom_surcharge_tax2' => (bool) $quote->custom_surcharge_tax2,
'custom_surcharge_tax3' => (bool) $quote->custom_surcharge_tax3,
'custom_surcharge_tax4' => (bool) $quote->custom_surcharge_tax4,
'line_items' => $quote->line_items ? $this->padLineItems($quote->line_items, $quote->client) : (array) [],
'exchange_rate' => (float) $quote->exchange_rate,
'paid_to_date' => (float) $quote->paid_to_date,
];
2023-09-25 07:56:32 +02:00
})->toArray();
2023-09-25 07:56:32 +02:00
2023-09-22 08:14:25 +02:00
}
2023-11-06 06:22:45 +01:00
2023-09-27 10:10:22 +02:00
/**
* Pushes credits through the appropriate transformer
* and builds any required relationships
*
2023-11-10 00:09:27 +01:00
* @param array | \Illuminate\Support\Collection $credits
2023-09-27 10:10:22 +02:00
* @return array
*/
2023-09-27 13:11:47 +02:00
public function processCredits($credits): array
2023-09-22 08:14:25 +02:00
{
2023-10-12 03:13:08 +02:00
$credits = collect($credits)
2023-10-25 10:04:27 +02:00
->map(function ($credit) {
2024-05-12 12:11:01 +02:00
$payments = [];
2023-11-13 22:59:42 +01:00
$this->entity = $credit;
2024-05-12 12:11:01 +02:00
if($credit->payments ?? false) {
$payments = $credit->payments->map(function ($payment) {
return $this->transformPayment($payment);
})->toArray();
}
2023-10-25 10:04:27 +02:00
return [
'amount' => Number::formatMoney($credit->amount, $credit->client),
'balance' => Number::formatMoney($credit->balance, $credit->client),
'balance_raw' => $credit->balance,
'number' => $credit->number ?: '',
'discount' => $credit->discount,
'po_number' => $credit->po_number ?: '',
'date' => $this->translateDate($credit->date, $credit->client->date_format(), $credit->client->locale()),
'last_sent_date' => $this->translateDate($credit->last_sent_date, $credit->client->date_format(), $credit->client->locale()),
'next_send_date' => $this->translateDate($credit->next_send_date, $credit->client->date_format(), $credit->client->locale()),
'due_date' => $this->translateDate($credit->due_date, $credit->client->date_format(), $credit->client->locale()),
'terms' => $credit->terms ?: '',
'public_notes' => $credit->public_notes ?: '',
'private_notes' => $credit->private_notes ?: '',
'uses_inclusive_taxes' => (bool) $credit->uses_inclusive_taxes,
'tax_name1' => $credit->tax_name1 ?? '',
'tax_rate1' => (float) $credit->tax_rate1,
'tax_name2' => $credit->tax_name2 ?? '',
'tax_rate2' => (float) $credit->tax_rate2,
'tax_name3' => $credit->tax_name3 ?? '',
'tax_rate3' => (float) $credit->tax_rate3,
'total_taxes' => Number::formatMoney($credit->total_taxes, $credit->client),
'total_taxes_raw' => $credit->total_taxes,
'is_amount_discount' => (bool) $credit->is_amount_discount ?? false,
'footer' => $credit->footer ?? '',
'partial' => $credit->partial ?? 0,
'partial_due_date' => $this->translateDate($credit->partial_due_date, $credit->client->date_format(), $credit->client->locale()),
'custom_value1' => (string) $credit->custom_value1 ?: '',
'custom_value2' => (string) $credit->custom_value2 ?: '',
'custom_value3' => (string) $credit->custom_value3 ?: '',
'custom_value4' => (string) $credit->custom_value4 ?: '',
'custom_surcharge1' => (float) $credit->custom_surcharge1,
'custom_surcharge2' => (float) $credit->custom_surcharge2,
'custom_surcharge3' => (float) $credit->custom_surcharge3,
'custom_surcharge4' => (float) $credit->custom_surcharge4,
'exchange_rate' => (float) $credit->exchange_rate,
'custom_surcharge_tax1' => (bool) $credit->custom_surcharge_tax1,
'custom_surcharge_tax2' => (bool) $credit->custom_surcharge_tax2,
'custom_surcharge_tax3' => (bool) $credit->custom_surcharge_tax3,
'custom_surcharge_tax4' => (bool) $credit->custom_surcharge_tax4,
2023-11-14 00:57:02 +01:00
'line_items' => $credit->line_items ? $this->padLineItems($credit->line_items, $credit->client) : (array) [],
2023-10-25 10:04:27 +02:00
'reminder1_sent' => $this->translateDate($credit->reminder1_sent, $credit->client->date_format(), $credit->client->locale()),
'reminder2_sent' => $this->translateDate($credit->reminder2_sent, $credit->client->date_format(), $credit->client->locale()),
'reminder3_sent' => $this->translateDate($credit->reminder3_sent, $credit->client->date_format(), $credit->client->locale()),
'reminder_last_sent' => $this->translateDate($credit->reminder_last_sent, $credit->client->date_format(), $credit->client->locale()),
'paid_to_date' => Number::formatMoney($credit->paid_to_date, $credit->client),
'auto_bill_enabled' => (bool) $credit->auto_bill_enabled,
'client' => [
'name' => $credit->client->present()->name(),
'balance' => $credit->client->balance,
'payment_balance' => $credit->client->payment_balance,
'credit_balance' => $credit->client->credit_balance,
2023-11-27 12:20:58 +01:00
'vat_number' => $credit->client->vat_number ?? '',
2024-04-04 02:37:21 +02:00
'currency' => $credit->client->currency()->code ?? 'USD',
2023-10-25 10:04:27 +02:00
],
2024-05-12 12:11:01 +02:00
'payments' => $payments,
2023-10-25 10:04:27 +02:00
'total_tax_map' => $credit->calc()->getTotalTaxMap(),
'line_tax_map' => $credit->calc()->getTaxMap(),
];
});
2023-09-25 07:56:32 +02:00
2023-10-12 03:13:08 +02:00
return $credits->toArray();
}
2023-09-25 07:56:32 +02:00
2023-09-27 10:10:22 +02:00
/**
* Pushes payments through the appropriate transformer
*
2023-11-10 00:09:27 +01:00
* @param array | \Illuminate\Support\Collection $payments
2023-09-27 10:10:22 +02:00
* @return array
*/
2023-09-27 13:11:47 +02:00
public function processPayments($payments): array
2023-09-22 08:14:25 +02:00
{
2023-11-23 06:13:06 +01:00
2023-10-12 03:13:08 +02:00
$payments = collect($payments)->map(function ($payment) {
2023-10-01 12:21:57 +02:00
return $this->transformPayment($payment);
})->toArray();
2023-11-23 06:13:06 +01:00
2023-11-06 06:22:45 +01:00
return $payments;
2023-09-22 15:31:12 +02:00
2023-09-22 08:14:25 +02:00
}
2023-11-06 06:22:45 +01:00
/**
* @todo refactor
*
2023-11-16 10:02:05 +01:00
* @param \App\Models\Task[] $tasks
2023-11-06 06:22:45 +01:00
* @return array
*/
2023-11-09 07:59:30 +01:00
public function processTasks($tasks, bool $nested = false): array
2023-09-22 08:14:25 +02:00
{
2023-09-25 07:56:32 +02:00
2023-11-13 22:59:42 +01:00
return collect($tasks)->map(function ($task) use ($nested) {
2023-11-16 10:51:20 +01:00
/** @var \App\Models\Task $task */
2023-11-09 07:59:30 +01:00
return [
'number' => (string) $task->number ?: '',
'description' => (string) $task->description ?: '',
2023-11-19 23:04:11 +01:00
'duration' => $task->calcDuration() ?: 0,
2023-11-13 22:59:42 +01:00
'rate' => Number::formatMoney($task->rate ?? 0, $task->client ?? $task->company),
2023-11-16 10:51:20 +01:00
'rate_raw' => $task->rate ?? 0,
2023-11-09 07:59:30 +01:00
'created_at' => $this->translateDate($task->created_at, $task->client ? $task->client->date_format() : $task->company->date_format(), $task->client ? $task->client->locale() : $task->company->locale()),
'updated_at' => $this->translateDate($task->updated_at, $task->client ? $task->client->date_format() : $task->company->date_format(), $task->client ? $task->client->locale() : $task->company->locale()),
2023-11-13 22:59:42 +01:00
'date' => $task->calculated_start_date ? $this->translateDate($task->calculated_start_date, $task->client ? $task->client->date_format() : $task->company->date_format(), $task->client ? $task->client->locale() : $task->company->locale()) : '',
2023-11-09 07:59:30 +01:00
// 'invoice_id' => $this->encodePrimaryKey($task->invoice_id) ?: '',
'project' => ($task->project && !$nested) ? $this->transformProject($task->project, true) : [],
2023-11-16 10:51:20 +01:00
'time_log' => $task->processLogsExpandedNotation(),
2023-11-09 07:59:30 +01:00
'custom_value1' => $task->custom_value1 ?: '',
'custom_value2' => $task->custom_value2 ?: '',
'custom_value3' => $task->custom_value3 ?: '',
'custom_value4' => $task->custom_value4 ?: '',
'status' => $task->status ? $task->status->name : '',
2023-11-20 03:53:39 +01:00
'user' => $this->userInfo($task->user),
2023-11-09 07:59:30 +01:00
'client' => $task->client ? [
'name' => $task->client->present()->name(),
'balance' => $task->client->balance,
'payment_balance' => $task->client->payment_balance,
'credit_balance' => $task->client->credit_balance,
2023-11-27 12:20:58 +01:00
'vat_number' => $task->client->vat_number ?? '',
2024-04-04 02:37:21 +02:00
'currency' => $task->client->currency()->code ?? 'USD',
2023-11-09 07:59:30 +01:00
] : [],
];
2023-09-25 07:56:32 +02:00
2023-11-09 07:59:30 +01:00
})->toArray();
2023-09-22 15:31:12 +02:00
2023-09-22 08:14:25 +02:00
}
2023-11-06 06:22:45 +01:00
/**
* @todo refactor
*
2023-11-10 00:09:27 +01:00
* @param array | \Illuminate\Support\Collection $projects
2023-11-06 06:22:45 +01:00
* @return array
*/
2023-09-27 13:11:47 +02:00
public function processProjects($projects): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 14:08:57 +02:00
2023-11-09 07:59:30 +01:00
return
2023-11-13 22:59:42 +01:00
collect($projects)->map(function ($project) {
2023-11-09 07:59:30 +01:00
return $this->transformProject($project);
})->toArray();
2023-09-22 15:31:12 +02:00
2023-09-22 08:14:25 +02:00
}
2023-11-20 03:53:39 +01:00
private function userInfo(User $user): array
{
return [
'name' => $user->present()->name(),
'email' => $user->email,
];
}
2023-11-09 07:59:30 +01:00
private function transformProject(Project $project, bool $nested = false): array
2023-09-22 08:14:25 +02:00
{
2024-01-14 05:05:00 +01:00
2023-11-09 07:59:30 +01:00
return [
'name' => $project->name ?: '',
'number' => $project->number ?: '',
'created_at' => $this->translateDate($project->created_at, $project->client->date_format(), $project->client->locale()),
'updated_at' => $this->translateDate($project->updated_at, $project->client->date_format(), $project->client->locale()),
'task_rate' => Number::formatMoney($project->task_rate ?? 0, $project->client),
2023-11-16 10:51:20 +01:00
'task_rate_raw' => $project->task_rate ?? 0,
2023-11-09 07:59:30 +01:00
'due_date' => $project->due_date ? $this->translateDate($project->due_date, $project->client->date_format(), $project->client->locale()) : '',
'private_notes' => (string) $project->private_notes ?: '',
'public_notes' => (string) $project->public_notes ?: '',
'budgeted_hours' => (float) $project->budgeted_hours,
'custom_value1' => (string) $project->custom_value1 ?: '',
'custom_value2' => (string) $project->custom_value2 ?: '',
'custom_value3' => (string) $project->custom_value3 ?: '',
'custom_value4' => (string) $project->custom_value4 ?: '',
'color' => (string) $project->color ?: '',
'current_hours' => (int) $project->current_hours ?: 0,
2024-01-14 05:05:00 +01:00
'tasks' => ($project->tasks && !$nested) ? $this->processTasks($project->tasks, true) : [],
2023-11-09 07:59:30 +01:00
'client' => $project->client ? [
'name' => $project->client->present()->name(),
'balance' => $project->client->balance,
'payment_balance' => $project->client->payment_balance,
'credit_balance' => $project->client->credit_balance,
2023-11-27 12:20:58 +01:00
'vat_number' => $project->client->vat_number ?? '',
2024-04-04 02:37:21 +02:00
'currency' => $project->client->currency()->code ?? 'USD',
2023-11-09 07:59:30 +01:00
] : [],
2023-11-20 03:53:39 +01:00
'user' => $this->userInfo($project->user)
2023-11-09 07:59:30 +01:00
];
2023-09-22 08:14:25 +02:00
}
2023-09-27 13:32:17 +02:00
2023-11-06 06:22:45 +01:00
/**
2023-11-13 22:59:42 +01:00
*
* @param array | \Illuminate\Support\Collection $purchase_orders
2023-11-06 06:22:45 +01:00
* @return array
*/
2023-09-27 13:11:47 +02:00
public function processPurchaseOrders($purchase_orders): array
2023-09-22 08:14:25 +02:00
{
2023-11-06 06:22:45 +01:00
2023-11-13 22:59:42 +01:00
return collect($purchase_orders)->map(function ($purchase_order) {
return [
'vendor' => $purchase_order->vendor ? [
'name' => $purchase_order->vendor->present()->name(),
2023-11-27 12:20:58 +01:00
'vat_number' => $purchase_order->vendor->vat_number ?? '',
2024-04-04 02:37:21 +02:00
'currency' => $purchase_order->vendor->currency()->code ?? 'USD',
] : [],
'amount' => (float)$purchase_order->amount,
'balance' => (float)$purchase_order->balance,
'client' => $purchase_order->client ? [
'name' => $purchase_order->client->present()->name(),
'balance' => $purchase_order->client->balance,
'payment_balance' => $purchase_order->client->payment_balance,
'credit_balance' => $purchase_order->client->credit_balance,
2023-11-27 12:20:58 +01:00
'vat_number' => $purchase_order->client->vat_number ?? '',
2024-03-11 09:37:53 +01:00
'address' => $purchase_order->client->present()->address(),
'shipping_address' => $purchase_order->client->present()->shipping_address(),
2024-04-04 02:37:21 +02:00
'currency' => $purchase_order->client->currency()->code ?? 'USD',
] : [],
'status_id' => (string)($purchase_order->status_id ?: 1),
'status' => PurchaseOrder::stringStatus($purchase_order->status_id ?? 1),
'is_deleted' => (bool)$purchase_order->is_deleted,
'number' => $purchase_order->number ?: '',
'discount' => (float)$purchase_order->discount,
'po_number' => $purchase_order->po_number ?: '',
'date' => $purchase_order->date ? $this->translateDate($purchase_order->date, $purchase_order->vendor->date_format(), $purchase_order->vendor->locale()) : '',
'last_sent_date' => $purchase_order->last_sent_date ? $this->translateDate($purchase_order->last_sent_date, $purchase_order->vendor->date_format(), $purchase_order->vendor->locale()) : '',
'next_send_date' => $purchase_order->next_send_date ? $this->translateDate($purchase_order->next_send_date, $purchase_order->vendor->date_format(), $purchase_order->vendor->locale()) : '',
'reminder1_sent' => $purchase_order->reminder1_sent ? $this->translateDate($purchase_order->reminder1_sent, $purchase_order->vendor->date_format(), $purchase_order->vendor->locale()) : '',
'reminder2_sent' => $purchase_order->reminder2_sent ? $this->translateDate($purchase_order->reminder2_sent, $purchase_order->vendor->date_format(), $purchase_order->vendor->locale()) : '',
'reminder3_sent' => $purchase_order->reminder3_sent ? $this->translateDate($purchase_order->reminder3_sent, $purchase_order->vendor->date_format(), $purchase_order->vendor->locale()) : '',
'reminder_last_sent' => $purchase_order->reminder_last_sent ? $this->translateDate($purchase_order->reminder_last_sent, $purchase_order->vendor->date_format(), $purchase_order->vendor->locale()) : '',
'due_date' => $purchase_order->due_date ? $this->translateDate($purchase_order->due_date, $purchase_order->vendor->date_format(), $purchase_order->vendor->locale()) : '',
'terms' => $purchase_order->terms ?: '',
'public_notes' => $purchase_order->public_notes ?: '',
'private_notes' => $purchase_order->private_notes ?: '',
'uses_inclusive_taxes' => (bool)$purchase_order->uses_inclusive_taxes,
'tax_name1' => $purchase_order->tax_name1 ? $purchase_order->tax_name1 : '',
'tax_rate1' => (float)$purchase_order->tax_rate1,
'tax_name2' => $purchase_order->tax_name2 ? $purchase_order->tax_name2 : '',
'tax_rate2' => (float)$purchase_order->tax_rate2,
'tax_name3' => $purchase_order->tax_name3 ? $purchase_order->tax_name3 : '',
'tax_rate3' => (float)$purchase_order->tax_rate3,
'total_taxes' => (float)$purchase_order->total_taxes,
'is_amount_discount' => (bool)($purchase_order->is_amount_discount ?: false),
'footer' => $purchase_order->footer ?: '',
'partial' => (float)($purchase_order->partial ?: 0.0),
2024-01-14 05:05:00 +01:00
'partial_due_date' => $purchase_order->partial_due_date ? $this->translateDate($purchase_order->partial_due_date, $purchase_order->vendor->date_format(), $purchase_order->vendor->locale()) : '',
'custom_value1' => (string)$purchase_order->custom_value1 ?: '',
'custom_value2' => (string)$purchase_order->custom_value2 ?: '',
'custom_value3' => (string)$purchase_order->custom_value3 ?: '',
'custom_value4' => (string)$purchase_order->custom_value4 ?: '',
'has_tasks' => (bool)$purchase_order->has_tasks,
'has_expenses' => (bool)$purchase_order->has_expenses,
'custom_surcharge1' => (float)$purchase_order->custom_surcharge1,
'custom_surcharge2' => (float)$purchase_order->custom_surcharge2,
'custom_surcharge3' => (float)$purchase_order->custom_surcharge3,
'custom_surcharge4' => (float)$purchase_order->custom_surcharge4,
'custom_surcharge_tax1' => (bool)$purchase_order->custom_surcharge_tax1,
'custom_surcharge_tax2' => (bool)$purchase_order->custom_surcharge_tax2,
'custom_surcharge_tax3' => (bool)$purchase_order->custom_surcharge_tax3,
'custom_surcharge_tax4' => (bool)$purchase_order->custom_surcharge_tax4,
2024-01-14 05:05:00 +01:00
'line_items' => $purchase_order->line_items ? $this->padLineItems($purchase_order->line_items, $purchase_order->vendor) : (array)[],
'exchange_rate' => (float)$purchase_order->exchange_rate,
'currency_id' => $purchase_order->currency_id ? (string) $purchase_order->currency_id : '',
];
})->toArray();
2023-09-22 08:14:25 +02:00
}
2023-09-27 13:32:17 +02:00
2023-11-06 06:22:45 +01:00
/**
* Set Company
*
2023-11-10 00:09:27 +01:00
* @param Company $company
2023-11-06 06:22:45 +01:00
* @return self
*/
2023-09-27 13:32:17 +02:00
public function setCompany(Company $company): self
{
$this->company = $company;
2023-11-06 06:22:45 +01:00
2023-09-27 13:32:17 +02:00
return $this;
}
2023-11-06 06:22:45 +01:00
/**
* Get Company
*
* @return Company
*/
2023-09-27 13:32:17 +02:00
public function getCompany(): Company
{
return $this->company;
}
2023-10-12 03:13:08 +02:00
2023-11-06 06:22:45 +01:00
/**
* Setter that allows external variables to override the
* resolved ones from this class
*
* @param mixed $variables
* @return self
*/
2023-10-12 03:13:08 +02:00
public function overrideVariables($variables): self
{
$this->variables = $variables;
2023-11-06 06:22:45 +01:00
2023-10-12 03:13:08 +02:00
return $this;
}
2023-11-06 06:22:45 +01:00
2023-11-05 22:50:36 +01:00
/**
2023-11-06 06:22:45 +01:00
* Parses and finds any field stacks to inject into the DOM Document
2023-11-05 22:50:36 +01:00
*
* @return self
*/
2023-11-06 02:54:29 +01:00
public function parseGlobalStacks(): self
2023-11-05 22:50:36 +01:00
{
$stacks = [
'entity-details',
'client-details',
'vendor-details',
'company-details',
'company-address',
'shipping-details',
];
collect($stacks)->filter(function ($stack) {
return $this->document->getElementById($stack) ?? false;
})
2023-11-13 22:59:42 +01:00
->map(function ($stack) {
$node = $this->document->getElementById($stack);
2023-11-13 22:59:42 +01:00
return ['stack' => $stack, 'labels' => $node->getAttribute('labels')];
})
->each(function ($stack) {
2023-11-05 22:50:36 +01:00
$this->parseStack($stack);
});
2023-11-06 05:47:16 +01:00
2023-11-05 22:50:36 +01:00
return $this;
}
2023-11-06 06:22:45 +01:00
2023-11-05 22:50:36 +01:00
/**
* Injects field stacks into Template
*
* @param array $stack
2023-11-05 22:50:36 +01:00
* @return self
*/
private function parseStack(array $stack): self
2023-11-05 22:50:36 +01:00
{
2023-11-06 05:47:16 +01:00
2023-11-06 06:22:45 +01:00
match($stack['stack']) {
2023-11-10 00:09:27 +01:00
'entity-details' => $this->entityDetails(),
2023-11-06 05:54:51 +01:00
'client-details' => $this->clientDetails($stack['labels'] == 'true'),
'vendor-details' => $this->vendorDetails($stack['labels'] == 'true'),
'company-details' => $this->companyDetails($stack['labels'] == 'true'),
'company-address' => $this->companyAddress($stack['labels'] == 'true'),
'shipping-details' => $this->shippingDetails($stack['labels'] == 'true'),
2024-06-17 02:14:04 +02:00
default => $this->entityDetails(),
2023-11-05 22:50:36 +01:00
};
2023-11-06 05:47:16 +01:00
$this->save();
2023-11-05 22:50:36 +01:00
return $this;
}
2023-11-06 06:22:45 +01:00
/**
* Inject the Company Details into the DOM Document
*
* @param bool $include_labels
* @return self
*/
2023-11-06 05:54:51 +01:00
private function companyDetails(bool $include_labels): self
2023-11-05 22:50:36 +01:00
{
2023-11-06 05:47:16 +01:00
$var_set = $this->getVarSet();
2023-11-05 22:50:36 +01:00
2023-11-06 06:22:45 +01:00
$company_details =
2023-11-28 14:27:04 +01:00
collect($this->getSettings()->pdf_variables->company_details)
2023-11-06 06:22:45 +01:00
->filter(function ($variable) use ($var_set) {
2023-11-06 05:47:16 +01:00
return isset($var_set['values'][$variable]) && !empty($var_set['values'][$variable]);
2023-11-05 22:50:36 +01:00
})
2023-11-06 06:22:45 +01:00
->when(!$include_labels, function ($collection) {
return $collection->map(function ($variable) {
return ['element' => 'p', 'content' => $variable, 'show_empty' => false, 'properties' => ['data-ref' => 'company_details-' . substr($variable, 1)]];
});
2023-11-06 02:54:29 +01:00
})->toArray();
2023-11-05 22:50:36 +01:00
$company_details = $include_labels ? $this->labelledFieldStack($company_details, 'company_details-') : $company_details;
2023-11-06 05:47:16 +01:00
$this->updateElementProperties('company-details', $company_details);
2023-11-05 22:50:36 +01:00
return $this;
}
2023-11-06 06:22:45 +01:00
private function companyAddress(bool $include_labels = false): self
2023-11-05 22:50:36 +01:00
{
2023-11-06 05:47:16 +01:00
$var_set = $this->getVarSet();
2023-11-05 22:50:36 +01:00
2023-11-06 05:47:16 +01:00
$company_address =
2023-11-28 14:27:04 +01:00
collect($this->getSettings()->pdf_variables->company_address)
2023-11-06 05:47:16 +01:00
->filter(function ($variable) use ($var_set) {
return isset($var_set['values'][$variable]) && !empty($var_set['values'][$variable]);
})
2023-11-06 06:22:45 +01:00
->when(!$include_labels, function ($collection) {
return $collection->map(function ($variable) {
return ['element' => 'p', 'content' => $variable, 'show_empty' => false, 'properties' => ['data-ref' => 'company_address-' . substr($variable, 1)]];
});
2023-11-06 05:47:16 +01:00
})->toArray();
2023-11-05 22:50:36 +01:00
$company_address = $include_labels ? $this->labelledFieldStack($company_address, 'company_address-') : $company_address;
2023-11-06 06:22:45 +01:00
2023-11-06 05:47:16 +01:00
$this->updateElementProperties('company-address', $company_address);
2023-11-05 22:50:36 +01:00
return $this;
}
2023-11-06 06:22:45 +01:00
/**
* Injects the Shipping Details into the DOM Document
*
* @param bool $include_labels
* @return self
*/
private function shippingDetails(bool $include_labels = false): self
2023-11-05 22:50:36 +01:00
{
2023-11-06 06:22:45 +01:00
if(!$this->entity->client) {
2023-11-06 05:47:16 +01:00
return $this;
2023-11-06 06:22:45 +01:00
}
2023-11-06 05:47:16 +01:00
$this->client = $this->entity->client;
$shipping_address = [
2023-11-27 12:20:58 +01:00
// ['element' => 'p', 'content' => ctrans('texts.shipping_address'), 'properties' => ['data-ref' => 'shipping_address-label', 'style' => 'font-weight: bold; text-transform: uppercase']],
2023-11-06 05:47:16 +01:00
['element' => 'p', 'content' => $this->client->name, 'show_empty' => false, 'properties' => ['data-ref' => 'shipping_address-client.name']],
['element' => 'p', 'content' => $this->client->shipping_address1, 'show_empty' => false, 'properties' => ['data-ref' => 'shipping_address-client.shipping_address1']],
['element' => 'p', 'content' => $this->client->shipping_address2, 'show_empty' => false, 'properties' => ['data-ref' => 'shipping_address-client.shipping_address2']],
['element' => 'p', 'show_empty' => false, 'elements' => [
['element' => 'span', 'content' => "{$this->client->shipping_city} ", 'properties' => ['ref' => 'shipping_address-client.shipping_city']],
['element' => 'span', 'content' => "{$this->client->shipping_state} ", 'properties' => ['ref' => 'shipping_address-client.shipping_state']],
['element' => 'span', 'content' => "{$this->client->shipping_postal_code} ", 'properties' => ['ref' => 'shipping_address-client.shipping_postal_code']],
]],
['element' => 'p', 'content' => optional($this->client->shipping_country)->name, 'show_empty' => false],
];
$shipping_address =
2023-11-06 06:22:45 +01:00
collect($shipping_address)->filter(function ($address) {
2023-11-06 05:47:16 +01:00
return isset($address['content']) && !empty($address['content']);
})->toArray();
$this->updateElementProperties('shipping-details', $shipping_address);
2023-11-05 22:50:36 +01:00
return $this;
}
2023-11-06 06:22:45 +01:00
/**
* Injects the Client Details into the DOM Document
*
* @param bool $include_labels
* @return self
*/
private function clientDetails(bool $include_labels = false): self
2023-11-05 22:50:36 +01:00
{
2023-11-06 05:47:16 +01:00
$var_set = $this->getVarSet();
$client_details =
2023-11-28 14:27:04 +01:00
collect($this->getSettings()->pdf_variables->client_details)
2023-11-06 05:47:16 +01:00
->filter(function ($variable) use ($var_set) {
return isset($var_set['values'][$variable]) && !empty($var_set['values'][$variable]);
})
2023-11-06 06:22:45 +01:00
->when(!$include_labels, function ($collection) {
return $collection->map(function ($variable) {
return ['element' => 'p', 'content' => $variable, 'show_empty' => false, 'properties' => ['data-ref' => 'client_details-' . substr($variable, 1)]];
});
2023-11-06 05:47:16 +01:00
})->toArray();
$client_details = $include_labels ? $this->labelledFieldStack($client_details, 'client_details-') : $client_details;
2023-11-06 05:47:16 +01:00
$this->updateElementProperties('client-details', $client_details);
2023-11-05 22:50:36 +01:00
return $this;
}
2023-11-06 06:22:45 +01:00
/**
* Resolves the entity.
*
* Only required for resolving the entity-details stack
*
* @return string
*/
2023-11-06 05:47:16 +01:00
private function resolveEntity(): string
{
$entity_string = '';
2023-11-06 06:22:45 +01:00
match($this->entity) {
2023-11-06 05:47:16 +01:00
($this->entity instanceof Invoice) => $entity_string = 'invoice',
($this->entity instanceof Quote) => $entity_string = 'quote',
($this->entity instanceof Credit) => $entity_string = 'credit',
($this->entity instanceof RecurringInvoice) => $entity_string = 'invoice',
2023-11-06 05:54:51 +01:00
($this->entity instanceof PurchaseOrder) => $entity_string = 'purchase_order',
2023-11-06 05:47:16 +01:00
default => $entity_string = 'invoice',
};
return $entity_string;
}
2023-11-06 06:22:45 +01:00
2023-11-06 05:47:16 +01:00
/**
* Returns the variable array by first key, if it exists
*
* @return array
*/
private function getVarSet(): array
{
return array_key_exists(array_key_first($this->variables), $this->variables) ? $this->variables[array_key_first($this->variables)] : $this->variables;
}
2023-11-06 06:22:45 +01:00
2023-11-06 05:47:16 +01:00
/**
* Injects the entity details to the DOM document
*
* @return self
*/
2023-11-05 22:50:36 +01:00
private function entityDetails(): self
{
2023-11-06 05:47:16 +01:00
$entity_string = $this->resolveEntity();
$entity_string_prop = "{$entity_string}_details";
$var_set = $this->getVarSet();
$entity_details =
2023-11-28 14:27:04 +01:00
collect($this->getSettings()->pdf_variables->{$entity_string_prop})
2023-11-06 05:47:16 +01:00
->filter(function ($variable) use ($var_set) {
return isset($var_set['values'][$variable]) && !empty($var_set['values'][$variable]);
})->toArray();
$this->updateElementProperties("entity-details", $this->labelledFieldStack($entity_details, 'entity_details-'));
2023-11-05 22:50:36 +01:00
return $this;
}
2023-11-06 06:22:45 +01:00
2023-11-06 05:54:51 +01:00
/**
* Generates the field stacks with labels
*
* @param array $variables
* @return array
*/
private function labelledFieldStack(array $variables, string $data_ref): array
2023-11-05 22:50:36 +01:00
{
2023-11-06 06:22:45 +01:00
2023-11-06 05:47:16 +01:00
$elements = [];
2023-11-05 22:50:36 +01:00
2023-11-06 05:47:16 +01:00
foreach ($variables as $variable) {
$_variable = explode('.', $variable)[1];
$_customs = ['custom1', 'custom2', 'custom3', 'custom4'];
2023-11-05 22:50:36 +01:00
2023-11-06 05:47:16 +01:00
$var = str_replace("custom", "custom_value", $_variable);
2023-11-06 02:54:29 +01:00
$hidden_prop = ($data_ref == 'entity_details-') ? $this->entityVariableCheck($variable) : false;
2024-01-14 05:05:00 +01:00
2023-11-06 05:47:16 +01:00
if (in_array($_variable, $_customs) && !empty($this->entity->{$var})) {
$elements[] = ['element' => 'tr', 'elements' => [
['element' => 'th', 'content' => $variable . '_label', 'properties' => ['data-ref' => $data_ref . substr($variable, 1) . '_label']],
['element' => 'th', 'content' => $variable, 'properties' => ['data-ref' => $data_ref . substr($variable, 1)]],
2023-11-06 05:47:16 +01:00
]];
} else {
$elements[] = ['element' => 'tr', 'properties' => ['hidden' => $hidden_prop], 'elements' => [
['element' => 'th', 'content' => $variable . '_label', 'properties' => ['data-ref' => $data_ref . substr($variable, 1) . '_label']],
['element' => 'th', 'content' => $variable, 'properties' => ['data-ref' => $data_ref . substr($variable, 1)]],
2023-11-06 05:47:16 +01:00
]];
}
}
2023-11-06 02:54:29 +01:00
2023-11-06 05:47:16 +01:00
return $elements;
2023-11-06 06:22:45 +01:00
2023-11-06 05:47:16 +01:00
}
2023-11-06 02:54:29 +01:00
2023-11-06 06:22:45 +01:00
/**
* Inject Vendor Details into DOM Document
*
* @param bool $include_labels
* @return self
*/
private function vendorDetails(bool $include_labels = false): self
2023-11-06 05:47:16 +01:00
{
$var_set = $this->getVarSet();
$vendor_details =
2023-11-28 14:27:04 +01:00
collect($this->getSettings()->pdf_variables->vendor_details)
2023-11-06 05:47:16 +01:00
->filter(function ($variable) use ($var_set) {
return isset($var_set['values'][$variable]) && !empty($var_set['values'][$variable]);
2023-11-06 06:22:45 +01:00
})->when(!$include_labels, function ($collection) {
return $collection->map(function ($variable) {
return ['element' => 'p', 'content' => $variable, 'show_empty' => false, 'properties' => ['data-ref' => 'vendor_details-' . substr($variable, 1)]];
});
2023-11-06 05:47:16 +01:00
})->toArray();
2023-11-05 22:50:36 +01:00
$vendor_details = $include_labels ? $this->labelledFieldStack($vendor_details, 'vendor_details-') : $vendor_details;
2023-11-06 02:54:29 +01:00
2023-11-06 05:47:16 +01:00
$this->updateElementProperties('vendor-details', $vendor_details);
2023-11-06 02:54:29 +01:00
return $this;
}
2023-11-06 05:47:16 +01:00
/**
* Performs a variable check to ensure
* the variable exists
*
* @param string $variable
* @return bool
*
*/
public function entityVariableCheck(string $variable): bool
{
// When it comes to invoice balance, we'll always show it.
if ($variable == '$invoice.total') {
return false;
}
// Some variables don't map 1:1 to table columns. This gives us support for such cases.
$aliases = [
'$quote.balance_due' => 'partial',
];
try {
$_variable = explode('.', $variable)[1];
} catch (\Exception $e) {
throw new \Exception('Company settings seems to be broken. Missing $this->service->config->entity.variable type.');
}
if (\in_array($variable, \array_keys($aliases))) {
$_variable = $aliases[$variable];
}
if (is_null($this->entity->{$_variable}) || empty($this->entity->{$_variable})) {
return true;
}
return false;
}
2023-11-06 02:54:29 +01:00
////////////////////////////////////////
// Dom Traversal
///////////////////////////////////////
public function updateElementProperties(string $element_id, array $elements): self
{
$node = $this->document->getElementById($element_id);
2023-11-06 06:22:45 +01:00
$this->createElementContent($node, $elements);
2023-11-06 05:47:16 +01:00
2023-11-06 02:54:29 +01:00
return $this;
}
public function updateElementProperty($element, string $attribute, ?string $value)
{
if ($attribute == 'hidden' && ($value == false || $value == 'false')) {
return $element;
2023-11-05 22:50:36 +01:00
}
2023-11-06 02:54:29 +01:00
$element->setAttribute($attribute, $value);
2023-11-05 22:50:36 +01:00
2023-11-06 02:54:29 +01:00
if ($element->getAttribute($attribute) === $value) {
return $element;
2023-11-05 22:50:36 +01:00
}
2023-11-06 02:54:29 +01:00
return $element;
}
2023-11-06 06:22:45 +01:00
public function createElementContent($element, $children): self
2023-11-06 02:54:29 +01:00
{
2023-11-06 05:47:16 +01:00
2023-11-06 02:54:29 +01:00
foreach ($children as $child) {
$contains_html = false;
2023-11-06 05:47:16 +01:00
//06-11-2023 for some reason this parses content as HTML
// if ($child['element'] !== 'script') {
// if ($this->company->markdown_enabled && array_key_exists('content', $child)) {
// $child['content'] = str_replace('<br>', "\r", $child['content']);
// $child['content'] = $this->commonmark->convert($child['content'] ?? '');
// }
// }
2023-11-06 02:54:29 +01:00
if (isset($child['content'])) {
if (isset($child['is_empty']) && $child['is_empty'] === true) {
continue;
}
2023-11-05 22:50:36 +01:00
2023-11-06 02:54:29 +01:00
$contains_html = preg_match('#(?<=<)\w+(?=[^<]*?>)#', $child['content'], $m) != 0;
}
2023-11-06 06:22:45 +01:00
2023-11-06 02:54:29 +01:00
if ($contains_html) {
// If the element contains the HTML, we gonna display it as is. Backend is going to
// encode it for us, preventing any errors on the processing stage.
// Later, we decode this using Javascript so it looks like it's normal HTML being injected.
// To get all elements that need frontend decoding, we use 'data-state' property.
$_child = $this->document->createElement($child['element'], '');
$_child->setAttribute('data-state', 'encoded-html');
$_child->nodeValue = htmlspecialchars($child['content']);
2023-11-15 04:57:07 +01:00
2023-11-06 02:54:29 +01:00
} else {
// .. in case string doesn't contain any HTML, we'll just return
// raw $content.
2023-11-15 04:57:07 +01:00
$_child = $this->document->createElement($child['element'], isset($child['content']) ? $child['content'] : '');
2023-11-06 02:54:29 +01:00
}
$element->appendChild($_child);
if (isset($child['properties'])) {
foreach ($child['properties'] as $property => $value) {
$this->updateElementProperty($_child, $property, $value);
}
}
if (isset($child['elements'])) {
$this->createElementContent($_child, $child['elements']);
}
2023-11-06 05:47:16 +01:00
2023-11-06 02:54:29 +01:00
}
2024-01-14 05:05:00 +01:00
2023-10-12 03:13:08 +02:00
return $this;
}
2023-11-26 08:41:42 +01:00
}