0byt3m1n1
Path:
/
data
/
17
/
1
/
18
/
11
/
1670011
/
user
/
1801231
/
htdocs
/
marketting
/
system
/
application
/
models
/
[
Home
]
File: coupons_model.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /* | ------------------------------------------------------------------------- | COPYRIGHT NOTICE | Copyright 2008 JROX Technologies, Inc. All Rights Reserved. | ------------------------------------------------------------------------- | This script may be only used and modified in accordance to the license | agreement attached (license.txt) except where expressly noted within | commented areas of the code body. This copyright notice and the | comments above and below must remain intact at all times. By using this | code you agree to indemnify JROX Technologies, Inc, its corporate agents | and affiliates from any liability that might arise from its use. | | Selling the code for this program without prior written consent is | expressly forbidden and in violation of Domestic and International | copyright laws. | | ------------------------------------------------------------------------- | FILENAME - coupons_model.php | ------------------------------------------------------------------------- | | This model handles the functions for managing coupons | */ class Coupons_Model extends Model { var $table = 'coupons'; function _add_coupon($array = '') { $data = $this->db_validation_model->_clean_data($array); if (!empty($data['assign_to_member'])) { //get member_id $member = $this->db_validation_model->_get_details('members', 'member_id', 'username', $data['assign_to_member']); $data['member_id'] = $member[0]['member_id']; } unset ($data['assign_to_member']); //format date $pub = explode('/', $data['coupon_start_date']); $data['coupon_start_date'] = mktime(0,0,0, $pub[0], $pub[1], $pub[2]); $pub = explode('/', $data['coupon_expire_date']); $data['coupon_expire_date'] = mktime(24,59,59, $pub[0], $pub[1], $pub[2]); //insert into db if (!$this->db->insert($this->table, $data)) { show_error($this->lang->line('could_not_add_coupon')); //log error log_message('error', 'Could not insert coupon into coupons table'); return false; } else { $coupon_id = $this->db->insert_id(); //log success log_message('info', 'coupon '. $coupon_id . ' inserted into coupons table'); } return $coupon_id; } // ------------------------------------------------------------------------ function _auto_generate_credit_coupons() { if ($this->config->item('sts_affiliate_auto_generate_coupon_credits') == 1) { $this->db->where('used', '0'); $this->db->join('members', 'members_credits.member_id =members.member_id'); $query = $this->db->get('members_credits'); if ($query->num_rows() > 0) { foreach ($query->result_array() as $row) { $this->_generate_credit_coupons($row['member_id'], $row['username'], $row['amount']); $data = array('used' => '1'); $this->db->where('member_id', $row['member_id']); $this->db->update('members_credits', $data); } } } } // ------------------------------------------------------------------------ function _generate_credit_coupons($id = '', $username = '', $amount = '') { $coupon_code = substr($username, 0, 10) . random_string('numeric', 4); while ($this->_check_coupon_code($coupon_code) == true) { $coupon_code = substr($username, 0, 10) . random_string('numeric', 4); } $data = array( 'coupon_type' => 'flat', 'status' => '1', 'coupon_code' => $coupon_code, 'coupon_amount' => round($amount, 2), 'coupon_minimum_order' => '0', 'coupon_start_date' => _generate_timestamp(), 'coupon_expire_date' => _generate_timestamp() + (60 * 60 * 24 * $this->config->item('sts_affiliate_coupon_expires')), 'uses_per_coupon' => '1', 'member_id' => $id, 'coupon_uses' => '0', 'coupon_product_type' => $this->config->item('sts_affiliate_auto_generate_coupon_recurring') == '1' ? 2 : 1, ); if ($this->db->insert($this->table, $data)) { return true; } return false; } // ------------------------------------------------------------------------ function _check_coupon_code($coupon = '') { $this->db->where('coupon_code', $coupon); $query = $this->db->get($this->table); if ($query->num_rows() > 0) { return true; } return false; } // ------------------------------------------------------------------------ function _get_member_coupons($id = '') { $this->db->where('member_id', $id); $query = $this->db->get($this->table); if ($query->num_rows() > 0) { return $query->result_array(); } return false; } // ------------------------------------------------------------------------ function _auto_generate_coupon($id = '', $username = '') { if ($this->config->item('sts_affiliate_auto_generate_coupon') == 1) { //check if the user already has a coupon in the coupon table $total = $this->_get_member_coupons($id); if (empty($total)) { $coupon_code = substr($username, 0, 10) . random_string('numeric', 4); while ($this->_check_coupon_code($coupon_code) == true) { $coupon_code = substr($username, 0, 10) . random_string('numeric', 4); } $data = array( 'coupon_type' => $this->config->item('sts_affiliate_auto_generate_coupon_type'), 'status' => '1', 'coupon_code' => $coupon_code, 'coupon_amount' => $this->config->item('sts_affiliate_auto_generate_coupon_amount'), 'coupon_minimum_order' => '0', 'coupon_start_date' => _generate_timestamp(), 'coupon_expire_date' => _generate_timestamp() + (60 * 60 * 24 * $this->config->item('sts_affiliate_coupon_expires')), 'uses_per_coupon' => $this->config->item('sts_affiliate_coupon_limit_use'), 'member_id' => $id, 'coupon_uses' => '0', 'coupon_product_type' => $this->config->item('sts_affiliate_auto_generate_coupon_recurring') == '1' ? 2 : 1, ); if ($this->db->insert($this->table, $data)) { return true; } } } return false; } // ------------------------------------------------------------------------ function _check_coupon_update($coupon = '') { $this->db->where('coupon_code', $coupon); $query = $this->db->get($this->table); if ($query->num_rows() > 0) { $row = $query->row(); $total = $row->coupon_uses + 1; //update row $new = array('coupon_uses' => $total); $this->db->where('coupon_code', $coupon); if ($this->db->update($this->table, $new)) { return true; } } return false; } // ------------------------------------------------------------------------ function _verify_coupon($array = '', $num_options = '', $product_type = '1') { $this->db->where('coupon_code', $array['name']); $this->db->where('status', '1'); $query = $this->db->get($this->table); if ($query->num_rows > 0) { $row = $query->row_array(); //check the coupon_product_type if ($row['coupon_product_type'] != $product_type) { $data = array('valid' => false, 'msg' => $this->lang->line('invalid_coupon')); return $data; } //check if coupon date is between start date and expire date $curdate = _generate_timestamp(); if ($curdate < $row['coupon_start_date']) { $data = array('valid' => false, 'msg' => $this->lang->line('invalid_coupon')); return $data; } if ($curdate > $row['coupon_expire_date']) { $data = array('valid' => false, 'msg' => $this->lang->line('coupon_expired')); return $data; } //check for coupon uses if ($row['uses_per_coupon'] > 0) { if ($row['coupon_uses'] >= $row['uses_per_coupon']) { $data = array('valid' => false, 'msg' => $this->lang->line('maximum_redemption_used')); return $data; } } //check for minimum order if ($array['order_amount'] < $row['coupon_minimum_order']) { $data = array('valid' => false, 'msg' => $row['coupon_minimum_order'] . ' ' . $this->lang->line('minimum_order_not_reached')); return $data; } //calculate the coupon amount if ($row['coupon_type'] == 'percent') { $coupon_amount = $array['order_amount'] * $row['coupon_amount']; } else { $coupon_amount = $row['coupon_amount']; } $coupon_amount = round($coupon_amount, 2); $data = array('valid' => true, 'coupon_amount' => $coupon_amount, 'coupon_code' => $row['coupon_code'], 'member_id' => empty($row['member_id']) ? '' : $row['member_id'], 'msg' => $this->lang->line('coupon_applied_successfully') ); //get the username if (!empty($row['member_id'])) { $member = $this->db_validation_model->_get_details('members', 'username', 'member_id', $row['member_id']); $data['member_username'] = $member[0]['username']; $data['jrox_coupon_referral_name'] = $data['member_id'] . '-' . $data['member_username']; $this->session->set_userdata('jrox_coupon_referral_name', $data['jrox_coupon_referral_name']); } else { $data['member_username'] = ''; $data['jrox_coupon_referral_name'] = ''; } return $data; } else { $data = array('valid' => false, 'msg' => $this->lang->line('invalid_coupon')); return $data; } } // ------------------------------------------------------------------------ function _delete_coupon($id = '') { //delete coupon $this->db->where('coupon_id', $id); if ($this->db->delete($this->table)) { //log success log_message('info', 'coupon ID #' . $id . ' deleted successfully'); } else { show_error($this->lang->line('could_not_delete_coupon')); //log error log_message('error', 'coupon ID #' . $id . ' could not be deleted'); } return true; } // ------------------------------------------------------------------------ function _get_coupon_details($id = '', $column = 'coupon_id') { //get the data from coupons table $this->db->where($column, $id); $query = $this->db->get($this->table); if ($query->num_rows() > 0) { return $query->row_array(); } else { return false; } } // ------------------------------------------------------------------------ function _get_coupons($limit = 25, $offset = 0, $sort_column = '', $sort_order = '') { //get all the admins from db for list view if (!$sort_order) $sort_order = $this->config->item('dbs_cou_order'); if (!$sort_column) $sort_column = $this->config->item('dbs_cou_column'); $sql = 'SELECT ' . $this->db->dbprefix($this->table) . '.* FROM ' . $this->db->dbprefix($this->table) . ' ORDER BY ' . $sort_column . ' ' . $sort_order . ' LIMIT ' . $offset. ', ' . $limit; $query = $this->db->query($sql); if ($query->num_rows() > 0) { return $query->result_array(); } return false; } // ------------------------------------------------------------------------ function _update_coupon($id = '') { //clean the data first $data = $this->db_validation_model->_clean_data($_POST); if (!empty($data['assign_to_member'])) { //get member_id $member = $this->db_validation_model->_get_details('members', 'member_id', 'username', $data['assign_to_member']); $data['member_id'] = $member[0]['member_id']; } else { $data['member_id'] = ''; } unset ($data['assign_to_member']); //format date $pub = explode('/', $data['coupon_start_date']); $data['coupon_start_date'] = mktime(0,0,0, $pub[0], $pub[1], $pub[2]); $pub = explode('/', $data['coupon_expire_date']); $data['coupon_expire_date'] = mktime(24,59,59, $pub[0], $pub[1], $pub[2]); //update coupon data $this->db->where('coupon_id', $id); if (!$this->db->update($this->table, $data)) { show_error($this->lang->line('could_not_update_coupon')); //log error log_message('error', 'Could not update coupon ID ' . $id . 'in coupons table'); return false; } else { //log success log_message('info', 'coupon ID '. $id . ' updated in coupons table'); } return true; } // ------------------------------------------------------------------------ function _get_user_coupons($id = '', $num_options = '') { $this->db->where('member_id', $id); //$this->db->or_where('member_id', ''); //$this->db->or_where('member_id', NULL); $this->db->where('coupon_start_date <', now()); $this->db->where('coupon_expire_date >', now()); $this->db->where('status', '1'); $this->db->order_by($this->config->item('mem_dbs_cou_column'), $this->config->item('mem_dbs_cou_order')); //$this->db->limit($limit, $offset); $query = $this->db->get($this->table); if ($query->num_rows() > 0) { $a['coupons'] = array(); foreach ($query->result_array() as $row) { //check if coupon has been used up if (!empty($row['uses_per_coupon'])) { if ($row['coupon_uses'] >= $row['uses_per_coupon']) { continue; } } $row['s_coupon_expire_date'] = _show_date($row['coupon_expire_date']); $row['s_coupon_product_type'] = $row['coupon_product_type'] == 1 ? $this->lang->line('products') : $this->lang->line('memberships'); $row['s_coupon_type'] = $row['coupon_type'] == 'percent' ? $this->lang->line('percent') : $this->lang->line('flat'); $row['s_coupon_amount'] = $row['coupon_type'] == 'flat' ? format_amounts($row['coupon_amount'], $num_options) : $row['coupon_amount']; $row['row_style'] = alternator('jroxRowStyle1', 'jroxRowStyle2'); array_push($a['coupons'], $row); } return $a; } return false; } } ?>
© 2017 -
ZeroByte.ID
.