2020-08-04 13:33:38 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-08-04 13:33:38 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
2020-08-07 15:58:05 +02:00
|
|
|
namespace App\Services\PdfMaker\Designs\Utilities;
|
2020-08-04 13:33:38 +02:00
|
|
|
|
2020-08-11 17:19:52 +02:00
|
|
|
use DOMDocument;
|
|
|
|
use DOMXPath;
|
|
|
|
|
2020-08-10 17:07:30 +02:00
|
|
|
trait DesignHelpers
|
2020-08-04 13:33:38 +02:00
|
|
|
{
|
2020-08-11 17:19:52 +02:00
|
|
|
public $document;
|
|
|
|
|
|
|
|
public $xpath;
|
|
|
|
|
|
|
|
public function setup(): self
|
|
|
|
{
|
|
|
|
if (isset($this->context['client'])) {
|
|
|
|
$this->client = $this->context['client'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->context['entity'])) {
|
|
|
|
$this->entity = $this->context['entity'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->document();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize local dom document instance. Used for getting raw HTML out of template.
|
2020-09-06 11:38:10 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
2020-08-11 17:19:52 +02:00
|
|
|
*/
|
|
|
|
public function document(): self
|
|
|
|
{
|
|
|
|
$document = new DOMDocument();
|
|
|
|
|
|
|
|
$document->validateOnParse = true;
|
|
|
|
@$document->loadHTML($this->html());
|
|
|
|
|
|
|
|
$this->document = $document;
|
|
|
|
$this->xpath = new DOMXPath($document);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get specific section HTML.
|
2020-09-06 11:38:10 +02:00
|
|
|
*
|
|
|
|
* @param string $section
|
|
|
|
* @param bool $id
|
|
|
|
* @return null|string
|
2020-08-11 17:19:52 +02:00
|
|
|
*/
|
|
|
|
public function getSectionHTML(string $section, $id = true): ?string
|
|
|
|
{
|
|
|
|
if ($id) {
|
|
|
|
$element = $this->document->getElementById($section);
|
|
|
|
} else {
|
|
|
|
$elements = $this->document->getElementsByTagName($section);
|
|
|
|
$element = $elements[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$document = new DOMDocument();
|
|
|
|
$document->preserveWhiteSpace = false;
|
|
|
|
$document->formatOutput = true;
|
|
|
|
|
|
|
|
if ($element) {
|
|
|
|
$document->appendChild(
|
|
|
|
$document->importNode($element, true)
|
|
|
|
);
|
|
|
|
|
2020-09-09 14:47:26 +02:00
|
|
|
$html = $this->document->saveHTML();
|
|
|
|
|
|
|
|
return str_replace('%24', '$', $html);
|
2020-08-11 17:19:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-20 17:48:10 +02:00
|
|
|
return '';
|
2020-08-11 17:19:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-04 13:33:38 +02:00
|
|
|
/**
|
|
|
|
* This method will help us decide either we show
|
|
|
|
* one "tax rate" column in the table or 3 custom tax rates.
|
2020-09-06 11:38:10 +02:00
|
|
|
*
|
2020-08-04 13:33:38 +02:00
|
|
|
* Logic below will help us calculate that & inject the result in the
|
|
|
|
* global state of the $context (design state).
|
2020-09-06 11:38:10 +02:00
|
|
|
*
|
2020-08-04 13:33:38 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function processTaxColumns(): void
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (in_array('$product.tax', (array) $this->context['pdf_variables']['product_columns'])) {
|
2020-08-05 11:52:01 +02:00
|
|
|
$line_items = collect($this->entity->line_items);
|
2020-08-04 13:33:38 +02:00
|
|
|
|
|
|
|
$tax1 = $line_items->where('tax_name1', '<>', '')->where('type_id', 1)->count();
|
|
|
|
$tax2 = $line_items->where('tax_name2', '<>', '')->where('type_id', 1)->count();
|
|
|
|
$tax3 = $line_items->where('tax_name3', '<>', '')->where('type_id', 1)->count();
|
|
|
|
$taxes = [];
|
|
|
|
|
|
|
|
if ($tax1 > 0) {
|
|
|
|
array_push($taxes, '$product.tax_rate1');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($tax2 > 0) {
|
|
|
|
array_push($taxes, '$product.tax_rate2');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($tax3 > 0) {
|
|
|
|
array_push($taxes, '$product.tax_rate3');
|
|
|
|
}
|
|
|
|
|
2020-08-21 11:59:39 +02:00
|
|
|
$key = array_search('$product.tax', $this->context['pdf_variables']['product_columns'], true);
|
2020-08-04 13:33:38 +02:00
|
|
|
|
|
|
|
if ($key) {
|
2020-08-21 11:59:39 +02:00
|
|
|
array_splice($this->context['pdf_variables']['product_columns'], $key, 1, $taxes);
|
2020-08-04 13:33:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-07 15:58:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the remaining colspans.
|
2020-09-06 11:38:10 +02:00
|
|
|
*
|
|
|
|
* @param int $taken
|
|
|
|
* @return int
|
2020-08-07 15:58:05 +02:00
|
|
|
*/
|
|
|
|
public function calculateColspan(int $taken): int
|
|
|
|
{
|
2020-08-21 11:59:39 +02:00
|
|
|
$total = (int) count($this->context['pdf_variables']['product_columns']);
|
2020-08-07 15:58:05 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
return (int) $total - $taken;
|
2020-08-07 15:58:05 +02:00
|
|
|
}
|
2020-08-07 18:30:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return "true" or "false" based on null or empty check.
|
|
|
|
* We need to return false as string because of HTML parsing.
|
2020-09-06 11:38:10 +02:00
|
|
|
*
|
|
|
|
* @param mixed $property
|
|
|
|
* @return string
|
2020-08-07 18:30:04 +02:00
|
|
|
*/
|
2020-08-10 12:57:53 +02:00
|
|
|
public function toggleHiddenProperty($property): string
|
2020-08-07 18:30:04 +02:00
|
|
|
{
|
|
|
|
if (is_null($property)) {
|
|
|
|
return 'false';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($property)) {
|
|
|
|
return 'false';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'true';
|
|
|
|
}
|
2020-08-11 17:53:11 +02:00
|
|
|
|
|
|
|
public function sharedFooterElements()
|
|
|
|
{
|
2020-09-08 14:37:21 +02:00
|
|
|
return ['element' => 'div', 'properties' => ['style' => 'display: flex; justify-content: space-between; margin-top: 1.5rem'], 'elements' => [
|
2020-09-04 10:18:41 +02:00
|
|
|
['element' => 'img', 'properties' => ['src' => '$contact.signature', 'style' => 'height: 5rem;']],
|
|
|
|
['element' => 'img', 'properties' => ['src' => '$app_url/images/created-by-invoiceninja-new.png', 'style' => 'height: 5rem;', 'hidden' => $this->entity->user->account->isPaid() ? 'true' : 'false']],
|
2020-08-11 17:53:11 +02:00
|
|
|
]];
|
|
|
|
}
|
2020-08-14 14:53:55 +02:00
|
|
|
|
|
|
|
public function entityVariableCheck(string $variable): bool
|
|
|
|
{
|
|
|
|
// Extract $invoice.date => date
|
|
|
|
// so we can append date as $entity->date and not $entity->$invoice.date;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$_variable = explode('.', $variable)[1];
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new \Exception('Company settings seems to be broken. Missing $entity.variable type.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->entity->{$_variable})) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->entity->{$_variable})) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-08 13:21:07 +02:00
|
|
|
|
|
|
|
public function composeFromPartials(array $partials)
|
|
|
|
{
|
|
|
|
$html = '';
|
|
|
|
|
|
|
|
$html .= $partials['header'];
|
|
|
|
$html .= $partials['body'];
|
|
|
|
$html .= $partials['footer'];
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
2020-08-04 13:33:38 +02:00
|
|
|
}
|