shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

coupon.php (6319B)


      1 <?php
      2 class ModelMarketingCoupon extends Model {
      3 	public function addCoupon($data) {
      4 		$this->db->query("INSERT INTO " . DB_PREFIX . "coupon SET name = '" . $this->db->escape($data['name']) . "', code = '" . $this->db->escape($data['code']) . "', discount = '" . (float)$data['discount'] . "', type = '" . $this->db->escape($data['type']) . "', total = '" . (float)$data['total'] . "', logged = '" . (int)$data['logged'] . "', shipping = '" . (int)$data['shipping'] . "', date_start = '" . $this->db->escape($data['date_start']) . "', date_end = '" . $this->db->escape($data['date_end']) . "', uses_total = '" . (int)$data['uses_total'] . "', uses_customer = '" . (int)$data['uses_customer'] . "', status = '" . (int)$data['status'] . "', date_added = NOW()");
      5 
      6 		$coupon_id = $this->db->getLastId();
      7 
      8 		if (isset($data['coupon_product'])) {
      9 			foreach ($data['coupon_product'] as $product_id) {
     10 				$this->db->query("INSERT INTO " . DB_PREFIX . "coupon_product SET coupon_id = '" . (int)$coupon_id . "', product_id = '" . (int)$product_id . "'");
     11 			}
     12 		}
     13 
     14 		if (isset($data['coupon_category'])) {
     15 			foreach ($data['coupon_category'] as $category_id) {
     16 				$this->db->query("INSERT INTO " . DB_PREFIX . "coupon_category SET coupon_id = '" . (int)$coupon_id . "', category_id = '" . (int)$category_id . "'");
     17 			}
     18 		}
     19 
     20 		return $coupon_id;
     21 	}
     22 
     23 	public function editCoupon($coupon_id, $data) {
     24 		$this->db->query("UPDATE " . DB_PREFIX . "coupon SET name = '" . $this->db->escape($data['name']) . "', code = '" . $this->db->escape($data['code']) . "', discount = '" . (float)$data['discount'] . "', type = '" . $this->db->escape($data['type']) . "', total = '" . (float)$data['total'] . "', logged = '" . (int)$data['logged'] . "', shipping = '" . (int)$data['shipping'] . "', date_start = '" . $this->db->escape($data['date_start']) . "', date_end = '" . $this->db->escape($data['date_end']) . "', uses_total = '" . (int)$data['uses_total'] . "', uses_customer = '" . (int)$data['uses_customer'] . "', status = '" . (int)$data['status'] . "' WHERE coupon_id = '" . (int)$coupon_id . "'");
     25 
     26 		$this->db->query("DELETE FROM " . DB_PREFIX . "coupon_product WHERE coupon_id = '" . (int)$coupon_id . "'");
     27 
     28 		if (isset($data['coupon_product'])) {
     29 			foreach ($data['coupon_product'] as $product_id) {
     30 				$this->db->query("INSERT INTO " . DB_PREFIX . "coupon_product SET coupon_id = '" . (int)$coupon_id . "', product_id = '" . (int)$product_id . "'");
     31 			}
     32 		}
     33 
     34 		$this->db->query("DELETE FROM " . DB_PREFIX . "coupon_category WHERE coupon_id = '" . (int)$coupon_id . "'");
     35 
     36 		if (isset($data['coupon_category'])) {
     37 			foreach ($data['coupon_category'] as $category_id) {
     38 				$this->db->query("INSERT INTO " . DB_PREFIX . "coupon_category SET coupon_id = '" . (int)$coupon_id . "', category_id = '" . (int)$category_id . "'");
     39 			}
     40 		}
     41 	}
     42 
     43 	public function deleteCoupon($coupon_id) {
     44 		$this->db->query("DELETE FROM " . DB_PREFIX . "coupon WHERE coupon_id = '" . (int)$coupon_id . "'");
     45 		$this->db->query("DELETE FROM " . DB_PREFIX . "coupon_product WHERE coupon_id = '" . (int)$coupon_id . "'");
     46 		$this->db->query("DELETE FROM " . DB_PREFIX . "coupon_category WHERE coupon_id = '" . (int)$coupon_id . "'");
     47 		$this->db->query("DELETE FROM " . DB_PREFIX . "coupon_history WHERE coupon_id = '" . (int)$coupon_id . "'");
     48 	}
     49 
     50 	public function getCoupon($coupon_id) {
     51 		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "coupon WHERE coupon_id = '" . (int)$coupon_id . "'");
     52 
     53 		return $query->row;
     54 	}
     55 
     56 	public function getCouponByCode($code) {
     57 		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "coupon WHERE code = '" . $this->db->escape($code) . "'");
     58 
     59 		return $query->row;
     60 	}
     61 
     62 	public function getCoupons($data = array()) {
     63 		$sql = "SELECT coupon_id, name, code, discount, date_start, date_end, status FROM " . DB_PREFIX . "coupon";
     64 
     65 		$sort_data = array(
     66 			'name',
     67 			'code',
     68 			'discount',
     69 			'date_start',
     70 			'date_end',
     71 			'status'
     72 		);
     73 
     74 		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
     75 			$sql .= " ORDER BY " . $data['sort'];
     76 		} else {
     77 			$sql .= " ORDER BY name";
     78 		}
     79 
     80 		if (isset($data['order']) && ($data['order'] == 'DESC')) {
     81 			$sql .= " DESC";
     82 		} else {
     83 			$sql .= " ASC";
     84 		}
     85 
     86 		if (isset($data['start']) || isset($data['limit'])) {
     87 			if ($data['start'] < 0) {
     88 				$data['start'] = 0;
     89 			}
     90 
     91 			if ($data['limit'] < 1) {
     92 				$data['limit'] = 20;
     93 			}
     94 
     95 			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
     96 		}
     97 
     98 		$query = $this->db->query($sql);
     99 
    100 		return $query->rows;
    101 	}
    102 
    103 	public function getCouponProducts($coupon_id) {
    104 		$coupon_product_data = array();
    105 
    106 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "coupon_product WHERE coupon_id = '" . (int)$coupon_id . "'");
    107 
    108 		foreach ($query->rows as $result) {
    109 			$coupon_product_data[] = $result['product_id'];
    110 		}
    111 
    112 		return $coupon_product_data;
    113 	}
    114 
    115 	public function getCouponCategories($coupon_id) {
    116 		$coupon_category_data = array();
    117 
    118 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "coupon_category WHERE coupon_id = '" . (int)$coupon_id . "'");
    119 
    120 		foreach ($query->rows as $result) {
    121 			$coupon_category_data[] = $result['category_id'];
    122 		}
    123 
    124 		return $coupon_category_data;
    125 	}
    126 
    127 	public function getTotalCoupons() {
    128 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "coupon");
    129 
    130 		return $query->row['total'];
    131 	}
    132 
    133 	public function getCouponHistories($coupon_id, $start = 0, $limit = 10) {
    134 		if ($start < 0) {
    135 			$start = 0;
    136 		}
    137 
    138 		if ($limit < 1) {
    139 			$limit = 10;
    140 		}
    141 
    142 		$query = $this->db->query("SELECT ch.order_id, CONCAT(c.firstname, ' ', c.lastname) AS customer, ch.amount, ch.date_added FROM " . DB_PREFIX . "coupon_history ch LEFT JOIN " . DB_PREFIX . "customer c ON (ch.customer_id = c.customer_id) WHERE ch.coupon_id = '" . (int)$coupon_id . "' ORDER BY ch.date_added ASC LIMIT " . (int)$start . "," . (int)$limit);
    143 
    144 		return $query->rows;
    145 	}
    146 
    147 	public function getTotalCouponHistories($coupon_id) {
    148 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "coupon_history WHERE coupon_id = '" . (int)$coupon_id . "'");
    149 
    150 		return $query->row['total'];
    151 	}
    152 }