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())