PHP File Manager
Editing File: Offers.php
<?php /* * Copyright (C) Wayne Purton-Smith - All Rights Reserved * Unauthorized copying of this file or removing this paragraph, via any medium is strictly prohibited * Proprietary and confidential * Written by Wayne Purton-Smith <waynepurtonsmith@hotmail.co.uk> February 2014 */ class Offers extends CI_Model { private $reason = ''; public function get($offer_id = 0, $offer_code = NULL, $filters = array()) { $filters = array_merge(array ( 'include_deleted' => false, 'include_stats' => false ), (array) $filters); $limit = NULL; $include_stats = ($filters['include_stats'] === true); $sql = "SELECT *, IF(`has_expired`, 0, `is_active`) AS `is_active`, IF(`has_expired`, 3, IF(`is_active` = 0, 2, 1)) AS `code_type`"; if($include_stats) { $sql .= ", IFNULL(`dc`.`total_used`, 0) AS `total_used` "; } $sql .= " FROM ( SELECT *, IF(`dc`.`expires` IS NOT NULL, IF(" . strtotime('23:59:59') . " > `dc`.`expires`, 1, 0), 0) AS `has_expired` FROM `discount_codes` `dc`"; if($include_stats) { $sql .= " LEFT OUTER JOIN ( SELECT COUNT(`order_id`) AS `total_used`, `discount_code` FROM `orders` WHERE `discount_code` IS NOT NULL GROUP BY `discount_code` ) AS `o` ON `o`.`discount_code` = `dc`.`code_id` "; } $sql .= " WHERE "; if($filters['include_deleted'] === true) { $sql .= " 1 = 1"; } else { $sql .= " `dc`.`is_deleted` = 0 "; } if(($offer_id = (int) $offer_id) > 0) { $sql .= " AND `dc`.`code_id` = $offer_id "; $limit = 1; } if(($offer_code = remove_whitespace($offer_code))) { $sql .= " AND `dc`.`code` = " . $this->db->escape($offer_code) . " "; $limit = 1; } $sql .= " GROUP BY `dc`.`code_id` ORDER BY `has_expired`, `dc`.`is_active` DESC"; if($limit) { $sql .= " LIMIT $limit"; } $sql .= " ) AS `dc`"; $result = $this->db->query($sql); if($result->num_rows()) { if($limit === 1) return $result->row(); $rows = $result->result(); $result->free_result(); return $rows; } return array(); } public function save($data = array(), $offer_id = 0) { $offer_id = (($offer_id = (int) $offer_id) > 0) ? $offer_id : 0; $is_update = ($offer_id > 0); $offer_code = (isset($data['offer_code']) && ($offer_code = strtoupper(remove_whitespace(preg_replace('/[^\w]/', '', $data['offer_code'])))) != '') ? $offer_code : NULL; if(!$offer_code) { return $this->common->setResponseMessage('An identifiable code is required'); } if($this->db->query("SELECT 1 FROM `discount_codes` WHERE `is_deleted` = 0 AND `code` = ? AND `code_id` <> ? LIMIT 1", array($offer_code, $offer_id))->num_rows()) { return $this->common->setResponseMessage('The identifiable code already exists in the database'); } $apply_type = (isset($data['apply']) && ($apply_type = (int) $data['apply']) > 0 && in_array($apply_type, array(OFFER_DISCOUNT_ORDER_TOTAL, OFFER_DISCOUNT_ITEMS, OFFER_DISCOUNT_SHIPPING))) ? $apply_type : OFFER_DISCOUNT_ORDER_TOTAL; $discount_amount = (isset($data['discount_amount']) && ($discount_amount = (float) $data['discount_amount']) > 0) ? round($discount_amount, 2) : NULL; $discount_type = (isset($data['discount_type']) && ($discount_type = (int) $data['discount_type']) >= 1 && $discount_type <= 2) ? $discount_type : NULL; if($discount_type == DISCOUNT_PERCENTAGE && $discount_amount > 100) { return $this->common->setResponseMessage('The discount cannot be over 100 percent'); } $expiry_date = (isset($data['expires']) && ($expiry_date = strtotime($data['expires'] . ' 23:59:59')) >= strtotime('tomorrow 23:59:59')) ? $expiry_date : NULL; $one_time_only = (isset($data['limit']) && $data['limit'] == 1); $once_per_customer = (isset($data['limit']) && $data['limit'] == 2); $minimum_amount = (isset($data['minimum_amount']) && ($minimum_amount = (float) $data['minimum_amount']) > 0) ? $minimum_amount : NULL; $remaining_count = (isset($data['remaining']) && ($remaining_count = (int) $data['remaining']) > 0) ? $remaining_count : NULL; $is_active = (isset($data['is_active'])) ? (bool) $data['is_active'] : false; $sql_data = array ( 'code' => $offer_code, 'discount_type' => $discount_type, 'discount_amount' => $discount_amount, 'added' => NULL, 'expires' => $expiry_date, 'one_time_only' => $one_time_only, 'once_per_customer' => $once_per_customer, 'minimum_amount' => $minimum_amount, 'remaining' => $remaining_count, 'apply_type' => $apply_type, 'is_active' => $is_active ); if($is_update) { unset($sql_data['added'], $sql_data['remaining']); $sql = $this->db->update_string('discount_codes', $sql_data, "`code_id` = $offer_id"); } else { $sql_data['added'] = time(); $sql = $this->db->insert_string('discount_codes', $sql_data); } $result = $this->db->query($sql); if(($is_update && $result) || (!$is_update && $this->db->affected_rows() === 1)) { return $this->common->setResponseMessage('The offer details was successfully saved', true); } return $this->common->setResponseMessage('The offer details could not be saved at this time - please try again'); } public function findByCode($offer_code = NULL, $with_customer = NULL) { if(($offer_code = remove_whitespace($offer_code)) != '') { $offer_info = $this->get(NULL, $offer_code); if(!$offer_info || !$offer_info->is_active || $offer_info->has_expired) { return $this->returnReason('The discount code was not found'); } if($offer_info->remaining !== NULL && $offer_info->remaining == 0) { return $this->returnReason('This offer has been used up'); } if($offer_info->one_time_only) { if($this->orders->get(NULL, NULL, array('discount_id' => $offer_info->code_id, 'count' => true))) { return $this->returnReason('The discount code has already been used'); } } elseif($offer_info->once_per_customer) { if(($customer_id = (int) $with_customer) > 0) { if($this->orders->get(NULL, NULL, array('discount_id' => $offer_info->code_id, 'customer_id' => $customer_id, 'count' => true))) { return $this->returnReason('This discount code can only be used once'); } } } return $offer_info; } return false; } public function deductOfferCount($offer_id = 0) { if(!is_numeric($offer_id) && strlen($offer_id)) { $offer_id = (($offer_info = $this->get(NULL, $offer_id))) ? $offer_info->code_id : 0; } if(($offer_id = (int) $offer_id) > 0 && ($offer_info = $this->get($offer_id))) { if($offer_info->remaining !== NULL && ($offer_info->remaining - 1) >= 0) { $this->db->query("UPDATE `discount_codes` SET `remaining` = (`remaining` - 1) WHERE `code_id` = $offer_id"); return true; } } return false; } public function explainOffer($offer_details = NULL) { if(is_object($offer_details) && isset($offer_details->code_id)) { return (object) array ( 'code' => $offer_details->code, 'applies_to' => ($offer_details->apply_type == OFFER_DISCOUNT_ORDER_TOTAL) ? 'ORDER' : (($offer_details->apply_type == OFFER_DISCOUNT_ITEMS) ? 'ITEMS' : 'SHIPPING'), 'discount_type' => (int) $offer_details->discount_type, 'discount_type_text' => ($offer_details->discount_type == DISCOUNT_PERCENTAGE) ? 'PERCENT' : 'FIXED', 'amount' => (float) $offer_details->discount_amount, 'amount_text' => ($offer_details->discount_type == DISCOUNT_PERCENTAGE) ? number_format($offer_details->discount_amount, 2) . '%' : display_money($offer_details->discount_amount), 'expires' => ($offer_details->expires) ? (int) $offer_details->expires : NULL, 'expires_text' => ($offer_details->expires) ? date('l jS F Y', $offer_details->expires) : 'Never', 'one_time' => (bool) $offer_details->one_time_only, 'one_per_customer' => (bool) $offer_details->once_per_customer, 'minimum_amount' => ($offer_details->minimum_amount) ? (float) $offer_details->minimum_amount : NULL ); } return NULL; } public function returnReason($reason = '', $return_type = false) { $this->reason = $reason; return ($return_type === true); } public function getReason() { return $this->reason; } public function delete($offer_id = 0) { if(($offer_id = (int) $offer_id) > 0) { $this->db->query($this->db->update_string('discount_codes', array('is_deleted' => 1), "`code_id` = $offer_id")); return ($this->db->affected_rows() === 1); } return false; } }
Cancel