invoices: choose language and duration format (#1438)

This commit is contained in:
Kevin Papst
2020-02-04 20:27:36 +01:00
committed by GitHub
parent 75dcae6fbe
commit 986d922855
64 changed files with 1559 additions and 541 deletions

View File

@@ -9,44 +9,37 @@
namespace App\Entity;
class InvoiceDocument
final class InvoiceDocument
{
/**
* @var \SplFileInfo
*/
private $file;
/**
* @param \SplFileInfo $file
*/
public function __construct(\SplFileInfo $file)
{
$this->file = $file;
}
/**
* @return string
*/
public function getId()
public function getId(): string
{
$file = $this->file->getFilename();
return substr($file, 0, strpos($file, '.'));
}
/**
* @return string
*/
public function getName()
public function getName(): string
{
return basename($this->getFilename());
}
/**
* @return string
*/
public function getFilename(): string
{
return $this->file->getRealPath();
}
public function getFileExtension(): string
{
return $this->file->getExtension();
}
}

View File

@@ -134,6 +134,25 @@ class InvoiceTemplate
*/
private $paymentDetails;
/**
* Used when rendering HTML templates.
*
* @var bool
*
* @ORM\Column(name="decimal_duration", type="boolean", nullable=false, options={"default": false})
* @Assert\NotNull()
*/
private $decimalDuration = false;
/**
* Used when rendering HTML templates.
*
* @var string
*
* @ORM\Column(name="language", type="string", length=6, nullable=true)
*/
private $language;
public function getId(): ?int
{
return $this->id;
@@ -295,6 +314,30 @@ class InvoiceTemplate
return $this;
}
public function isDecimalDuration(): bool
{
return $this->decimalDuration;
}
public function setDecimalDuration(bool $decimalDuration): InvoiceTemplate
{
$this->decimalDuration = $decimalDuration;
return $this;
}
public function getLanguage(): ?string
{
return $this->language;
}
public function setLanguage(?string $language): InvoiceTemplate
{
$this->language = $language;
return $this;
}
/**
* @return string
*/

View File

@@ -13,6 +13,8 @@ use App\Entity\InvoiceTemplate;
use App\Form\Type\InvoiceCalculatorType;
use App\Form\Type\InvoiceNumberGeneratorType;
use App\Form\Type\InvoiceRendererType;
use App\Form\Type\LanguageType;
use App\Form\Type\YesNoType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
@@ -70,6 +72,12 @@ class InvoiceTemplateForm extends AbstractType
->add('renderer', InvoiceRendererType::class, [])
->add('calculator', InvoiceCalculatorType::class, [])
->add('numberGenerator', InvoiceNumberGeneratorType::class, [])
->add('language', LanguageType::class, [
'required' => false,
])
->add('decimalDuration', YesNoType::class, [
'label' => 'label.decimalDuration'
])
;
}

View File

