Skip to content

Commit

Permalink
add template turoiso
Browse files Browse the repository at this point in the history
  • Loading branch information
MartkCz committed May 4, 2022
1 parent 9307b8c commit b5d3a44
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/Templates/Template/TuroisoTemplateObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php declare(strict_types = 1);

namespace Contributte\Invoice\Templates\Template;

use Contributte\Invoice\Data\Extension\IAccountNumber;
use Contributte\Invoice\Data\Extension\IConstantSymbol;
use Contributte\Invoice\Data\Extension\ISpecificSymbol;
use Contributte\Invoice\Data\Extension\IVariableSymbol;
use Contributte\Invoice\Data\IOrder;
use Contributte\Invoice\Templates\Translator\ITranslator;
use WebChemistry\SvgPdf\Pdf\Color;

class TuroisoTemplateObject extends TemplateObject
{

/** @var Color[] */
public array $colors;

/** @var string[] */
public array $leftHeaderLeft;

/** @var string[] */
public array $leftHeaderRight;

/** @var string[] */
public array $rightHeader;

/** @var string[] */
public array $leftSubheader;

/** @var string[] */
public array $rightSubheader;

public function __construct(IOrder $order, ITranslator $translator)
{
parent::__construct($order, $translator);

$company = $order->getCompany();
$customer = $order->getCustomer();
$payment = $order->getPayment();
$account = $payment->getFirstAccount();

$this->colors = [
'primary' => new Color(6, 178, 194),
'secondary' => new Color(52, 52, 53),
'even' => new Color(241, 240, 240),
'text' => Color::black(),
'white' => Color::white(),
'odd' => Color::white(),
];

$this->leftHeaderLeft = array_filter([
$this->implode([$company->getZip(), $company->getTown()], ' '),
$company->getAddress(),
$company->getCountry(),
]);

$this->leftHeaderRight = array_filter([
$this->prepend($translator->translate('ID') . ': ', $company->getId()),
$this->prepend($translator->translate('VAT Number') . ': ', $company->getVatNumber()),
]);

$this->rightHeader = [
$translator->translate('Date') . ': ' . $order->getTimestamps()->getCreated(),
$translator->translate('Invoice number') . ': ' . $order->getNumber(),
];

$this->leftSubheader = array_filter([
$customer->getName(),
$this->implode([$company->getZip(), $company->getTown()], ', '),
$customer->getAddress(),
$customer->getCountry(),
$this->prepend($translator->translate('ID') . ': ', $customer->getId()),
$this->prepend($translator->translate('VAT Number') . ': ', $customer->getVatNumber()),
]);

$this->rightSubheader = array_filter([
$this->prepend($translator->translate('Due to') . ': ', $order->getTimestamps()->getDueTo()),
$account instanceof IAccountNumber ?
$this->prepend($translator->translate('Account number') . ': ', $account->getAccountNumber()) : null,
$this->prepend($translator->translate('IBAN') . ': ', $account?->getIban()),
$payment instanceof IVariableSymbol ?
$this->prepend($translator->translate('Variable symbol') . ': ', $payment->getVariableSymbol()) : null,
$payment instanceof IConstantSymbol ?
$this->prepend($translator->translate('Constant symbol') . ': ', $payment->getConstantSymbol()) : null,
$payment instanceof ISpecificSymbol ?
$this->prepend($translator->translate('Specific symbol') . ': ', $payment->getSpecificSymbol()) : null,
]);
}

}
34 changes: 34 additions & 0 deletions src/Templates/Translator/TuroisoTemplateTranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace Contributte\Invoice\Templates\Translator;

final class TuroisoTemplateTranslator extends Translator
{

public function __construct(string $lang = 'en')
{
$this->addLanguage('en', [
'Invoice' => 'Invoice',
'Payment' => 'Payment',
'Total price' => 'Total price',
'Customer' => 'Customer',
'Item' => 'Item',
'ID' => 'ID',
'Price per item' => 'Price per item',
'Price' => 'Price',
'Quantity' => 'Quantity',
'VAT Number' => 'VAT Number',
'Invoice number' => 'Invoice number',
'Date' => 'Date',
'Due to' => 'Due to',
'Account number' => 'Account number',
'IBAN' => 'IBAN',
'Variable symbol' => 'Variable symbol',
'Constant symbol' => 'Constant symbol',
'Specific symbol' => 'Specific symbol',
]);

parent::__construct($lang);
}

}
42 changes: 42 additions & 0 deletions src/Templates/TuroisoTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace Contributte\Invoice\Templates;

