<?php 
class ControllerAccount extends Controller { 

	function index() {
		$customer =& $this->locator->get('customer');      
		$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');
		$currency =& $this->locator->get('currency');
		$database =& $this->locator->get('database');
		if (!$customer->isLogged()) {
	  		$session->set('redirect', $url->ssl('account'));
	  		$response->redirect($url->ssl('account_login'));
    	} 
		$language->load('controller/account.php');
		$template->set('title', $language->get('heading_title'));
    	$view = $this->locator->create('template');
    	
		$customer_id = (int)$customer->getId();
		$customer_info=$database->getRow("select customer_id, vip_rank, current_points, email from customer where customer_id=$customer_id");    
        $view->set('customer_info', $customer_info);            
        
    	$view->set('heading_title', $language->get('heading_title'));
    	$view->set('text_my_account', $language->get('text_my_account'));	
		$view->set('text_my_newsletter', $language->get('text_my_newsletter'));
		$view->set('text_my_Reviews', $language->get('text_my_Reviews'));
    	$view->set('text_information', $language->get('text_information'));
    	$view->set('text_password', $language->get('text_password'));
    	$view->set('text_address', $language->get('text_address'));
    	$view->set('text_history', $language->get('text_history'));
    	$view->set('text_download', $language->get('text_download'));
		$view->set('text_newsletter', $language->get('text_newsletter'));
    	$view->set('message', $session->get('message'));
		$session->delete('message');

    	$view->set('information', $url->ssl('account_edit'));
    	$view->set('password', $url->ssl('account_password'));
		$view->set('address', $url->ssl('account_address'));
    	$view->set('history', $url->ssl('account_history'));  
		$view->set('newsletter', $url->ssl('account_newsletter'));
		$view->set('question', $url->ssl('my_questions'));	
        
		$utc_time = new DateTimeZone('UTC');		
	    $customer_time_zone = $customer->getTimeZone(); // 'America/Los_Angeles'
	    
        $potential_order_data = array();
		$processing_order_data = array();
		$shipped_order_data = array();
		$canceled_order_data = array();			
      		
      // potential orders
    	$potential_order_rows = $database->getRows("SELECT * FROM  `order_data` WHERE  `customer_id` = $customer_id AND  `order_status` =1 ORDER BY `order_id` DESC");    
		
    	foreach ($potential_order_rows as $row) {
			
			$amla = new DateTimeZone($customer_time_zone); 
			$now_date = $row['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($row['data']);
				$potential_order_data[] = array(
					'date_added'=>$order_time,					 
					'reference' => $row['reference'],
					'href'       => $url->ssl('potential_order', FALSE, array('order_id' => $row['order_id'])),
					'status'     => 'Awaiting Payment', 
					'total'      => end( array_slice( end($order_data['totals']), -3, 1 ) )		 
					 );	
			}  

    	//Processing Orders  
    	$processing_order_rows = $database->getRows("SELECT o.*, 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 AND o.`order_status_id` IN ( 1, 2 ) order by order_id desc limit 8");
//    	print_r($processing_order_rows); exit;
    	foreach ($processing_order_rows as $row) {
    	
    		$amla = new DateTimeZone($customer_time_zone); 
			$now_date = $row['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');
			
    		$processing_order_data[] = array(
    			'date_added' => $order_time, // date($language->get('date_format_short'), strtotime($row['date_added']))
      			'reference'  => $row['reference'],
      			'href'       => $url->ssl('account_invoice', FALSE, array('order_id' => $row['order_id'])),      			
      			'status'     => $row['status'],      			 			
      			'total'      => $currency->format($row['total'], $row['currency'], $row['value'])
    		);
  		}
  		
  		 //Shipped Orders 
    	$shipped_order_rows = $database->getRows("select o.order_id, o.reference, o.firstname, o.lastname, os.name as status, o.date_added, o.total, o.currency, o.value, o.refund from `order` o left join order_status os on o.order_status_id = os.order_status_id where customer_id = $customer_id and (o.order_status_id = 3) order by order_id desc limit 8");
    	foreach ($shipped_order_rows as $row) {
    	
    		$amla = new DateTimeZone($customer_time_zone); 
			$now_date = $row['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');
			
    		$shipped_order_data[] = array(
    		    'date_added' => $order_time, // date($language->get('date_format_short'), strtotime($row['date_added']))
      			'reference'  => $row['reference'],
      			'href'       => $url->ssl('account_invoice', FALSE, array('order_id' => $row['order_id'])),      		
      			'status'     => $row['status'],      			 		
      			'total'      => $currency->format($row['total'], $row['currency'], $row['value'])     		
      			
    		);
  		}
  		
  		// canceled orders
    	$canceled_order_rows = $database->getRows("SELECT * FROM  `order_data` WHERE  `customer_id` =$customer_id AND  `order_status` =0 ORDER BY `order_id` DESC");    
	
    	foreach ($canceled_order_rows as $row) {
    		$amla = new DateTimeZone($customer_time_zone); 
			$now_date = $row['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($row['data']);
				$canceled_order_data[] = array(
					'date_added' => $order_time, // date($language->get('date_format_short'), strtotime($row['date_added']))			 
					'reference' => $row['reference'],
					'href'       => $url->ssl('potential_order', FALSE, array('order_id' => $row['order_id'])),
					'status'     => 'Canceled', 
					'total'      => end( array_slice( end($order_data['totals']), -3, 1 ) )		 
					 );	
    	}   		
	
		$view->set('potential_orders', $potential_order_data);	
		$view->set('processing_orders', $processing_order_data);	
		$view->set('shipped_orders', $shipped_order_data);	
		$view->set('canceled_orders', $canceled_order_data);
		
		$view->set('email', $customer->getEmail());
		$view->set('fullname', $customer->getFirstName().' '.$customer->getLastName());
			
		$template->set('content', $view->fetch('content/account.tpl'));
		$template->set($module->fetchAccount());	
    	$response->set($template->fetch('layout.tpl')); 

  	}

}

?>

