1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 01:11:34 +02:00
invoiceninja/app/Services/PdfMaker/Designs/Plain.php

187 lines
6.9 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Services\PdfMaker\Designs;
use App\Services\PdfMaker\Designs\Utilities\BaseDesign;
use App\Services\PdfMaker\Designs\Utilities\TableHelpers;
2020-07-30 17:47:40 +02:00
use App\Utils\Traits\MakesInvoiceValues;
class Plain extends BaseDesign
{
use MakesInvoiceValues, TableHelpers;
2020-07-30 17:47:40 +02:00
2020-08-04 13:37:51 +02:00
/** Global list of table elements, @var array */
2020-07-30 16:43:57 +02:00
public $elements;
2020-08-04 13:37:51 +02:00
/** @var App\Models\Client */
2020-07-30 17:47:40 +02:00
public $client;
2020-08-05 11:52:01 +02:00
/** @var App\Models\Invoice || @var App\Models\Quote */
public $entity;
2020-07-30 17:47:40 +02:00
2020-08-05 11:52:01 +02:00
/** Global state of the design, @var array */
2020-07-30 17:47:40 +02:00
public $context;
2020-07-30 16:43:57 +02:00
/** Type of entity => product||task */
2020-08-05 11:52:01 +02:00
public $type;
2020-07-29 14:53:58 +02:00
public function html(): ?string
{
return file_get_contents(
2020-07-30 16:43:57 +02:00
base_path('resources/views/pdf-designs/plain.html')
);
}
2020-07-29 14:53:58 +02:00
public function elements(array $context, string $type = 'product'): array
2020-07-29 14:53:58 +02:00
{
2020-07-30 17:47:40 +02:00
$this->context = $context;
2020-08-10 17:04:51 +02:00
2020-08-05 11:52:01 +02:00
$this->type = $type;
2020-07-30 17:47:40 +02:00
$this->setup();
2020-07-29 14:53:58 +02:00
return [
'company-address' => [
'id' => 'company-address',
2020-08-04 17:32:28 +02:00
'elements' => $this->companyAddress(),
],
'entity-details' => [
'id' => 'entity-details',
'elements' => $this->entityDetails(),
],
'client-details' => [
'id' => 'client-details',
'elements' => $this->clientDetails(),
],
'product-table' => [
'id' => 'product-table',
'elements' => $this->productTable(),
2020-07-29 14:53:58 +02:00
],
2020-07-30 17:47:40 +02:00
];
}
2020-08-04 17:32:28 +02:00
public function companyAddress(): array
{
2020-08-05 11:52:01 +02:00
$variables = $this->entity->company->settings->pdf_variables->company_address;
2020-08-04 17:32:28 +02:00
$elements = [];
foreach ($variables as $variable) {
$elements[] = ['element' => 'p', 'content' => $variable];
}
return $elements;
}
public function entityDetails(): array
{
2020-08-05 11:52:01 +02:00
$variables = $this->entity->company->settings->pdf_variables->invoice_details;
2020-08-04 17:32:28 +02:00
$elements = [];
foreach ($variables as $variable) {
$elements[] = ['element' => 'tr', 'content' => '', 'elements' => [
['element' => 'th', 'content' => $variable . '_label', 'properties' => ['class' => 'text-left pr-4 font-normal']],
['element' => 'th', 'content' => $variable, 'properties' => ['class' => 'text-left pr-4 font-medium']],
]];
}
return $elements;
}
public function clientDetails(): array
{
2020-08-05 11:52:01 +02:00
$variables = $this->entity->company->settings->pdf_variables->client_details;
2020-08-04 17:32:28 +02:00
$elements = [];
foreach ($variables as $variable) {
$elements[] = ['element' => 'p', 'content' => $variable];
}
return $elements;
}
2020-08-04 16:56:17 +02:00
public function productTable(): array
2020-07-30 17:47:40 +02:00
{
return [
2020-08-04 17:32:28 +02:00
['element' => 'thead', 'content' => '', 'properties' => ['class' => 'text-left bg-gray-200'], 'elements' => $this->buildTableHeader()],
['element' => 'tbody', 'content' => '', 'elements' => $this->buildTableBody()],
['element' => 'tfoot', 'content' => '', 'elements' => $this->tableFooter()],
];
}
public function tableFooter()
{
return [
['element' => 'tr', 'content' => '', 'elements' => [
['element' => 'td', 'content' => '$entity.public_notes', 'properties' => ['class' => 'border-l-4 border-white px-4 py-4', 'colspan' => $this->calculateColspan(3)]],
2020-08-10 17:04:51 +02:00
['element' => 'td', 'content' => '$subtotal_label', 'properties' => [$this->toggleHiddenProperty($this->entity->calc()->getSubTotal()), 'class' => 'px-4 py-4 text-right', 'colspan' => '2']],
['element' => 'td', 'content' => '$subtotal', 'properties' => [$this->toggleHiddenProperty($this->entity->calc()->getSubTotal()), 'class' => 'px-4 py-2 text-right']],
]],
2020-08-10 17:04:51 +02:00
['element' => 'tr', 'properties' => ['hidden' => $this->toggleHiddenProperty($this->entity->calc()->getTotalDiscount())], 'content' => '', 'elements' => [
['element' => 'td', 'content' => '$discount_label', 'properties' => ['class' => 'border-l-4 border-white px-4 text-right', 'colspan' => $this->calculateColspan(1)]],
['element' => 'td', 'content' => '$discount', 'properties' => ['class' => 'px-4 py-2 text-right']],
]],
2020-08-10 17:04:51 +02:00
['element' => 'tr', ['hidden' => $this->toggleHiddenProperty($this->entity->partial)], 'content' => '', 'elements' => [
['element' => 'td', 'content' => '$partial_due_label', 'properties' => ['class' => 'border-l-4 border-white px-4 text-right', 'colspan' => $this->calculateColspan(1)]],
['element' => 'td', 'content' => '$partial_due', 'properties' => ['class' => 'px-4 py-2 text-right']],
]],
['element' => 'tr', 'content' => '', 'elements' => [
['element' => 'td', 'content' => '$invoice_total_label', 'properties' => ['class' => 'border-l-4 border-white px-4 text-right', 'colspan' => $this->calculateColspan(1)]],
['element' => 'td', 'content' => '$invoice_total', 'properties' => ['class' => 'px-4 py-2 text-right']],
]],
2020-08-10 17:04:51 +02:00
['element' => 'tr', 'content' => '', 'properties' => ['hidden' => $this->toggleHiddenProperty($this->entity->balance), 'class' => 'mt-8 px-4 py-2 bg-gray-300'], 'elements' => [
['element' => 'td', 'content' => '$balance_due_label', 'properties' => ['class' => 'border-l-4 border-white px-4 text-right', 'colspan' => $this->calculateColspan(1)]],
['element' => 'td', 'content' => '$balance_due', 'properties' => ['class' => 'px-4 py-2 text-right']],
2020-08-04 17:32:28 +02:00
]],
2020-07-29 14:53:58 +02:00
];
}
2020-07-30 16:43:57 +02:00
2020-07-30 17:47:40 +02:00
public function buildTableHeader(): array
2020-07-30 16:43:57 +02:00
{
$this->processTaxColumns();
2020-08-03 13:29:57 +02:00
$elements = [];
2020-08-03 13:29:57 +02:00
2020-07-30 17:47:40 +02:00
foreach ($this->context['product-table-columns'] as $column) {
2020-07-30 16:43:57 +02:00
$elements[] = ['element' => 'th', 'content' => $column . '_label', 'properties' => ['class' => 'px-4 py-2']];
}
return $elements;
}
2020-07-30 17:47:40 +02:00
public function buildTableBody(): array
2020-07-30 16:43:57 +02:00
{
2020-07-30 17:47:40 +02:00
$elements = [];
2020-08-05 11:52:01 +02:00
$items = $this->transformLineItems($this->entity->line_items);
2020-07-30 17:47:40 +02:00
if (count($items) == 0) {
return [];
}
foreach ($items as $row) {
$element = ['element' => 'tr', 'content' => '', 'elements' => []];
2020-08-03 13:29:57 +02:00
foreach ($this->context['product-table-columns'] as $key => $cell) {
$element['elements'][] = ['element' => 'td', 'content' => $row[$cell], 'properties' => ['class' => 'border-t-2 border-b border-gray-200 px-4 py-4']];
2020-07-30 17:47:40 +02:00
}
$elements[] = $element;
}
return $elements;
2020-07-30 16:43:57 +02:00
}
}