use Contributte\Invoice\Data\IOrder;
use Contributte\Invoice\Templates\Template\TemplateObject;
use Contributte\Invoice\Templates\Template\TuroisoTemplateObject;
use Contributte\Invoice\Templates\Translator\ITranslator;
use Contributte\Invoice\Templates\Translator\TuroisoTemplateTranslator;
use WebChemistry\SvgPdf\Pdf\Color;
use WebChemistry\SvgPdf\PdfSvg;

class TuroisoTemplate extends Template
{

/** @var Color[] */
public array $colors = [];

private ITranslator $translator;

public function __construct(?ITranslator $translator = null)
{
$renderer = new PdfSvg();
$renderer->addFont('Montserrat', __DIR__ . '/assets/font/Montserrat-Regular.php')
->addFont('Montserrat', __DIR__ . '/assets/font/Montserrat-Bold.php', 'bold');

$this->translator = $translator ?? new TuroisoTemplateTranslator();

parent::__construct($renderer);
}

protected function getTemplate(): string
{
return __DIR__ . '/templates/turoiso.phtml';
}

protected function createTemplateObject(IOrder $order): TemplateObject
{
return new TuroisoTemplateObject($order, $this->translator);
}

}
143 changes: 143 additions & 0 deletions src/Templates/templates/turoiso.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

use Contributte\Invoice\Data\Extension\IPriceWithTax;
use Contributte\Invoice\Templates\Template\TuroisoTemplateObject;
use Contributte\Invoice\Templates\TuroisoTemplate;
use WebChemistry\SvgPdf\Utility\TemplateUtility;

/** @var $this TuroisoTemplate */
/** @var $template TuroisoTemplateObject */
$formatMoney = $template->formatMoneyCallback();
$order = $template->getOrder();
$payment = $order->getPayment();

$height = '841.89';
$width = '595.28';
$globalY = 0;

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<svg xmlns="http://www.w3.org/2000/svg" width="<?= $width ?>" height="<?= $height ?>">
<style>
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap');
* {
font-family: Montserrat;
}
p { margin: 0; }
</style>
<!-- Header design -->
<polygon points="0,0 0,100 245,100 312,0" fill="<?= $template->colors['secondary'] ?>" />
<polygon points="312,0 245,100 <?= $width ?>,100 <?= $height ?>,0" fill="<?= $template->colors['primary'] ?>" />
<polygon points="0,100 0,106 168,106 188,120 200,100" fill="<?= $template->colors['primary'] ?>" />
<polygon points="200,100 188,120 205,125 222,100" fill="<?= $template->colors['primary']->darken(15) ?>" />
<polygon points="222,100 217,106 242,106 247,100" fill="<?= $template->colors['primary'] ?>" />
<!-- / Header design -->
<!-- Header text -->
<text x="120" y="25" font-size="18" fill="<?= $template->colors['white'] ?>" text-anchor="middle"><?= TemplateUtility::escape($order->getCompany()->getName()) ?></text>

<?php $y = TemplateUtility::multiplier(48, 12) ?>
<?php foreach ($template->leftHeaderLeft as $item): ?>
<text x="112" y="<?= $y ?>" font-size="7" fill="<?= $template->colors['white'] ?>" text-anchor="end"><?= TemplateUtility::escape($item) ?></text>
<?php endforeach; ?>

<?php $y = $y->copy(); ?>
<?php foreach ($template->leftHeaderRight as $item): ?>
<text x="130" y="<?= $y ?>" font-size="7" fill="<?= $template->colors['white'] ?>"><?= TemplateUtility::escape($item) ?></text>
<?php endforeach; ?>
<!-- / Header text -->

<!-- Header text right -->

