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

Allow custom orders #64

Merged
merged 2 commits into from
Nov 9, 2020
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
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ jobs:
- name: Lint PHP
run: composer ci:php:lint

- name: Unit Tests
run: |
sudo timedatectl set-timezone Europe/Berlin
sudo systemctl start mysql.service
composer ci:tests:unit

- name: Functional Tests
env:
typo3DatabaseName: test
Expand Down
5 changes: 2 additions & 3 deletions Classes/Controller/CheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,16 @@ public function formAction(Period $period)

/**
* @param \JWeiland\Reserve\Domain\Model\Order $order
* @param int $amountOfPeople
* @TYPO3\CMS\Extbase\Annotation\Validate("JWeiland\Reserve\Domain\Validation\OrderValidator", param="order")
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
*/
public function createAction(Order $order, int $amountOfPeople = 1)
public function createAction(Order $order)
{
if (!$order->_isNew() || !OrderSessionUtility::isUserAllowedToOrder($order->getBookedPeriod()->getFacility()->getUid())) {
$this->addFlashMessage('You are not allowed to order right now.', '', AbstractMessage::ERROR);
return $this->redirect('list');
}
if ($this->checkoutService->checkout($order, $amountOfPeople, (int)$this->settings['orderPid'])) {
if ($this->checkoutService->checkout($order, (int)$this->settings['orderPid'])) {
$this->checkoutService->sendConfirmationMail($order);
} else {
$this->addFlashMessage(
Expand Down
130 changes: 130 additions & 0 deletions Classes/Domain/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ class Order extends AbstractEntity
*/
protected $email = '';

/**
* @var string
*/
protected $phone = '';

/**
* @var string
*/
protected $address = '';

/**
* @var string
*/
protected $zip = '';

/**
* @var string
*/
protected $city = '';

/**
* @TYPO3\CMS\Extbase\Annotation\ORM\Transient
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\JWeiland\Reserve\Domain\Model\Participant>
*/
protected $participants = [];

/**
* @TYPO3\CMS\Extbase\Annotation\ORM\Cascade("remove")
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\JWeiland\Reserve\Domain\Model\Reservation>
Expand All @@ -66,6 +92,7 @@ class Order extends AbstractEntity
public function __construct()
{
$this->reservations = new ObjectStorage();
$this->participants = new ObjectStorage();
}

/**
Expand Down Expand Up @@ -164,6 +191,90 @@ public function setEmail(string $email)
$this->email = $email;
}

/**
* @param string $phone
*/
public function setPhone(string $phone)
{
$this->phone = $phone;
}

/**
* @return string
*/
public function getPhone(): string
{
return $this->phone;
}

/**
* @param string $address
*/
public function setAddress(string $address)
{
$this->address = $address;
}

/**
* @return string
*/
public function getAddress(): string
{
return $this->address;
}

/**
* @param string $zip
*/
public function setZip(string $zip)
{
$this->zip = $zip;
}

/**
* @return string
*/
public function getZip(): string
{
return $this->zip;
}

/**
* @param string $city
*/
public function setCity(string $city)
{
$this->city = $city;
}

/**
* @return string
*/
public function getCity(): string
{
return $this->city;
}

/**
* @param ObjectStorage|Participant[] $participants
*/
public function setParticipants(ObjectStorage $participants)
{
foreach ($participants as $participant) {
if ($participant->getFirstName() || $participant->getLastName()) {
$this->participants->attach($participant);
}
}
}

/**
* @return ObjectStorage|Participant[]
*/
public function getParticipants(): ObjectStorage
{
return $this->participants;
}

/**
* @return ObjectStorage|Reservation[]
*/
Expand Down Expand Up @@ -221,4 +332,23 @@ public function getCancelableUntil()
}
return $cancelableUntil;
}

/**
* @return bool
*/
public function canBeBooked(): bool
{
$numberOfParticipants = $this->getParticipants()->count();
return $numberOfParticipants > 0
&& $numberOfParticipants <= $this->getBookedPeriod()->getMaxParticipantsPerOrder()
;
}

/**
* @return bool
*/
public function shouldBlockFurtherOrdersForFacility(): bool
{
return true;
}
}
60 changes: 60 additions & 0 deletions Classes/Domain/Model/Participant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package jweiland/reserve.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace JWeiland\Reserve\Domain\Model;

/**
* A participant is transient part of an order
*/
class Participant
{
/**
* @var string
*/
protected $firstName = '';

/**
* @var string
*/
protected $lastName = '';

/**
* @return string
*/
public function getFirstName(): string
{
return $this->firstName;
}

/**
* @param string $firstName
*/
public function setFirstName(string $firstName)
{
$this->firstName = $firstName;
}

/**
* @return string
*/
public function getLastName(): string
{
return $this->lastName;
}

/**
* @param string $lastName
*/
public function setLastName(string $lastName)
{
$this->lastName = $lastName;
}
}
15 changes: 15 additions & 0 deletions Classes/Domain/Model/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,21 @@ public function getMaxParticipantsPerOrder(): int
return $this->maxParticipantsPerOrder > $remaining ? $remaining : $this->maxParticipantsPerOrder;
}

