<?php
// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();
/*
  $Id: auspost.php,v 2.0.2 2003/10/15

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

  class skynet {
    var $code, $title, $description, $icon, $enabled;

// class constructor
    function skynet() {
      global $order;
      $this->code = 'skynet';
      $this->title = MODULE_SHIPPING_SKYNET_TEXT_TITLE;
      $this->description = MODULE_SHIPPING_SKYNET_TEXT_DESCRIPTION;
      $this->sort_order = MODULE_SHIPPING_SKYNET_SORT_ORDER;
      $this->icon = DIR_WS_ICONS . 'skynet.gif';
      $this->tax_class = MODULE_SHIPPING_SKYNET_TAX_CLASS;
      $this->enabled = ((MODULE_SHIPPING_SKYNET_STATUS == 'True') ? true : false);	  
	  //tep_check_shipping_module_status($this,MODULE_SHIPPING_AUSPOST_ZONE,trim(MODULE_SHIPPING_AUSPOST_EXCEPT_ZONE),trim(MODULE_SHIPPING_AUSPOST_EXCEPT_COUNTRY));
    }

// class methods
    function quote($method = '') {
      global $order,  $cart, $shipping_weight, $shipping_num_boxes, $total_weight;
      $frompcode = MODULE_SHIPPING_SKYNET_SPCODE;
      $topcode = $order->delivery['postcode'];
      $sweight = $total_weight;
      
      $error = false;
      if($frompcode!=""){
			$select_frompcode=tep_db_query("SELECT zone FROM skynet_zones_postcodes WHERE postcode_start <= '".$frompcode."' AND postcode_end >= '".$frompcode."'");
			if(tep_db_num_rows($select_frompcode)>0){
				$select_fromp=tep_db_fetch_array($select_frompcode);
				$select_from_code=$select_fromp['zone'];
			}							  			
	  }		
	  		  
	  if($topcode!=""){
	  		$select_topcode=tep_db_query("SELECT zone FROM skynet_zones_postcodes WHERE postcode_start <= '".$topcode."' AND postcode_end >= '".$topcode."'");
			if(tep_db_num_rows($select_topcode)>0){
				$select_top=tep_db_fetch_array($select_topcode);
				$select_to_code=$select_top['zone'];
			}
	  }								  
	  if($select_from_code!="" && $select_to_code!=""){
	  	$price_for_zone_query=tep_db_query("SELECT sr.basic, sr.per_kg FROM skynet_zones_bands s,skynet_rates sr WHERE s.zone_from = '".$select_from_code."' AND s.zone_to = '".$select_to_code."' and sr.service = 'DP' and sr.band=s.band");	  	
	  	if(tep_db_num_rows($price_for_zone_query)>0){
			$price=tep_db_fetch_array($price_for_zone_query);
			$basic_rate=$price['basic'];		  		
			$per_kg=$price['per_kg'];
	  	}	   	  	
	  }
	  $charge=$basic_rate;
	  if($sweight>1){
		$cal_weight=$sweight-1;
		$charge_per_kg=$cal_weight*$per_kg;								 
	  }
	  $shipping_skynet_cost=$charge+$charge_per_kg;	
      if ($shipping_skynet_cost <= 0) 
        $error = true;
      else {
		$handling = MODULE_SHIPPING_SKYNET_HANDLING;
		$shipping_auspost_method = $shipping_num_boxes . "&nbsp;" .MODULE_SHIPPING_SKYNET_TEXT_BOXES  . " " . $total_weight . MODULE_SHIPPING_SKYNET_TEXT_UNITS;	
      }
	     $this->quotes = array('id' => $this->code,
                            'module' => MODULE_SHIPPING_SKYNET_TEXT_TITLE,
                            'methods' => array(array('id' => $this->code,
                                                     'title' => $shipping_auspost_method,
                                                     'cost' => $shipping_skynet_cost)));

      if ($this->tax_class > 0) {
        $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
      }

      if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title,'103','33');

      if ($error == true) $this->quotes['error'] = MODULE_SHIPPING_SKYNET_TEXT_ERROR;
	  return $this->quotes;
    }

    function check() {
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_AUSPOST_STATUS'");
        $this->_check = tep_db_num_rows($check_query);
      }
      return $this->_check;
    }

    function install() {
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) VALUES ('Enable skynet', 'MODULE_SHIPPING_SKYNET_STATUS', 'True', 'Do you want to offer skynet?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");            
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_SHIPPING_SKYNET_TAX_CLASS', '0', 'Use the following tax class on the shipping fee.', '6', '11', 'tep_get_tax_class_title', 'tep_cfg_pull_down_tax_classes(', now())");      
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort order of display.', 'MODULE_SHIPPING_SKYNET_SORT_ORDER', '1', 'Sort order of display. Lowest is displayed first.', '6', '12', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('Dispatch Postcode', 'MODULE_SHIPPING_SKYNET_SPCODE', '2000', 'Dispatch Postcode?', '6', '3', now())");           
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function,  date_added) values ('zone settings', 'MODULE_SHIPPING_ZONE_SETTING', '', 'Use the shipping zone.', '6', '11','tep_cfg_skynet_zone_settings(', now())");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
      return array('MODULE_SHIPPING_SKYNET_STATUS','MODULE_SHIPPING_SKYNET_TAX_CLASS','MODULE_SHIPPING_SKYNET_SORT_ORDER','MODULE_SHIPPING_SKYNET_SPCODE','MODULE_SHIPPING_ZONE_SETTING');
    }
  }
?>