shop.balmet.com

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

return.php (9232B)


      1 <?php
      2 class ModelSaleReturn extends Model {
      3 	public function addReturn($data) {
      4 		$this->db->query("INSERT INTO `" . DB_PREFIX . "return` SET order_id = '" . (int)$data['order_id'] . "', product_id = '" . (int)$data['product_id'] . "', customer_id = '" . (int)$data['customer_id'] . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', product = '" . $this->db->escape($data['product']) . "', model = '" . $this->db->escape($data['model']) . "', quantity = '" . (int)$data['quantity'] . "', opened = '" . (int)$data['opened'] . "', return_reason_id = '" . (int)$data['return_reason_id'] . "', return_action_id = '" . (int)$data['return_action_id'] . "', return_status_id = '" . (int)$data['return_status_id'] . "', comment = '" . $this->db->escape($data['comment']) . "', date_ordered = '" . $this->db->escape($data['date_ordered']) . "', date_added = NOW(), date_modified = NOW()");
      5 	
      6 		return $this->db->getLastId();
      7 	}
      8 
      9 	public function editReturn($return_id, $data) {
     10 		$this->db->query("UPDATE `" . DB_PREFIX . "return` SET order_id = '" . (int)$data['order_id'] . "', product_id = '" . (int)$data['product_id'] . "', customer_id = '" . (int)$data['customer_id'] . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', product = '" . $this->db->escape($data['product']) . "', model = '" . $this->db->escape($data['model']) . "', quantity = '" . (int)$data['quantity'] . "', opened = '" . (int)$data['opened'] . "', return_reason_id = '" . (int)$data['return_reason_id'] . "', return_action_id = '" . (int)$data['return_action_id'] . "', comment = '" . $this->db->escape($data['comment']) . "', date_ordered = '" . $this->db->escape($data['date_ordered']) . "', date_modified = NOW() WHERE return_id = '" . (int)$return_id . "'");
     11 	}
     12 
     13 	public function deleteReturn($return_id) {
     14 		$this->db->query("DELETE FROM `" . DB_PREFIX . "return` WHERE `return_id` = '" . (int)$return_id . "'");
     15 		$this->db->query("DELETE FROM `" . DB_PREFIX . "return_history` WHERE `return_id` = '" . (int)$return_id . "'");
     16 	}
     17 
     18 	public function getReturn($return_id) {
     19 		$query = $this->db->query("SELECT DISTINCT *, (SELECT CONCAT(c.firstname, ' ', c.lastname) FROM " . DB_PREFIX . "customer c WHERE c.customer_id = r.customer_id) AS customer, (SELECT rs.name FROM " . DB_PREFIX . "return_status rs WHERE rs.return_status_id = r.return_status_id AND rs.language_id = '" . (int)$this->config->get('config_language_id') . "') AS return_status FROM `" . DB_PREFIX . "return` r WHERE r.return_id = '" . (int)$return_id . "'");
     20 
     21 		return $query->row;
     22 	}
     23 
     24 	public function getReturns($data = array()) {
     25 		$sql = "SELECT *, CONCAT(r.firstname, ' ', r.lastname) AS customer, (SELECT rs.name FROM " . DB_PREFIX . "return_status rs WHERE rs.return_status_id = r.return_status_id AND rs.language_id = '" . (int)$this->config->get('config_language_id') . "') AS return_status FROM `" . DB_PREFIX . "return` r";
     26 
     27 		$implode = array();
     28 
     29 		if (!empty($data['filter_return_id'])) {
     30 			$implode[] = "r.return_id = '" . (int)$data['filter_return_id'] . "'";
     31 		}
     32 
     33 		if (!empty($data['filter_order_id'])) {
     34 			$implode[] = "r.order_id = '" . (int)$data['filter_order_id'] . "'";
     35 		}
     36 
     37 		if (!empty($data['filter_customer'])) {
     38 			$implode[] = "CONCAT(r.firstname, ' ', r.lastname) LIKE '" . $this->db->escape($data['filter_customer']) . "%'";
     39 		}
     40 
     41 		if (!empty($data['filter_product'])) {
     42 			$implode[] = "r.product = '" . $this->db->escape($data['filter_product']) . "'";
     43 		}
     44 
     45 		if (!empty($data['filter_model'])) {
     46 			$implode[] = "r.model = '" . $this->db->escape($data['filter_model']) . "'";
     47 		}
     48 
     49 		if (!empty($data['filter_return_status_id'])) {
     50 			$implode[] = "r.return_status_id = '" . (int)$data['filter_return_status_id'] . "'";
     51 		}
     52 
     53 		if (!empty($data['filter_date_added'])) {
     54 			$implode[] = "DATE(r.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
     55 		}
     56 
     57 		if (!empty($data['filter_date_modified'])) {
     58 			$implode[] = "DATE(r.date_modified) = DATE('" . $this->db->escape($data['filter_date_modified']) . "')";
     59 		}
     60 
     61 		if ($implode) {
     62 			$sql .= " WHERE " . implode(" AND ", $implode);
     63 		}
     64 
     65 		$sort_data = array(
     66 			'r.return_id',
     67 			'r.order_id',
     68 			'customer',
     69 			'r.product',
     70 			'r.model',
     71 			'status',
     72 			'r.date_added',
     73 			'r.date_modified'
     74 		);
     75 
     76 		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
     77 			$sql .= " ORDER BY " . $data['sort'];
     78 		} else {
     79 			$sql .= " ORDER BY r.return_id";
     80 		}
     81 
     82 		if (isset($data['order']) && ($data['order'] == 'DESC')) {
     83 			$sql .= " DESC";
     84 		} else {
     85 			$sql .= " ASC";
     86 		}
     87 
     88 		if (isset($data['start']) || isset($data['limit'])) {
     89 			if ($data['start'] < 0) {
     90 				$data['start'] = 0;
     91 			}
     92 
     93 			if ($data['limit'] < 1) {
     94 				$data['limit'] = 20;
     95 			}
     96 
     97 			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
     98 		}
     99 
    100 		$query = $this->db->query($sql);
    101 
    102 		return $query->rows;
    103 	}
    104 
    105 	public function getTotalReturns($data = array()) {
    106 		$sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "return`r";
    107 
    108 		$implode = array();
    109 
    110 		if (!empty($data['filter_return_id'])) {
    111 			$implode[] = "r.return_id = '" . (int)$data['filter_return_id'] . "'";
    112 		}
    113 
    114 		if (!empty($data['filter_customer'])) {
    115 			$implode[] = "CONCAT(r.firstname, ' ', r.lastname) LIKE '" . $this->db->escape($data['filter_customer']) . "%'";
    116 		}
    117 
    118 		if (!empty($data['filter_order_id'])) {
    119 			$implode[] = "r.order_id = '" . $this->db->escape($data['filter_order_id']) . "'";
    120 		}
    121 
    122 		if (!empty($data['filter_product'])) {
    123 			$implode[] = "r.product = '" . $this->db->escape($data['filter_product']) . "'";
    124 		}
    125 
    126 		if (!empty($data['filter_model'])) {
    127 			$implode[] = "r.model = '" . $this->db->escape($data['filter_model']) . "'";
    128 		}
    129 
    130 		if (!empty($data['filter_return_status_id'])) {
    131 			$implode[] = "r.return_status_id = '" . (int)$data['filter_return_status_id'] . "'";
    132 		}
    133 
    134 		if (!empty($data['filter_date_added'])) {
    135 			$implode[] = "DATE(r.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
    136 		}
    137 
    138 		if (!empty($data['filter_date_modified'])) {
    139 			$implode[] = "DATE(r.date_modified) = DATE('" . $this->db->escape($data['filter_date_modified']) . "')";
    140 		}
    141 
    142 		if ($implode) {
    143 			$sql .= " WHERE " . implode(" AND ", $implode);
    144 		}
    145 
    146 		$query = $this->db->query($sql);
    147 
    148 		return $query->row['total'];
    149 	}
    150 
    151 	public function getTotalReturnsByReturnStatusId($return_status_id) {
    152 		$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "return` WHERE return_status_id = '" . (int)$return_status_id . "'");
    153 
    154 		return $query->row['total'];
    155 	}
    156 
    157 	public function getTotalReturnsByReturnReasonId($return_reason_id) {
    158 		$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "return` WHERE return_reason_id = '" . (int)$return_reason_id . "'");
    159 
    160 		return $query->row['total'];
    161 	}
    162 
    163 	public function getTotalReturnsByReturnActionId($return_action_id) {
    164 		$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "return` WHERE return_action_id = '" . (int)$return_action_id . "'");
    165 
    166 		return $query->row['total'];
    167 	}
    168 	
    169 	public function addReturnHistory($return_id, $return_status_id, $comment, $notify) {
    170 		$this->db->query("UPDATE `" . DB_PREFIX . "return` SET `return_status_id` = '" . (int)$return_status_id . "', date_modified = NOW() WHERE return_id = '" . (int)$return_id . "'");
    171 		$this->db->query("INSERT INTO `" . DB_PREFIX . "return_history` SET `return_id` = '" . (int)$return_id . "', return_status_id = '" . (int)$return_status_id . "', notify = '" . (int)$notify . "', comment = '" . $this->db->escape(strip_tags($comment)) . "', date_added = NOW()");
    172 	}
    173 
    174 	public function getReturnHistories($return_id, $start = 0, $limit = 10) {
    175 		if ($start < 0) {
    176 			$start = 0;
    177 		}
    178 
    179 		if ($limit < 1) {
    180 			$limit = 10;
    181 		}
    182 
    183 		$query = $this->db->query("SELECT rh.date_added, rs.name AS status, rh.comment, rh.notify FROM " . DB_PREFIX . "return_history rh LEFT JOIN " . DB_PREFIX . "return_status rs ON rh.return_status_id = rs.return_status_id WHERE rh.return_id = '" . (int)$return_id . "' AND rs.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY rh.date_added DESC LIMIT " . (int)$start . "," . (int)$limit);
    184 
    185 		return $query->rows;
    186 	}
    187 
    188 	public function getTotalReturnHistories($return_id) {
    189 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "return_history WHERE return_id = '" . (int)$return_id . "'");
    190 
    191 		return $query->row['total'];
    192 	}
    193 
    194 	public function getTotalReturnHistoriesByReturnStatusId($return_status_id) {
    195 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "return_history WHERE return_status_id = '" . (int)$return_status_id . "'");
    196 
    197 		return $query->row['total'];
    198 	}
    199 }