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

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