<?php
class ModelAffiliateAfforder extends Model {
	public function getOrder($order_id) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order` WHERE order_id = '" . (int)$order_id . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND order_status_id > '0'");
	
		return $query->row;	
	}
	 
	public function getOrders($start = 1, $limit = 20) {
		$query = $this->db->query("SELECT o.order_id, o.firstname, o.lastname, os.name as status, o.date_added, o.total, o.currency, o.value FROM `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_status os ON (o.order_status_id = os.order_status_id) WHERE customer_id = '" . (int)$this->customer->getId() . "' AND o.order_status_id > '0' AND os.language_id = '" . (int)$this->language->getId() . "' ORDER BY o.order_id DESC LIMIT " . (int)$start . "," . (int)$limit);	
	
		return $query->rows;
	}

	
	public function getTotalSaleOrder($affiliate_id) {
		$query = $this->db->query("SELECT sum(commission) as total FROM `" . DB_PREFIX . "order` a  WHERE order_status_id = 5 and a.affiliate_id = '" . (int)$affiliate_id . "' ");	
	
		return $query->row['total'];
	}
	
	
	public function getURLCount($affiliate_id) {
		$query = $this->db->query("SELECT urlcount FROM `" . DB_PREFIX . "affiliate_transaction` au  WHERE au.transaction_type=1 and au.affiliate_id = '" . (int)$affiliate_id . "' ");	
		if(isset($query->row['urlcount']))
		{
		return $query->row['urlcount'];
		}
	}
	public function getOrderProducts($order_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
	
		return $query->rows;
	}
	public function getOrderService($order_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'"." and product_id = '" . (int)$service_id . "'" );
	
		return $query->rows;
	}
	
	public function getOrderOptions($order_id, $order_product_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$order_product_id . "'");
	
		return $query->rows;
	}

	public function getOrderTotals($order_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int)$order_id . "'");
	
		return $query->rows;
	}	

	public function getOrderHistorys($order_id) {
		$query = $this->db->query("SELECT date_added, os.name AS status, oh.comment, oh.notify FROM " . DB_PREFIX . "order_history oh LEFT JOIN " . DB_PREFIX . "order_status os ON oh.order_status_id = os.order_status_id WHERE oh.order_id = '" . (int)$order_id . "' AND oh.notify = '1' AND os.language_id = '" . (int)$this->language->getId() . "' ORDER BY oh.date_added");
	
		return $query->rows;
	}	

	public function getServiceDetails($order_id, $order_product_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "service_details WHERE order_id = '" . (int)$order_id . "' AND product_id = '" . (int)$order_product_id . "'");
	
		return $query->rows;
	}	

	public function getServiceHistorys($order_id,$service_id) {
		$query = $this->db->query("SELECT date_added, os.name AS status,oh.AddedBy, oh.comment, oh.notify FROM " . DB_PREFIX . "service_history oh LEFT JOIN " . DB_PREFIX . "order_status os ON oh.order_status_id = os.order_status_id WHERE oh.order_id = '" . (int)$order_id . "' AND oh.notify = '1' AND os.language_id = '" . (int)$this->language->getId() . "' and oh.product_id = '" . (int)$service_id . "'  ORDER BY oh.date_added");
	
		return $query->rows;
	}	


	public function getOrderDownloads($order_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_download WHERE order_id = '" . (int)$order_id . "' ORDER BY name");
	
		return $query->rows; 
	}	

	public function getTotalOrders() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE customer_id = '" . (int)$this->customer->getId() . "' AND order_status_id > '0'");
		
		return $query->row['total'];
	}
		
	public function getTotalOrderProductsByOrderId($order_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
		
		return $query->row['total'];
	}	
	
	public function insertnote($order_id,$service_id,$comments) 
	{
		$query = $this->db->query("insert into " . DB_PREFIX . "service_history(order_id,product_id,order_status_id,AddedBy,notify,comment,date_added) values ('" . (int)$order_id . "','".(int)$service_id . "',9,'Customer',1,'" . $comments . "',sysdate())" ) ;					
	}	
	
	
	public function getReferralsDetails($affiliate_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "referrals a, " . DB_PREFIX . "customer c WHERE a.customer_id=c.customer_id and a.affiliate_id =" . (int)$affiliate_id . "");
		
		return $query->rows; 
	}	

	public function getReferralOrderDetails($affiliate_id) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order` o ," . DB_PREFIX . "referrals a, " . DB_PREFIX . "referralorders c WHERE a.customer_id=o.customer_id and  o.order_id=c.order_id and a.affiliate_id =" . (int)$affiliate_id . "");
		
		return $query->rows; 
	}	

	public function getAffilate($customer_id) {
		$query = $this->db->query("SELECT affiliate_id FROM " . DB_PREFIX . "referrals a WHERE a.customer_id='" . (int)$customer_id . "'");
	
		return $query; 
	}	

	public function getrefamount(){
		$refamt =  $this->db->query("SELECT * FROM " . DB_PREFIX . "setting a WHERE a.key = 'config_refamount'");
	  
		return $refamt->rows; 
	}	
	public function getrefbonus(){
		$refamt =  $this->db->query("SELECT * FROM " . DB_PREFIX . "setting a WHERE a.key = 'config_reforder'");
	  
		return $refamt->rows; 
	}	
	public function getrefpercent(){
		$refamt =  $this->db->query("SELECT * FROM " . DB_PREFIX . "setting a WHERE a.key = 'config_refpercentage'");
	  
		return $refamt->rows; 
	}
	
	public function getTotalReferrals($affiliate_id) {
    	$query = $this->db->query("SELECT COUNT(*) As tot FROM " . DB_PREFIX . "referrals a WHERE a.affiliate_id='" . (int)$affiliate_id . "'");
	
		  return $query->rows; 
	}	

	public function getTotalReferralsCount($affiliate_id) {
    	$query = $this->db->query("SELECT COUNT(*) As total FROM " . DB_PREFIX . "referrals a WHERE a.affiliate_id='" . (int)$affiliate_id . "'");
	
		  return $query->row['total']; 
	}	

	public function getTotalGuestCount($affiliate_id) {
    	$query = $this->db->query("SELECT COUNT(*) As total FROM `" . DB_PREFIX . "order` a WHERE a.affiliate_id='" . (int)$affiliate_id . "' and customer_id=0");
	
		  return $query->row['total']; 
	}	

	public function getAffAcctDetails($affiliate_id) {
    	$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "affaccount a WHERE a.payment_status=0 and a.affiliate_id='" . (int)$affiliate_id . "'");	
		  return $query->rows; 
	}	

	public function getAffhistoryDetails($affiliate_id) {
    	$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "affaccount WHERE payment_status=1 and affiliate_id='" . (int)$affiliate_id . "'");	
		  
		  return $query->rows; 
	}			
	public function getAffinprogdetails($affiliate_id) {
    	$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "affaccount WHERE payment_status=2 and affiliate_id='" . (int)$affiliate_id . "'");	
		  
		  return $query->rows; 
	}			
	public function getTotalGuestReferrals($affiliate_id) {
    	$query = $this->db->query("SELECT COUNT(*) As tot FROM `" . DB_PREFIX . "order` a,  " . DB_PREFIX . "referralorders b WHERE a.customer_id=0 and  a.order_id=b.order_id and b.affiliate_id='" . (int)$affiliate_id . "'");
	
		  return $query->rows; 
	}
}
?>