setId('customerpurchasesGrid'); $this->setUseAjax(true); } protected function _prepareCollection() { $customer_id = $id = $this->getRequest()->getParam('id'); $collection = Mage::getModel('sales/order_item')->getCollection(); if (preg_match('/^1.4.0/', Mage::getVersion())) { $collection->getSelect() ->from('', array( 'entity_id' => 'i.entity_id', 'increment_id' => 'i.increment_id', 'is_active' => 'i.is_active', 'p_updated_at' => 'i.updated_at', 'sku' => 'main_table.sku', 'name' => 'main_table.name', 'price' => 'main_table.base_price', 'total' => '(main_table.price*main_table.qty_ordered)', 'qty' => 'main_table.qty_ordered', 'order_link' => 'main_table.order_id', 'order_status' => 'i.status', 'discount' => 'main_table.discount_amount', 'row_sum_p' => '(main_table.row_total-main_table.discount_amount)', 'row_sum_b' => '(main_table.base_row_total-main_table.base_discount_amount)', )) ->joinInner(array('i' => $collection->getTable('sales/order')), 'main_table.order_id = i.entity_id') ->where('i.customer_id = ?', $customer_id) ->where('main_table.parent_item_id is NULL'); } else if (preg_match('/^1.3/', Mage::getVersion())) { $collection->getSelect() ->from('', array( 'entity_id' => 'i.entity_id', 'increment_id' => 'i.increment_id', 'is_active' => 'i.is_active', 'p_updated_at' => 'i.updated_at', 'sku' => 'main_table.sku', 'name' => 'main_table.name', 'price' => 'main_table.base_price', 'total' => '(main_table.price*main_table.qty_ordered)', 'qty' => 'main_table.qty_ordered', 'order_link' => 'main_table.order_id', 'order_status' => 'j.value', 'discount' => 'main_table.discount_amount', 'row_sum_p' => '(main_table.row_total-main_table.discount_amount)', 'row_sum_b' => '(main_table.base_row_total-main_table.base_discount_amount)', )) ->joinInner(array('i' => $collection->getTable('sales/order')), 'main_table.order_id = i.entity_id') ->joinInner(array('j' => $collection->getTable('sales/order').'_varchar'), 'main_table.order_id = j.entity_id') ->joinInner(array('k' => $collection->getTable('eav/attribute')), 'k.attribute_id = j.attribute_id') ->where('i.customer_id = ?', $customer_id) ->where('k.attribute_code = "status"') ->where('main_table.parent_item_id is NULL'); } else { $collection->getSelect() ->from('', array( 'entity_id' => 'i.entity_id', 'increment_id' => 'i.increment_id', 'p_updated_at' => 'i.updated_at', 'sku' => 'main_table.sku', 'name' => 'main_table.name', 'price' => 'main_table.base_price', 'total' => '(main_table.price*main_table.qty_ordered)', 'qty' => 'main_table.qty_ordered', 'order_link' => 'main_table.order_id', 'order_status' => 'i.status', 'discount' => 'main_table.base_discount_amount', 'row_sum_p' => '(main_table.row_total-main_table.discount_amount)', 'row_sum_b' => '(main_table.base_row_total-main_table.base_discount_amount)', )) ->joinInner(array('i' => $collection->getTable('sales/order')), 'main_table.order_id = i.entity_id') ->where('i.customer_id = ?', $customer_id) ->where('main_table.parent_item_id is NULL'); } $this->setCollection($collection); parent::_prepareCollection(); foreach ($collection as $key => $value) { $url = $this->getUrl('adminhtml/sales_order/view', array('order_id' => $value->getEntityId())); $value->setOrderLink('View'); $value->setItemStatus($value->getStatus()); //assign values to ordered currency code $order_currency_code=$value->getOrder()->getOrderCurrencyCode(); $value->setTotal(Mage::app()->getLocale()->currency($order_currency_code)->toCurrency($value->getTotal())); $value->setDiscount(Mage::app()->getLocale()->currency($order_currency_code)->toCurrency($value->getDiscount())); $value->setRow_sum_p(Mage::app()->getLocale()->currency($order_currency_code)->toCurrency($value->getRow_sum_p())); $options = $value->getProductOptions(); $options_html = ''; if (isset($options['options'])) { foreach ($options['options'] as $option) { $options_html .= '
'.$option['label'].'
'.$option['value'].''; } } $value->setName($value->getName().$options_html); } return; } protected function _prepareColumns() { //Order#, Purchase On, SKU, Product, Price, Qty, View Order (Link to order) $this->addColumn('order_id', array( 'header' => Mage::helper('customerpurchases')->__('Order #'), 'index' => 'increment_id', 'type' => 'text', )); $this->addColumn('purchase_on', array( 'header' => Mage::helper('customerpurchases')->__('Purchase On'), 'index' => 'p_updated_at', 'type' => 'datetime', 'filter_condition_callback' => array($this, 'filter_purchaseon_callback'), )); $this->addColumn('SKU', array( 'header' => Mage::helper('customerpurchases')->__('SKU'), 'index' => 'sku', 'type' => 'text', )); $this->addColumn('Product', array( 'header' => Mage::helper('customerpurchases')->__('Product'), 'index' => 'name', 'type' => 'text', )); $this->addColumn('qty', array( 'header' => Mage::helper('customerpurchases')->__('Qty'), 'index' => 'qty', 'type' => 'number', 'filter_condition_callback' => array($this, 'filter_qty_callback'), )); $this->addColumn('item_status', array( 'header' => Mage::helper('customerpurchases')->__('Item Status'), 'index' => 'item_status', 'type' => 'text', 'filter' => false, 'sortable' => false, )); $store = $this->_getStore(); $this->addColumn('price', array( 'header' => Mage::helper('customerpurchases')->__('Price'), 'index' => 'base_price', 'type' => 'currency', 'currency_code' => $store->getBaseCurrencyCode(), )); $this->addColumn('total', array( 'header' => Mage::helper('customerpurchases')->__('Subtotal'), 'index' => 'total', 'type' => 'currency', //'currency_code' => 'order_currency_code', 'filter_condition_callback' => array($this, 'filter_total_callback'), )); $this->addColumn('discount', array( 'header' => Mage::helper('customerpurchases')->__('Discount amount'), 'index' => 'discount', 'type' => 'currency', //'currency' => 'order_currency_code', 'filter_condition_callback' => array($this, 'filter_discount_callback'), )); $this->addColumn('row_sum_b', array( 'header' => Mage::helper('customerpurchases')->__('G.T. (Base)'), 'index' => 'row_sum_b', 'type' => 'currency', 'currency_code' => $store->getBaseCurrencyCode(), 'filter_condition_callback' => array($this, 'filter_rowsum_b_callback'), )); $this->addColumn('row_sum_p', array( 'header' => Mage::helper('customerpurchases')->__('G.T. (Purchased)'), 'index' => 'row_sum_p', 'type' => 'currency', //'currency' => 'order_currency_code', 'filter_condition_callback' => array($this, 'filter_rowsum_p_callback'), )); $this->addColumn('order_status', array( 'header' => Mage::helper('customerpurchases')->__('Status'), 'index' => 'order_status', 'type' => 'options', 'options' => Mage::getSingleton('sales/order_config')->getStatuses(), 'filter_condition_callback' => array($this, 'filter_orderstatus_callback'), )); $this->addColumn('view_order', array( 'header' => Mage::helper('customerpurchases')->__('Action'), 'index' => 'order_link', 'type' => 'text', 'filter' => false, 'sortable' => false, )); return parent::_prepareColumns(); } protected function filter_total_callback($collection, $column) { $fromTo = $column->getFilter()->getValue(); if(!@$fromTo['from'] && !@$fromTo['to']) return; $fromExpr = $toExpr = null; $cond = array(); if(@$fromTo['from']) $cond[] = "main_table.price*main_table.qty_ordered >= '".$fromTo['from']."'"; if(@$fromTo['to']) $cond[] = "main_table.price*main_table.qty_ordered <= '".$fromTo['to']."'"; $collection->getSelect()->where("(".implode(' AND ', $cond).")"); } protected function filter_qty_callback($collection, $column) { $fromTo = $column->getFilter()->getValue(); if(!@$fromTo['from'] && !@$fromTo['to']) return; $fromExpr = $toExpr = null; $cond = array(); if(@$fromTo['from']) $cond[] = "main_table.qty_ordered >= '".$fromTo['from']."'"; if(@$fromTo['to']) $cond[] = "main_table.qty_ordered <= '".$fromTo['to']."'"; $collection->getSelect()->where("(".implode(' AND ', $cond).")"); } protected function filter_purchaseon_callback($collection, $column) { $fromTo = $column->getFilter()->getValue(); if(!@$fromTo['from'] && !@$fromTo['to']) return; $fromExpr = $toExpr = null; $cond = array(); if(@$fromTo['from']) { $fromExpr = date('Y-m-d H:i:s', $fromTo['from']->getTimestamp()); $cond[] = "main_table.updated_at >= '$fromExpr'"; } if(@$fromTo['to']) { $toExpr = date('Y-m-d H:i:s', $fromTo['to']->getTimestamp()); $cond[] = "main_table.updated_at <= '$toExpr'"; } $collection->getSelect()->where("(".implode(' AND ', $cond).")"); } protected function filter_orderstatus_callback($collection, $column) { $val = $column->getFilter()->getValue(); if(!@val) return; if (preg_match('/^1.3/', Mage::getVersion())) { if(@$val) $cond = "j.value = '".$val."'"; } else { if(@$val) $cond = "i.status = '".$val."'"; } $collection->getSelect()->where($cond); } protected function filter_discount_callback($collection, $column) { $fromTo = $column->getFilter()->getValue(); if(!@$fromTo['from'] && !@$fromTo['to']) return; $fromExpr = $toExpr = null; $cond = array(); if(@$fromTo['from']) $cond[] = "main_table.discount_amount >= '".$fromTo['from']."'"; if(@$fromTo['to']) $cond[] = "main_table.discount_amount <= '".$fromTo['to']."'"; $collection->getSelect()->where("(".implode(' AND ', $cond).")"); } protected function filter_rowsum_p_callback($collection, $column) { $fromTo = $column->getFilter()->getValue(); if(!@$fromTo['from'] && !@$fromTo['to']) return; $fromExpr = $toExpr = null; $cond = array(); if(@$fromTo['from']) $cond[] = "main_table.row_total-main_table.discount_amount >= '".$fromTo['from']."'"; if(@$fromTo['to']) $cond[] = "main_table.row_total-main_table.discount_amount <= '".$fromTo['to']."'"; $collection->getSelect()->where("(".implode(' AND ', $cond).")"); } protected function filter_rowsum_b_callback($collection, $column) { $fromTo = $column->getFilter()->getValue(); if(!@$fromTo['from'] && !@$fromTo['to']) return; $fromExpr = $toExpr = null; $cond = array(); if(@$fromTo['from']) $cond[] = "main_table.base_row_total-main_table.base_discount_amount >= '".$fromTo['from']."'"; if(@$fromTo['to']) $cond[] = "main_table.base_row_total-main_table.base_discount_amount <= '".$fromTo['to']."'"; $collection->getSelect()->where("(".implode(' AND ', $cond).")"); } protected function _getStore() { $storeId = (int) $this->getRequest()->getParam('store', 0); return Mage::app()->getStore($storeId); } }