getPrimaryKey(); } return $this->_getTable()->searchBy(array('id' => $cartId), 'transactionId'); } /** * Create new shopping cart * * @param array $data * @return mixed */ public function create($data = array()) { return $this->_getTable()->insert($data); } /** * @return null|Zend_Db_Select */ public function getListSelect() { if (null === $this->_select) { parent::getListSelect(); $this->_select->join( $this->_getPair('CartItem'), '`CartItem`.`cartId` = `' . $this->_tableAlias . '`.`id`', array('*', 'summary' => new Zend_Db_Expr('(`CartItem`.`price` + `CartItem`.`paymentFee`)')) ); $this->_select->joinLeft( $this->_getPair('CartItemPromo'), '`CartItemPromo`.`cartId` = `' . $this->_tableAlias . '`.`id` ' . 'AND `CartItem`.`productId` = `CartItemPromo`.`productId`', array( 'promoValue' => 'value', 'promoType' => 'type', 'promoMinOrderValue' => 'minOrderValue', ) ); $this->_select->where("`{$this->_tableAlias}`.id = ?", $this->getPrimaryKey(), Qs_Db::INT_TYPE); } return $this->_select; } /** * Add new item into the shopping cart * * @param array $data * @return int|bool */ public function addItem(array $data) { unset($data['id'], $data['added'], $data['changed']); $data['cartId'] = $this->getPrimaryKey(); return $this->_getTable('CartItem')->insert($data); } public function updateItem($cartItemId, array $data) { unset($data['id'], $data['added'], $data['changed']); return $this->_getTable('CartItem')->update($data, array('id = ?' => $cartItemId)); } /** * Return shopping cart items count * * @return int */ public function getItemsCount() { $select = clone $this->getListSelect(); $select->reset(Zend_Db_Select::COLUMNS); $select->reset(Zend_Db_Select::ORDER); $select->columns('COUNT(*)'); return (int) $this->_db->fetchOne($select); } /** * Remove item from shopping cart * * @param int $itemId * @return int */ public function removeItem($itemId) { $this->_itemData = $this->getItem(array('id' => $itemId)); if ($this->_itemData) { $this->_getTable('CartItemPromo')->delete(array( '`cartId` = ?' => $this->getPrimaryKey(), '`productId` = ?' => $this->_itemData['productId'], )); if (App_ECommerce_Cart_View::ITEM_TYPE_EVENT === $this->_itemData['cartItemType']) { \App\Event\Obj::removeCartItems($this->_itemData['cartId'], $this->_itemData['productId']); } $removeStatus = $this->_getTable('CartItem')->delete(array( '`id` = ?' => $itemId, '`cartId` = ?' => $this->getPrimaryKey() )); $this->initSponsorshipFlagDb(); return $removeStatus; } return false; } public function initSponsorshipFlagDb() { if (($cartId = $this->getPrimaryKey())) { $sql = "UPDATE " . $this->_getTableName() . " AS `c` " . "SET `c`.`hasSponsorship` = IFNULL( ( SELECT 'y' FROM `" . $this->_getTableName('CartItem') . "` AS `ci` WHERE `ci`.`cartId` = " . $cartId . " AND `ci`.`cartItemType` = '" . App_ECommerce_Cart_View::ITEM_TYPE_SPONSOR . "' LIMIT 1 ), 'n') WHERE `c`.`id`= " . $cartId . ";"; $this->_db->query($sql); } return $this; } /** * Return cart item * * @param array $filter * @return array|bool */ public function getItem($filter) { if (!empty($filter)) { $select = clone $this->getListSelect(); Qs_Db::filter($select, $filter, 'CartItem'); return $this->_db->fetchRow($select); } return false; } /** * Return cat items total * @return float|int */ public function getSubtotal() { $select = $this->getListSelect(); $list = (array) $this->_db->fetchAll($select); $subtotal = 0; foreach ($list as $item) { $subtotal = Zend_Locale_Math::Add($subtotal, $item['price'], 4); } return $subtotal; } /** * Get total price * * @return float|int */ public function getTotal() { $items = (array) $this->getList(); $total = 0; foreach ($items as $item) { $total = Zend_Locale_Math::Add($total, $item['price'], 4); $total = Zend_Locale_Math::Add($total, $item['paymentFee'], 4); } return $total; } public function renderPromoCodeDescription() { $text = ''; $data = $this->getData(); if ($data['promoCode']) { if ($data['promoCodeMinOrderValue'] > $this->getSubtotal()) { $text .= 'The Promo Code cannot be applied. The order total must be greater than ' . '$' . number_format($data['promoCodeMinOrderValue'], 2); $text = '' . $text . '.'; } else { $text .= 'Promo code has been applied. '; if ($data['promoCodeType'] == App_ECommerce_Promo_Admin_View::PROMO_TYPE_PERCENT) { require_once 'lib/Smarty/app_plugins/modifier.money.php'; $text .= smarty_modifier_money($data['promoCodeValue'], 2, true) . '% '; } else { $text .= '$' . number_format($data['promoCodeValue'], 2, '.', ',') . ' '; } $text .= 'off '; if ($data['promoCodeProduct']) { $product = $this->getItem(array('productId' => $data['promoCodeProduct'])); if ($product) { $text .= 'for ' . htmlspecialchars($product['title']) . ' '; } } if ($data['promoCodeMinOrderValue'] > 0) { $text .= 'for orders over $' . number_format($data['promoCodeMinOrderValue'], 2, '.', ','); $text .= ''; } $text = '  ' . $text . '.'; } } return $text; } public function renderGiftCardDescription() { $text = ''; $data = $this->getData(); if ($data['giftCardCode']) { $giftCardObj = new App_ECommerce_GiftCard_Admin_Obj(); $codeData = $giftCardObj->getDataByCode($data['giftCardCode']); if (!empty($codeData)) { if ($codeData['statusId'] == App_ECommerce_GiftCard_Admin_View::CARD_STATUS_REDEEMED) { $text .= 'The Gift Card has been redeemed already.'; } else { $text .= 'Gift Card has been accepted. '; $text .= '$' . number_format($data['giftCardValue'], 2, '.', ',') . ' OFF!'; $text = '  ' . $text . ''; } } else { $text .= 'The Gift Card cannot be applied. Gift Card is not valid.'; } } return $text; } public function getObjectInfo() { $data = parent::getObjectInfo(); if (($itemId = Qs_Request::getRequestValue('itemId'))) { $item = $this->getItem(array('id' => $itemId)); if (!$item && $this->_itemData) { $item = $this->_itemData; } if ($item) { $data['itemTitle'] = $item['title']; } else { $data['itemTitle'] = 'UNKNOWN ITEM'; } } return $data; } public function isSponsorAtCart() { $select = $this->_db->select(); $select->from($this->_getPair('CartItem'), array('id')); $select->where('`CartItem`.`cartItemType` = ?', \App_ECommerce_Cart_View::ITEM_TYPE_SPONSOR); $select->where('`CartItem`.`cartId` = ?', $this->_primaryKey, \Qs_Db::INT_TYPE); $select->limit(1); return (bool) $this->_db->fetchOne($select); } /** * @param int|null $cartId * @return bool */ public static function isEventAtCart($cartId = null) { $select = \Qs_Db::getSelect(); $select->from(\Qs_Db::getPair('CartItem'), array('id')); $select->where('`CartItem`.`cartItemType` = ?', \App_ECommerce_Cart_View::ITEM_TYPE_EVENT); if (!(int)$cartId) { $cart = new App_ECommerce_Cart_Obj(); $cartId = $cart->getCartId(); } $select->where('`CartItem`.`cartId` = ?', $cartId, \Qs_Db::INT_TYPE); $select->limit(1); return (bool)\Qs_Db::getInstance()->fetchOne($select); } }