<?php
class ModelAccountReward extends Model {	
	public function getRewards($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "customer_reward` WHERE customer_id = '" . (int)$this->customer->getId() . "'";
		   
		$sort_data = array(
			'points',
			'description',
			'date_added'
		);
	
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY date_added";	
		}
			
		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 getMinusRewards1($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "customer_reward` WHERE customer_id = '" . (int)$this->customer->getId() . "'AND  points < 0 ";
		   
		$sort_data = array(
			'points',
			'description',
			'date_added'
		);
	
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY date_added";	
		}
			
		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 getPlusRewards1($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "customer_reward` WHERE customer_id = '" . (int)$this->customer->getId() . "' AND  points > 0 ";
		   
		$sort_data = array(
			'points',
			'description',
			'date_added'
		);
	
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY date_added";	
		}
			
		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 getPlusRewards($data = array()) {
      $query =  $this->db->query("SELECT o.order_id,r.currency,o.date_added,crd.reward_amount as reward,grd.reward_amount as reward1,op.name as description,r.sale_amount as points,r.owner_count FROM `" . DB_PREFIX . "order_product` op,`" . DB_PREFIX . "order` o,`" . DB_PREFIX . "rewards` r,`" . DB_PREFIX . "customer_rewards_distribution` crd,`" . DB_PREFIX . "group_rewards_distribution` grd WHERE o.order_id = r.order_id and r.product_id = op.product_id and r.reward_id = crd.reward_id and grd.reward_id = crd.reward_id and o.customer_id = '" . (int)$this->customer->getId() . "'");
		return $query->rows;
	}	

	public function getMinusRewards($data = array()) {
      $query =  $this->db->query("SELECT code,date_added,amount,voucher_id FROM `" . DB_PREFIX . "voucher`  WHERE customer_id = '" . (int)$this->customer->getId() . "'");
		return $query->rows;

	}	

	public function getTotalMinusRewards($data = array()) {
		$query =  $this->db->query("SELECT sum(amount) as total FROM `" . DB_PREFIX . "voucher`  WHERE customer_id = '" . (int)$this->customer->getId() . "'");
		if ($query->num_rows) {
			return $query->row['total'];
		} else {
			return '0';
		}
	}	
	
	public function getTotalRewards() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "customer_reward` WHERE customer_id = '" . (int)$this->customer->getId() . "'");
			
		return $query->row['total'];
	}	
			
	public function getTotalPoints() {
		$query = $this->db->query("SELECT SUM(points) AS total FROM `" . DB_PREFIX . "customer_reward` WHERE customer_id = '" . (int)$this->customer->getId() . "' GROUP BY customer_id");
		
		if ($query->num_rows) {
			return $query->row['total'];
		} else {
			return 0;	
		}
	}
}
?>