/**
* Allows to iterate over the number of allowed participants.
*
* @return array
*/
public function getMaxParticipantsPerOrderIterable(): array
{
$available = $this->getMaxParticipantsPerOrder();
if ($available === 0) {
return [];
}

return range(1, $available);
}

/**
* @param int $maxParticipantsPerOrder
*/
Expand Down
42 changes: 42 additions & 0 deletions Classes/Domain/Model/Reservation.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ class Reservation extends AbstractEntity
*/
protected $customerOrder;

/**
* @var string
*/
protected $firstName = '';

/**
* @var string
*/
protected $lastName = '';

/**
* @var string
*/
Expand All @@ -49,6 +59,38 @@ public function setCustomerOrder(Order $customerOrder)
$this->customerOrder = $customerOrder;
}

/**
* @return string
*/
public function getFirstName(): string
{
return $this->firstName;
}

/**
* @param string $firstName
*/
public function setFirstName(string $firstName)
{
$this->firstName = $firstName;
}

/**
* @return string
*/
public function getLastName(): string
{
return $this->lastName;
}

/**
* @param string $lastName
*/
public function setLastName(string $lastName)
{
$this->lastName = $lastName;
}

/**
* @return string
*/
Expand Down
6 changes: 6 additions & 0 deletions Classes/Domain/Model/v8/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class Order extends \JWeiland\Reserve\Domain\Model\Order
*/
protected $email = '';

/**
* @transient
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\JWeiland\Reserve\Domain\Model\Participant>
*/
protected $participants = [];

/**
* @cascade remove
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\JWeiland\Reserve\Domain\Model\Reservation>
Expand Down
26 changes: 26 additions & 0 deletions Classes/Domain/Validation/OrderValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use JWeiland\Reserve\Domain\Model\Order;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;

Expand All @@ -21,6 +23,16 @@
*/
class OrderValidator extends AbstractValidator
{
/**
* @var Dispatcher
*/
protected $dispatcher;

public function injectDispatcher(Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}

protected function isValid($order)
{
if (!$order instanceof Order) {
Expand All @@ -33,5 +45,19 @@ protected function isValid($order)
if (!GeneralUtility::validEmail($order->getEmail())) {
$this->addError('The selected email is not valid!', 1590480086004);
}

$this->attachForeignResults($order);
}

protected function attachForeignResults(Order $order)
{
$results = new ObjectStorage();
$this->dispatcher->dispatch(__CLASS__, 'validateOrder', [
'order' => $order,
'errorResults' => $results,
]);
foreach ($results as $result) {
$this->result->merge($result);
}
}
}
Loading