Skip to content

Commit

Permalink
Merge pull request #2414 from skaut/education-cashbook-years
Browse files Browse the repository at this point in the history
Pokladní knihy vzdělávaček a jejich kategorie specifické pro rok
  • Loading branch information
marekdedic authored Oct 26, 2023
2 parents 702330e + 4baf686 commit 7a697b8
Show file tree
Hide file tree
Showing 21 changed files with 152 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function (Education $education): EducationListItem {

private function chitNumberPrefix(Education $camp): string|null
{
$cashbookId = $this->queryBus->handle(new EducationCashbookIdQuery($camp->getId()));
$cashbookId = $this->queryBus->handle(new EducationCashbookIdQuery($camp->getId(), $camp->startDate->year));

assert($cashbookId instanceof CashbookId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public function renderDefault(int $aid): void

$educationId = new SkautisEducationId($aid);

$inconsistentTotals = $this->queryBus->handle(new InconsistentEducationCategoryTotalsQuery($educationId));
$inconsistentTotals = $this->queryBus->handle(new InconsistentEducationCategoryTotalsQuery($educationId, $this->event->startDate->year));
$this->template->setParameters([
'isConsistent' => count($inconsistentTotals) === 0,
'toRepair' => $inconsistentTotals,
'budgetEntries' => $this->queryBus->handle(new EducationBudgetQuery($educationId, $this->event->grantId)),
'categoriesSummary' => $this->queryBus->handle(new CategoriesSummaryQuery($this->getCashbookId($aid))),
'categoriesSummary' => $this->queryBus->handle(new CategoriesSummaryQuery($this->getCashbookId($aid, $this->event->startDate->year))),
'isUpdateStatementAllowed' => $this->authorizator->isAllowed(Grant::UPDATE_REAL_BUDGET_SPENDING, $this->event->grantId->toInt()),
]);
if (! $this->isAjax()) {
Expand All @@ -66,7 +66,7 @@ public function handleConvert(int $aid): void
{
$this->editableOnly();

$this->commandBus->handle(new UpdateEducationCategoryTotals($this->getCashbookId($aid)));
$this->commandBus->handle(new UpdateEducationCategoryTotals($this->getCashbookId($aid, $this->event->startDate->year)));
$this->flashMessage('Kategorie byly přepočítány.');

if ($this->isAjax()) {
Expand All @@ -76,8 +76,8 @@ public function handleConvert(int $aid): void
}
}

private function getCashbookId(int $educationId): CashbookId
private function getCashbookId(int $educationId, int $year): CashbookId
{
return $this->queryBus->handle(new EducationCashbookIdQuery(new SkautisEducationId($educationId)));
return $this->queryBus->handle(new EducationCashbookIdQuery(new SkautisEducationId($educationId), $year));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private function formImportHpdSubmitted(BaseForm $form): void
$body = new ChitBody(null, $this->event->getStartDate(), null);

$categoryId = $this->queryBus->handle(
new EducationParticipantCategoryIdQuery(new SkautisEducationId($this->aid)),
new EducationParticipantCategoryIdQuery(new SkautisEducationId($this->aid), $this->event->startDate->year),
);
$categoriesDto = $this->queryBus->handle(new CategoryListQuery($this->getCashbookId()));

Expand All @@ -128,7 +128,7 @@ private function formImportHpdSubmitted(BaseForm $form): void

private function getCashbookId(): CashbookId
{
return $this->queryBus->handle(new EducationCashbookIdQuery(new SkautisEducationId($this->aid)));
return $this->queryBus->handle(new EducationCashbookIdQuery(new SkautisEducationId($this->aid), $this->event->startDate->year));
}

private function isCashbookEmpty(): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public function renderDefault(int|null $aid): void
$this->redirect('Default:');
}

$cashbook = $this->queryBus->handle(new CashbookQuery($this->getCashbookId($aid)));
$cashbook = $this->queryBus->handle(new CashbookQuery($this->getCashbookId($aid, $this->event->startDate->year)));
assert($cashbook instanceof Cashbook);

$finalRealBalance = null;
if ($this->authorizator->isAllowed(Education::ACCESS_BUDGET, $aid)) {
try {
$finalRealBalance = $this->queryBus->handle(new FinalRealBalanceQuery($this->getCashbookId($aid)));
$finalRealBalance = $this->queryBus->handle(new FinalRealBalanceQuery($this->getCashbookId($aid, $this->event->startDate->year)));
} catch (MissingCategory) {
}
}
Expand Down Expand Up @@ -134,15 +134,15 @@ public function renderReport(int $aid): void
$this->redirect('default', ['aid' => $aid]);
}

$template = $this->exportService->getEducationReport(new SkautisEducationId($aid));
$template = $this->exportService->getEducationReport(new SkautisEducationId($aid), $this->event->startDate->year);

$this->pdf->render($template, 'report.pdf');
$this->terminate();
}

private function getCashbookId(int $skautisEducationId): CashbookId
private function getCashbookId(int $skautisEducationId, int $year): CashbookId
{
return $this->queryBus->handle(new EducationCashbookIdQuery(new SkautisEducationId($skautisEducationId)));
return $this->queryBus->handle(new EducationCashbookIdQuery(new SkautisEducationId($skautisEducationId), $year));
}

/**
Expand Down
16 changes: 15 additions & 1 deletion app/model/Cashbook/Education.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@ class Education extends Aggregate
*/
private SkautisEducationId $id;

/**
* For education events spanning multiple years, a separate cashbook is needed for each year.
*
* @ORM\Id()
* @ORM\Column(type="integer")
*/
private int $year;

/** @ORM\Column(type="cashbook_id") */
private CashbookId $cashbookId;

public function __construct(SkautisEducationId $id, CashbookId $cashbookId)
public function __construct(SkautisEducationId $id, int $year, CashbookId $cashbookId)
{
$this->id = $id;
$this->year = $year;
$this->cashbookId = $cashbookId;
}

Expand All @@ -35,6 +44,11 @@ public function getSkautisId(): SkautisEducationId
return $this->id;
}

public function getYear(): int
{
return $this->year;
}

public function getCashbookId(): CashbookId
{
return $this->cashbookId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class EducationCashbookIdQuery
{
private SkautisEducationId $educationId;

public function __construct(SkautisEducationId $eventId)
public function __construct(SkautisEducationId $eventId, private int $year)
{
$this->educationId = $eventId;
}
Expand All @@ -20,4 +20,9 @@ public function getEducationId(): SkautisEducationId
{
return $this->educationId;
}

public function getYear(): int
{
return $this->year;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
/** @see EducationParticipantCategoryIdQueryHandler */
final class EducationParticipantCategoryIdQuery
{
public function __construct(private SkautisEducationId $educationId)
public function __construct(private SkautisEducationId $educationId, private int $year)
{
}

public function getEducationId(): SkautisEducationId
{
return $this->educationId;
}

public function getYear(): int
{
return $this->year;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
*/
final class InconsistentEducationCategoryTotalsQuery
{
public function __construct(private SkautisEducationId $educationId)
public function __construct(private SkautisEducationId $educationId, private int $year)
{
}

public function getEducationId(): SkautisEducationId
{
return $this->educationId;
}

public function getYear(): int
{
return $this->year;
}
}
21 changes: 21 additions & 0 deletions app/model/Cashbook/ReadModel/Queries/SkautisEducationYearQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Model\Cashbook\ReadModel\Queries;

use Model\Cashbook\Cashbook\CashbookId;
use Model\Cashbook\ReadModel\QueryHandlers\SkautisEducationYearQueryHandler;

/** @see SkautisEducationYearQueryHandler */
final class SkautisEducationYearQuery
{
public function __construct(private CashbookId $cashbookId)
{
}

public function getCashbookId(): CashbookId
{
return $this->cashbookId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public function __construct(private IEducationRepository $eventRepository)

public function __invoke(EducationCashbookIdQuery $query): CashbookId
{
return $this->eventRepository->findBySkautisId($query->getEducationId())->getCashbookId();
return $this->eventRepository->findBySkautisIdAndYear($query->getEducationId(), $query->getYear())->getCashbookId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(private IEducationCategoryRepository $categories)

public function __invoke(EducationParticipantCategoryIdQuery $query): int
{
foreach ($this->categories->findForEducation($query->getEducationId()->toInt()) as $category) {
foreach ($this->categories->findForEducation($query->getEducationId()->toInt(), $query->getYear()) as $category) {
if ($category->getName() === 'Účastnické poplatky') {
return $category->getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public function __construct(private IEducationCategoryRepository $educationCateg
/** @return float[] */
public function __invoke(InconsistentEducationCategoryTotalsQuery $query): array
{
$cashbookId = $this->queryBus->handle(new EducationCashbookIdQuery($query->getEducationId()));
$cashbookId = $this->queryBus->handle(new EducationCashbookIdQuery($query->getEducationId(), $query->getYear()));
$categories = $this->queryBus->handle(new CategoriesSummaryQuery($cashbookId));

$skautisTotals = [];

foreach ($this->educationCategories->findForEducation($query->getEducationId()->toInt()) as $educationCategory) {
foreach ($this->educationCategories->findForEducation($query->getEducationId()->toInt(), $query->getYear()) as $educationCategory) {
$id = $educationCategory->getId();
$total = $educationCategory->getTotal();
$category = $categories[$id];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Model\Cashbook\ReadModel\QueryHandlers;

use Model\Cashbook\CashbookNotFound;
use Model\Cashbook\Exception\UnitNotFound;
use Model\Cashbook\ObjectType;
use Model\Cashbook\ReadModel\Queries\SkautisEducationYearQuery;
use Model\Cashbook\Repositories\ICashbookRepository;
use Model\Cashbook\Repositories\IEducationRepository;
use Model\Common\ShouldNotHappen;

class SkautisEducationYearQueryHandler
{
public function __construct(
private ICashbookRepository $cashbooks,
private IEducationRepository $educationRepository,
) {
}

/**
* @throws CashbookNotFound
* @throws UnitNotFound
*/
public function __invoke(SkautisEducationYearQuery $query): int
{
$cashbook = $this->cashbooks->find($query->getCashbookId());
$objectType = $cashbook->getType()->getSkautisObjectType();

if (! $objectType->equalsValue(ObjectType::EDUCATION)) {
throw new ShouldNotHappen();
}

return $this->educationRepository->findByCashbookId($query->getCashbookId())->getYear();
}
}
4 changes: 3 additions & 1 deletion app/model/Cashbook/Repositories/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Model\Cashbook\Cashbook\CashbookType;
use Model\Cashbook\CategoryNotFound;
use Model\Cashbook\ICategory;
use Model\Cashbook\ReadModel\Queries\SkautisEducationYearQuery;
use Model\Cashbook\ReadModel\Queries\SkautisIdQuery;
use Model\Common\Services\QueryBus;

Expand Down Expand Up @@ -39,7 +40,8 @@ public function findForCashbook(CashbookId $cashbookId, CashbookType $type): arr

if ($skautisType->equalsValue(CashbookType::EDUCATION)) {
$educationId = $this->queryBus->handle(new SkautisIdQuery($cashbookId));
$educationCategories = $this->educationCategories->findForEducation($educationId);
$educationYear = $this->queryBus->handle(new SkautisEducationYearQuery($cashbookId));
$educationCategories = $this->educationCategories->findForEducation($educationId, $educationYear);

return array_merge($categories, $educationCategories);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
interface IEducationCategoryRepository
{
/** @return EducationCategory[] */
public function findForEducation(int $educationId): array;
public function findForEducation(int $educationId, int $year): array;
}
2 changes: 1 addition & 1 deletion app/model/Cashbook/Repositories/IEducationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
interface IEducationRepository
{
/** @throws CashbookNotFound */
public function findBySkautisId(SkautisEducationId $id): Education;
public function findBySkautisIdAndYear(SkautisEducationId $id, int $year): Education;

/** @throws CashbookNotFound */
public function findByCashbookId(CashbookId $cashbookId): Education;
Expand Down
4 changes: 2 additions & 2 deletions app/model/Export/ExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ public function getCampReport(int $skautisCampId, bool $areTotalsConsistentWithS
]);
}

public function getEducationReport(SkautisEducationId $educationId): string
public function getEducationReport(SkautisEducationId $educationId, int $year): string
{
$cashbookId = $this->queryBus->handle(new EducationCashbookIdQuery($educationId));
$cashbookId = $this->queryBus->handle(new EducationCashbookIdQuery($educationId, $year));
$categories = $this->queryBus->handle(new CategoriesSummaryQuery($cashbookId));

$total = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(EntityManager $entityManager, EventBus $eventBus, pr
parent::__construct($entityManager, $eventBus);
}

public function findBySkautisId(SkautisEducationId $id): Education
public function findBySkautisIdAndYear(SkautisEducationId $id, int $year): Education
{
$builder = $this->getEntityManager()->createQueryBuilder();

Expand All @@ -36,7 +36,7 @@ public function findBySkautisId(SkautisEducationId $id): Education
->getQuery()
->getSingleResult();
} catch (NoResultException) {
$cashbook = new Education($id, CashbookId::generate());
$cashbook = new Education($id, $year, CashbookId::generate());
$this->save($cashbook);

$this->commandBus->handle(new CreateCashbook($cashbook->getCashbookId(), CashbookType::get(CashbookType::EDUCATION)));
Expand Down
7 changes: 4 additions & 3 deletions app/model/Skautis/Cashbook/EducationCategoryUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function __construct(
public function updateCategories(CashbookId $cashbookId, array $cashbookTotals): void
{
$educationSkautisId = $this->educationRepository->findByCashbookId($cashbookId)->getSkautisId();
$skautisTotals = $this->getSkautisTotals($educationSkautisId);
$educationYear = $this->educationRepository->findByCashbookId($cashbookId)->getYear();
$skautisTotals = $this->getSkautisTotals($educationSkautisId, $educationYear);

// Update categories that are not in cashbook, has total > 0 in Skautis
$categoriesOnlyInSkautis = array_diff(array_keys($skautisTotals), array_keys($cashbookTotals));
Expand Down Expand Up @@ -74,9 +75,9 @@ public function updateCategories(CashbookId $cashbookId, array $cashbookTotals):
}

/** @return float[] */
private function getSkautisTotals(SkautisEducationId $educationSkautisId): array
private function getSkautisTotals(SkautisEducationId $educationSkautisId, int $year): array
{
$categories = $this->educationCategories->findForEducation($educationSkautisId->toInt());
$categories = $this->educationCategories->findForEducation($educationSkautisId->toInt(), $year);
$totals = [];

foreach ($categories as $category) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ public function __construct(private WebServiceInterface $grantsWebService)

/** @return EducationCategory[] */
// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
public function findForEducation(int $educationId): array
public function findForEducation(int $educationId, int $year): array
{
$skautisCategories = $this->grantsWebService->StatementAll([
'ID_EventEducation' => $educationId,
'IsBudget' => false,
'Year' => $year,
]);

if (is_object($skautisCategories)) {
Expand Down
Loading

0 comments on commit 7a697b8

Please sign in to comment.