<?php
class ModelRewardRewardPromotion extends Model {
	public function addRewardPromotionSetting($data) {
		$this->db->query("INSERT INTO `" . DB_PREFIX . "reward_promotion_setting` SET reward_setting_id = '" . $this->db->escape(trim($data['reward_setting_id'])) . "',point = '" . $this->db->escape($data['point']) . "',start_date = '" . $this->db->escape($data['start_date']) . "',end_date = '" . $this->db->escape($data['end_date']) . "',status = '" . $this->db->escape($data['status']) . "',created_by = '" . $this->session->data['username'] . "', created_date = NOW(), updated_by = '" . $this->session->data['username'] . "', updated_date = NOW();");

		$reward_promotion_setting_id = $this->db->getLastId();
		$this->insertRewardPromotionSettingLog($reward_promotion_setting_id);

		return $reward_promotion_setting_id;
	}

	public function editRewardPromotionSetting($reward_promotion_setting_id, $data) {
		$this->db->query("UPDATE `" . DB_PREFIX . "reward_promotion_setting` SET reward_setting_id = '" . $this->db->escape(trim($data['reward_setting_id'])) . "',point = '" . $this->db->escape($data['point']) . "',start_date = '" . $this->db->escape($data['start_date']) . "',end_date = '" . $this->db->escape($data['end_date']) . "',status = '" . $this->db->escape($data['status']) . "',updated_by = '" . $this->session->data['username'] . "', updated_date = NOW() WHERE reward_promotion_setting_id = '" . (int)$reward_promotion_setting_id . "';");
        $this->insertRewardPromotionSettingLog($reward_promotion_setting_id);
	}

	public function deleteRewardPromotionSetting($reward_promotion_setting_id) {
		$this->db->query("DELETE FROM `" . DB_PREFIX . "reward_promotion_setting` WHERE reward_promotion_setting_id = '" . (int)$reward_promotion_setting_id . "'");
    }

	public function getRewardPromotionSetting($reward_promotion_setting_id) {
		$query = $this->db->query("SELECT *, (SELECT rs.title FROM `" . DB_PREFIX . "reward_setting` rs WHERE rs.reward_setting_id = rps.reward_setting_id) AS reward_setting_name FROM `" . DB_PREFIX . "reward_promotion_setting` rps WHERE rps.reward_promotion_setting_id = '" . (int)$reward_promotion_setting_id . "';");
        return $query->row;
	}

	public function getRewardPromotionSettings($data = array()) {
		$sql = "SELECT *, (SELECT rs.title FROM `" . DB_PREFIX . "reward_setting` rs WHERE rs.reward_setting_id = rps.reward_setting_id) AS reward_setting_name FROM `" . DB_PREFIX . "reward_promotion_setting` rps";

		$sort_data = array(
			'reward_setting_name',
			'status',
			'created_date'
		);

		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];
		} else {
			$sql .= " ORDER BY title";
		}

		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 getTotalRewardPromotionSettings() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "reward_promotion_setting`");
		return $query->row['total'];
	}

	public function insertRewardPromotionSettingLog($reward_promotion_setting_id) {
		$this->db->query("INSERT INTO `" . DB_PREFIX . "reward_promotion_setting_log` (reward_promotion_setting_id, reward_setting_id, point, start_date, end_date, status, created_by, created_date) SELECT reward_promotion_setting_id, reward_setting_id, point, start_date, end_date, status, updated_by as created_by, NOW() AS created_date FROM `" . DB_PREFIX . "reward_promotion_setting` WHERE reward_promotion_setting_id = '" . $reward_promotion_setting_id . "';");
	}

	public function getRewardPromotionSettingByRewardSettingId($reward_setting_id, $start_date, $end_date){
		$result = $this->db->query("SELECT * FROM  `" . DB_PREFIX . "reward_promotion_setting` 
		WHERE reward_setting_id = '" . $reward_setting_id. "'
		AND start_date <= '" . $end_date. "'
		AND (end_date >= '" . $start_date. "' OR end_date IS NULL OR end_date = '' )");
		
		if ($result->num_rows > 0) {
			return true;
		}
		else
		{
			return false;
		}
	
	}
}