@@ -0,0 +1,135 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Invoice\Hydrator;
use App\Invoice\InvoiceItem;
use App\Invoice\InvoiceItemHydrator;
use App\Invoice\InvoiceModel;
class InvoiceItemDefaultHydrator implements InvoiceItemHydrator
{
/**
* @var InvoiceModel
*/
private $model;
public function setInvoiceModel(InvoiceModel $model)
{
$this->model = $model;
}
public function hydrate(InvoiceItem $item): array
{
$formatter = $this->model->getFormatter();
$rate = $item->getRate();
$appliedRate = $item->getHourlyRate();
$amount = $formatter->getFormattedDuration($item->getDuration());
$description = $item->getDescription();
if ($item->isFixedRate()) {
$appliedRate = $item->getFixedRate();
$amount = $item->getAmount();
}
$activity = $item->getActivity();
$project = $item->getProject();
$customer = $project->getCustomer();
$currency = $customer->getCurrency();
$user = $item->getUser();
$begin = $item->getBegin();
$end = $item->getEnd();
if (empty($description) && null !== $activity) {
$description = $activity->getName();
}
// this should never happen!
if (empty($appliedRate)) {
$appliedRate = 0;
}
$values = [
'entry.row' => '',
'entry.description' => $description,
'entry.amount' => $amount,
'entry.type' => $item->getType(),
'entry.category' => $item->getCategory(),
'entry.rate' => $formatter->getFormattedMoney($appliedRate, $currency),
'entry.rate_nc' => $formatter->getFormattedMoney($appliedRate, null),
'entry.rate_plain' => $appliedRate,
'entry.total' => $formatter->getFormattedMoney($rate, $currency),
'entry.total_nc' => $formatter->getFormattedMoney($rate, null),
'entry.total_plain' => $rate,
'entry.currency' => $currency,
'entry.duration' => $item->getDuration(),
'entry.duration_decimal' => $formatter->getFormattedDecimalDuration($item->getDuration()),
'entry.duration_minutes' => number_format($item->getDuration() / 60),
'entry.begin' => $formatter->getFormattedDateTime($begin),
'entry.begin_time' => $formatter->getFormattedTime($begin),
'entry.begin_timestamp' => $begin->getTimestamp(),
'entry.end' => $formatter->getFormattedDateTime($end),
'entry.end_time' => $formatter->getFormattedTime($end),
'entry.end_timestamp' => $end->getTimestamp(),
'entry.date' => $formatter->getFormattedDateTime($begin),
'entry.user_id' => $user->getId(),
'entry.user_name' => $user->getUsername(),
'entry.user_title' => $user->getTitle(),
'entry.user_alias' => $user->getAlias(),
];
if (null !== $activity) {
$values = array_merge($values, [
'entry.activity' => $activity->getName(),
'entry.activity_id' => $activity->getId(),
]);
foreach ($activity->getVisibleMetaFields() as $metaField) {
$values = array_merge($values, [
'entry.activity.meta.' . $metaField->getName() => $metaField->getValue(),
]);
}
}
if (null !== $project) {
$values = array_merge($values, [
'entry.project' => $project->getName(),
'entry.project_id' => $project->getId(),
]);
foreach ($project->getVisibleMetaFields() as $metaField) {
$values = array_merge($values, [
'entry.project.meta.' . $metaField->getName() => $metaField->getValue(),
]);
}
}
if (null !== $customer) {
$values = array_merge($values, [
'entry.customer' => $customer->getName(),
'entry.customer_id' => $customer->getId(),
]);
foreach ($customer->getVisibleMetaFields() as $metaField) {
$values = array_merge($values, [
'entry.customer.meta.' . $metaField->getName() => $metaField->getValue(),
]);
}
}
foreach ($item->getAdditionalFields() as $name => $value) {
$values = array_merge($values, [
'entry.meta.' . $name => $value,
]);
}
return $values;
}
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Invoice\Hydrator;
use App\Invoice\InvoiceModel;
use App\Invoice\InvoiceModelHydrator;
class InvoiceModelActivityHydrator implements InvoiceModelHydrator
{
public function hydrate(InvoiceModel $model): array
{
$activity = $model->getQuery()->getActivity();
if (null === $activity) {
return [];
}
$formatter = $model->getFormatter();
$currency = $model->getCurrency();
$values = [
'activity.id' => $activity->getId(),
'activity.name' => $activity->getName(),
'activity.comment' => $activity->getComment(),
'activity.fixed_rate' => $formatter->getFormattedMoney($activity->getFixedRate(), $currency),
'activity.fixed_rate_nc' => $formatter->getFormattedMoney($activity->getFixedRate(), null),
'activity.fixed_rate_plain' => $activity->getFixedRate(),
'activity.hourly_rate' => $formatter->getFormattedMoney($activity->getHourlyRate(), $currency),
'activity.hourly_rate_nc' => $formatter->getFormattedMoney($activity->getHourlyRate(), null),
'activity.hourly_rate_plain' => $activity->getHourlyRate(),
];
foreach ($activity->getVisibleMetaFields() as $metaField) {
$values = array_merge($values, [
'activity.meta.' . $metaField->getName() => $metaField->getValue(),
]);
}
return $values;
}
}

View File

@@ -0,0 +1,55 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Invoice\Hydrator;
use App\Invoice\InvoiceModel;
use App\Invoice\InvoiceModelHydrator;
class InvoiceModelCustomerHydrator implements InvoiceModelHydrator
{
public function hydrate(InvoiceModel $model): array
{
$customer = $model->getCustomer();
if (null === $customer) {
return [];
}
$formatter = $model->getFormatter();
$currency = $model->getCurrency();
$values = [
'customer.id' => $customer->getId(),
'customer.address' => $customer->getAddress(),
'customer.name' => $customer->getName(),
'customer.contact' => $customer->getContact(),
'customer.company' => $customer->getCompany(),
'customer.vat' => $customer->getVatId(),
'customer.number' => $customer->getNumber(),
'customer.country' => $customer->getCountry(),
'customer.homepage' => $customer->getHomepage(),
'customer.comment' => $customer->getComment(),
'customer.fixed_rate' => $formatter->getFormattedMoney($customer->getFixedRate(), $currency),
'customer.fixed_rate_nc' => $formatter->getFormattedMoney($customer->getFixedRate(), null),
'customer.fixed_rate_plain' => $customer->getFixedRate(),
'customer.hourly_rate' => $formatter->getFormattedMoney($customer->getHourlyRate(), $currency),
'customer.hourly_rate_nc' => $formatter->getFormattedMoney($customer->getHourlyRate(), null),
'customer.hourly_rate_plain' => $customer->getHourlyRate(),
];
foreach ($customer->getVisibleMetaFields() as $metaField) {
$values = array_merge($values, [
'customer.meta.' . $metaField->getName() => $metaField->getValue(),
]);
}
return $values;
}
}

View File

@@ -0,0 +1,64 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Invoice\Hydrator;
use App\Invoice\InvoiceModel;
use App\Invoice\InvoiceModelHydrator;
class InvoiceModelDefaultHydrator implements InvoiceModelHydrator
{
public function hydrate(InvoiceModel $model): array
{
$currency = $model->getCurrency();
$tax = $model->getCalculator()->getTax();
$total = $model->getCalculator()->getTotal();
$subtotal = $model->getCalculator()->getSubtotal();
$formatter = $model->getFormatter();
$values = [
'invoice.due_date' => $formatter->getFormattedDateTime($model->getDueDate()),
'invoice.date' => $formatter->getFormattedDateTime($model->getInvoiceDate()),
'invoice.number' => $model->getNumberGenerator()->getInvoiceNumber(),
'invoice.currency' => $currency,
'invoice.currency_symbol' => $formatter->getCurrencySymbol($currency),
'invoice.vat' => $model->getCalculator()->getVat(),
'invoice.tax' => $formatter->getFormattedMoney($tax, $currency),
'invoice.tax_nc' => $formatter->getFormattedMoney($tax, null),
'invoice.tax_plain' => $tax,
'invoice.total_time' => $formatter->getFormattedDuration($model->getCalculator()->getTimeWorked()),
'invoice.duration_decimal' => $formatter->getFormattedDecimalDuration($model->getCalculator()->getTimeWorked()),
'invoice.total' => $formatter->getFormattedMoney($total, $currency),
'invoice.total_nc' => $formatter->getFormattedMoney($total, null),
'invoice.total_plain' => $total,
'invoice.subtotal' => $formatter->getFormattedMoney($subtotal, $currency),
'invoice.subtotal_nc' => $formatter->getFormattedMoney($subtotal, null),
'invoice.subtotal_plain' => $subtotal,
'template.name' => $model->getTemplate()->getName(),
'template.company' => $model->getTemplate()->getCompany(),
'template.address' => $model->getTemplate()->getAddress(),
'template.title' => $model->getTemplate()->getTitle(),
'template.payment_terms' => $model->getTemplate()->getPaymentTerms(),
'template.due_days' => $model->getTemplate()->getDueDays(),
'template.vat_id' => $model->getTemplate()->getVatId(),
'template.contact' => $model->getTemplate()->getContact(),
'template.payment_details' => $model->getTemplate()->getPaymentDetails(),
'query.begin' => $formatter->getFormattedDateTime($model->getQuery()->getBegin()),
'query.day' => $model->getQuery()->getBegin()->format('d'),
'query.end' => $formatter->getFormattedDateTime($model->getQuery()->getEnd()),
'query.month' => $formatter->getFormattedMonthName($model->getQuery()->getBegin()),
'query.month_number' => $model->getQuery()->getBegin()->format('m'),
'query.year' => $model->getQuery()->getBegin()->format('Y'),
];
return $values;
}
}

View File

@@ -0,0 +1,58 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Invoice\Hydrator;
use App\Invoice\InvoiceModel;
use App\Invoice\InvoiceModelHydrator;
class InvoiceModelProjectHydrator implements InvoiceModelHydrator
{
public function hydrate(InvoiceModel $model): array
{
$project = $model->getQuery()->getProject();
if (null === $project) {
return [];
}
$formatter = $model->getFormatter();
$currency = $model->getCurrency();
$values = [
'project.id' => $project->getId(),
'project.name' => $project->getName(),
'project.comment' => $project->getComment(),
'project.order_number' => $project->getOrderNumber(),
'project.start_date' => null !== $project->getStart() ? $formatter->getFormattedDateTime($project->getStart()) : '',
'project.end_date' => null !== $project->getEnd() ? $formatter->getFormattedDateTime($project->getEnd()) : '',
'project.order_date' => null !== $project->getOrderDate() ? $formatter->getFormattedDateTime($project->getOrderDate()) : '',
'project.fixed_rate' => $formatter->getFormattedMoney($project->getFixedRate(), $currency),
'project.fixed_rate_nc' => $formatter->getFormattedMoney($project->getFixedRate(), null),
'project.fixed_rate_plain' => $project->getFixedRate(),
'project.hourly_rate' => $formatter->getFormattedMoney($project->getHourlyRate(), $currency),
'project.hourly_rate_nc' => $formatter->getFormattedMoney($project->getHourlyRate(), null),
'project.hourly_rate_plain' => $project->getHourlyRate(),
'project.budget_money' => $formatter->getFormattedMoney($project->getBudget(), $currency),
'project.budget_money_nc' => $formatter->getFormattedMoney($project->getBudget(), null),
'project.budget_money_plain' => $project->getBudget(),
'project.budget_time' => $project->getTimeBudget(),
'project.budget_time_decimal' => $formatter->getFormattedDecimalDuration($project->getTimeBudget()),
'project.budget_time_minutes' => number_format($project->getTimeBudget() / 60),
];
foreach ($project->getVisibleMetaFields() as $metaField) {
$values = array_merge($values, [
'project.meta.' . $metaField->getName() => $metaField->getValue(),
]);
}
return $values;
}
}

View File

@@ -0,0 +1,42 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Invoice\Hydrator;
use App\Entity\UserPreference;
use App\Invoice\InvoiceModel;
use App\Invoice\InvoiceModelHydrator;
class InvoiceModelUserHydrator implements InvoiceModelHydrator
{
public function hydrate(InvoiceModel $model): array
{
$user = $model->getUser();
if (null === $user) {
return [];
}
$values = [
'user.name' => $user->getUsername(),
'user.email' => $user->getEmail(),
'user.title' => $user->getTitle(),
'user.alias' => $user->getAlias(),
];
/** @var UserPreference $metaField */
foreach ($user->getPreferences() as $metaField) {
$values = array_merge($values, [
'user.meta.' . $metaField->getName() => $metaField->getValue(),
]);
}
return $values;
}
}

View File

@@ -0,0 +1,17 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Invoice;
interface InvoiceItemHydrator
{
public function setInvoiceModel(InvoiceModel $model);
public function hydrate(InvoiceItem $item): array;
}

View File

@@ -12,7 +12,12 @@ namespace App\Invoice;
use App\Entity\Customer;
use App\Entity\InvoiceTemplate;
use App\Entity\User;
use App\Entity\UserPreference;
use App\Invoice\Hydrator\InvoiceItemDefaultHydrator;
use App\Invoice\Hydrator\InvoiceModelActivityHydrator;
use App\Invoice\Hydrator\InvoiceModelCustomerHydrator;
use App\Invoice\Hydrator\InvoiceModelDefaultHydrator;
use App\Invoice\Hydrator\InvoiceModelProjectHydrator;
use App\Invoice\Hydrator\InvoiceModelUserHydrator;
use App\Repository\Query\InvoiceQuery;
/**
@@ -57,11 +62,25 @@ final class InvoiceModel
* @var InvoiceFormatter
*/
private $formatter;
/**
* @var InvoiceModelHydrator[]
*/
private $modelHydrator = [];
/**
* @var InvoiceItemHydrator[]
*/
private $itemHydrator = [];
public function __construct(InvoiceFormatter $formatter)
{
$this->invoiceDate = new \DateTime();
$this->formatter = $formatter;
$this->addModelHydrator(new InvoiceModelDefaultHydrator());
$this->addModelHydrator(new InvoiceModelCustomerHydrator());
$this->addModelHydrator(new InvoiceModelProjectHydrator());
$this->addModelHydrator(new InvoiceModelActivityHydrator());
$this->addModelHydrator(new InvoiceModelUserHydrator());
$this->addItemHydrator(new InvoiceItemDefaultHydrator());
}
/**
@@ -118,6 +137,22 @@ final class InvoiceModel
return $this;
}
public function addModelHydrator(InvoiceModelHydrator $hydrator): InvoiceModel
{
$this->modelHydrator[] = $hydrator;
return $this;
}
public function addItemHydrator(InvoiceItemHydrator $hydrator): InvoiceModel
{
$hydrator->setInvoiceModel($this);
$this->itemHydrator[] = $hydrator;
return $this;
}
public function getTemplate(): ?InvoiceTemplate
{
return $this->template;
@@ -136,7 +171,7 @@ final class InvoiceModel
}
/**
* @param Customer $customer
* @param Customer|null $customer
* @return InvoiceModel
*/
public function setCustomer($customer): InvoiceModel
@@ -223,146 +258,10 @@ final class InvoiceModel
public function toArray(): array
{
$model = $this;
$customer = $model->getCustomer();
$project = $model->getQuery()->getProject();
$activity = $model->getQuery()->getActivity();
$currency = $this->getCurrency();
$tax = $model->getCalculator()->getTax();
$total = $model->getCalculator()->getTotal();
$subtotal = $model->getCalculator()->getSubtotal();
$formatter = $model->getFormatter();
$values = [];
$values = [
'invoice.due_date' => $formatter->getFormattedDateTime($model->getDueDate()),
'invoice.date' => $formatter->getFormattedDateTime($model->getInvoiceDate()),
'invoice.number' => $model->getNumberGenerator()->getInvoiceNumber(),
'invoice.currency' => $currency,
'invoice.currency_symbol' => $formatter->getCurrencySymbol($currency),
'invoice.vat' => $model->getCalculator()->getVat(),
'invoice.tax' => $formatter->getFormattedMoney($tax, $currency),
'invoice.tax_nc' => $formatter->getFormattedMoney($tax, null),
'invoice.tax_plain' => $tax,
'invoice.total_time' => $formatter->getFormattedDuration($model->getCalculator()->getTimeWorked()),
'invoice.duration_decimal' => $formatter->getFormattedDecimalDuration($model->getCalculator()->getTimeWorked()),
'invoice.total' => $formatter->getFormattedMoney($total, $currency),
'invoice.total_nc' => $formatter->getFormattedMoney($total, null),
'invoice.total_plain' => $total,
'invoice.subtotal' => $formatter->getFormattedMoney($subtotal, $currency),
'invoice.subtotal_nc' => $formatter->getFormattedMoney($subtotal, null),
'invoice.subtotal_plain' => $subtotal,
'template.name' => $model->getTemplate()->getName(),
'template.company' => $model->getTemplate()->getCompany(),
'template.address' => $model->getTemplate()->getAddress(),
'template.title' => $model->getTemplate()->getTitle(),
'template.payment_terms' => $model->getTemplate()->getPaymentTerms(),
'template.due_days' => $model->getTemplate()->getDueDays(),
'template.vat_id' => $model->getTemplate()->getVatId(),
'template.contact' => $model->getTemplate()->getContact(),
'template.payment_details' => $model->getTemplate()->getPaymentDetails(),
'query.begin' => $formatter->getFormattedDateTime($model->getQuery()->getBegin()),
'query.day' => $model->getQuery()->getBegin()->format('d'),
'query.end' => $formatter->getFormattedDateTime($model->getQuery()->getEnd()),
'query.month' => $formatter->getFormattedMonthName($model->getQuery()->getBegin()),
'query.month_number' => $model->getQuery()->getBegin()->format('m'),
'query.year' => $model->getQuery()->getBegin()->format('Y'),
];
if (null !== $model->getUser()) {
$user = $model->getUser();
/** @var UserPreference $metaField */
foreach ($user->getPreferences() as $metaField) {
$values = array_merge($values, [
'user.meta.' . $metaField->getName() => $metaField->getValue(),
]);
}
$values = array_merge($values, [
'user.name' => $user->getUsername(),
'user.email' => $user->getEmail(),
'user.title' => $user->getTitle(),
'user.alias' => $user->getAlias(),
]);
}
if (null !== $activity) {
$values = array_merge($values, [
'activity.id' => $activity->getId(),
'activity.name' => $activity->getName(),
'activity.comment' => $activity->getComment(),
'activity.fixed_rate' => $formatter->getFormattedMoney($activity->getFixedRate(), $currency),
'activity.fixed_rate_nc' => $formatter->getFormattedMoney($activity->getFixedRate(), null),
'activity.fixed_rate_plain' => $activity->getFixedRate(),
'activity.hourly_rate' => $formatter->getFormattedMoney($activity->getHourlyRate(), $currency),
'activity.hourly_rate_nc' => $formatter->getFormattedMoney($activity->getHourlyRate(), null),
'activity.hourly_rate_plain' => $activity->getHourlyRate(),
]);
foreach ($activity->getVisibleMetaFields() as $metaField) {
$values = array_merge($values, [
'activity.meta.' . $metaField->getName() => $metaField->getValue(),
]);
}
}
if (null !== $project) {
$values = array_merge($values, [
'project.id' => $project->getId(),
'project.name' => $project->getName(),
'project.comment' => $project->getComment(),
'project.order_number' => $project->getOrderNumber(),
'project.start_date' => null !== $project->getStart() ? $formatter->getFormattedDateTime($project->getStart()) : '',
'project.end_date' => null !== $project->getEnd() ? $formatter->getFormattedDateTime($project->getEnd()) : '',
'project.order_date' => null !== $project->getOrderDate() ? $formatter->getFormattedDateTime($project->getOrderDate()) : '',
'project.fixed_rate' => $formatter->getFormattedMoney($project->getFixedRate(), $currency),
'project.fixed_rate_nc' => $formatter->getFormattedMoney($project->getFixedRate(), null),
'project.fixed_rate_plain' => $project->getFixedRate(),
'project.hourly_rate' => $formatter->getFormattedMoney($project->getHourlyRate(), $currency),
'project.hourly_rate_nc' => $formatter->getFormattedMoney($project->getHourlyRate(), null),
'project.hourly_rate_plain' => $project->getHourlyRate(),
'project.budget_money' => $formatter->getFormattedMoney($project->getBudget(), $currency),
'project.budget_money_nc' => $formatter->getFormattedMoney($project->getBudget(), null),
'project.budget_money_plain' => $project->getBudget(),
'project.budget_time' => $project->getTimeBudget(),
'project.budget_time_decimal' => $formatter->getFormattedDecimalDuration($project->getTimeBudget()),
'project.budget_time_minutes' => number_format($project->getTimeBudget() / 60),
]);
foreach ($project->getVisibleMetaFields() as $metaField) {
$values = array_merge($values, [
'project.meta.' . $metaField->getName() => $metaField->getValue(),
]);
}
}
if (null !== $customer) {
$values = array_merge($values, [
'customer.id' => $customer->getId(),
'customer.address' => $customer->getAddress(),
'customer.name' => $customer->getName(),
'customer.contact' => $customer->getContact(),
'customer.company' => $customer->getCompany(),
'customer.vat' => $customer->getVatId(),
'customer.number' => $customer->getNumber(),
'customer.country' => $customer->getCountry(),
'customer.homepage' => $customer->getHomepage(),
'customer.comment' => $customer->getComment(),
'customer.fixed_rate' => $formatter->getFormattedMoney($customer->getFixedRate(), $currency),
'customer.fixed_rate_nc' => $formatter->getFormattedMoney($customer->getFixedRate(), null),
'customer.fixed_rate_plain' => $customer->getFixedRate(),
'customer.hourly_rate' => $formatter->getFormattedMoney($customer->getHourlyRate(), $currency),
'customer.hourly_rate_nc' => $formatter->getFormattedMoney($customer->getHourlyRate(), null),
'customer.hourly_rate_plain' => $customer->getHourlyRate(),
]);
foreach ($customer->getVisibleMetaFields() as $metaField) {
$values = array_merge($values, [
'customer.meta.' . $metaField->getName() => $metaField->getValue(),
]);
}
foreach ($this->modelHydrator as $hydrator) {
$values = array_merge($values, $hydrator->hydrate($this));
}
return $values;
@@ -370,107 +269,10 @@ final class InvoiceModel
public function itemToArray(InvoiceItem $invoiceItem): array
{
$formatter = $this->getFormatter();
$values = [];
$rate = $invoiceItem->getRate();
$appliedRate = $invoiceItem->getHourlyRate();
$amount = $formatter->getFormattedDuration($invoiceItem->getDuration());
$description = $invoiceItem->getDescription();
if ($invoiceItem->isFixedRate()) {
$appliedRate = $invoiceItem->getFixedRate();
$amount = $invoiceItem->getAmount();
}
$activity = $invoiceItem->getActivity();
$project = $invoiceItem->getProject();
$customer = $project->getCustomer();
$currency = $customer->getCurrency();
$user = $invoiceItem->getUser();
$begin = $invoiceItem->getBegin();
$end = $invoiceItem->getEnd();
if (empty($description) && null !== $activity) {
$description = $activity->getName();
}
// this should never happen!
if (empty($appliedRate)) {
$appliedRate = 0;
}
$values = [
'entry.row' => '',
'entry.description' => $description,
'entry.amount' => $amount,
'entry.type' => $invoiceItem->getType(),
'entry.category' => $invoiceItem->getCategory(),
'entry.rate' => $formatter->getFormattedMoney($appliedRate, $currency),
'entry.rate_nc' => $formatter->getFormattedMoney($appliedRate, null),
'entry.rate_plain' => $appliedRate,
'entry.total' => $formatter->getFormattedMoney($rate, $currency),
'entry.total_nc' => $formatter->getFormattedMoney($rate, null),
'entry.total_plain' => $rate,
'entry.currency' => $currency,
'entry.duration' => $invoiceItem->getDuration(),
'entry.duration_decimal' => $formatter->getFormattedDecimalDuration($invoiceItem->getDuration()),
'entry.duration_minutes' => number_format($invoiceItem->getDuration() / 60),
'entry.begin' => $formatter->getFormattedDateTime($begin),
'entry.begin_time' => $formatter->getFormattedTime($begin),
'entry.begin_timestamp' => $begin->getTimestamp(),
'entry.end' => $formatter->getFormattedDateTime($end),
'entry.end_time' => $formatter->getFormattedTime($end),
'entry.end_timestamp' => $end->getTimestamp(),
'entry.date' => $formatter->getFormattedDateTime($begin),
'entry.user_id' => $user->getId(),
'entry.user_name' => $user->getUsername(),
'entry.user_title' => $user->getTitle(),
'entry.user_alias' => $user->getAlias(),
];
if (null !== $activity) {
$values = array_merge($values, [
'entry.activity' => $activity->getName(),
'entry.activity_id' => $activity->getId(),
]);
foreach ($activity->getVisibleMetaFields() as $metaField) {
$values = array_merge($values, [
'entry.activity.meta.' . $metaField->getName() => $metaField->getValue(),
]);
}
}
if (null !== $project) {
$values = array_merge($values, [
'entry.project' => $project->getName(),
'entry.project_id' => $project->getId(),
]);
foreach ($project->getVisibleMetaFields() as $metaField) {
$values = array_merge($values, [
'entry.project.meta.' . $metaField->getName() => $metaField->getValue(),
]);
}
}
if (null !== $customer) {
$values = array_merge($values, [
'entry.customer' => $customer->getName(),
'entry.customer_id' => $customer->getId(),
]);
foreach ($customer->getVisibleMetaFields() as $metaField) {
$values = array_merge($values, [
'entry.customer.meta.' . $metaField->getName() => $metaField->getValue(),
]);
}
}
foreach ($invoiceItem->getAdditionalFields() as $name => $value) {
$values = array_merge($values, [
'entry.meta.' . $name => $value,
]);
foreach ($this->itemHydrator as $hydrator) {
$values = array_merge($values, $hydrator->hydrate($invoiceItem));
}
return $values;

View File

@@ -0,0 +1,15 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Invoice;
interface InvoiceModelHydrator
{
public function hydrate(InvoiceModel $model): array;
}

View File

@@ -10,8 +10,10 @@
namespace App\Invoice\Renderer;
use App\Entity\InvoiceDocument;
use App\Invoice\InvoiceModel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\String\UnicodeString;
/**
* @internal
@@ -43,6 +45,23 @@ abstract class AbstractRenderer
return false;
}
protected function buildFilename(InvoiceModel $model): string
{
$filename = $model->getNumberGenerator()->getInvoiceNumber();
$company = $model->getCustomer()->getCompany();
if (empty($company)) {
$company = $model->getCustomer()->getName();
}
if (!empty($company)) {
$company = new UnicodeString($company);
$filename .= '-' . $company->snake();
}
return $filename;
}
/**
* @param mixed $file
* @param string $filename

View File

@@ -99,8 +99,9 @@ abstract class AbstractSpreadsheetRenderer extends AbstractRenderer
}
$filename = $this->saveSpreadsheet($spreadsheet);
$userFilename = $this->buildFilename($model) . '.' . $document->getFileExtension();
return $this->getFileResponse($filename, basename($document->getFilename()));
return $this->getFileResponse($filename, $userFilename);
}
/**

View File

@@ -30,8 +30,6 @@ final class DocxRenderer extends AbstractRenderer implements RendererInterface
{
Settings::setOutputEscapingEnabled(false);
$filename = basename($document->getFilename());
$xmlEscaper = new Xml();
$template = new TemplateProcessor($document->getFilename());
@@ -70,6 +68,8 @@ final class DocxRenderer extends AbstractRenderer implements RendererInterface
clearstatcache(true, $cacheFile);
$filename = $this->buildFilename($model) . '.' . $document->getFileExtension();
return $this->getFileResponse(new Stream($cacheFile), $filename);
}

View File

@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DoctrineMigrations;
use App\Doctrine\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
final class Version20200204124425 extends AbstractMigration
{
public function getDescription(): string
{
return 'Adds language and decimal_duration column to invoice template table';
}
protected function isSupportingForeignKeys(): bool
{
return false;
}
public function isTransactional(): bool
{
if ($this->isPlatformSqlite()) {
// does fail if we use transactions, as tables are re-created and foreign keys would fail
return false;
}
return true;
}
public function up(Schema $schema): void
{
$invoiceTemplates = $schema->getTable('kimai2_invoice_templates');
$invoiceTemplates->addColumn('decimal_duration', 'boolean', ['notnull' => true, 'default' => false]);
$invoiceTemplates->addColumn('language', 'string', ['notnull' => false, 'length' => 6]);
}
public function down(Schema $schema): void
{
$invoiceTemplates = $schema->getTable('kimai2_invoice_templates');
$invoiceTemplates->dropColumn('language');
$invoiceTemplates->dropColumn('decimal_duration');
}
}

View File

@@ -102,17 +102,18 @@ class Extensions extends AbstractExtension
* Transforms seconds into a duration string.
*
* @param int|Timesheet $duration
* @param string $format
* @param bool $decimal
* @return string
*/
public function duration($duration, $format = null)
public function duration($duration, $decimal = false)
{
$duration = $this->getSecondsForDuration($duration);
if (null === $format) {
$format = $this->localeSettings->getDurationFormat();
if ($decimal) {
return $this->durationDecimal($duration);
}
$duration = $this->getSecondsForDuration($duration);
$format = $this->localeSettings->getDurationFormat();
return $this->formatDuration($duration, $format);
}