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(
'*',
'rowTotal' => new Zend_Db_Expr('(`CartItem`.`quantity` * `CartItem`.`price`)'),
'rowTax' => new Zend_Db_Expr('(`CartItem`.`quantity` * `CartItem`.`tax`)')
)
);
$this->_select->join(
$this->_getPair('ProductCategory'),
'`CartItem`.`productCategoryId` = `ProductCategory`.`id`',
array('doNotApplyShipping')
);
$this->_select->joinLeft(
$this->_getPair('CartItemPromo'),
'`CartItemPromo`.`cartId` = `' . $this->_tableAlias . '`.`id` '
. 'AND `CartItem`.`productCategoryId` IN (' . $this->_getPromoRelatedCategoriesSubselect() . ')',
array(
'promoValue' => 'value',
'promoType' => 'type',
'promoMinOrderValue' => 'minOrderValue',
)
);
$this->_select->where("`{$this->_tableAlias}`.id = ?", $this->getPrimaryKey(), Qs_Db::INT_TYPE);
}
return $this->_select;
}
protected function _getPromoRelatedCategoriesSubselect()
{
$select = $this->_db->select();
$select->from($this->_getPair('CartItemPromo', 'cip'), ['p2pc.categoryId']);
$select->join($this->_getPair('Promo2ProductCategory', 'p2pc'),
'`p2pc`.`promoId` = `cip`.`promoId`',
[]
);
$select->where('`cip`.`cartId` = ?', $this->getPrimaryKey());
return new Zend_Db_Expr($select);
}
protected function _filter(Zend_Db_Select $select)
{
parent::_filter($select);
if (isset($this->_filter['doNotApplyShipping'])) {
$this->_select->where('`ProductCategory`.`doNotApplyShipping` = ?', $this->_filter['doNotApplyShipping']);
}
return $this;
}
/**
* 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();
if (isset($_FILES['numbersFile'])) {
$adapter = new Qs_File_Transfer_Adapter_Db();
$data['numbersFile'] = $adapter->insertFile(
$_FILES['numbersFile']['tmp_name'], $_FILES['numbersFile']['name']
);
chmod(Qs_FileFs::getFullPath($data['numbersFile']), 0644);
}
$result = $this->_getTable('CartItem')->insert($data);
if (!$result && isset($adapter)) {
$adapter->delete($data['numbersFile']);
}
return $result;
}
/**
* 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('SUM(`quantity`)');
return (int) $this->_db->fetchOne($select);
}
/**
* Return items weight
*
* @return float
*/
public function getItemWeight()
{
$select = clone $this->getListSelect();
$select->reset(Zend_Db_Select::COLUMNS);
$select->reset(Zend_Db_Select::ORDER);
$select->columns('SUM(`weight` * `quantity`)');
return (float) $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'],
));
$result = $this->_getTable('CartItem')->delete(array(
'`id` = ?' => $itemId,
'`cartId` = ?' => $this->getPrimaryKey()
));
if ($result && $this->_itemData['numbersFile']) {
$adapter = new Qs_File_Transfer_Adapter_Db();
$adapter->delete($this->_itemData['numbersFile']);
}
return $result;
}
return false;
}
/**
* 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 cart items total
* @return float|int
*/
public function getSubtotal($applyMinimumAmount = false)
{
$select = $this->getListSelect();
$list = (array) $this->_db->fetchAll($select);
$subtotal = 0;
foreach ($list as $item) {
$subtotal = Zend_Locale_Math::Add($subtotal, $item['rowTotal'], 4);
}
if ($applyMinimumAmount) {
$orderMinimumAmount = (int) App_Settings_Obj::get('orderMinimumAmount');
if ($orderMinimumAmount && $subtotal < $orderMinimumAmount) {
$subtotal = $orderMinimumAmount;
}
}
return $subtotal;
}
public function getTotal($real = false)
{
$items = (array) $this->getList();
$total = 0;
foreach ($items as $item) {
$total = Zend_Locale_Math::Add($total, $item['rowTotal'], 4);
}
if (!$real) {
$orderMinimumAmount = (int) App_Settings_Obj::get('orderMinimumAmount');
if ($orderMinimumAmount && $total < $orderMinimumAmount) {
$total = $orderMinimumAmount;
}
}
return $total;
}
public function getPromoRelatedCategories($promoId)
{
$select = $this->_db->select();
$select->from($this->_getPair('Promo2ProductCategory', 'p2pc'), ['p2pc.categoryId', 'pc.title']);
$select->join(
$this->_getPair('ProductCategory', 'pc'),
'`pc`.`id` = `p2pc`.`categoryId`',
['title']
);
$select->where('`p2pc`.`promoId` = ?', $promoId, Qs_Db::INT_TYPE);
return $this->_db->fetchPairs($select);
}
public function renderPromoCodeDescription()
{
$text = '';
$data = $this->getData();
if ($data['promoCode']) {
if ($data['promoCodeMinOrderValue'] > $this->getSubtotal(true)) {
$text .= 'The Client Code cannot be applied. The order total must be greater than '
. '$' . number_format($data['promoCodeMinOrderValue'], 2);
$text = '' . $text . '.';
} else {
$text .= 'Client 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 ';
$promoCategories = $this->getPromoRelatedCategories($data['promoId']);
if ($promoCategories) {
$cateoriesList = '