<?php 

class ControllerAccountHistory extends Controller {	

	function index() {
ini_set("display_errors", "On");
error_reporting(1);

		$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');
  		$language->load('controller/account_history.php');
  		
    	if (!$customer->isLogged()) {
      		$session->set('redirect', $url->ssl('&status=process'));
	     	$response->redirect($url->ssl('account_login'));
    	} 
		if($request->has('page')){
		   $page=$request->get('page');
		}else{
			$page = 1;
		}      	    	
    	
    	$utc_time = new DateTimeZone('UTC');		
	    $customer_time_zone = $customer->getTimeZone(); // 'America/Los_Angeles'
	    
    	$payment_status = $request->get('status','get');
    	$view->set('order_status', $payment_status);    	
    	$view->set('heading_title', ucfirst($payment_status)." Orders");
    	$customer_id=(int)$customer->getId(); $order_data = array();
    	$sql="select *, os.name as status from `order` o left join `order_status` os on (o.order_status_id = os.order_status_id) where (o.customer_id = $customer_id)";
    	switch($payment_status){
    		case 'processing':
    			{
    				$sql.= " and (o.order_status_id in (1, 2))";
    				break;
    			}
    		case 'shipped':
    			{
    				$sql.= " and (o.order_status_id=3)";
    				break;
    			}
    	}
    	$sql.= " order by o.order_id desc";
    	$order_rows = $database->getRows($database->splitQuery($sql, $page, 10));
  		foreach ($order_rows as $f_order) { 
  			$amla = new DateTimeZone($customer_time_zone); 
			$now_date = $f_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[] = array(
    		    'date_added' => $order_time, // date('Y-m-d', strtotime($order['date_added'])),   
      			'reference'  => $f_order['reference'],
      			'href'       => $url->ssl('account_invoice', FALSE, array('order_id' => $f_order['order_id'])),      			
      			'status'     => $f_order['status'],      			       			
                'total'      => $currency->format($f_order['total'], $f_order['currency'], $f_order['value']), 	
      			'points'     => round($f_order['total'],2)
    		);
  		}
  	         		
  	    $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('account_history', NULL, array('status' => $request->get('status'))));		        			
  		
    	$view->set('text_order', $language->get('text_order'));
  		$view->set('text_status', $language->get('text_status'));
 		$view->set('text_date_added', $language->get('text_date_added'));
  		$view->set('text_customer', $language->get('text_customer'));
  		$view->set('text_products', $language->get('text_products'));
  		$view->set('text_total', $language->get('text_total'));   
    	$view->set('orders', $order_data);   
      	
      	$view->set('continue', $url->ssl('account'));
	  	$template->set('content', $view->fetch('content/account_history.tpl'));
	  	$template->set($module->fetchAccount());	
    	$response->set($template->fetch('layout.tpl')); 		
	}

	function page() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$url      =& $this->locator->get('url');
		if ($request->has('page', 'post')) {
			$session->set('account_history.page', $request->get('page', 'post'));
		}
		$response->redirect($url->ssl('account_history'));
	}
}
?>

