/** * Generate vouchers for any items added to an order that are voucher * products. * * The product ID for the item must be listed in the "product-ids" config * element in the "voucher" group. * * The voucher amount is set to the *list price* of the item, not the net * or gross amount. * * The voucher ID is then set as the personalisation key "voucher_id" on the * relevant item. * * The queries for adding the voucher are added to the same transaction as * the item creation queries. * * @param Order\Event\EntityEvent $event The event object */ public function generateForSales(Order\Event\EntityEvent $event) { $item = $event->getEntity(); if (!$item instanceof Order\Entity\Item\Item) { return false; } $isVoucher = $item->getProduct()->getType()->getName() === 'voucher'; if (false === $isVoucher) { $this->_loadProductIDs(); // Skip if no voucher product IDs are defined in the config if (!$this->_voucherProductIDs || empty($this->_voucherProductIDs)) { return false; } if (!in_array($item->productID, $this->_voucherProductIDs)) { return false; } } $unit = $item->getUnit(); $voucher = new Voucher(); if ($unit && isset($unit->options['currency']) && isset($unit->options['amount'])) { $voucher->currencyID = $unit->options['currency']; $voucher->amount = $unit->options['amount']; } else { $voucher->currencyID = $item->order->currencyID; $voucher->amount = $item->actualPrice; } $voucher->id = $this->_idGenerator->generate(); $voucher->purchasedAsItem = $item; if ($item->order->user && !$item->order->user instanceof AnonymousUser) { $voucher->authorship->create(new DateTimeImmutable(), $item->order->user->id); } $this->_create->setTransaction($event->getTransaction()); $this->_create->create($voucher); $item->personalisation->voucher_id = $voucher->id; }
/** * Send a customer a notification. * * @param Event\EntityEvent $event */ public function sendCustomerNotification(Event\EntityEvent $event) { $note = $event->getEntity(); if (!$note instanceof Note or !$note->customerNotified) { return false; } $factory = $this->get('mail.factory.order.note.notification')->set('order', $event->getOrder())->set('note', $note); $this->get('mail.dispatcher')->send($factory->getMessage()); }
/** * Creates a transaction for contract payments when a payment is added to a * contract. * * @param Event\EntityEvent $event event carrying information about created * entity */ public function createPaymentTransaction(Event\EntityEvent $event) { $payment = $event->getEntity(); $order = $event->getOrder(); if ($payment instanceof Payment && $order->status->code === Statuses::PAYMENT_PENDING && null !== $order->id) { $transaction = new Transaction(); $transaction->records->add($payment); $transaction->type = Types::CONTRACT_PAYMENT; $this->get('order.transaction.create')->setDbTransaction($event->getTransaction())->create($transaction); } }
/** * Clears the "id" property on the `Payment` entity before it is created. * * This is because the "id" is sometimes temporarily filled with something * else as a way to identify the payment as unique. For example, voucher * payments have the voucher ID set as the payment ID during assembly. * * @param EntityEvent $event */ public function removeTemporaryIDs(EntityEvent $event) { $entity = $event->getEntity(); if (!$entity instanceof Payment) { return false; } // Skip if the ID is a MySQL variable. We need this! if ('@' === substr($entity->id, 0, 1)) { return false; } $entity->id = null; }
/** * Calculate the gross, tax and net amounts for each item in an order before * it gets created in the database. * * This event is skipped if the order is not taxable. * * @param EntityEvent $event The event object */ public function calculateTax(Event\EntityEvent $event) { $item = $event->getEntity(); if (!$item instanceof Item) { return false; } $this->_calculateTaxForItem($item); }