<?php  
class ModuleAccount extends Controller {
	function fetch() { 	
		$config   =& $this->locator->get('config');
		$language =& $this->locator->get('language');
		$session  =& $this->locator->get('session');
		$url      =& $this->locator->get('url');
		$database =& $this->locator->get('database');
		$customer =& $this->locator->get('customer');
		$response =& $this->locator->get('response');
		$request  =& $this->locator->get('request');	
		$template = $this->locator->create('template');  
				
		//total
		if (!$customer->isLogged()) { return NULL;}
		
        $customer_id=(int)$customer->getId();
        $email = $customer->getEmail(); 
			
		$customer_info=$database->getRow("select * from customer where customer_id=$customer_id"); 	
		
		$waiting_orders_count=$database->countRows("select order_id from `order_data` where customer_id=$customer_id and (order_status=1)"); 	
		$processing_orders_count=$database->countRows("select order_id from `order` where (customer_id=$customer_id) and (order_status_id in (1,2))"); 	
		$shipped_orders_count=$database->countRows("select order_id from `order` where (customer_id=$customer_id) and (order_status_id=3)"); 	
		$canceled_orders_count=$database->countRows("select order_id from `order_data` where customer_id=$customer_id and (order_status=0)"); 	
		
	    // collect waiting for reviews
    	$reviewed_products_rows=$database->getRows("select product_id from review where customer_id=$customer_id");        	            	
        foreach($reviewed_products_rows as $f_product){
          $reviewed_product_array[]=$f_product['product_id'];
        }
	    if(isset($reviewed_product_array)){
	    	$reviewed_product_set = implode(',', $reviewed_product_array);
	    	$sql="select op.itemcode, op.name, op.price, o.reference, o.date_added from `order_product` op left join `order` o on (op.order_id=o.order_id) where o.order_status_id=3 and o.customer_id=$customer_id and op.itemcode not in ($reviewed_product_set) group by op.itemcode order by op.order_id desc";
	    		    	   
	    }else{
	    	$sql="select op.itemcode, op.name, op.price, o.reference, o.date_added from `order_product` op left join `order` o on (op.order_id=o.order_id) where o.order_status_id=3 and o.customer_id=$customer_id group by op.itemcode order by op.order_id desc";	    	  
	    }	   
		$waiting_reviews_count=$database->countRows($sql);		
		
		// completed reviews
		$completed_reviews_count=$database->countRows("select review_id from `review` where customer_id=$customer_id"); 	
		$my_coupons_count=$database->countRows("select coupon_id from `coupon` where customer_id=$customer_id"); 	
		$my_tickets_count=$database->countRows("select ticket_id from `ticket` where customer_id=$customer_id"); 		
		$my_questions_count=$database->countRows("select question_id from `question` where `email` LIKE '.$email.'");
		$my_wishlist_count=$database->countRows("select wishlist_id from `wishlist` where customer_id=$customer_id");
		$my_affiliate_count=$database->countRows("select affiliate_id from `affiliate` where customer_id = $customer_id");				
		
		$template->set('customer_info', $customer_info);		
		$template->set('waiting_orders_count', $waiting_orders_count);	
		$template->set('processing_orders_count', $processing_orders_count);
		$template->set('shipped_orders_count', $shipped_orders_count);
		$template->set('canceled_orders_count', $canceled_orders_count);
		$template->set('waiting_reviews_count', $waiting_reviews_count);
		$template->set('completed_reviews_count', $completed_reviews_count);
		$template->set('my_coupons_count', $my_coupons_count);
		$template->set('my_tickets_count', $my_tickets_count);
		$template->set('my_questions_count', $my_questions_count);
		$template->set('my_wishlist_count', $my_wishlist_count);
		$template->set('my_affiliate_count', $my_affiliate_count);
				
		return $template->fetch('module/account.tpl');
		
		}  	
}

?>

