Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Přidání zpáteční cesty #2413

Merged
merged 10 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/AccountancyModule/TravelModule/presenters/DefaultPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Forms\BaseForm;
use Assert\Assertion;
use Model\Services\PdfRenderer;
use Model\Travel\Commands\Command\AddReturnTravel;
use Model\Travel\Commands\Command\DuplicateTravel;
use Model\Travel\Travel\TransportType;
use Model\TravelService;
Expand Down Expand Up @@ -171,6 +172,20 @@ public function handleDuplicateTravel(int $commandId, int $travelId): void
$this->redirect('this');
}

public function handleAddReturnTravel(int $commandId, int $travelId): void
{
if (! $this->isCommandEditable($commandId)) {
$this->flashMessage('Nemáte oprávnění přidat zpáteční cestu.', 'danger');
$this->redirect('default');
}

$this->commandBus->handle(new AddReturnTravel($commandId, $travelId));

$this->flashMessage('Zpáteční cesta byla přidána.', 'success');

$this->redirect('this');
}

public function handleOpenCommand(int $commandId): void
{
if (! $this->isCommandAccessible($commandId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<td n:if="$isEditable">
<div class="btn-group">
<a n:href="duplicateTravel! $command->id, $t->id" class="btn btn-sm btn-info" title="duplikovat cestu"><i class="far fa-copy"></i></a>
<a n:href="addReturnTravel! $command->id, $t->id" class="btn btn-sm btn-info" title="přidat zpáteční cestu"><i class="far fa-arrow-alt-circle-left"></i></a>
<a n:href="editTravel! $t->id" class="ajax btn btn-sm btn-primary"><i class="far fa-edit"></i></a>
<a n:href="removeTravel! $command->id, $t->id" class="btn btn-sm btn-danger" onclick="return confirm('Opravdu chcete smazat cestu?')" title="smazat cestu"><i class="far fa-trash-alt"></i></a>
</div>
Expand Down
18 changes: 17 additions & 1 deletion app/model/Travel/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,23 @@ public function duplicateTravel(int $id): void
} elseif ($travel instanceof TransportTravel) {
$this->addTransportTravel($travel->getPrice(), $travel->getDetails());
} else {
throw new ShouldNotHappen('Uknown travel type');
throw new ShouldNotHappen('Unknown travel type');
}
}

/** @throws TravelNotFound */
public function addReturnTravel(int $id): void
{
$travel = $this->getTravel($id);
$details = $travel->getDetails();
$reversedDetails = new TravelDetails($details->getDate(), $details->getTransportType(), $details->getEndPlace(), $details->getStartPlace());

if ($travel instanceof VehicleTravel) {
$this->addVehicleTravel($travel->getDistance(), $reversedDetails);
} elseif ($travel instanceof TransportTravel) {
$this->addTransportTravel($travel->getPrice(), $reversedDetails);
} else {
throw new ShouldNotHappen('Unknown travel type');
}
}

Expand Down
25 changes: 25 additions & 0 deletions app/model/Travel/Commands/Command/AddReturnTravel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Model\Travel\Commands\Command;

use Model\Travel\Handlers\Command\AddReturnTravelHandler;

/** @see AddReturnTravelHandler */
final class AddReturnTravel
{
public function __construct(private int $commandId, private int $travelId)
{
}

public function getCommandId(): int
{
return $this->commandId;
}

public function getTravelId(): int
{
return $this->travelId;
}
}
24 changes: 24 additions & 0 deletions app/model/Travel/Handlers/Command/AddReturnTravelHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Model\Travel\Handlers\Command;

use Model\Travel\Commands\Command\AddReturnTravel;
use Model\Travel\Repositories\ICommandRepository;

final class AddReturnTravelHandler
{
public function __construct(private ICommandRepository $commands)
{
}

public function __invoke(AddReturnTravel $command): void
{
$travelCommand = $this->commands->find($command->getCommandId());

$travelCommand->addReturnTravel($command->getTravelId());

$this->commands->save($travelCommand);
}
}
1 change: 1 addition & 0 deletions frontend/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ library.add(
far.faTrashAlt,
far.faUser,
far.faQuestionCircle,
far.faArrowAltCircleLeft,

// Brands
fab.faGithub as IconDefinition,
Expand Down
64 changes: 64 additions & 0 deletions tests/unit/Travel/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,70 @@ public function testVehicleTravelIsDuplicated(): void
self::assertEquals($travelDetails->getEndPlace(), $duplicatedTravelDetails->getEndPlace());
}

public function testTransportTravelIsReturnAdded(): void
{
$passenger = new Passenger('Frantisek Masa', '---', 'Brno');
$purpose = 'Cesta na střediskovku';
$command = new Command(1, null, $passenger, $purpose, 'Brno', '', Money::CZK(0), Money::CZK(0), '', null, [], '');

$command->addTransportTravel(
Money::CZK(100),
new Command\TravelDetails(
new Date('now'),
TransportType::get(TransportType::BUS),
'Praha',
'Brno',
),
);

$travel = $command->getTravels()[0];

$command->addReturnTravel($travel->getId());

$backTravel = $command->getTravels()[1];

$travelDetails = $travel->getDetails();
$backTravelDetails = $backTravel->getDetails();

self::assertNotEquals($travel->getId(), $backTravel->getId());
self::assertEquals($travelDetails->getDate(), $backTravelDetails->getDate());
self::assertEquals($travelDetails->getTransportType(), $backTravelDetails->getTransportType());
self::assertEquals($travelDetails->getEndPlace(), $backTravelDetails->getStartPlace());
self::assertEquals($travelDetails->getStartPlace(), $backTravelDetails->getEndPlace());
}

public function testVehicleTravelIsReturnAdded(): void
{
$passenger = new Passenger('Frantisek Masa', '---', 'Brno');
$purpose = 'Cesta na střediskovku';
$command = new Command(1, null, $passenger, $purpose, 'Brno', '', Money::CZK(3120), Money::CZK(500), '', null, [], '');

$command->addVehicleTravel(
123,
new Command\TravelDetails(
new Date('now'),
TransportType::get(TransportType::CAR),
'Praha',
'Brno',
),
);

$travel = $command->getTravels()[0];

$command->addReturnTravel($travel->getId());

$backTravel = $command->getTravels()[1];

$travelDetails = $travel->getDetails();
$backTravelDetails = $backTravel->getDetails();

self::assertNotEquals($travel->getId(), $backTravel->getId());
self::assertEquals($travelDetails->getDate(), $backTravelDetails->getDate());
self::assertEquals($travelDetails->getTransportType(), $backTravelDetails->getTransportType());
self::assertEquals($travelDetails->getEndPlace(), $backTravelDetails->getStartPlace());
self::assertEquals($travelDetails->getStartPlace(), $backTravelDetails->getEndPlace());
}

private function createCommand(Vehicle|null $vehicle = null): Command
{
return new Command(
Expand Down
Loading