<?php
/*
 osCommRes, Services Online 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare 

Released under the GNU General Public License 
*/
// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();
  class internal {
    var $code, $title, $description, $enabled;

// class constructor
	function internal($backend=0) {
		global $order,$HTTP_SESSION_VARS;
		
		$this->code = 'internal';
		$name = "Internal Only";
		$image = "";
		if(MODULE_PAYMENT_INTERNAL_DISPLAY_NAME != "MODULE_PAYMENT_INTERNAL_DISPLAY_NAME")$name = MODULE_PAYMENT_INTERNAL_DISPLAY_NAME;
		if(MODULE_PAYMENT_INTERNAL_IMAGE != "MODULE_PAYMENT_INTERNAL_IMAGE")$image = MODULE_PAYMENT_INTERNAL_IMAGE;
		if($image != "" && file_exists("../" . DIR_WS_IMAGES . $image)){
			$image = '<img src="'. HTTP_SERVER . DIR_WS_CATALOG . DIR_WS_IMAGES . $image . '" width="103" height="33">';
		}else{
			$image_array = array('.gif','.jpg','.jpeg','.png');
			$image_check = true;
			for($i=0;$i<sizeof($image_array);$i++){
				if($image_check && $image != "" && file_exists(DIR_WS_IMAGES . $image . $image_array[$i])){
					$image = '<img src="'. HTTP_SERVER . DIR_WS_CATALOG . DIR_WS_IMAGES . $image . $image_array[$i] . '" width="103" height="33">';
					$image_check = false;
				}
			}
			if($image_check)$image = "";
		}
		define('MODULE_PAYMENT_INTERNAL_TEXT_TITLE', $name . '&nbsp;&nbsp;' . $image);
		define('MODULE_PAYMENT_INTERNAL_TEXT_TEXT_TITLE', $name);
		$this->title = MODULE_PAYMENT_INTERNAL_TEXT_TITLE;
		$this->text_title = MODULE_PAYMENT_INTERNAL_TEXT_TEXT_TITLE;
		$this->description = MODULE_PAYMENT_INTERNAL_TEXT_DESCRIPTION;
		$this->sort_order = MODULE_PAYMENT_INTERNAL_SORT_ORDER;
		$this->enabled = ((MODULE_PAYMENT_INTERNAL_STATUS == 'True') ? true : false);
		if (tep_session_is_registered('login_id') && $HTTP_SESSION_VARS['login_id']!='')
		$this->enabled=true;
		else
		$this->enabled=false;
		
		if ((int)MODULE_PAYMENT_INTERNAL_ORDER_STATUS_ID > 0) {
			$this->order_status = MODULE_PAYMENT_INTERNAL_ORDER_STATUS_ID;
		}
		
		if (is_object($order)) $this->update_status();
		
		$this->email_footer = MODULE_PAYMENT_INTERNAL_TEXT_EMAIL_FOOTER;
	}

// class methods
    function update_status() {
      global $order;
	  
  	  tep_check_module_status($this,MODULE_PAYMENT_INTERNAL_ZONE,trim(MODULE_PAYMENT_INTERNAL_EXCEPT_ZONE),trim(MODULE_PAYMENT_INTERNAL_EXCEPT_COUNTRY));	
	 /* $except_zone_list=trim(MODULE_PAYMENT_INTERNAL_EXCEPT_ZONE);
	  $except_county_list=trim(MODULE_PAYMENT_INTERNAL_EXCEPT_COUNTRY);
	  //first filter the exception list
	  if ( ($this->enabled == true) && ($except_zone_list!='' || $except_country_list!='')) {
	  	$add_option='';
		if ($except_zone_list!='') $add_option.=" geo_zone_id in(" . $except_zone_list . ") ";
		if ($except_country_list!='') {
			if ($add_option!='') $add_option.=' or ';
			$add_option.=" zone_country_id in(" . $except_country_list . ") ";
		}
		$check_query=tep_db_query("SELECT zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where  (" . $add_option. ") and zone_country_id='" . $order->billing['country']['id'] . "' and (zone_id is null or zone_id = '0' or zone_id ='" . $order->billing['zone_id'] ."');");
		if (tep_db_num_rows($check_query)>0){
			$this->enabled=false;
		}
	  }
	  if ( ($this->enabled == true) && (MODULE_PAYMENT_INTERNAL_ZONE!='') ) {
        $check_flag = false;
		$internal_zone=MODULE_PAYMENT_INTERNAL_ZONE!=""?MODULE_PAYMENT_INTERNAL_ZONE:'';
        $check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id in(" . $internal_zone . ") and zone_country_id = '" . $order->billing['country']['id'] . "' order by zone_id");
        while ($check = tep_db_fetch_array($check_query)) {
          if ($check['zone_id'] < 1) {
            $check_flag = true;
            break;
          } elseif ($check['zone_id'] == $order->billing['zone_id']) {
            $check_flag = true;
            break;
          }
        }

        if ($check_flag == false) {
          $this->enabled = false;
        }
      } */
    }

    function javascript_validation() {
      return false;
    }

    function selection() {
      return array('id' => $this->code,
                   'module' => $this->title);
    }

    function pre_confirmation_check() {
      return false;
    }

    function confirmation() {
      return array('title' => MODULE_PAYMENT_INTERNAL_TEXT_DESCRIPTION);
    }

    function process_button() {
      return false;
    }

    function before_process() {
      return false;
    }

    function after_process() {
      return false;
    }

    function get_error() {
      return false;
    }

    function check() {
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_INTERNAL_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 Internal Only Order Module', 'MODULE_PAYMENT_INTERNAL_STATUS', 'True', 'Do you want to accept Cash Order payments?', '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, date_added) values ('Cash  Make Payable to:', 'MODULE_PAYMENT_CASH_PAYTO', '', 'Who should payments be made payable to?', '6', '1', now());");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Internal Only  Sort order of display.', 'MODULE_PAYMENT_INTERNAL_SORT_ORDER', '1', 'Sort order of display. Lowest is displayed first.', '6', '2', 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 ('Internal Only  Payment Zone', 'MODULE_PAYMENT_INTERNAL_ZONE', '0', 'If a zone is selected, only enable this payment method for that zone.', '6', '3', 'tep_get_zone_class_title', 'tep_cfg_pull_down_zone_classes(', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('Internal Only  Exclude these Countries', 'MODULE_PAYMENT_INTERNAL_EXCEPT_COUNTRY', '', 'If countries are selected, disable this payment method for that countries.', '6', '4', 'tep_cfg_pull_down_zone_except_countries(MODULE_PAYMENT_INTERNAL_ZONE,', 'tep_get_zone_except_country', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('Internal Only  Exclude these Zones', 'MODULE_PAYMENT_INTERNAL_EXCEPT_ZONE', '', 'If a zone is selected, disable this payment method for that zone.', '6', '5', 'tep_cfg_pull_down_zone_classes(','tep_get_zone_class_title', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('Internal Only  Set Order Status', 'MODULE_PAYMENT_INTERNAL_ORDER_STATUS_ID', '2', 'Set the status of orders made with this payment module to this value', '6', '6', 'tep_cfg_pull_down_order_statuses(', 'tep_get_order_status_name', now())");
	  tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Display Name', 'MODULE_PAYMENT_INTERNAL_DISPLAY_NAME', 'Internal Only', 'Set the Display name to payment module', '6', '7', 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 ('Image', 'MODULE_PAYMENT_INTERNAL_IMAGE', 'internal', 'Set the Image of payment module', '6', '8', 'tep_cfg_file_field(', now())");
    }

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

    function keys() {
      return array('MODULE_PAYMENT_INTERNAL_STATUS', 'MODULE_PAYMENT_INTERNAL_ZONE', 'MODULE_PAYMENT_INTERNAL_EXCEPT_ZONE', 'MODULE_PAYMENT_INTERNAL_EXCEPT_COUNTRY', 'MODULE_PAYMENT_INTERNAL_ORDER_STATUS_ID', 'MODULE_PAYMENT_INTERNAL_SORT_ORDER','MODULE_PAYMENT_INTERNAL_DISPLAY_NAME','MODULE_PAYMENT_INTERNAL_IMAGE');
    }
  }
?>