<?php $y = TemplateUtility::multiplier(45, 12) ?>
<?php foreach ($template->rightHeader as $item): ?>
<text x="424" y="<?= $y ?>" font-size="7" fill="<?= $template->colors['white'] ?>" text-anchor="end"><?= TemplateUtility::escape($item) ?></text>
<?php endforeach; ?>

<text x="434" y="<?= $y->centerPosition(-2) ?>" font-size="19" fill="<?= $template->colors['white'] ?>" font-weight="bold"><?= TemplateUtility::escape(mb_strtoupper($template->translate('Invoice'))) ?></text>
<!-- / Header text right -->

<!-- Subheader left -->
<?php $catcher = TemplateUtility::createMaxWidthCatcher($this->getRenderer(), $width); $catcher->start(); ?>
<text x="33" y="175" font-size="16" font-weight="bold" fill="<?= $template->colors['primary'] ?>"><?= TemplateUtility::escape(mb_strtoupper($template->translate('Customer'))) ?></text>
<?= $catcher->endAndGetContent() ?>

<?php
$x = $catcher->getWidth(true) + 48;
$y = 190;
?>

<polygon points="<?= $x ?>,<?= $y - 10 ?> <?= $x ?>,<?= $y ?> <?= $x - 7 ?>,<?= $y - 8 ?> 0,<?= $y - 8 ?> 0,<?= $y - 10 ?>" fill="<?= $template->colors['primary'] ?>" />

<?php $y = TemplateUtility::multiplier(210, 17) ?>
<?php foreach ($template->leftSubheader as $i => $item): ?>
<text x="33" y="<?= $y ?>" font-size="<?= $i === 0 ? 15 : 10 ?>"<?= $i === 0 ? ' font-weight="bold"' : '' ?>><?= TemplateUtility::escape($item) ?></text>
<?php endforeach; ?>
<!-- / Subheader left -->
<?php $globalY = $y->current() ?>

<!-- Subheader right -->
<?php $x = 350 ?>
<text x="<?= $x ?>" y="175" font-size="16" font-weight="bold" fill="<?= $template->colors['primary'] ?>"><?= TemplateUtility::escape(mb_strtoupper($template->translate('Payment'))) ?></text>

<?php $y = 182; ?>
<polygon points="<?= $x - 10 ?>,<?= $y ?> <?= $x - 10 ?>,<?= $y + 10 ?> <?= $x + 2 ?>,<?= $y + 2 ?> <?= $width ?>,<?= $y + 2 ?> <?= $width ?>,<?= $y ?>" fill="<?= $template->colors['primary'] ?>" />

<?php $y = TemplateUtility::multiplier(210, 17) ?>
<?php foreach ($template->rightSubheader as $i => $item): ?>
<text x="<?= $x ?>" y="<?= $y ?>" font-size="10"><?= TemplateUtility::escape($item) ?></text>
<?php endforeach; ?>
<!-- / Subheader right -->
<?php $globalY = ($y->current() > $globalY ? $y->current() : $globalY) + 55; ?>

<!-- Items header -->
<?php $y = TemplateUtility::multiplier($globalY, 29, false) ?>

<rect width="<?= $width ?>" height="29" x="0" y="<?= $y->current() - 29 ?>" fill="<?= $template->colors['even'] ?>" />
<polygon points="<?= $width ?>,<?= $y->current() - 29 ?> <?= $width ?>,<?= $y->current() ?> <?= $width - 124 ?>,<?= $y->current() ?> <?= $width - 100 ?>,<?= $y->current() - 29 ?>" fill="<?= $template->colors['primary'] ?>" />
<rect width="<?= $width ?>" height="1" x="0" y="<?= $y->current() ?>" fill="<?= $template->colors['primary'] ?>" />

<text y="<?= $y->current() - 10 ?>" x="35" font-size="10" fill="<?= $template->colors['primary'] ?>" font-weight="bold"><?= TemplateUtility::escape($template->translate('Item')) ?></text>
<text y="<?= $y->current() - 10 ?>" x="305" font-size="10" fill="<?= $template->colors['primary'] ?>" font-weight="bold" text-anchor="middle"><?= TemplateUtility::escape($template->translate('Price per item')) ?></text>
<text y="<?= $y->current() - 10 ?>" x="415" font-size="10" fill="<?= $template->colors['primary'] ?>" font-weight="bold" text-anchor="middle"><?= TemplateUtility::escape($template->translate('Quantity')) ?></text>

