<?php 		
class ModelCatalogLoyalty extends Model {

	public function getLoyaltyDiscounts() {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "loyalty");
		return $query->rows;
	}
	
	public function getLoyaltyselect($data = array()) {
		$sql = "SELECT * FROM " . DB_PREFIX . "loyaltyselect";
		$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 getLoyaltyDiscountType($data = array()) {
		$sql = "SELECT * FROM " . DB_PREFIX . "loyaltyselect1";
		$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 deleteLoyalty($loyalty_id) {
		$this->db->query("DELETE FROM `" . DB_PREFIX . "loyalty` WHERE loyalty_id = '" . (int)$loyalty_id . "'");
	}
	public function editLoyalty( $data) {
		$this->load->model('setting/setting');

		$this->model_setting_setting->editSetting('loyalty', $this->request->post); 

		//$this->db->query("UPDATE `" . DB_PREFIX . "loyalty` SET SET percent = '" . $this->db->escape($data['percent']) . "',minorder = '" . $this->db->escape($data['minorder']) . "',date_end = '" . $this->db->escape($data['date_end']) . "',date_start = '" . $this->db->escape($data['date_start']) . "',numorder = '" . $this->db->escape($data['numorder']) . "',flat = '" . $this->db->escape($data['flat']) . "',ordertotal = '" . $this->db->escape($data['ordertotal']) . "', loyaltyselect1_id = '" . (int)$data['loyaltyselect1_id'] . "' discounttype = '" . (int)$data['discounttype'] . "' status = '" . (int)$data['status'] . "' WHERE loyalty_id = '" . (int)$loyalty_id . "'");
		$this->db->query("delete from " . DB_PREFIX . "loyalty "); 
		//$this->db->query("INSERT INTO `" . DB_PREFIX . "loyalty` SET percent = '" . $this->db->escape($data['percent']) . "',minorder = '" . $this->db->escape($data['minorder']) . "',date_end = '" . $this->db->escape($data['date_end']) . "',date_start = '" . $this->db->escape($data['date_start']) . "',numorder = '" . $this->db->escape($data['numorder']) . "',flat = '" . $this->db->escape($data['flat']) . "',ordertotal = '" . $this->db->escape($data['ordertotal']) . "', loyaltyselect1_id = '" . (int)$data['loyaltyselect1_id'] . "' loyaltyselect_id = '" . (int)$data['loyaltyselect_id'] . "' status = '" . (int)$data['status'] . "'");
		//$this->db->query("DELETE FROM " . DB_PREFIX . "loyalty WHERE loyalty_id = '" . (int)$loyalty_id . "'");

		foreach ($data['loyalty_discount'] as  $loyalty) {
			//$this->db->query("INSERT INTO `" . DB_PREFIX . "loyalty` SET percent = '" . $this->db->escape($loyalty['percent']) . "',minorder = '" . $this->db->escape($loyalty['minorder']) . "'");
			$this->db->query("INSERT INTO `" . DB_PREFIX . "loyalty` SET percent = '" . $this->db->escape($loyalty['percent']) . "',minorder = '" . $this->db->escape($loyalty['minorder']) . "',date_end = '" . $this->db->escape($loyalty['date_end']) . "',date_start = '" . $this->db->escape($loyalty['date_start']) . "',numorder = '" . $this->db->escape($loyalty['numorder']) . "',flat = '" . $this->db->escape($loyalty['flat']) . "',ordertotal = '" . $this->db->escape($loyalty['ordertotal']) . "', loyaltyselect_id = '" . (int)$loyalty['loyaltyselect_id'] . "', discounttype = '" . (int)$loyalty['discounttype'] . "', status = '" . (int)$loyalty['status'] . "'");
		}
	}
	public function addLoyalty($data) {
		$this->db->query("INSERT INTO `" . DB_PREFIX . "loyalty` SET percent = '" . $this->db->escape($data['percent']) . "',minorder = '" . $this->db->escape($data['minorder']) . "',date_end = '" . $this->db->escape($data['date_end']) . "',date_start = '" . $this->db->escape($data['date_start']) . "',numorder = '" . $this->db->escape($data['numorder']) . "',flat = '" . $this->db->escape($data['flat']) . "',ordertotal = '" . $this->db->escape($data['ordertotal']) . "', loyaltyselect1_id = '" . (int)$data['loyaltyselect_id1'] . "', loyaltyselect_id = '" . (int)$data['loyaltyselect_id'] . "', status = '" . (int)$data['status'] . "'");
		
		$loyalty_id = $this->db->getLastId();
		
		//foreach ($data['option_description'] as $language_id => $value) {
		//	$this->db->query("INSERT INTO " . DB_PREFIX . "option_description SET option_id = '" . (int)$option_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "'");
		//}

	}
}
?>