<?php 
class ControllerCanceledOrder extends Controller { 

	function index() {
	$config   =& $this->locator->get('config');
		$currency =& $this->locator->get('currency');
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		$request  =& $this->locator->get('request');	
		$view = $this->locator->create('template');
    	if (!$customer->isLogged()) {
      		$session->set('redirect', $url->ssl('canceled_order'));
	     	$response->redirect($url->ssl('account_login'));
    	} 

    	if($request->has('page')){
		   $page=$request->get('page');
		}else{
			$page = 1;
		}    
		$customer_id = $customer->getId();	
    	$utc_time = new DateTimeZone('UTC');		
	    $customer_time_zone = $customer->getTimeZone(); // 'America/Los_Angeles'	    
	
		$canceled_order_data = array();
		  
		$canceled_order_rows = $database->getRows($database->splitQuery("select * from `order_data` where customer_id = $customer_id and order_status=0 order by order_id desc", $page, 10));
    	foreach ($canceled_order_rows as $f_canceled_order) {
    		$amla = new DateTimeZone($customer_time_zone); 
			$now_date = $f_canceled_order['date_added']; // date('Y-m-d H:i:s');			
			$two_past_midnight = new DateTime($now_date);
			$two_past_midnight->setTimeZone($amla);
			$order_time=$two_past_midnight->format('Y-m-d H:i:s');
    		
			$order_data = unserialize($f_canceled_order['data']);
				$canceled_order_data[] = array(					 
					 'grand_total'  => end( array_slice( end($order_data['totals']), -3, 1 ) ),					 
					 'expire'=> $f_canceled_order['expire'],	
					 'date_added'=>$order_time,				 
					 'reference' => $f_canceled_order['reference'],					 
					 'order_id'  => $f_canceled_order['order_id'],
					 'href'       => $url->ssl('potential_order', FALSE, array('order_id' => $f_canceled_order['order_id']))				 
					 );				
    	}
		$view->set('heading_title', 'Canceled Orders');
		$view->set('canceled_orders', $canceled_order_data);	
		
		 $page_data = array();
	    for ($i = 1; $i <= $database->getPages(); $i++) {
				$page_data[] = array(
	  				'text'  => $language->get('text_pages', $i, $database->getPages()),
	  				'value' => $i
				);
	      	}
	    $view->set('pages', $page_data);  	      	        		
  		$view->set('page', $page);	
  		$view->set('text_results', $language->get('text_results',$page, $database->getPages()));		
  		$view->set('action', $url->href('canceled_order'));	
  		
		$template->set('content', $view->fetch('content/canceled_order.tpl'));
		$template->set($module->fetchAccount());	
    	$response->set($template->fetch('layout.tpl')); 
	}	
}
?>