<text y="<?= $y->current() - 10 ?>" x="<?= $width - 56 ?>" font-size="10" fill="<?= $template->colors['white'] ?>" font-weight="bold" text-anchor="middle"><?= TemplateUtility::escape($template->translate('Price')) ?></text>

<?php $y->additional(1) ?>
<!-- / Items header -->

<!-- Items -->
<?php foreach ($order->getItems() as $i => $item): ?>
<?php $y->increment() ?>

<rect width="<?= $width ?>" height="29" x="0" y="<?= $y->current() - 29 ?>" fill="<?= $i % 2 === 0 ? $template->colors['odd'] : $template->colors['even'] ?>" />
<text y="<?= $y->current() - 11 ?>" x="35" font-size="8" fill="<?= $template->colors['text'] ?>"><?= TemplateUtility::escape($item->getName()) ?></text>
<text y="<?= $y->current() - 11 ?>" x="305" font-size="8" fill="<?= $template->colors['text'] ?>" text-anchor="middle"><?= TemplateUtility::escape($item->getUnitPrice()) ?></text>
<text y="<?= $y->current() - 11 ?>" x="415" font-size="8" fill="<?= $template->colors['text'] ?>" text-anchor="middle"><?= TemplateUtility::escape($item->getQuantity()) ?></text>
<text y="<?= $y->current() - 11 ?>" x="<?= $width - 56 ?>" font-size="8" fill="<?= $template->colors['text'] ?>" text-anchor="middle"><?= TemplateUtility::escape($item->getTotalPrice()) ?></text>
<?php endforeach; ?>
<!-- / Items -->

<!-- Items footer -->
<?php if ($order instanceof IPriceWithTax): ?>

<?php $y->increment() ?>

<rect width="<?= $width - 380 ?>" height="29" x="380" y="<?= $y->current() - 29 ?>" fill="<?= $template->colors['even'] ?>" />
<text y="<?= $y->current() - 10 ?>" x="402" font-size="10" fill="<?= $template->colors['text'] ?>" text-anchor="start" font-weight="bold"><?= TemplateUtility::escape($template->translate('Subtotal')) ?></text>
<text y="<?= $y->current() - 11 ?>" x="<?= $width - 56 ?>" font-size="8" fill="<?= $template->colors['text'] ?>" text-anchor="middle"><?= TemplateUtility::escape($order->getPriceBeforeTax()) ?></text>

<?php $y->increment() ?>

<rect width="<?= $width - 380 ?>" height="29" x="380" y="<?= $y->current() - 29 ?>" fill="<?= $template->colors['even'] ?>" />
<text y="<?= $y->current() - 10 ?>" x="402" font-size="10" fill="<?= $template->colors['text'] ?>" text-anchor="start" font-weight="bold"><?= TemplateUtility::escape($template->translate('Tax')) ?></text>
<text y="<?= $y->current() - 11 ?>" x="<?= $width - 56 ?>" font-size="8" fill="<?= $template->colors['text'] ?>" text-anchor="middle"><?= TemplateUtility::escape($order->getTax()) ?></text>

<?php endif; ?>

<?php $y->increment() ?>

<rect width="<?= $width - 380 ?>" height="29" x="380" y="<?= $y->current() - 29 ?>" fill="<?= $template->colors['even'] ?>" />
<text y="<?= $y->current() - 10 ?>" x="402" font-size="10" fill="<?= $template->colors['text'] ?>" text-anchor="start" font-weight="bold"><?= TemplateUtility::escape($template->translate('Total price')) ?></text>
<text y="<?= $y->current() - 11 ?>" x="<?= $width - 56 ?>" font-size="8" fill="<?= $template->colors['text'] ?>" text-anchor="middle"><?= TemplateUtility::escape($order->getTotalPrice()) ?></text>
<!-- / Items footer -->
</svg>

0 comments on commit b5d3a44

Please sign in to comment.