new invoice template variables for budgets (#3005)

This commit is contained in:
Kevin Papst
2021-12-08 22:15:07 +01:00
committed by GitHub
parent 82525f382d
commit 22ce6b047a
31 changed files with 389 additions and 188 deletions

View File

@@ -9,12 +9,20 @@
namespace App\Invoice\Hydrator;
use App\Activity\ActivityStatisticService;
use App\Entity\Activity;
use App\Invoice\InvoiceModel;
use App\Invoice\InvoiceModelHydrator;
class InvoiceModelActivityHydrator implements InvoiceModelHydrator
{
private $activityStatistic;
public function __construct(ActivityStatisticService $activityStatistic)
{
$this->activityStatistic = $activityStatistic;
}
public function hydrate(InvoiceModel $model): array
{
if (!$model->getQuery()->hasActivities()) {
@@ -24,19 +32,23 @@ class InvoiceModelActivityHydrator implements InvoiceModelHydrator
$values = [];
$i = 0;
if (\count($model->getQuery()->getActivities()) === 1) {
$values['activity'] = $model->getQuery()->getActivities()[0]->getName();
}
foreach ($model->getQuery()->getActivities() as $activity) {
$prefix = '';
if ($i > 0) {
$prefix = $i . '.';
}
$values = array_merge($values, $this->getValuesFromActivity($activity, $prefix));
$values = array_merge($values, $this->getValuesFromActivity($model, $activity, $prefix));
$i++;
}
return $values;
}
private function getValuesFromActivity(Activity $activity, string $prefix): array
private function getValuesFromActivity(InvoiceModel $model, Activity $activity, string $prefix): array
{
$prefix = 'activity.' . $prefix;
@@ -46,6 +58,23 @@ class InvoiceModelActivityHydrator implements InvoiceModelHydrator
$prefix . 'comment' => $activity->getComment(),
];
$statistic = $this->activityStatistic->getBudgetStatisticModel($activity, $model->getQuery()->getEnd());
$formatter = $model->getFormatter();
$currency = $model->getCurrency();
if ($model->getTemplate()->isDecimalDuration()) {
$budgetOpenDuration = $formatter->getFormattedDecimalDuration($statistic->getTimeBudgetOpen());
} else {
$budgetOpenDuration = $formatter->getFormattedDuration($statistic->getTimeBudgetOpen());
}
$values = array_merge($values, [
$prefix . 'budget_open' => $formatter->getFormattedMoney($statistic->getBudgetOpen(), $currency),
$prefix . 'budget_open_plain' => $statistic->getBudgetOpen(),
$prefix . 'time_budget_open' => $budgetOpenDuration,
$prefix . 'time_budget_open_plain' => $statistic->getTimeBudgetOpen(),
]);
foreach ($activity->getVisibleMetaFields() as $metaField) {
$values = array_merge($values, [
$prefix . 'meta.' . $metaField->getName() => $metaField->getValue(),

View File

@@ -9,11 +9,19 @@
namespace App\Invoice\Hydrator;
use App\Customer\CustomerStatisticService;
use App\Invoice\InvoiceModel;
use App\Invoice\InvoiceModelHydrator;
class InvoiceModelCustomerHydrator implements InvoiceModelHydrator
{
private $customerStatistic;
public function __construct(CustomerStatisticService $customerStatistic)
{
$this->customerStatistic = $customerStatistic;
}
public function hydrate(InvoiceModel $model): array
{
$customer = $model->getCustomer();
@@ -22,9 +30,6 @@ class InvoiceModelCustomerHydrator implements InvoiceModelHydrator
return [];
}
$formatter = $model->getFormatter();
$currency = $model->getCurrency();
$values = [
'customer.id' => $customer->getId(),
'customer.address' => $customer->getAddress(),
@@ -40,12 +45,25 @@ class InvoiceModelCustomerHydrator implements InvoiceModelHydrator
'customer.fax' => $customer->getFax(),
'customer.phone' => $customer->getPhone(),
'customer.mobile' => $customer->getMobile(),
// budget
// remaining budget?
// time-budget
// remaining time-budget?
];
$statistic = $this->customerStatistic->getBudgetStatisticModel($customer, $model->getQuery()->getEnd());
$currency = $model->getCurrency();
$formatter = $model->getFormatter();
if ($model->getTemplate()->isDecimalDuration()) {
$budgetOpenDuration = $formatter->getFormattedDecimalDuration($statistic->getTimeBudgetOpen());
} else {
$budgetOpenDuration = $formatter->getFormattedDuration($statistic->getTimeBudgetOpen());
}
$values = array_merge($values, [
'customer.budget_open' => $formatter->getFormattedMoney($statistic->getBudgetOpen(), $currency),
'customer.budget_open_plain' => $statistic->getBudgetOpen(),
'customer.time_budget_open' => $budgetOpenDuration,
'customer.time_budget_open_plain' => $statistic->getTimeBudgetOpen(),
]);
foreach ($customer->getMetaFields() as $metaField) {
$values = array_merge($values, [
'customer.meta.' . $metaField->getName() => $metaField->getValue(),

View File

@@ -10,40 +10,51 @@
namespace App\Invoice\Hydrator;
use App\Entity\Project;
use App\Invoice\InvoiceFormatter;
use App\Invoice\InvoiceModel;
use App\Invoice\InvoiceModelHydrator;
use App\Project\ProjectStatisticService;
class InvoiceModelProjectHydrator implements InvoiceModelHydrator
{
private $projectStatistic;
public function __construct(ProjectStatisticService $projectStatistic)
{
$this->projectStatistic = $projectStatistic;
}
public function hydrate(InvoiceModel $model): array
{
if (!$model->getQuery()->hasProjects()) {
return [];
}
$formatter = $model->getFormatter();
$currency = $model->getCurrency();
$values = [];
$i = 0;
if (\count($model->getQuery()->getProjects()) === 1) {
$values['project'] = $model->getQuery()->getProjects()[0]->getName();
}
foreach ($model->getQuery()->getProjects() as $project) {
$prefix = '';
if ($i > 0) {
$prefix = $i . '.';
}
$values = array_merge($values, $this->getValuesFromProject($project, $formatter, $currency, $prefix));
$values = array_merge($values, $this->getValuesFromProject($model, $project, $prefix));
$i++;
}
return $values;
}
private function getValuesFromProject(Project $project, InvoiceFormatter $formatter, string $currency, string $prefix): array
private function getValuesFromProject(InvoiceModel $model, Project $project, string $prefix): array
{
$prefix = 'project.' . $prefix;
$formatter = $model->getFormatter();
$currency = $model->getCurrency();
$values = [
$prefix . 'id' => $project->getId(),
$prefix . 'name' => $project->getName(),
@@ -60,6 +71,21 @@ class InvoiceModelProjectHydrator implements InvoiceModelHydrator
$prefix . 'budget_time_minutes' => (int) ($project->getTimeBudget() / 60),
];
$statistic = $this->projectStatistic->getBudgetStatisticModel($project, $model->getQuery()->getEnd());
if ($model->getTemplate()->isDecimalDuration()) {
$budgetOpenDuration = $formatter->getFormattedDecimalDuration($statistic->getTimeBudgetOpen());
} else {
$budgetOpenDuration = $formatter->getFormattedDuration($statistic->getTimeBudgetOpen());
}
$values = array_merge($values, [
$prefix . 'budget_open' => $formatter->getFormattedMoney($statistic->getBudgetOpen(), $currency),
$prefix . 'budget_open_plain' => $statistic->getBudgetOpen(),
$prefix . 'time_budget_open' => $budgetOpenDuration,
$prefix . 'time_budget_open_plain' => $statistic->getTimeBudgetOpen(),
]);
foreach ($project->getVisibleMetaFields() as $metaField) {
$values = array_merge($values, [
$prefix . 'meta.' . $metaField->getName() => $metaField->getValue(),

View File

@@ -9,6 +9,8 @@
namespace App\Invoice;
use App\Activity\ActivityStatisticService;
use App\Customer\CustomerStatisticService;
use App\Entity\Customer;
use App\Entity\InvoiceTemplate;
use App\Entity\User;
@@ -18,6 +20,7 @@ use App\Invoice\Hydrator\InvoiceModelCustomerHydrator;
use App\Invoice\Hydrator\InvoiceModelDefaultHydrator;
use App\Invoice\Hydrator\InvoiceModelProjectHydrator;
use App\Invoice\Hydrator\InvoiceModelUserHydrator;
use App\Project\ProjectStatisticService;
use App\Repository\Query\InvoiceQuery;
/**
@@ -75,14 +78,14 @@ final class InvoiceModel
*/
private $invoiceNumber;
public function __construct(InvoiceFormatter $formatter)
public function __construct(InvoiceFormatter $formatter, CustomerStatisticService $customerStatistic, ProjectStatisticService $projectStatistic, ActivityStatisticService $activityStatistic)
{
$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 InvoiceModelCustomerHydrator($customerStatistic));
$this->addModelHydrator(new InvoiceModelProjectHydrator($projectStatistic));
$this->addModelHydrator(new InvoiceModelActivityHydrator($activityStatistic));
$this->addModelHydrator(new InvoiceModelUserHydrator());
$this->addItemHydrator(new InvoiceItemDefaultHydrator());
}

View File

@@ -0,0 +1,33 @@
<?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;
use App\Activity\ActivityStatisticService;
use App\Customer\CustomerStatisticService;
use App\Project\ProjectStatisticService;
final class InvoiceModelFactory
{
private $customerStatisticService;
private $projectStatisticService;
private $activityStatisticService;
public function __construct(CustomerStatisticService $customerStatistic, ProjectStatisticService $projectStatistic, ActivityStatisticService $activityStatistic)
{
$this->customerStatisticService = $customerStatistic;
$this->projectStatisticService = $projectStatistic;
$this->activityStatisticService = $activityStatistic;
}
public function createModel(InvoiceFormatter $formatter): InvoiceModel
{
return new InvoiceModel($formatter, $this->customerStatisticService, $this->projectStatisticService, $this->activityStatisticService);
}
}

View File

@@ -37,7 +37,10 @@ abstract class AbstractTwigRenderer implements RendererInterface
$previousLocale = $this->changeTwigLocale($this->twig, $model->getTemplate()->getLanguage());
$content = $this->twig->render('@invoice/' . basename($document->getFilename()), [
'model' => $model
// model should not be used in the future, but we can likely not remove it
'model' => $model,
// new since 1.16.7 - templates should only use the pre-generated values
'invoice' => $model->toArray(),
]);
$this->changeTwigLocale($this->twig, $previousLocale);

View File

@@ -46,29 +46,19 @@ final class ServiceInvoice
* @var array InvoiceItemRepositoryInterface[]
*/
private $invoiceItemRepositories = [];
/**
* @var InvoiceDocumentRepository
*/
private $documents;
/**
* @var FileHelper
*/
private $fileHelper;
/**
* @var LanguageFormattings
*/
private $formatter;
/**
* @var InvoiceRepository
*/
private $invoiceRepository;
private $invoiceModelFactory;
public function __construct(InvoiceDocumentRepository $repository, FileHelper $fileHelper, InvoiceRepository $invoiceRepository, LanguageFormattings $formatter)
public function __construct(InvoiceDocumentRepository $repository, FileHelper $fileHelper, InvoiceRepository $invoiceRepository, LanguageFormattings $formatter, InvoiceModelFactory $invoiceModelFactory)
{
$this->documents = $repository;
$this->fileHelper = $fileHelper;
$this->invoiceRepository = $invoiceRepository;
$this->formatter = $formatter;
$this->invoiceModelFactory = $invoiceModelFactory;
}
public function addNumberGenerator(NumberGeneratorInterface $generator): ServiceInvoice
@@ -469,7 +459,9 @@ final class ServiceInvoice
@trigger_error('Using invoice templates without a language is is deprecated and trigger and will throw an exception with 2.0', E_USER_DEPRECATED);
}
$model = new InvoiceModel(new DefaultInvoiceFormatter($this->formatter, $template->getLanguage()));
$formatter = new DefaultInvoiceFormatter($this->formatter, $template->getLanguage());
$model = $this->invoiceModelFactory->createModel($formatter);
$model
->setTemplate($template)
->setInvoiceDate($this->getDateTimeFactory($query)->createDateTime())

View File

@@ -2,124 +2,135 @@
{% extends 'invoice/layout.html.twig' %}
{% block invoice %}
{% set isDecimal = model.template.decimalDuration|default(false) %}
<div class="row">
<div class="col-xs-12">
<h2 class="page-header">
<span contenteditable="true">{{ model.template.title }}</span>
</h2>
{% set isDecimal = model.template.decimalDuration|default(false) %}
<div class="row">
<div class="col-xs-12">
<h2 class="page-header">
<span contenteditable="true">{{ model.template.title }}</span>
</h2>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<table class="table no-border table-condensed">
<tr>
<th>{{ 'invoice.from'|trans }}</th>
<td contenteditable="true">
{% if model.query.user is not empty %}
{{ widgets.username(model.query.user) }}
{% else %}
{{ model.template.company }}
{% endif %}
</td>
</tr>
<tr>
<th>{{ 'label.date'|trans }}</th>
<td contenteditable="true">
{% if model.query.begin|date('m') != model.query.end|date('m') or model.query.begin|date('Y') != model.query.end|date('Y') %}
{{ model.query.begin|date_short }} - {{ model.query.end|date_short }}
{% else %}
{{ model.query.end|month_name }} {{ model.query.end|date('Y') }}
{% endif %}
</td>
</tr>
<tr>
<th>{{ 'label.customer'|trans }}</th>
<td contenteditable="true">
{% if model.customer.number is not empty %}[{{ model.customer.number }}]{% endif %}
{{ model.customer.name }}{% if model.customer.contact is not empty %} / {{ model.customer.contact }}{% endif %}
</td>
</tr>
{% if model.query.project is not empty and model.query.project.orderNumber is not empty %}
<div class="row">
<div class="col-xs-12">
<table class="table no-border table-condensed">
<tr>
<th>{{ 'label.orderNumber'|trans }}</th>
<th>{{ 'invoice.from'|trans }}</th>
<td contenteditable="true">
{{ model.query.project.orderNumber }}
{% if model.query.user is not empty %}
{{ widgets.username(model.query.user) }}
{% else %}
{{ model.template.company }}
{% endif %}
</td>
</tr>
{% endif %}
</table>
</div>
</div>
<div class="row invoice-items">
<div class="col-xs-12 table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>{{ 'label.date'|trans }}</th>
{% if model.query.user is empty %}
<th>{{ 'label.user'|trans }}</th>
{% endif %}
<th>{{ 'label.activity'|trans }}</th>
<th>{{ 'label.hours'|trans }}</th>
</tr>
</thead>
<tbody>
{% for entry in model.calculator.entries %}
<tr>
<td>{{ entry.begin|date_short }}</td>
{% if model.query.user is empty %}
<td>{{ widgets.username(entry.user) }}</td>
{% endif %}
<td contenteditable="true">
{% if entry.description is not empty %}
{{ entry.description|nl2br }}
{% else %}
{% if entry.activity is not null %}{{ entry.activity.name }} / {% endif %}{{ entry.project.name }}
{% endif %}
</td>
<td class="text-nowrap">{{ entry.duration|duration(isDecimal) }}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<th></th>
{% if model.query.user is empty %}
<th></th>
{% endif %}
<th>{{ 'invoice.total_working_time'|trans }}</th>
<th class="text-nowrap">{{ model.calculator.timeWorked|duration(isDecimal) }}</th>
</tr>
</tfoot>
</table>
</div>
</div>
<div class="row">
<div class="col-xs-12">
{% if model.template.paymentTerms is not empty %}
<p class="lead">{{ 'label.payment_terms'|trans }}</p>
<p class="text-muted well well-sm no-shadow" contenteditable="true" style="margin-bottom: 100px">
{{ model.template.paymentTerms|trim|nl2br }}
</p>
{% endif %}
<div class="table-responsive">
<table class="table">
<tbody>
<tr>
<th style="padding-bottom: 60px">{{ 'invoice.signature_user'|trans }}</th>
<th>{{ 'label.date'|trans }}</th>
<td contenteditable="true">
{% if model.query.begin|date('m') != model.query.end|date('m') or model.query.begin|date('Y') != model.query.end|date('Y') %}
{{ model.query.begin|date_short }} - {{ model.query.end|date_short }}
{% else %}
{{ model.query.end|month_name }} {{ model.query.end|date('Y') }}
{% endif %}
</td>
</tr>
<tr>
<th>{{ 'invoice.signature_customer'|trans }}</th>
<th>{{ 'label.customer'|trans }}</th>
<td contenteditable="true">
{% if model.customer.number is not empty %}[{{ model.customer.number }}]{% endif %}
{{ model.customer.name }}{% if model.customer.contact is not empty %} / {{ model.customer.contact }}{% endif %}
</td>
</tr>
</tbody>
{% if model.query.project is not empty %}
<tr>
<th>{{ 'label.project'|trans }}</th>
<td contenteditable="true">
{{ model.query.project.name }}
{% if model.query.project.orderNumber is not empty %}
({{ 'label.orderNumber'|trans }}: {{ model.query.project.orderNumber }})
{% endif %}
</td>
</tr>
{% endif %}
{% if model.query.activity is not empty %}
<tr>
<th>{{ 'label.activity'|trans }}</th>
<td contenteditable="true">
{{ model.query.activity.name }}
</td>
</tr>
{% endif %}
</table>
</div>
</div>
</div>
<div class="row invoice-items">
<div class="col-xs-12 table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>{{ 'label.date'|trans }}</th>
{% if model.query.user is empty %}
<th>{{ 'label.user'|trans }}</th>
{% endif %}
<th>{{ 'label.activity'|trans }}</th>
<th class="text-right">{{ 'label.hours'|trans }}</th>
</tr>
</thead>
<tbody>
{% for entry in model.calculator.entries %}
<tr>
<td>{{ entry.begin|date_short }}</td>
{% if model.query.user is empty %}
<td>{{ widgets.username(entry.user) }}</td>
{% endif %}
<td contenteditable="true">
{% if entry.description is not empty %}
{{ entry.description|nl2br }}
{% else %}
{% if entry.activity is not null %}{{ entry.activity.name }} / {% endif %}{{ entry.project.name }}
{% endif %}
</td>
<td class="text-right text-nowrap">{{ entry.duration|duration(isDecimal) }}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<th></th>
{% if model.query.user is empty %}
<th></th>
{% endif %}
<th>{{ 'invoice.total_working_time'|trans }}</th>
<th class="text-right text-nowrap">{{ model.calculator.timeWorked|duration(isDecimal) }}</th>
</tr>
</tfoot>
</table>
</div>
</div>
<div class="row">
<div class="col-xs-12">
{% if model.template.paymentTerms is not empty %}
<p class="lead">{{ 'label.payment_terms'|trans }}</p>
<p class="text-muted well well-sm no-shadow" contenteditable="true" style="margin-bottom: 100px">
{{ model.template.paymentTerms|trim|nl2br }}
</p>
{% endif %}
<div class="table-responsive">
<table class="table">
<tbody>
<tr>
<th style="padding-bottom: 60px">{{ 'invoice.signature_user'|trans }}</th>
</tr>
<tr>
<th>{{ 'invoice.signature_customer'|trans }}</th>
</tr>
</tbody>
</table>
</div>
</div>
</div>
{% endblock %}

View File

@@ -26,6 +26,7 @@ use App\Invoice\NumberGenerator\DateNumberGenerator;
use App\Repository\InvoiceRepository;
use App\Repository\Query\InvoiceQuery;
use App\Tests\Invoice\DebugFormatter;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
use PHPUnit\Framework\TestCase;
/**
@@ -158,7 +159,7 @@ class InvoiceTest extends TestCase
$query->setBegin(new \DateTime());
$query->setEnd(new \DateTime());
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$model->setCustomer($customer);
$model->setTemplate($template);
$model->addEntries($entries);

View File

@@ -11,9 +11,9 @@ namespace App\Tests\Event;
use App\Entity\InvoiceDocument;
use App\Event\InvoicePostRenderEvent;
use App\Invoice\InvoiceModel;
use App\Tests\Invoice\DebugFormatter;
use App\Tests\Invoice\Renderer\DebugRenderer;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;
@@ -24,7 +24,7 @@ class InvoicePostRenderEventTest extends TestCase
{
public function testDefaultValues()
{
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$document = new InvoiceDocument(new \SplFileInfo(__FILE__));
$renderer = new DebugRenderer();
$response = new Response();

View File

@@ -11,9 +11,9 @@ namespace App\Tests\Event;
use App\Entity\InvoiceDocument;
use App\Event\InvoicePreRenderEvent;
use App\Invoice\InvoiceModel;
use App\Tests\Invoice\DebugFormatter;
use App\Tests\Invoice\Renderer\DebugRenderer;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
use PHPUnit\Framework\TestCase;
/**
@@ -23,7 +23,7 @@ class InvoicePreRenderEventTest extends TestCase
{
public function testDefaultValues()
{
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$document = new InvoiceDocument(new \SplFileInfo(__FILE__));
$renderer = new DebugRenderer();

View File

@@ -16,9 +16,9 @@ use App\Entity\Project;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Invoice\CalculatorInterface;
use App\Invoice\InvoiceModel;
use App\Repository\Query\InvoiceQuery;
use App\Tests\Invoice\DebugFormatter;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
use PHPUnit\Framework\TestCase;
abstract class AbstractCalculatorTest extends TestCase
@@ -44,7 +44,7 @@ abstract class AbstractCalculatorTest extends TestCase
$template = new InvoiceTemplate();
$query = new InvoiceQuery();
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$model->setCustomer($customer);
$model->setTemplate($template);
$model->setQuery($query);
@@ -88,7 +88,7 @@ abstract class AbstractCalculatorTest extends TestCase
->setActivity($activity)
->setProject($project);
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$model->setCustomer($customer);
$model->setTemplate($template);
$model->addEntries([$timesheet]);

View File

@@ -16,9 +16,9 @@ use App\Entity\Project;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Invoice\Calculator\ActivityInvoiceCalculator;
use App\Invoice\InvoiceModel;
use App\Repository\Query\InvoiceQuery;
use App\Tests\Invoice\DebugFormatter;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
/**
* @covers \App\Invoice\Calculator\ActivityInvoiceCalculator
@@ -134,7 +134,7 @@ class ActivityInvoiceCalculatorTest extends AbstractCalculatorTest
$query = new InvoiceQuery();
$query->setActivity($activity1);
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$model->setCustomer($customer);
$model->setTemplate($template);
$model->addEntries($entries);

View File

@@ -16,9 +16,9 @@ use App\Entity\Project;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Invoice\Calculator\DateInvoiceCalculator;
use App\Invoice\InvoiceModel;
use App\Repository\Query\InvoiceQuery;
use App\Tests\Invoice\DebugFormatter;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
use DateTime;
/**
@@ -107,7 +107,7 @@ class DateInvoiceCalculatorTest extends AbstractCalculatorTest
$query = new InvoiceQuery();
$query->setProjects([$project1]);
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$model->setCustomer($customer);
$model->setTemplate($template);
$model->addEntries($entries);

View File

@@ -15,9 +15,9 @@ use App\Entity\InvoiceTemplate;
use App\Entity\Tag;
use App\Entity\Timesheet;
use App\Invoice\Calculator\DefaultCalculator;
use App\Invoice\InvoiceModel;
use App\Repository\Query\InvoiceQuery;
use App\Tests\Invoice\DebugFormatter;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
/**
* @covers \App\Invoice\Calculator\DefaultCalculator
@@ -63,7 +63,7 @@ class DefaultCalculatorTest extends AbstractCalculatorTest
$entries = [$timesheet, $timesheet2, $timesheet3];
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$model->setCustomer($customer);
$model->setTemplate($template);
$model->addEntries($entries);

View File

@@ -16,9 +16,9 @@ use App\Entity\Project;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Invoice\Calculator\ProjectInvoiceCalculator;
use App\Invoice\InvoiceModel;
use App\Repository\Query\InvoiceQuery;
use App\Tests\Invoice\DebugFormatter;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
use DateTime;
/**
@@ -107,7 +107,7 @@ class ProjectInvoiceCalculatorTest extends AbstractCalculatorTest
$query = new InvoiceQuery();
$query->setProjects([$project1]);
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$model->setCustomer($customer);
$model->setTemplate($template);
$model->addEntries($entries);

View File

@@ -18,9 +18,9 @@ use App\Entity\Timesheet;
use App\Entity\User;
use App\Invoice\Calculator\ShortInvoiceCalculator;
use App\Invoice\InvoiceItem;
use App\Invoice\InvoiceModel;
use App\Repository\Query\InvoiceQuery;
use App\Tests\Invoice\DebugFormatter;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
/**
* @covers \App\Invoice\Calculator\ShortInvoiceCalculator
@@ -91,7 +91,7 @@ class ShortInvoiceCalculatorTest extends AbstractCalculatorTest
$query = new InvoiceQuery();
$query->setActivity($activity);
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$model->setCustomer($customer);
$model->setTemplate($template);
$model->addEntries($entries);
@@ -173,7 +173,7 @@ class ShortInvoiceCalculatorTest extends AbstractCalculatorTest
$query = new InvoiceQuery();
$query->setActivity($activity);
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$model->setCustomer($customer);
$model->setTemplate($template);
$model->addEntries($entries);
@@ -252,7 +252,7 @@ class ShortInvoiceCalculatorTest extends AbstractCalculatorTest
$query = new InvoiceQuery();
$query->setActivity($activity);
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$model->setCustomer($customer);
$model->setTemplate($template);
$model->addEntries($entries);

View File

@@ -16,9 +16,9 @@ use App\Entity\Project;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Invoice\Calculator\UserInvoiceCalculator;
use App\Invoice\InvoiceModel;
use App\Repository\Query\InvoiceQuery;
use App\Tests\Invoice\DebugFormatter;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
/**
* @covers \App\Invoice\Calculator\UserInvoiceCalculator
@@ -105,7 +105,7 @@ class UserInvoiceCalculatorTest extends AbstractCalculatorTest
$query = new InvoiceQuery();
$query->setActivity($activity);
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$model->setCustomer($customer);
$model->setTemplate($template);
$model->addEntries($entries);

View File

@@ -16,9 +16,9 @@ use App\Entity\Project;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Invoice\Calculator\WeeklyInvoiceCalculator;
use App\Invoice\InvoiceModel;
use App\Repository\Query\InvoiceQuery;
use App\Tests\Invoice\DebugFormatter;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
use DateTime;
/**
@@ -110,7 +110,7 @@ class WeeklyInvoiceCalculatorTest extends AbstractCalculatorTest
$query = new InvoiceQuery();
$query->setProjects([$project1]);
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$model->setCustomer($customer);
$model->setTemplate($template);
$model->addEntries($entries);

View File

@@ -9,6 +9,7 @@
namespace App\Tests\Invoice\Hydrator;
use App\Activity\ActivityStatisticService;
use App\Invoice\Hydrator\InvoiceModelActivityHydrator;
use App\Tests\Invoice\Renderer\RendererTestTrait;
use PHPUnit\Framework\TestCase;
@@ -24,7 +25,7 @@ class InvoiceModelActivityHydratorTest extends TestCase
{
$model = $this->getInvoiceModel();
$sut = new InvoiceModelActivityHydrator();
$sut = new InvoiceModelActivityHydrator($this->createMock(ActivityStatisticService::class));
$result = $sut->hydrate($model);
$this->assertModelStructure($result);
@@ -41,10 +42,18 @@ class InvoiceModelActivityHydratorTest extends TestCase
'activity.name',
'activity.comment',
'activity.meta.foo-activity',
'activity.budget_open',
'activity.budget_open_plain',
'activity.time_budget_open',
'activity.time_budget_open_plain',
'activity.1.id',
'activity.1.name',
'activity.1.comment',
'activity.1.meta.foo-activity',
'activity.1.budget_open',
'activity.1.budget_open_plain',
'activity.1.time_budget_open',
'activity.1.time_budget_open_plain',
];
$givenKeys = array_keys($model);

View File

@@ -9,6 +9,7 @@
namespace App\Tests\Invoice\Hydrator;
use App\Customer\CustomerStatisticService;
use App\Invoice\Hydrator\InvoiceModelCustomerHydrator;
use App\Tests\Invoice\Renderer\RendererTestTrait;
use PHPUnit\Framework\TestCase;
@@ -24,7 +25,7 @@ class InvoiceModelCustomerHydratorTest extends TestCase
{
$model = $this->getInvoiceModel();
$sut = new InvoiceModelCustomerHydrator();
$sut = new InvoiceModelCustomerHydrator($this->createMock(CustomerStatisticService::class));
$result = $sut->hydrate($model);
$this->assertModelStructure($result);
@@ -52,6 +53,10 @@ class InvoiceModelCustomerHydratorTest extends TestCase
'customer.phone',
'customer.mobile',
'customer.meta.foo-customer',
'customer.budget_open',
'customer.budget_open_plain',
'customer.time_budget_open',
'customer.time_budget_open_plain',
];
$givenKeys = array_keys($model);

View File

@@ -10,6 +10,7 @@
namespace App\Tests\Invoice\Hydrator;
use App\Invoice\Hydrator\InvoiceModelProjectHydrator;
use App\Project\ProjectStatisticService;
use App\Tests\Invoice\Renderer\RendererTestTrait;
use PHPUnit\Framework\TestCase;
@@ -24,7 +25,7 @@ class InvoiceModelProjectHydratorTest extends TestCase
{
$model = $this->getInvoiceModel();
$sut = new InvoiceModelProjectHydrator();
$sut = new InvoiceModelProjectHydrator($this->createMock(ProjectStatisticService::class));
$result = $sut->hydrate($model);
$this->assertModelStructure($result);
@@ -51,6 +52,10 @@ class InvoiceModelProjectHydratorTest extends TestCase
'project.budget_time',
'project.budget_time_decimal',
'project.budget_time_minutes',
'project.budget_open',
'project.budget_open_plain',
'project.time_budget_open',
'project.time_budget_open_plain',
'project.1.id',
'project.1.name',
'project.1.comment',
@@ -65,6 +70,10 @@ class InvoiceModelProjectHydratorTest extends TestCase
'project.1.budget_time',
'project.1.budget_time_decimal',
'project.1.budget_time_minutes',
'project.1.budget_open',
'project.1.budget_open_plain',
'project.1.time_budget_open',
'project.1.time_budget_open_plain',
];
$givenKeys = array_keys($model);

View File

@@ -13,10 +13,10 @@ use App\Entity\Customer;
use App\Entity\InvoiceTemplate;
use App\Entity\Project;
use App\Invoice\InvoiceFilename;
use App\Invoice\InvoiceModel;
use App\Invoice\NumberGenerator\DateNumberGenerator;
use App\Repository\InvoiceRepository;
use App\Repository\Query\InvoiceQuery;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
use PHPUnit\Framework\TestCase;
/**
@@ -29,7 +29,7 @@ class InvoiceFilenameTest extends TestCase
$customer = new Customer();
$template = new InvoiceTemplate();
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$model->setNumberGenerator($this->getNumberGeneratorSut());
$model->setTemplate($template);
$model->setCustomer($customer);

View File

@@ -16,6 +16,7 @@ use App\Invoice\Calculator\DefaultCalculator;
use App\Invoice\InvoiceModel;
use App\Repository\Query\InvoiceQuery;
use App\Tests\Invoice\NumberGenerator\IncrementingNumberGenerator;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
use PHPUnit\Framework\TestCase;
/**
@@ -26,7 +27,7 @@ class InvoiceModelTest extends TestCase
public function testEmptyObject()
{
$formatter = new DebugFormatter();
$sut = new InvoiceModel($formatter);
$sut = (new InvoiceModelFactoryFactory($this))->create()->createModel($formatter);
self::assertNull($sut->getQuery());
self::assertNull($sut->getCustomer());
@@ -51,7 +52,7 @@ class InvoiceModelTest extends TestCase
public function testEmptyObjectThrowsExceptionOnNumberGenerator()
{
$formatter = new DebugFormatter();
$sut = new InvoiceModel($formatter);
$sut = (new InvoiceModelFactoryFactory($this))->create()->createModel($formatter);
$this->expectException(\Exception::class);
$this->expectExceptionMessage('InvoiceModel::getInvoiceNumber() cannot be called before calling setNumberGenerator()');
@@ -60,7 +61,7 @@ class InvoiceModelTest extends TestCase
public function testSetter()
{
$sut = new InvoiceModel(new DebugFormatter());
$sut = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$query = new InvoiceQuery();
self::assertInstanceOf(InvoiceModel::class, $sut->setQuery($query));
@@ -95,7 +96,7 @@ class InvoiceModelTest extends TestCase
*/
public function testDeprecations()
{
$sut = new InvoiceModel(new DebugFormatter());
$sut = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$entries = [new Timesheet()];
self::assertInstanceOf(InvoiceModel::class, $sut->setEntries($entries));

View File

@@ -12,10 +12,10 @@ namespace App\Tests\Invoice\NumberGenerator;
use App\Configuration\SystemConfiguration;
use App\Entity\Customer;
use App\Entity\User;
use App\Invoice\InvoiceModel;
use App\Invoice\NumberGenerator\ConfigurableNumberGenerator;
use App\Repository\InvoiceRepository;
use App\Tests\Invoice\DebugFormatter;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
use PHPUnit\Framework\TestCase;
/**
@@ -156,7 +156,7 @@ class ConfigurableNumberGeneratorTest extends TestCase
$user->method('getAccountNumber')->willReturn('0815');
$sut = $this->getSut($format, $counter);
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$model->setInvoiceDate($invoiceDate);
$model->setCustomer($customer);
$model->setUser($user);
@@ -197,7 +197,7 @@ class ConfigurableNumberGeneratorTest extends TestCase
$this->expectExceptionMessage(sprintf('Unknown %s found', $brokenPart));
$sut = $this->getSut($format);
$model = new InvoiceModel(new DebugFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter());
$model->setInvoiceDate($invoiceDate);
$model->setCustomer(new Customer());
$sut->setModel($model);

View File

@@ -9,10 +9,10 @@
namespace App\Tests\Invoice\NumberGenerator;
use App\Invoice\InvoiceModel;
use App\Invoice\NumberGenerator\DateNumberGenerator;
use App\Repository\InvoiceRepository;
use App\Tests\Invoice\DebugFormatter;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
use PHPUnit\Framework\TestCase;
/**
@@ -40,7 +40,7 @@ class DateNumberGeneratorTest extends TestCase
public function testGetInvoiceNumber()
{
$sut = $this->getSut(false, false);
$sut->setModel(new InvoiceModel(new DebugFormatter()));
$sut->setModel((new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter()));
$this->assertEquals(date('ymd'), $sut->getInvoiceNumber());
$this->assertEquals('date', $sut->getId());
@@ -49,7 +49,7 @@ class DateNumberGeneratorTest extends TestCase
public function testGetInvoiceNumberWithExisting()
{
$sut = $this->getSut(true, false);
$sut->setModel(new InvoiceModel(new DebugFormatter()));
$sut->setModel((new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter()));
$this->assertEquals(date('ymd-01'), $sut->getInvoiceNumber());
$this->assertEquals('date', $sut->getId());
@@ -58,7 +58,7 @@ class DateNumberGeneratorTest extends TestCase
public function testGetInvoiceNumberWithManyExisting()
{
$sut = $this->getSut(true, true);
$sut->setModel(new InvoiceModel(new DebugFormatter()));
$sut->setModel((new InvoiceModelFactoryFactory($this))->create()->createModel(new DebugFormatter()));
$this->assertEquals(date('ymd-99'), $sut->getInvoiceNumber());
$this->assertEquals('date', $sut->getId());

View File

@@ -131,12 +131,20 @@ class DebugRendererTest extends TestCase
'customer.email',
'customer.fax',
'customer.phone',
'customer.budget_open',
'customer.budget_open_plain',
'customer.time_budget_open',
'customer.time_budget_open_plain',
'customer.mobile',
'customer.meta.foo-customer',
'activity.id',
'activity.name',
'activity.comment',
'activity.meta.foo-activity',
'activity.budget_open',
'activity.budget_open_plain',
'activity.time_budget_open',
'activity.time_budget_open_plain',
'user.alias',
'user.email',
'user.name',
@@ -146,8 +154,18 @@ class DebugRendererTest extends TestCase
'testFromModelHydrator',
];
if ($activityCounter === 1) {
$keys = array_merge($keys, [
'activity',
]);
}
if ($activityCounter > 1) {
$keys = array_merge($keys, [
'activity.1.budget_open',
'activity.1.budget_open_plain',
'activity.1.time_budget_open',
'activity.1.time_budget_open_plain',
'activity.1.id',
'activity.1.name',
'activity.1.comment',
@@ -171,7 +189,16 @@ class DebugRendererTest extends TestCase
'project.budget_time',
'project.budget_time_decimal',
'project.budget_time_minutes',
'project.budget_open',
'project.budget_open_plain',
'project.time_budget_open',
'project.time_budget_open_plain',
]);
if ($projectCounter === 1) {
$keys = array_merge($keys, [
'project',
]);
}
if ($projectCounter > 1) {
$keys = array_merge($keys, [
'project.1.id',
@@ -188,6 +215,10 @@ class DebugRendererTest extends TestCase
'project.1.budget_time',
'project.1.budget_time_decimal',
'project.1.budget_time_minutes',
'project.1.budget_open',
'project.1.budget_open_plain',
'project.1.time_budget_open',
'project.1.time_budget_open_plain',
]);
}
}

View File

@@ -30,6 +30,7 @@ use App\Invoice\NumberGenerator\DateNumberGenerator;
use App\Invoice\Renderer\AbstractRenderer;
use App\Repository\InvoiceRepository;
use App\Repository\Query\InvoiceQuery;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
trait RendererTestTrait
{
@@ -217,7 +218,7 @@ trait RendererTestTrait
$query->setEnd(new \DateTime());
$query->setProjects([$project, $project2]);
$model = new InvoiceModel($this->getFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel($this->getFormatter());
$model->setCustomer($customer);
$model->setTemplate($template);
$model->addEntries($entries);
@@ -301,7 +302,7 @@ trait RendererTestTrait
$query->setBegin(new \DateTime());
$query->setEnd(new \DateTime());
$model = new InvoiceModel($this->getFormatter());
$model = (new InvoiceModelFactoryFactory($this))->create()->createModel($this->getFormatter());
$model->setCustomer($customer);
$model->setTemplate($template);
$model->addEntries($entries);

View File

@@ -66,10 +66,8 @@ class TwigRendererTest extends KernelTestCase
$filename = $model->getInvoiceNumber() . '-customer_with_special_name';
$this->assertStringContainsString('<title>' . $filename . '</title>', $content);
$this->assertStringContainsString('<h2 class="page-header">
<span contenteditable="true">a very *long* test invoice / template title with [ßpecial] chäracter</span>
</h2>', $content);
$this->assertEquals(2, substr_count($content, 'activity description'));
$this->assertStringContainsString('<span contenteditable="true">a very *long* test invoice / template title with [ßpecial] chäracter</span>', $content);
$this->assertEquals(3, substr_count($content, 'activity description'));
$this->assertStringContainsString(nl2br("foo\n" .
"foo\r\n" .
'foo' . PHP_EOL .

View File

@@ -24,6 +24,7 @@ use App\Invoice\ServiceInvoice;
use App\Repository\InvoiceDocumentRepository;
use App\Repository\InvoiceRepository;
use App\Repository\Query\InvoiceQuery;
use App\Tests\Mocks\InvoiceModelFactoryFactory;
use App\Utils\FileHelper;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
@@ -48,7 +49,7 @@ class ServiceInvoiceTest extends TestCase
$repo = new InvoiceDocumentRepository($paths);
$invoiceRepo = $this->createMock(InvoiceRepository::class);
return new ServiceInvoice($repo, new FileHelper(realpath(__DIR__ . '/../../var/data/')), $invoiceRepo, $formattings);
return new ServiceInvoice($repo, new FileHelper(realpath(__DIR__ . '/../../var/data/')), $invoiceRepo, $formattings, (new InvoiceModelFactoryFactory($this))->create());
}
public function testInvalidExceptionOnChangeState()

View File

@@ -0,0 +1,30 @@
<?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\Tests\Mocks;
use App\Activity\ActivityStatisticService;
use App\Customer\CustomerStatisticService;
use App\Invoice\InvoiceModelFactory;
use App\Project\ProjectStatisticService;
class InvoiceModelFactoryFactory extends AbstractMockFactory
{
public function create(): InvoiceModelFactory
{
/** @var CustomerStatisticService $customerStatistic */
$customerStatistic = $this->getMockBuilder(CustomerStatisticService::class)->disableOriginalConstructor()->getMock();
/** @var ProjectStatisticService $projectStatistic */
$projectStatistic = $this->getMockBuilder(ProjectStatisticService::class)->disableOriginalConstructor()->getMock();
/** @var ActivityStatisticService $activityStatistic */
$activityStatistic = $this->getMockBuilder(ActivityStatisticService::class)->disableOriginalConstructor()->getMock();
return new InvoiceModelFactory($customerStatistic, $projectStatistic, $activityStatistic);
}
}