diff --git a/app/AccountancyModule/EducationModule/presenters/CashbookPresenter.php b/app/AccountancyModule/EducationModule/presenters/CashbookPresenter.php index 5da4a2862..d5776fae3 100644 --- a/app/AccountancyModule/EducationModule/presenters/CashbookPresenter.php +++ b/app/AccountancyModule/EducationModule/presenters/CashbookPresenter.php @@ -6,14 +6,22 @@ use App\AccountancyModule\Components\CashbookControl; use App\AccountancyModule\Factories\ICashbookControlFactory; +use App\Forms\BaseForm; use Model\Auth\Resources\Education; +use Model\Cashbook\Cashbook\Amount; use Model\Cashbook\Cashbook\CashbookId; +use Model\Cashbook\Cashbook\ChitBody; use Model\Cashbook\Cashbook\PaymentMethod; +use Model\Cashbook\Commands\Cashbook\AddChitToCashbook; use Model\Cashbook\MissingCategory; +use Model\Cashbook\ReadModel\Queries\CategoryListQuery; use Model\Cashbook\ReadModel\Queries\ChitListQuery; use Model\Cashbook\ReadModel\Queries\EducationCashbookIdQuery; +use Model\Cashbook\ReadModel\Queries\EducationParticipantCategoryIdQuery; +use Model\Cashbook\ReadModel\Queries\EducationParticipantIncomeQuery; use Model\Cashbook\ReadModel\Queries\FinalCashBalanceQuery; use Model\Cashbook\ReadModel\Queries\FinalRealBalanceQuery; +use Model\DTO\Cashbook\ChitItem; use Model\Event\SkautisEducationId; use Money\Money; @@ -65,6 +73,53 @@ protected function createComponentCashbook(): CashbookControl ); } + protected function createComponentFormImportHpd(): BaseForm + { + $form = new BaseForm(); + $form->addRadioList('isAccount', 'Placeno:', ['N' => 'Hotově', 'Y' => 'Přes účet']) + ->addRule($form::FILLED, 'Musíte vyplnit způsob platby.') + ->setDefaultValue('N'); + + $form->addSubmit('send', 'Importovat') + ->setHtmlAttribute('class', 'btn btn-primary'); + + $form->onSuccess[] = function (BaseForm $form): void { + $this->formImportHpdSubmitted($form); + }; + + $form->setDefaults(['category' => 'un']); + + return $form; + } + + private function formImportHpdSubmitted(BaseForm $form): void + { + $this->editableOnly(); + $values = $form->getValues(); + + $amount = $this->queryBus->handle(new EducationParticipantIncomeQuery(new SkautisEducationId($this->aid))); + + if ($amount === 0.0) { + $this->flashMessage('Nemáte žádné příjmy od účastníků, které by bylo možné importovat.', 'warning'); + $this->redirect('default', ['aid' => $this->aid]); + } + + $purpose = 'úč. příspěvky ' . ($values->isAccount === 'Y' ? '- účet' : '- hotovost'); + $body = new ChitBody(null, $this->event->getStartDate(), null); + + $categoryId = $this->queryBus->handle( + new EducationParticipantCategoryIdQuery(new SkautisEducationId($this->aid)), + ); + $categoriesDto = $this->queryBus->handle(new CategoryListQuery($this->getCashbookId())); + + $items = [new ChitItem(Amount::fromFloat($amount), $categoriesDto[$categoryId], $purpose)]; + $this->commandBus->handle(new AddChitToCashbook($this->getCashbookId(), $body, $values->isAccount === 'Y' ? PaymentMethod::BANK() : PaymentMethod::CASH(), $items)); + + $this->flashMessage('HPD byl importován'); + + $this->redirect('default', $this->aid); + } + private function getCashbookId(): CashbookId { return $this->queryBus->handle(new EducationCashbookIdQuery(new SkautisEducationId($this->aid))); diff --git a/app/AccountancyModule/EducationModule/templates/Cashbook/default.latte b/app/AccountancyModule/EducationModule/templates/Cashbook/default.latte index 88cecc0a9..534bfc20f 100644 --- a/app/AccountancyModule/EducationModule/templates/Cashbook/default.latte +++ b/app/AccountancyModule/EducationModule/templates/Cashbook/default.latte @@ -3,6 +3,14 @@ {block #content} {include ../header.latte} +
+
+ +  Načíst příjmy od účastníků + +
+
+

Evidence plateb je v záporných číslech a to nesmí nastat! Je importovaný HPD? @@ -14,3 +22,17 @@


Výsledek hospodaření: {if $finalRealBalance === null}Chybí povolené automatické dopočítávání!{else}{$finalRealBalance|price} Kč{/if}
+ + diff --git a/app/model/Cashbook/ReadModel/Queries/EducationParticipantCategoryIdQuery.php b/app/model/Cashbook/ReadModel/Queries/EducationParticipantCategoryIdQuery.php new file mode 100644 index 000000000..fc1c97a7e --- /dev/null +++ b/app/model/Cashbook/ReadModel/Queries/EducationParticipantCategoryIdQuery.php @@ -0,0 +1,21 @@ +educationId; + } +} diff --git a/app/model/Cashbook/ReadModel/QueryHandlers/EducationParticipantCategoryIdQueryHandler.php b/app/model/Cashbook/ReadModel/QueryHandlers/EducationParticipantCategoryIdQueryHandler.php new file mode 100644 index 000000000..359c228a6 --- /dev/null +++ b/app/model/Cashbook/ReadModel/QueryHandlers/EducationParticipantCategoryIdQueryHandler.php @@ -0,0 +1,27 @@ +categories->findForEducation($query->getEducationId()->toInt()) as $category) { + if ($category->getName() === 'Účastnické poplatky') { + return $category->getId(); + } + } + + throw new InvalidStateException('There is no participant category.'); + } +}