0byt3m1n1
Path:
/
data
/
17
/
1
/
18
/
11
/
1670011
/
user
/
1801231
/
htdocs
/
marketting
/
system
/
application
/
models
/
[
Home
]
File: zones_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 - zones_model.php | ------------------------------------------------------------------------- | | This model handles the functions for managing zones | */ class Zones_Model extends Model { function _add_region() { $data = $this->db_validation_model->_clean_data($_POST); //insert into db if (!$this->db->insert('regions', $data)) { show_error($this->lang->line('could_not_add_region')); //log error log_message('error', 'Could not insert region into regions table'); return false; } else { $region_id = $this->db->insert_id(); //log success log_message('info', 'region '. $region_id . ' inserted into region table'); } return $region_id; } // ------------------------------------------------------------------------ function _calculate_tax($data = '') { if (!empty($data['region'])) { $sql = 'SELECT ' . $this->db->dbprefix('zones') . '.*, ' . $this->db->dbprefix('tax_zones') . '.* FROM ' . $this->db->dbprefix('zones') . ' LEFT JOIN ' . $this->db->dbprefix('tax_zones') . ' ON (' . $this->db->dbprefix('zones') . '.tax_zone_id = ' . $this->db->dbprefix('tax_zones') . '.tax_zone_id) WHERE ' . $this->db->dbprefix('zones') . '.country_id = \'' . $data['country'] . '\' AND ' . $this->db->dbprefix('zones') . '.region_id = \'' . $data['region'] . '\''; } else { $sql = 'SELECT ' . $this->db->dbprefix('zones') . '.*, ' . $this->db->dbprefix('tax_zones') . '.* FROM ' . $this->db->dbprefix('zones') . ' LEFT JOIN ' . $this->db->dbprefix('tax_zones') . ' ON (' . $this->db->dbprefix('zones') . '.tax_zone_id = ' . $this->db->dbprefix('tax_zones') . '.tax_zone_id) WHERE ' . $this->db->dbprefix('zones') . '.country_id = \'' . $data['country'] . '\''; } $query = $this->db->query($sql); if ($query->num_rows() > 0) { return $query->row_array(); } else { //check for default tax $sql = 'SELECT ' . $this->db->dbprefix('zones') . '.*, ' . $this->db->dbprefix('tax_zones') . '.* FROM ' . $this->db->dbprefix('zones') . ' LEFT JOIN ' . $this->db->dbprefix('tax_zones') . ' ON (' . $this->db->dbprefix('zones') . '.tax_zone_id = ' . $this->db->dbprefix('tax_zones') . '.tax_zone_id) WHERE ' . $this->db->dbprefix('zones') . '.country_id = \'0\' AND ' . $this->db->dbprefix('zones') . '.region_id = \'0\''; $query = $this->db->query($sql); if ($query->num_rows() > 0) { return $query->row_array(); } } return false; } // ------------------------------------------------------------------------ function _add_zone() { $data = $this->db_validation_model->_clean_data($_POST); if (!empty($data['regions']) AND count($data['regions']) > 0) { $regions = $data['regions']; unset($data['regions']); } //insert into db if (!$this->db->insert('tax_zones', $data)) { show_error($this->lang->line('could_not_add_zone')); //log error log_message('error', 'Could not insert zone into tax zones table'); return false; } else { $zone_id = $this->db->insert_id(); //check if there are any tax regions selected if (!empty($regions)) { foreach ($regions as $v) { $options = explode('.', $v); $zone = array( 'tax_zone_id' => $zone_id, 'country_id' => $options[0], 'region_id' => $options[1] ); $this->db->insert('zones', $zone); } } //log success log_message('info', 'zone '. $zone_id . ' inserted into tax zone table'); } return $zone_id; } // ------------------------------------------------------------------------ function _change_status($data = '', $type = '0') { foreach ($data as $id) { $ship_to = $type; if ($id == $this->config->item('sts_store_default_country')) { $ship_to = '1'; } $this->db->where('country_id', $id); //update country in db $data = array( 'ship_to' => $ship_to, ); if (!$this->db->update('countries', $data)) { show_error($this->lang->line('could_not_update_country')); //log error log_message('error', 'Could not update country ID #' . $id . ' in countries table'); return false; } //log success log_message('info', 'Status Changed for country ID# ' . $id); } return true; } // ------------------------------------------------------------------------ function _add_country() { $data = $this->db_validation_model->_clean_data($_POST); //insert into db if (!$this->db->insert('countries', $data)) { show_error($this->lang->line('could_not_add_country')); //log error log_message('error', 'Could not insert country into zones table'); return false; } else { $country_id = $this->db->insert_id(); //log success log_message('info', 'zone '. $country_id . ' inserted into country table'); } return $country_id; } // ------------------------------------------------------------------------ function _delete_zone($id = '') { if ($id == 1) { return false; } //delete zones $this->db->where('tax_zone_id', $id); if ($this->db->delete('zones')) { //log success log_message('info', 'zone ID #' . $id . ' deleted successfully'); } else { show_error($this->lang->line('could_not_delete_zone')); //log error log_message('error', 'zone ID #' . $id . ' could not be deleted'); } //delete tax zone $this->db->where('tax_zone_id', $id); if ($this->db->delete('tax_zones')) { //log success log_message('info', 'tax zone ID #' . $id . ' deleted successfully'); } else { show_error($this->lang->line('could_not_delete_zone')); //log error log_message('error', 'tax zone ID #' . $id . ' could not be deleted'); } return true; } // ------------------------------------------------------------------------ function _delete_country($id = '') { if ($id == 223) { return false; } //delete regions $this->db->where('region_country_id', $id); if ($this->db->delete('regions')) { //log success log_message('info', 'regions deleted successfully'); } else { show_error($this->lang->line('could_not_delete_region')); //log error log_message('error', 'regions could not be deleted'); } //delete zones $this->db->where('country_id', $id); if ($this->db->delete('zones')) { //log success log_message('info', 'zones deleted successfully'); } else { show_error($this->lang->line('could_not_delete_region')); //log error log_message('error', 'regions could not be deleted'); } //delete country $this->db->where('country_id', $id); if ($this->db->delete('countries')) { //log success log_message('info', 'country ID #' . $id . ' deleted successfully'); } else { show_error($this->lang->line('could_not_delete_country')); //log error log_message('error', 'country ID #' . $id . ' could not be deleted'); } return true; } // ------------------------------------------------------------------------ function _delete_region($id = '') { //update products first $this->db->where('region_id', $id); $data = array('region_id' => 0); if ($this->db->update('zones', $data)) { //log success log_message('info', 'region ID #' . $id . ' updated successfully'); } else { show_error($this->lang->line('could_not_delete_region')); //log error log_message('error', 'region ID #' . $id . ' could not be updated'); } //delete region $this->db->where('region_id', $id); if ($this->db->delete('regions')) { //log success log_message('info', 'region ID #' . $id . ' deleted successfully'); } else { show_error($this->lang->line('could_not_delete_region')); //log error log_message('error', 'region ID #' . $id . ' could not be deleted'); } return true; } // ------------------------------------------------------------------------ function _get_country_details($id = '') { //get the data from zones table $this->db->where('country_id', $id); $query = $this->db->get('countries'); if ($query->num_rows() > 0) { return $query->result_array(); } else { return false; } } // ------------------------------------------------------------------------ function _get_region_details($id = '') { //get the data from zones table $this->db->where('region_id', $id); $query = $this->db->get('regions'); if ($query->num_rows() > 0) { return $query->result_array(); } else { return false; } } // ------------------------------------------------------------------------ function _get_zone_details($id = '') { //get the data from zones table $this->db->where('tax_zone_id', $id); $query = $this->db->get('tax_zones'); if ($query->num_rows() > 0) { return $query->result_array(); } else { return false; } } // ------------------------------------------------------------------------ function _get_regions($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_reg_order'); if (!$sort_column) $sort_column = $this->config->item('dbs_reg_column'); $sql = 'SELECT ' . $this->db->dbprefix('regions') . '.*, (SELECT country_name from ' . $this->db->dbprefix('countries') . ' WHERE ' . $this->db->dbprefix('regions') . '.region_country_id = ' . $this->db->dbprefix('countries') . '.country_id) as country FROM ' . $this->db->dbprefix('regions') . ' 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 _get_countries($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_con_order'); if (!$sort_column) $sort_column = $this->config->item('dbs_con_column'); $this->db->order_by($sort_column, $sort_order); $query = $this->db->get('countries', $limit, $offset); if ($query->num_rows() > 0) { return $query->result_array(); } return false; } // ------------------------------------------------------------------------ function _get_zones($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_zon_order'); if (!$sort_column) $sort_column = $this->config->item('dbs_zon_column'); $this->db->order_by($sort_column, $sort_order); $query = $this->db->get('tax_zones', $limit, $offset); if ($query->num_rows() > 0) { return $query->result_array(); } return false; } // ------------------------------------------------------------------------ function _get_tax_zones($id = '') { $this->db->where('tax_zone_id', $id); $query = $this->db->get('zones'); if ($query->num_rows() > 0) { $selected = array(); foreach ($query->result_array() as $options) { array_push($selected, $options['country_id'] . '.' . $options['region_id']); } return $selected; } return false; } // ------------------------------------------------------------------------ function _select_zones() { //get all countries //get all regions $sql = 'SELECT CONCAT(country_id, \'.0\') AS country_id, CONCAT(country_name, \' - All Regions\') AS country_name FROM ' . $this->db->dbprefix('countries') . ' ORDER BY country_id '; $query = $this->db->query($sql); if ($query->num_rows() > 0) { $countries = $query->result_array(); } $new_countries = format_array($countries, 'country_id', 'country_name'); //get all regions $sql = 'SELECT CONCAT(region_country_id, \'.\', region_id) as region_id, (SELECT country_name from ' . $this->db->dbprefix('countries') . ' WHERE ' . $this->db->dbprefix('regions') . '.region_country_id = ' . $this->db->dbprefix('countries') . '.country_id) as country, region_name FROM ' . $this->db->dbprefix('regions') . ' ORDER BY region_id'; $query = $this->db->query($sql); if ($query->num_rows() > 0) { $regions = $query->result_array(); } $a = array('0.0'); $b = array($this->lang->line('all_countries') . ' - ' . $this->lang->line('all_regions')); foreach ($regions as $v) { array_push($a, $v['region_id']); array_push($b, $v['country'] . ' - ' . $v['region_name']); } $new_regions = combine_array($a, $b); $options = array_merge($new_countries, $new_regions); ksort($options); return $options; } // ------------------------------------------------------------------------ function _update_country() { //clean the data first $data = $this->db_validation_model->_clean_data($_POST); //update data $this->db->where('country_id', $data['country_id']); if (!$this->db->update('countries', $data)) { show_error($this->lang->line('could_not_update_country')); //log error log_message('error', 'Could not update country ID ' . $data['country_id'] . 'in countries table'); return false; } else { //log success log_message('info', 'zone ID '. $data['country_id'] . ' updated in countries table'); } return $data['country_id']; } // ------------------------------------------------------------------------ function _update_region() { //clean the data first $data = $this->db_validation_model->_clean_data($_POST); //update data $this->db->where('region_id', $data['region_id']); if (!$this->db->update('regions', $data)) { show_error($this->lang->line('could_not_update_region')); //log error log_message('error', 'Could not update region ID ' . $data['zone_id'] . 'in region table'); return false; } else { //log success log_message('info', 'zone ID '. $data['region_id'] . ' updated in region table'); } return $data['region_id']; } // ------------------------------------------------------------------------ function _update_zone($id = '') { $data = $this->db_validation_model->_clean_data($_POST); if (!empty($data['regions']) AND count($data['regions']) > 0) { $regions = $data['regions']; unset($data['regions']); } //update into db $this->db->where('tax_zone_id', $id); if (!$this->db->update('tax_zones', $data)) { show_error($this->lang->line('could_not_update_zone')); //log error log_message('error', 'Could not update zone into tax zones table'); return false; } else { $this->db->where('tax_zone_id', $id); $this->db->delete('zones'); //check if there are any tax regions selected if (!empty($regions)) { foreach ($regions as $v) { $options = explode('.', $v); $zone = array( 'tax_zone_id' => $id, 'country_id' => $options[0], 'region_id' => $options[1] ); $this->db->insert('zones', $zone); } } //log success log_message('info', 'zone '. $id . ' updated into tax zone table'); } return $id; } // ------------------------------------------------------------------------ } ?>