diff --git a/src/Controller/Admin/PurchaseController.php b/src/Controller/Admin/PurchaseController.php index 319807b..4cc041f 100644 --- a/src/Controller/Admin/PurchaseController.php +++ b/src/Controller/Admin/PurchaseController.php @@ -7,6 +7,7 @@ use ConferenceTools\Attendance\Domain\Delegate\DietaryRequirements; use ConferenceTools\Attendance\Domain\Delegate\Event\DelegateRegistered; use ConferenceTools\Attendance\Domain\Delegate\ReadModel\Delegate; +use ConferenceTools\Attendance\Domain\Discounting\ReadModel\DiscountCode; use ConferenceTools\Attendance\Domain\Payment\Command\ConfirmPayment; use ConferenceTools\Attendance\Domain\Payment\Command\SelectPaymentMethod; use ConferenceTools\Attendance\Domain\Payment\Event\PaymentRaised; @@ -14,6 +15,7 @@ use ConferenceTools\Attendance\Domain\Payment\ReadModel\Payment; use ConferenceTools\Attendance\Domain\Purchasing\Basket; use ConferenceTools\Attendance\Domain\Purchasing\Command\AllocateTicketToDelegate; +use ConferenceTools\Attendance\Domain\Purchasing\Command\ApplyDiscount; use ConferenceTools\Attendance\Domain\Purchasing\Command\Checkout; use ConferenceTools\Attendance\Domain\Purchasing\Command\PurchaseItems; use ConferenceTools\Attendance\Domain\Purchasing\Event\TicketsReserved; @@ -49,7 +51,14 @@ public function viewAction() public function createAction() { - $form = $this->form(NumberOfDelegates::class); + $discountOptions = []; + /** @var DiscountCode[] $discounts */ + $discounts = $this->repository(DiscountCode::class)->matching(Criteria::create()); + foreach ($discounts as $discount) { + $discountOptions[(string) $discount] = $discount->getDiscountType()->getName(); + } + + $form = $this->form(NumberOfDelegates::class, ['discountOptions' => $discountOptions]); $form->setAttribute('action', $this->url()->fromRoute('attendance-admin/purchase/delegates')); $form->setAttribute('method', 'GET'); @@ -126,6 +135,20 @@ public function delegatesAction() $messages = $this->messageBus()->fire(new PurchaseItems($email, (int) $delegates, new Basket($selectedTickets, []))); $purchaseId = $this->messageBus()->firstInstanceOf(TicketsReserved::class, ...$messages)->getId(); + if (isset($queryData['discount_code']) && !empty($queryData['discount_code'])) { + $code = $this->repository(DiscountCode::class)->get($queryData['discount_code']); + if ($code instanceof DiscountCode) { + $command = new ApplyDiscount( + $purchaseId, + $code->getDiscountType()->getId(), + $queryData['discount_code'], + $code->getDiscountType()->getDiscount() + ); + + $this->messageBus()->fire($command); + } + } + for ($i = 0; $i < $delegates; $i++) { $delegate = $data['delegate_' . $i]; diff --git a/src/Form/NumberOfDelegates.php b/src/Form/NumberOfDelegates.php index 80bf810..2f869e1 100644 --- a/src/Form/NumberOfDelegates.php +++ b/src/Form/NumberOfDelegates.php @@ -6,6 +6,7 @@ use Zend\Form\Element\DateTime; use Zend\Form\Element\MultiCheckbox; use Zend\Form\Element\Radio; +use Zend\Form\Element\Select; use Zend\Form\Element\Submit; use Zend\Form\Element\Text; use Zend\Form\Form; @@ -43,6 +44,21 @@ public function init() ], ]); + $discountOptions = $this->getOption('discountOptions'); + if (!empty($discountOptions)) { + $this->add([ + 'type' => Select::class, + 'name' => 'discount_code', + 'options' => [ + 'label' => 'Discount Code', + 'value_options' => array_merge( + ['' => 'None'], + $discountOptions + ) + ] + ]); + } + $this->add(new Submit('continue', ['label' => 'Continue'])); } @@ -74,6 +90,10 @@ public function getInputFilterSpecification() ['name' => NotEmpty::class], ['name' => GreaterThan::class, 'options' => ['min' => 0]] ] + ], + 'discount_code' => [ + 'required' => false, + 'allow_empty' => true, ] ]; }