shop.balmet.com

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

customer.php (22920B)


      1 <?php
      2 class ModelCustomerCustomer extends Model {
      3 	public function addCustomer($data) {
      4 		$this->db->query("INSERT INTO " . DB_PREFIX . "customer SET customer_group_id = '" . (int)$data['customer_group_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']) . "', custom_field = '" . $this->db->escape(isset($data['custom_field']) ? json_encode($data['custom_field']) : json_encode(array())) . "', newsletter = '" . (int)$data['newsletter'] . "', salt = '" . $this->db->escape($salt = token(9)) . "', password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "', status = '" . (int)$data['status'] . "', safe = '" . (int)$data['safe'] . "', date_added = NOW()");
      5 
      6 		$customer_id = $this->db->getLastId();
      7 
      8 		if (isset($data['address'])) {
      9 			foreach ($data['address'] as $address) {
     10 				$this->db->query("INSERT INTO " . DB_PREFIX . "address SET customer_id = '" . (int)$customer_id . "', firstname = '" . $this->db->escape($address['firstname']) . "', lastname = '" . $this->db->escape($address['lastname']) . "', company = '" . $this->db->escape($address['company']) . "', address_1 = '" . $this->db->escape($address['address_1']) . "', address_2 = '" . $this->db->escape($address['address_2']) . "', city = '" . $this->db->escape($address['city']) . "', postcode = '" . $this->db->escape($address['postcode']) . "', country_id = '" . (int)$address['country_id'] . "', zone_id = '" . (int)$address['zone_id'] . "', custom_field = '" . $this->db->escape(isset($address['custom_field']) ? json_encode($address['custom_field']) : json_encode(array())) . "'");
     11 
     12 				if (isset($address['default'])) {
     13 					$address_id = $this->db->getLastId();
     14 
     15 					$this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$customer_id . "'");
     16 				}
     17 			}
     18 		}
     19 		
     20 		if ($data['affiliate']) {
     21 			$this->db->query("INSERT INTO " . DB_PREFIX . "customer_affiliate SET customer_id = '" . (int)$customer_id . "', company = '" . $this->db->escape($data['company']) . "', website = '" . $this->db->escape($data['website']) . "', tracking = '" . $this->db->escape($data['tracking']) . "', commission = '" . (float)$data['commission'] . "', tax = '" . $this->db->escape($data['tax']) . "', payment = '" . $this->db->escape($data['payment']) . "', cheque = '" . $this->db->escape($data['cheque']) . "', paypal = '" . $this->db->escape($data['paypal']) . "', bank_name = '" . $this->db->escape($data['bank_name']) . "', bank_branch_number = '" . $this->db->escape($data['bank_branch_number']) . "', bank_swift_code = '" . $this->db->escape($data['bank_swift_code']) . "', bank_account_name = '" . $this->db->escape($data['bank_account_name']) . "', bank_account_number = '" . $this->db->escape($data['bank_account_number']) . "', custom_field = '" . $this->db->escape(isset($data['custom_field']) ? json_encode($data['custom_field']) : json_encode(array())) . "', status = '" . (int)$data['affiliate'] . "', date_added = NOW()");
     22 		}
     23 		
     24 		return $customer_id;
     25 	}
     26 
     27 	public function editCustomer($customer_id, $data) {
     28 		$this->db->query("UPDATE " . DB_PREFIX . "customer SET customer_group_id = '" . (int)$data['customer_group_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']) . "', custom_field = '" . $this->db->escape(isset($data['custom_field']) ? json_encode($data['custom_field']) : json_encode(array())) . "', newsletter = '" . (int)$data['newsletter'] . "', status = '" . (int)$data['status'] . "', safe = '" . (int)$data['safe'] . "' WHERE customer_id = '" . (int)$customer_id . "'");
     29 
     30 		if ($data['password']) {
     31 			$this->db->query("UPDATE " . DB_PREFIX . "customer SET salt = '" . $this->db->escape($salt = token(9)) . "', password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "' WHERE customer_id = '" . (int)$customer_id . "'");
     32 		}
     33 
     34 		$this->db->query("DELETE FROM " . DB_PREFIX . "address WHERE customer_id = '" . (int)$customer_id . "'");
     35 
     36 		if (isset($data['address'])) {
     37 			foreach ($data['address'] as $address) {
     38 				$this->db->query("INSERT INTO " . DB_PREFIX . "address SET address_id = '" . (int)$address['address_id'] . "', customer_id = '" . (int)$customer_id . "', firstname = '" . $this->db->escape($address['firstname']) . "', lastname = '" . $this->db->escape($address['lastname']) . "', company = '" . $this->db->escape($address['company']) . "', address_1 = '" . $this->db->escape($address['address_1']) . "', address_2 = '" . $this->db->escape($address['address_2']) . "', city = '" . $this->db->escape($address['city']) . "', postcode = '" . $this->db->escape($address['postcode']) . "', country_id = '" . (int)$address['country_id'] . "', zone_id = '" . (int)$address['zone_id'] . "', custom_field = '" . $this->db->escape(isset($address['custom_field']) ? json_encode($address['custom_field']) : json_encode(array())) . "'");
     39 
     40 				if (isset($address['default'])) {
     41 					$address_id = $this->db->getLastId();
     42 
     43 					$this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$customer_id . "'");
     44 				}
     45 			}
     46 		}
     47 		
     48 		if ($data['affiliate']) {
     49 			$this->db->query("REPLACE INTO " . DB_PREFIX . "customer_affiliate SET customer_id = '" . (int)$customer_id . "', company = '" . $this->db->escape($data['company']) . "', website = '" . $this->db->escape($data['website']) . "', tracking = '" . $this->db->escape($data['tracking']) . "', commission = '" . (float)$data['commission'] . "', tax = '" . $this->db->escape($data['tax']) . "', payment = '" . $this->db->escape($data['payment']) . "', cheque = '" . $this->db->escape($data['cheque']) . "', paypal = '" . $this->db->escape($data['paypal']) . "', bank_name = '" . $this->db->escape($data['bank_name']) . "', bank_branch_number = '" . $this->db->escape($data['bank_branch_number']) . "', bank_swift_code = '" . $this->db->escape($data['bank_swift_code']) . "', bank_account_name = '" . $this->db->escape($data['bank_account_name']) . "', bank_account_number = '" . $this->db->escape($data['bank_account_number']) . "', status = '" . (int)$data['affiliate'] . "', date_added = NOW()");
     50 		}		
     51 	}
     52 
     53 	public function editToken($customer_id, $token) {
     54 		$this->db->query("UPDATE " . DB_PREFIX . "customer SET token = '" . $this->db->escape($token) . "' WHERE customer_id = '" . (int)$customer_id . "'");
     55 	}
     56 
     57 	public function deleteCustomer($customer_id) {
     58 		$this->db->query("DELETE FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$customer_id . "'");
     59 		$this->db->query("DELETE FROM " . DB_PREFIX . "customer_activity WHERE customer_id = '" . (int)$customer_id . "'");
     60 		$this->db->query("DELETE FROM " . DB_PREFIX . "customer_affiliate WHERE customer_id = '" . (int)$customer_id . "'");
     61 		$this->db->query("DELETE FROM " . DB_PREFIX . "customer_approval WHERE customer_id = '" . (int)$customer_id . "'");
     62 		$this->db->query("DELETE FROM " . DB_PREFIX . "customer_reward WHERE customer_id = '" . (int)$customer_id . "'");
     63 		$this->db->query("DELETE FROM " . DB_PREFIX . "customer_transaction WHERE customer_id = '" . (int)$customer_id . "'");
     64 		$this->db->query("DELETE FROM " . DB_PREFIX . "customer_ip WHERE customer_id = '" . (int)$customer_id . "'");
     65 		$this->db->query("DELETE FROM " . DB_PREFIX . "address WHERE customer_id = '" . (int)$customer_id . "'");
     66 	}
     67 
     68 	public function getCustomer($customer_id) {
     69 		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$customer_id . "'");
     70 
     71 		return $query->row;
     72 	}
     73 
     74 	public function getCustomerByEmail($email) {
     75 		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "customer WHERE LCASE(email) = '" . $this->db->escape(utf8_strtolower($email)) . "'");
     76 
     77 		return $query->row;
     78 	}
     79 	
     80 	public function getCustomers($data = array()) {
     81 		$sql = "SELECT *, CONCAT(c.firstname, ' ', c.lastname) AS name, cgd.name AS customer_group FROM " . DB_PREFIX . "customer c LEFT JOIN " . DB_PREFIX . "customer_group_description cgd ON (c.customer_group_id = cgd.customer_group_id)";
     82 		
     83 		if (!empty($data['filter_affiliate'])) {
     84 			$sql .= " LEFT JOIN " . DB_PREFIX . "customer_affiliate ca ON (c.customer_id = ca.customer_id)";
     85 		}		
     86 		
     87 		$sql .= " WHERE cgd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
     88 		
     89 		$implode = array();
     90 
     91 		if (!empty($data['filter_name'])) {
     92 			$implode[] = "CONCAT(c.firstname, ' ', c.lastname) LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
     93 		}
     94 
     95 		if (!empty($data['filter_email'])) {
     96 			$implode[] = "c.email LIKE '" . $this->db->escape($data['filter_email']) . "%'";
     97 		}
     98 
     99 		if (isset($data['filter_newsletter']) && !is_null($data['filter_newsletter'])) {
    100 			$implode[] = "c.newsletter = '" . (int)$data['filter_newsletter'] . "'";
    101 		}
    102 
    103 		if (!empty($data['filter_customer_group_id'])) {
    104 			$implode[] = "c.customer_group_id = '" . (int)$data['filter_customer_group_id'] . "'";
    105 		}
    106 
    107 		if (!empty($data['filter_affiliate'])) {
    108 			$implode[] = "ca.status = '" . (int)$data['filter_affiliate'] . "'";
    109 		}
    110 		
    111 		if (!empty($data['filter_ip'])) {
    112 			$implode[] = "c.customer_id IN (SELECT customer_id FROM " . DB_PREFIX . "customer_ip WHERE ip = '" . $this->db->escape($data['filter_ip']) . "')";
    113 		}
    114 
    115 		if (isset($data['filter_status']) && $data['filter_status'] !== '') {
    116 			$implode[] = "c.status = '" . (int)$data['filter_status'] . "'";
    117 		}
    118 
    119 		if (!empty($data['filter_date_added'])) {
    120 			$implode[] = "DATE(c.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
    121 		}
    122 
    123 		if ($implode) {
    124 			$sql .= " AND " . implode(" AND ", $implode);
    125 		}
    126 
    127 		$sort_data = array(
    128 			'name',
    129 			'c.email',
    130 			'customer_group',
    131 			'c.status',
    132 			'c.ip',
    133 			'c.date_added'
    134 		);
    135 
    136 		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
    137 			$sql .= " ORDER BY " . $data['sort'];
    138 		} else {
    139 			$sql .= " ORDER BY name";
    140 		}
    141 
    142 		if (isset($data['order']) && ($data['order'] == 'DESC')) {
    143 			$sql .= " DESC";
    144 		} else {
    145 			$sql .= " ASC";
    146 		}
    147 
    148 		if (isset($data['start']) || isset($data['limit'])) {
    149 			if ($data['start'] < 0) {
    150 				$data['start'] = 0;
    151 			}
    152 
    153 			if ($data['limit'] < 1) {
    154 				$data['limit'] = 20;
    155 			}
    156 
    157 			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
    158 		}
    159 
    160 		$query = $this->db->query($sql);
    161 
    162 		return $query->rows;
    163 	}
    164 
    165 	public function getAddress($address_id) {
    166 		$address_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "address WHERE address_id = '" . (int)$address_id . "'");
    167 
    168 		if ($address_query->num_rows) {
    169 			$country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$address_query->row['country_id'] . "'");
    170 
    171 			if ($country_query->num_rows) {
    172 				$country = $country_query->row['name'];
    173 				$iso_code_2 = $country_query->row['iso_code_2'];
    174 				$iso_code_3 = $country_query->row['iso_code_3'];
    175 				$address_format = $country_query->row['address_format'];
    176 			} else {
    177 				$country = '';
    178 				$iso_code_2 = '';
    179 				$iso_code_3 = '';
    180 				$address_format = '';
    181 			}
    182 
    183 			$zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$address_query->row['zone_id'] . "'");
    184 
    185 			if ($zone_query->num_rows) {
    186 				$zone = $zone_query->row['name'];
    187 				$zone_code = $zone_query->row['code'];
    188 			} else {
    189 				$zone = '';
    190 				$zone_code = '';
    191 			}
    192 
    193 			return array(
    194 				'address_id'     => $address_query->row['address_id'],
    195 				'customer_id'    => $address_query->row['customer_id'],
    196 				'firstname'      => $address_query->row['firstname'],
    197 				'lastname'       => $address_query->row['lastname'],
    198 				'company'        => $address_query->row['company'],
    199 				'address_1'      => $address_query->row['address_1'],
    200 				'address_2'      => $address_query->row['address_2'],
    201 				'postcode'       => $address_query->row['postcode'],
    202 				'city'           => $address_query->row['city'],
    203 				'zone_id'        => $address_query->row['zone_id'],
    204 				'zone'           => $zone,
    205 				'zone_code'      => $zone_code,
    206 				'country_id'     => $address_query->row['country_id'],
    207 				'country'        => $country,
    208 				'iso_code_2'     => $iso_code_2,
    209 				'iso_code_3'     => $iso_code_3,
    210 				'address_format' => $address_format,
    211 				'custom_field'   => json_decode($address_query->row['custom_field'], true)
    212 			);
    213 		}
    214 	}
    215 
    216 	public function getAddresses($customer_id) {
    217 		$address_data = array();
    218 
    219 		$query = $this->db->query("SELECT address_id FROM " . DB_PREFIX . "address WHERE customer_id = '" . (int)$customer_id . "'");
    220 
    221 		foreach ($query->rows as $result) {
    222 			$address_info = $this->getAddress($result['address_id']);
    223 
    224 			if ($address_info) {
    225 				$address_data[$result['address_id']] = $address_info;
    226 			}
    227 		}
    228 
    229 		return $address_data;
    230 	}
    231 
    232 	public function getTotalCustomers($data = array()) {
    233 		$sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer";
    234 
    235 		$implode = array();
    236 
    237 		if (!empty($data['filter_name'])) {
    238 			$implode[] = "CONCAT(firstname, ' ', lastname) LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
    239 		}
    240 
    241 		if (!empty($data['filter_email'])) {
    242 			$implode[] = "email LIKE '" . $this->db->escape($data['filter_email']) . "%'";
    243 		}
    244 
    245 		if (isset($data['filter_newsletter']) && !is_null($data['filter_newsletter'])) {
    246 			$implode[] = "newsletter = '" . (int)$data['filter_newsletter'] . "'";
    247 		}
    248 
    249 		if (!empty($data['filter_customer_group_id'])) {
    250 			$implode[] = "customer_group_id = '" . (int)$data['filter_customer_group_id'] . "'";
    251 		}
    252 
    253 		if (!empty($data['filter_ip'])) {
    254 			$implode[] = "customer_id IN (SELECT customer_id FROM " . DB_PREFIX . "customer_ip WHERE ip = '" . $this->db->escape($data['filter_ip']) . "')";
    255 		}
    256 
    257 		if (isset($data['filter_status']) && $data['filter_status'] !== '') {
    258 			$implode[] = "status = '" . (int)$data['filter_status'] . "'";
    259 		}
    260 
    261 		if (!empty($data['filter_date_added'])) {
    262 			$implode[] = "DATE(date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
    263 		}
    264 
    265 		if ($implode) {
    266 			$sql .= " WHERE " . implode(" AND ", $implode);
    267 		}
    268 
    269 		$query = $this->db->query($sql);
    270 
    271 		return $query->row['total'];
    272 	}
    273         
    274         public function getAffliateByTracking($tracking) {
    275                 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_affiliate WHERE tracking = '" . $this->db->escape($tracking) . "'");
    276                 
    277                 return $query->row;
    278         }
    279 	
    280 	public function getAffiliate($customer_id) {
    281 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_affiliate WHERE customer_id = '" . (int)$customer_id . "'");
    282 
    283 		return $query->row;
    284 	}
    285 	
    286 	public function getAffiliates($data = array()) {
    287 		$sql = "SELECT DISTINCT *, CONCAT(c.firstname, ' ', c.lastname) AS name FROM " . DB_PREFIX . "customer_affiliate ca LEFT JOIN " . DB_PREFIX . "customer c ON (ca.customer_id = c.customer_id)";
    288 		
    289 		$implode = array();
    290 
    291 		if (!empty($data['filter_name'])) {
    292 			$implode[] = "CONCAT(c.firstname, ' ', c.lastname) LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
    293 		}		
    294 		
    295 		if ($implode) {
    296 			$sql .= " WHERE " . implode(" AND ", $implode);
    297 		}
    298 		
    299 		if (isset($data['start']) || isset($data['limit'])) {
    300 			if ($data['start'] < 0) {
    301 				$data['start'] = 0;
    302 			}
    303 
    304 			if ($data['limit'] < 1) {
    305 				$data['limit'] = 20;
    306 			}
    307 
    308 			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
    309 		}
    310 						
    311 		$query = $this->db->query($sql . "ORDER BY name");
    312 
    313 		return $query->rows;
    314 	}
    315 	
    316 	public function getTotalAffiliates($data = array()) {
    317 		$sql = "SELECT DISTINCT COUNT(*) AS total FROM " . DB_PREFIX . "customer_affiliate ca LEFT JOIN " . DB_PREFIX . "customer c ON (ca.customer_id = c.customer_id)";
    318 		
    319 		$implode = array();
    320 
    321 		if (!empty($data['filter_name'])) {
    322 			$implode[] = "CONCAT(c.firstname, ' ', c.lastname) LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
    323 		}		
    324 		
    325 		if ($implode) {
    326 			$sql .= " WHERE " . implode(" AND ", $implode);
    327 		}
    328 		
    329 		return $query->row['total'];
    330 	}
    331 
    332 	public function getTotalAddressesByCustomerId($customer_id) {
    333 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "address WHERE customer_id = '" . (int)$customer_id . "'");
    334 
    335 		return $query->row['total'];
    336 	}
    337 
    338 	public function getTotalAddressesByCountryId($country_id) {
    339 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "address WHERE country_id = '" . (int)$country_id . "'");
    340 
    341 		return $query->row['total'];
    342 	}
    343 
    344 	public function getTotalAddressesByZoneId($zone_id) {
    345 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "address WHERE zone_id = '" . (int)$zone_id . "'");
    346 
    347 		return $query->row['total'];
    348 	}
    349 
    350 	public function getTotalCustomersByCustomerGroupId($customer_group_id) {
    351 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer WHERE customer_group_id = '" . (int)$customer_group_id . "'");
    352 
    353 		return $query->row['total'];
    354 	}
    355 
    356 	public function addHistory($customer_id, $comment) {
    357 		$this->db->query("INSERT INTO " . DB_PREFIX . "customer_history SET customer_id = '" . (int)$customer_id . "', comment = '" . $this->db->escape(strip_tags($comment)) . "', date_added = NOW()");
    358 	}
    359 
    360 	public function getHistories($customer_id, $start = 0, $limit = 10) {
    361 		if ($start < 0) {
    362 			$start = 0;
    363 		}
    364 
    365 		if ($limit < 1) {
    366 			$limit = 10;
    367 		}
    368 
    369 		$query = $this->db->query("SELECT comment, date_added FROM " . DB_PREFIX . "customer_history WHERE customer_id = '" . (int)$customer_id . "' ORDER BY date_added DESC LIMIT " . (int)$start . "," . (int)$limit);
    370 
    371 		return $query->rows;
    372 	}
    373 
    374 	public function getTotalHistories($customer_id) {
    375 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer_history WHERE customer_id = '" . (int)$customer_id . "'");
    376 
    377 		return $query->row['total'];
    378 	}
    379 
    380 	public function addTransaction($customer_id, $description = '', $amount = '', $order_id = 0) {
    381 		$this->db->query("INSERT INTO " . DB_PREFIX . "customer_transaction SET customer_id = '" . (int)$customer_id . "', order_id = '" . (int)$order_id . "', description = '" . $this->db->escape($description) . "', amount = '" . (float)$amount . "', date_added = NOW()");
    382 	}
    383 
    384 	public function deleteTransactionByOrderId($order_id) {
    385 		$this->db->query("DELETE FROM " . DB_PREFIX . "customer_transaction WHERE order_id = '" . (int)$order_id . "'");
    386 	}
    387 
    388 	public function getTransactions($customer_id, $start = 0, $limit = 10) {
    389 		if ($start < 0) {
    390 			$start = 0;
    391 		}
    392 
    393 		if ($limit < 1) {
    394 			$limit = 10;
    395 		}
    396 
    397 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_transaction WHERE customer_id = '" . (int)$customer_id . "' ORDER BY date_added DESC LIMIT " . (int)$start . "," . (int)$limit);
    398 
    399 		return $query->rows;
    400 	}
    401 
    402 	public function getTotalTransactions($customer_id) {
    403 		$query = $this->db->query("SELECT COUNT(*) AS total  FROM " . DB_PREFIX . "customer_transaction WHERE customer_id = '" . (int)$customer_id . "'");
    404 
    405 		return $query->row['total'];
    406 	}
    407 
    408 	public function getTransactionTotal($customer_id) {
    409 		$query = $this->db->query("SELECT SUM(amount) AS total FROM " . DB_PREFIX . "customer_transaction WHERE customer_id = '" . (int)$customer_id . "'");
    410 
    411 		return $query->row['total'];
    412 	}
    413 
    414 	public function getTotalTransactionsByOrderId($order_id) {
    415 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer_transaction WHERE order_id = '" . (int)$order_id . "'");
    416 
    417 		return $query->row['total'];
    418 	}
    419 
    420 	public function addReward($customer_id, $description = '', $points = '', $order_id = 0) {
    421 		$this->db->query("INSERT INTO " . DB_PREFIX . "customer_reward SET customer_id = '" . (int)$customer_id . "', order_id = '" . (int)$order_id . "', points = '" . (int)$points . "', description = '" . $this->db->escape($description) . "', date_added = NOW()");
    422 	}
    423 
    424 	public function deleteReward($order_id) {
    425 		$this->db->query("DELETE FROM " . DB_PREFIX . "customer_reward WHERE order_id = '" . (int)$order_id . "' AND points > 0");
    426 	}
    427 
    428 	public function getRewards($customer_id, $start = 0, $limit = 10) {
    429 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_reward WHERE customer_id = '" . (int)$customer_id . "' ORDER BY date_added DESC LIMIT " . (int)$start . "," . (int)$limit);
    430 
    431 		return $query->rows;
    432 	}
    433 
    434 	public function getTotalRewards($customer_id) {
    435 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer_reward WHERE customer_id = '" . (int)$customer_id . "'");
    436 
    437 		return $query->row['total'];
    438 	}
    439 
    440 	public function getRewardTotal($customer_id) {
    441 		$query = $this->db->query("SELECT SUM(points) AS total FROM " . DB_PREFIX . "customer_reward WHERE customer_id = '" . (int)$customer_id . "'");
    442 
    443 		return $query->row['total'];
    444 	}
    445 
    446 	public function getTotalCustomerRewardsByOrderId($order_id) {
    447 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer_reward WHERE order_id = '" . (int)$order_id . "' AND points > 0");
    448 
    449 		return $query->row['total'];
    450 	}
    451 
    452 	public function getIps($customer_id, $start = 0, $limit = 10) {
    453 		if ($start < 0) {
    454 			$start = 0;
    455 		}
    456 		if ($limit < 1) {
    457 			$limit = 10;
    458 		}
    459 
    460 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_ip WHERE customer_id = '" . (int)$customer_id . "' ORDER BY date_added DESC LIMIT " . (int)$start . "," . (int)$limit);
    461 		
    462 		return $query->rows;
    463 	}
    464 
    465 	public function getTotalIps($customer_id) {
    466 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer_ip WHERE customer_id = '" . (int)$customer_id . "'");
    467 
    468 		return $query->row['total'];
    469 	}
    470 
    471 	public function getTotalCustomersByIp($ip) {
    472 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer_ip WHERE ip = '" . $this->db->escape($ip) . "'");
    473 
    474 		return $query->row['total'];
    475 	}
    476 
    477 	public function getTotalLoginAttempts($email) {
    478 		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_login` WHERE `email` = '" . $this->db->escape($email) . "'");
    479 
    480 		return $query->row;
    481 	}
    482 
    483 	public function deleteLoginAttempts($email) {
    484 		$this->db->query("DELETE FROM `" . DB_PREFIX . "customer_login` WHERE `email` = '" . $this->db->escape($email) . "'");
    485 	}
    486 }