<?php
class ModelAffiliateAffiliateGroup extends Model {
	public function addAffiliateGroup($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "affiliate_group SET name = '" . $this->db->escape($data['name']) . "',flatratesale = '" . $this->db->escape($data['flatratesale']) . "',flatratesaledesc = '" . $this->db->escape($data['flatratesaledesc']) . "', paypersale = '" . $this->db->escape($data['paypersale']) . "', paypersaledesc = '" . $this->db->escape($data['paypersaledesc']) . "',paypercust = '" . $this->db->escape($data['paypercust']) . "', paypercustdesc = '" . $this->db->escape($data['paypercustdesc']) . "',payclickdescr = '" . $this->db->escape($data['payclickdescr']) . "', paypersalecount = '" . $this->db->escape($data['paypersalecount']) . "', paypersalecountdesc = '" . $this->db->escape($data['paypersalecountdesc']) . "', creditguestref = '" . $this->db->escape($data['creditguestref']) . "', clickcount = '" . $this->db->escape($data['clickcount']) . "',guestcredit = '" . $this->db->escape($data['guestcredit']) . "',payclickcount = '" . $this->db->escape($data['payclickcount']) . "',flatfatesale_enable = '" . $this->db->escape($data['flatfatesale_enable']) . "',paypersale_enable = '" . $this->db->escape($data['paypersale_enable']) . "',guestreferral_enable = '" . $this->db->escape($data['guestreferral_enable']) . "', paypercust_enable = '" . $this->db->escape($data['paypercust_enable']) . "',paypersalecount_enable = '" . $this->db->escape($data['paypersalecount_enable']) . "',payclickcount_enable = '" . $this->db->escape($data['payclickcount_enable']) . "'");
	}

	public function editAffiliateGroup($affiliate_group_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "affiliate_group SET name = '" . $this->db->escape($data['name']) . "',flatratesale = '" . $this->db->escape($data['flatratesale']) . "',flatratesaledesc = '" . $this->db->escape($data['flatratesaledesc']) . "',paypersale = '" . $this->db->escape($data['paypersale']) . "', paypersaledesc = '" . $this->db->escape($data['paypersaledesc']) . "', paypercust = '" . $this->db->escape($data['paypercust']) . "', paypercustdesc = '" . $this->db->escape($data['paypercustdesc']) . "',payclickdescr = '" . $this->db->escape($data['payclickdescr']) . "', paypersalecount = '" . $this->db->escape($data['paypersalecount']) . "',paypersalecountdesc = '" . $this->db->escape($data['paypersalecountdesc']) . "', creditguestref = '" . $this->db->escape($data['creditguestref']) . "', clickcount = '" . $this->db->escape($data['clickcount']) . "',guestcredit = '" . $this->db->escape($data['guestcredit']) . "',payclickcount = '" . $this->db->escape($data['payclickcount']) . "',flatfatesale_enable = '" . $this->db->escape($data['flatfatesale_enable']) . "',paypersale_enable = '" . $this->db->escape($data['paypersale_enable']) . "',guestreferral_enable = '" . $this->db->escape($data['guestreferral_enable']) . "',paypercust_enable = '" . $this->db->escape($data['paypercust_enable']) . "',paypersalecount_enable = '" . $this->db->escape($data['paypersalecount_enable']) . "',payclickcount_enable = '" . $this->db->escape($data['payclickcount_enable']) . "'  WHERE affiliate_group_id = '" . (int)$affiliate_group_id . "'");
	}
	
	function mynewfunction()
    {
	 if(isset($this->request->get['affiliate_id'])){
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "affiliate_transaction af WHERE affiliate_id = '" . $this->request->get['affiliate_id'] . "' AND amount>0");
		return $query->rows;
	  }else{
		 return 0;
	  }
    }	
	
	function mynewfunctionminus()
    {
	 if(isset($this->request->get['affiliate_id'])){
     $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "affiliate_transaction af WHERE affiliate_id = '" . $this->request->get['affiliate_id'] . "' AND amount<0");
     return $query->rows;
	 }else{
		 return 0;
	  }
    }
	
	function mycombalance()
    {
	 if(isset($this->request->get['affiliate_id'])){
     $query = $this->db->query("SELECT SUM(amount) as total from " . DB_PREFIX . "affiliate_transaction WHERE affiliate_id = '" . $this->request->get['affiliate_id'] . "' AND amount>0");
     return $query->rows;
	  }else{
		 return 0;
	  }
    }
	
	function mycombalanceminus()
    {
	if(isset($this->request->get['affiliate_id'])){
     $query = $this->db->query("SELECT SUM(amount) as totalminus from " . DB_PREFIX . "affiliate_transaction WHERE affiliate_id = '" . $this->request->get['affiliate_id'] . "' AND amount<0");
     return $query->rows;
	  }else{
		 return 0;
	  }
    }
	
	function mycusfunction()
    {
     $query = $this->db->query("SELECT CONCAT(c.firstname, ' ', c.lastname) AS customername,c.email, r.customer_id, r.referred_date from " . DB_PREFIX . "customer c ," . DB_PREFIX . "referrals r  where c.customer_id=r.customer_id AND affiliate_id = '" . (int)$affiliate_id . "'");
     return $query->rows;
    }


	public function deleteAffiliateGroup($affiliate_group_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "affiliate_group WHERE affiliate_group_id = '" . (int)$affiliate_group_id . "'");

	}

	public function getAffiliateGroup($affiliate_group_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "affiliate_group WHERE affiliate_group_id = '" . (int)$affiliate_group_id . "'");

		return $query->row;
	}

	public function getAffiliateGroups($data = array()) {
		$sql = "SELECT * FROM " . DB_PREFIX . "affiliate_group";

		$sql .= " ORDER BY name";

		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}

		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalAffiliateGroups() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "affiliate_group");

		return $query->row['total'];
	}
	
	public function getTotalStoresByAffiliateGroupId($customer_group_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "store WHERE customer_group_id = '" . (int)$customer_group_id . "'");
		
		return $query->row['total'];		
	}	

}
?>