Skip to content

Commit

Permalink
Merge pull request #54 from conferencetools/admin-purchase-discounts
Browse files Browse the repository at this point in the history
Can now add a discount when creating purchases in admin panel
  • Loading branch information
carnage authored Jan 12, 2020
2 parents a065312 + 9cbffa9 commit 0e108e6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Controller/Admin/PurchaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
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;
use ConferenceTools\Attendance\Domain\Payment\PaymentType;
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;
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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];

Expand Down
20 changes: 20 additions & 0 deletions src/Form/NumberOfDelegates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']));
}

Expand Down Expand Up @@ -74,6 +90,10 @@ public function getInputFilterSpecification()
['name' => NotEmpty::class],
['name' => GreaterThan::class, 'options' => ['min' => 0]]
]
],
'discount_code' => [
'required' => false,
'allow_empty' => true,
]
];
}
Expand Down

0 comments on commit 0e108e6

Please sign in to comment.