<?php
/*
 osCommRes, Services Oline 
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 ausbank {
    var $code, $title, $description, $enabled;

// class constructor
	function ausbank() {
		global $order;
		
		$this->code = 'ausbank';
		$name = "Bank Transfer Payment";
		$image = "";
		if(MODULE_PAYMENT_AUSBANK_DISPLAY_NAME != "MODULE_PAYMENT_AUSBANK_DISPLAY_NAME")$name = MODULE_PAYMENT_AUSBANK_DISPLAY_NAME;
		if(MODULE_PAYMENT_AUSBANK_IMAGE != "MODULE_PAYMENT_AUSBANK_IMAGE")$image = MODULE_PAYMENT_AUSBANK_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_AUSBANK_TEXT_TITLE', $name . '&nbsp;&nbsp;' . $image);
		define('MODULE_PAYMENT_AUSBANK_TEXT_TEXT_TITLE', $name);
		$this->title = MODULE_PAYMENT_AUSBANK_TEXT_TITLE;
		$this->text_title = MODULE_PAYMENT_AUSBANK_TEXT_TEXT_TITLE;
		$this->description = MODULE_PAYMENT_AUSBANK_TEXT_DESCRIPTION;
		$this->email_footer = MODULE_PAYMENT_AUSBANK_TEXT_EMAIL_FOOTER;
		$this->sort_order = MODULE_PAYMENT_AUSBANK_SORT_ORDER;
		$this->enabled = ((MODULE_PAYMENT_AUSBANK_STATUS == 'True') ? true : false);
		
		if ((int)MODULE_PAYMENT_AUSBANK_ORDER_STATUS_ID > 0) {
			$this->order_status = MODULE_PAYMENT_AUSBANK_ORDER_STATUS_ID;
		}
		
		if (is_object($order)) $this->update_status();
	}

// class methods
    function update_status() {
      global $order;
	  
  	  tep_check_module_status($this,MODULE_PAYMENT_AUSBANK_ZONE,trim(MODULE_PAYMENT_AUSBANK_EXCEPT_ZONE),trim(MODULE_PAYMENT_AUSBANK_EXCEPT_COUNTRY));	
	 /* $except_zone_list=trim(MODULE_PAYMENT_AUSBANK_EXCEPT_ZONE);
	  $except_county_list=trim(MODULE_PAYMENT_AUSBANK_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) && ((int)MODULE_PAYMENT_AUSBANK_ZONE > 0) ) {
      if ( ($this->enabled == true) && (MODULE_PAYMENT_AUSBANK_ZONE!='') ) {
        $check_flag = false;
		$ausbank_zone=MODULE_PAYMENT_AUSBANK_ZONE!=""?MODULE_PAYMENT_AUSBANK_ZONE:''; // multizone
       // $check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_PAYMENT_AUSBANK_ZONE . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
        $check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id in(" . $ausbank_zone . ") and zone_country_id = '" . $order->delivery['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->delivery['zone_id']) {
            $check_flag = true;
            break;
          }
        }

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

// disable the module if the order only contains virtual products
      //if ($this->enabled == true) {
       // if ($order->content_type == 'virtual') {
        //  $this->enabled = false;
        //}
      //}
    }

    function javascript_validation() {
      return false;
    }

    function selection() {
	 global $order, $referenceID;
	 $order_query=tep_db_query("select max(orders_id) as orders_id from " . TABLE_ORDERS );
	 $order_result=tep_db_fetch_array($order_query);
	 $orders_id=$order_result['orders_id'] + 1;
	 $reference= '<b>' . HEADING_REFERENCE_ID . '</b>' .  $referenceID ;
	  $selection_text='<table cellpadding="0" cellspacing="0" border="0" width="100%" >
						<tr>
						<td class="smallText" width="50%">' . MODULE_PAYMENT_AUSBANK_BANK_DETAILS . '<br><small>' .  $reference . '</small>' .'</td>
						<td  class="smallText" width="50%" valign="bottom"><small><b>' .TITLE_PLEASE_NOTE .'</b><br>'. REFERENCE_DESCRIPTION  .'</small>
						</td> 
						</tr></table>';
      $selection = array('id' => $this->code,
                         'module' => $this->title,
                         'fields' => array(array('title' => '',
                                                 'field' => $selection_text)
						));
	  return $selection;
    }

    function pre_confirmation_check() {
      return false;
    }

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

    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_AUSBANK_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 Australian Bank Deposit Module', 'MODULE_PAYMENT_AUSBANK_STATUS', 'True', 'Do you want to accept Australian Bank Deposit 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, use_function, set_function, date_added) values ('Payment Zone', 'MODULE_PAYMENT_AUSBANK_ZONE', '0', 'If a zone is selected, only enable this payment method for that zone.', '6', '2', '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 ('Exclude these Countries', 'MODULE_PAYMENT_AUSBANK_EXCEPT_COUNTRY', '', 'If countries are selected, disable this payment method for that countries.', '6', '3', 'tep_cfg_pull_down_zone_except_countries(MODULE_PAYMENT_AUSBANK_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 ('Exclude these Zones', 'MODULE_PAYMENT_AUSBANK_EXCEPT_ZONE', '', 'If a zone is selected, disable this payment method for that zone.', '6', '4', '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, date_added) values ('Sort order of display.', 'MODULE_PAYMENT_AUSBANK_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '5', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('BSB Number', 'MODULE_PAYMENT_AUSBANK_BSB', '00-00-00', 'BSB Number in the format 000-000', '6', '6', now());");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Bank Account No.', 'MODULE_PAYMENT_AUSBANK_ACCNUM', '12345678', 'Bank Account No.', '6', '7', now());");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Swift Code.', 'MODULE_PAYMENT_AUSBANK_SWIFT', '12345678', 'Swift Code.', '6', '8', now());");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Bank Account Name', 'MODULE_PAYMENT_AUSBANK_ACCNAM', 'Joe Bloggs', 'Bank account name', '6', '9', now());");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Bank Name', 'MODULE_PAYMENT_AUSBANK_BANKNAM', 'The Bank', 'Bank Name', '6', '10', 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 ('Set Order Status', 'MODULE_PAYMENT_AUSBANK_ORDER_STATUS_ID', '0', 'Set the status of orders made with this payment module to this value', '6', '11', '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_AUSBANK_DISPLAY_NAME', 'Bank Transfer Payment', 'Set the Display name to payment module', '6', '12', 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_AUSBANK_IMAGE', 'ausbank', 'Set the Image of payment module', '6', '13', '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_AUSBANK_STATUS', 'MODULE_PAYMENT_AUSBANK_EXCEPT_ZONE', 'MODULE_PAYMENT_AUSBANK_EXCEPT_COUNTRY', 'MODULE_PAYMENT_AUSBANK_ZONE', 'MODULE_PAYMENT_AUSBANK_SORT_ORDER', 'MODULE_PAYMENT_AUSBANK_BSB', 'MODULE_PAYMENT_AUSBANK_ACCNUM', 'MODULE_PAYMENT_AUSBANK_ACCNAM', 'MODULE_PAYMENT_AUSBANK_SWIFT', 'MODULE_PAYMENT_AUSBANK_BANKNAM', 'MODULE_PAYMENT_AUSBANK_ORDER_STATUS_ID','MODULE_PAYMENT_AUSBANK_DISPLAY_NAME','MODULE_PAYMENT_AUSBANK_IMAGE');

    }
	
	function get_comments() {
		global $referenceID;
		  $comments.= MODULE_AUSBANK_INFO . TEXT_REFERENCEID . ' : ' . $referenceID ;
		  if($comments!="")
		  	return $comments;
		  else
		  	return;	
	}
  }
?>