float, * 'attendee' => [ * [ * 'teamId' => int, * 'userId' => int, * 'firstName' => string, * 'lastName' => string, * 'email' => string, * 'amount' => string, * ] * ], * * ] * @throws \Exception * @throws \Qs_Exception_EmptyPropertyException * @return int|null */ public function insert(array $data = null) { $data = (null === $data) ? $this->_data : $data; // check eventId $this->getEventId(); if (!isset($data['total']) || empty($data['attendee'])) { throw new Exception('Not enough data'); } $auth = UserAuth::getInstance(); $user = ($auth->isLoggedIn()) ? $auth->getData() : null; $this->_clearErrors(); $this->_db->beginTransaction(); $boughtAttendeeIds = []; try { $event = $this->getEventData(); $attendees = (array) $data['attendee']; $attendeeTable = $this->_getTable('EventAttendee'); foreach ($attendees as &$attendee) { $attendee['eventId'] = $event['id']; $attendee['createdByName'] = ($user) ? $user['firstName'] . ' ' . $user['lastName'] : null; if (empty($attendee['amount'])) { $attendee['amount'] = null; $attendee['paymentType'] = null; $attendee['paymentDate'] = null; $attendee['registrationDate'] = date('Y-m-d H:i:s'); $attendee['bought'] = 'y'; $attendeeId = $attendeeTable->insert($attendee); $boughtAttendeeIds[] = $attendeeId; } else { $attendee['cartId'] = $this->_getCart()->getCartId(); $attendeeId = $attendeeTable->insert($attendee); $itemData = [ 'cartItemType' => EventEntity::CART_ITEM_TYPE, 'productId' => $attendeeId, 'productCategoryId' => $event['id'], 'title' => $event['title'], 'description' => $this->_getCartItemDescription($attendee), 'quantity' => 1, 'price' => $attendee['amount'], 'applyShipping' => 'n', 'image' => empty($event['image']) ? null : $event['image'], ]; $this->_getCart()->addItem($itemData); } $this->_insertAttendeeDependency($attendee, $attendeeId); } $this->_insertDependency(); if ($boughtAttendeeIds) { CartItemObj::sendSignupNotification($boughtAttendeeIds); } $this->_db->commit(); } catch (Exception $e) { $this->_db->rollBack(); Qs_Debug::processException($e); return false; } return $this->getPrimaryKey(); } public function update(array $data = null) { throw new Exception('Not supported'); } protected function _getCart() { if (null === $this->_cart) { $this->_cart = new App_ECommerce_Cart_Obj(); $this->_cart->setPrimaryKey($this->_cart->getCartId()); } return $this->_cart; } public function getInCartAttendees() { $select = $this->_getRegisteredAttendeesSelect(); $select->where('ea.bought IS NULL'); $select->join( $this->_getPair('CartItem', 'ci'), '`ci`.`cartId` = ' . $this->_db->quote($this->_getCart()->getCartId()) . ' AND `ci`.`cartItemType` = ' . $this->_db->quote(EventEntity::CART_ITEM_TYPE) . ' AND `ci`.`productId` = `ea`.`id`', [] ); return $this->_db->fetchAll($select); } /** * Filter bought and in cart attendees of current user * @param string $alias EventAttendee table alias * @return array */ public function getWhere($alias = null) { $aliasPrefix = (null === $alias) ? '' : "`{$alias}`."; $inCartExpr = $this->_getInCartExpr($alias); $where[] = new Zend_Db_Expr( "{$aliasPrefix}`bought` = 'y' OR ({$aliasPrefix}`bought` IS NULL AND EXISTS({$inCartExpr}))" ); return $where; } /** * @param string $alias EventAttendee table alias * @return \Zend_Db_Expr */ protected function _getInCartExpr($alias = null) { $aliasPrefix = (null === $alias) ? '' : "`{$alias}`."; $_cartId = (int) $this->_getCart()->getCartId(); $_cartItemTable = Qs_Db::getTableName('CartItem'); $sql = "SELECT 1 FROM `{$_cartItemTable}` `ci` " . "WHERE `ci`.`cartId` = {$_cartId} " . " AND `ci`.`productId` = {$aliasPrefix}`id` " . "LIMIT 1"; return new Zend_Db_Expr($sql); } private function _insertAttendeeDependency(array $attendee, $attendeeId) { $this->_insertAttendeeClaCarteItems(Qs_Array::get($attendee, 'alaCarteItems', []), $attendeeId); return $this; } private function _insertAttendeeClaCarteItems($items, $attendeeId) { if (empty($items)) { return $this; } foreach ($items as &$item) { $item = Qs_Array::map($item, ['originalId' => 'id', 'title', 'price']); } $this->_getTable('EventAttendeeAlaCarteItem')->saveRelations($items, compact('attendeeId')); return $this; } }