<?php
/*
###################################################################################
  Bigware Shop 2.3
  Release Datum: 23.08.2015
  
  Bigware Shop
  http://www.bigware.de

  Copyright (c) 2015 Bigware LTD
  (c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
  (c) 2015	Bigware LTD

  Copyrightvermerke duerfen nicht entfernt werden.
  ------------------------------------------------------------------------
  Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
  der GNU General Public License, wie von der Free Software Foundation
  veroeffentlicht, weitergeben und/oder modifizieren, entweder gemaess Version 2 
  der Lizenz oder (nach Ihrer Option) jeder spaeteren Version.
  Die Veroeffentlichung dieses Programms erfolgt in der Hoffnung, dass es Ihnen
  von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die
  implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FUER EINEN
  BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.
  
  Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
  Programm erhalten haben. Falls nicht, schreiben Sie an die Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

  Infos:
  ------------------------------------------------------------------------
  Der Bigware Shop wurde vor vielen Jahren bereits aus dem bekannten Shopsystem osCommerce
  weiter- und neuentwickelt.
  Der Bigware Shop legt im hohen Masse Wert auf Bedienerfreundlichkeit, beinhaltet eine leichte
  Installation, viele neue professionelle Werkzeuge und zeichnet sich aus durch eine grosse 
  Community, die bei Problemen weiterhelfen kann.
  
  Der Bigware Shop ist auf jedem System lauffaehig, welches eine PHP Umgebung
  (ab PHP 4.1.3) und mySQL zur Verfuegung stellt und auf Linux basiert.
 
  Hilfe erhalten Sie im Forum auf www.bigware.de 
  
  -----------------------------------------------------------------------
  
 ##################################################################################




*/
?>
<?php
class free {
	var $title, $output;
	function free() {
		global $order;
		if (!defined(CONSTITUENT_SHIPPING_FREE_SHIPPING_OVER)) {
			define(CONSTITUENT_SHIPPING_FREE_SHIPPING_OVER, 'X');
		}
		if (CONSTITUENT_SHIPPING_FREE_STATUS == 'true') {
			switch (CONSTITUENT_SHIPPING_FREE_DESTINATION) {
				case 'national':
					if ($order->delivery['land_id'] == SHOP_LAND) $pass = true; break;
				case 'international':
					if ($order->delivery['land_id'] != SHOP_LAND) $pass = true; break;
				case 'both':
					$pass = true; break;
				default:
					$pass = false; break;
			}
			if ( ($pass == true) && ( ($order->info['total'] - $order->info['shipping_cost']) >= CONSTITUENT_SHIPPING_FREE_SHIPPING_OVER) ) {
				$this->enabled = true;
			} else {
				$this->enabled = false;
			}
		}
		$this->code = 'free';
		$this->title = CONSTITUENT_SHIPPING_FREE_TEXT_TITLE;
		$this->description = sprintf(CONSTITUENT_SHIPPING_FREE_TEXT_DESCRIPTION, CONSTITUENT_SHIPPING_FREE_SHIPPING_OVER);
		$this->sort_order = CONSTITUENT_SHIPPING_FREE_SORT_ORDER;
		$this->output = array();
	}
	function process() {
		global $order, $currencies;
		if (CONSTITUENT_SHIPPING_FREE_STATUS == 'true') {
			switch (CONSTITUENT_SHIPPING_FREE_DESTINATION) {
				case 'national':
					if ($order->delivery['land_id'] == SHOP_LAND) $pass = true; break;
				case 'international':
					if ($order->delivery['land_id'] != SHOP_LAND) $pass = true; break;
				case 'both':
					$pass = true; break;
				default:
					$pass = false; break;
			}
			if ( ($pass == true) && ( ($order->info['total'] - $order->info['shipping_cost']) >= CONSTITUENT_SHIPPING_FREE_SHIPPING_OVER) ) {
				$order->info['shipping_method'] = $this->title;
				$order->info['total'] -= $order->info['shipping_cost'];
				$order->info['shipping_cost'] = 0;
			}
		}
		$module = substr($GLOBALS['shipping']['id'], 0, strpos($GLOBALS['shipping']['id'], '_'));
		if (go_not_null($order->info['shipping_method'])) {
			if ($GLOBALS[$module]->tax_class > 0) {
				$shipping_tax = go_get_tax_rate($GLOBALS[$module]->tax_class, $order->delivery['land']['id'], $order->delivery['zone_id']);
				$shipping_tax_description = go_get_tax_description($GLOBALS[$module]->tax_class, $order->delivery['land']['id'], $order->delivery['zone_id']);
				global $attendee_id;
				$attendee_group_query = go_db_query("select attendees_group_id from " . DB_TBL_ATTENDEES . " where attendees_id =  '" . $attendee_id . "'");
				$attendee_group = go_db_fetch_array($attendee_group_query);
				$group_tax_qry = go_db_query("select group_tax from ". DB_TBL_ATTENDEES_GROUPS ." where attendees_group_id = '". $attendee_group['attendees_group_id'] ."'");
				$group_tax = go_db_fetch_array($group_tax_qry);
				if(!empty($group_tax)) {
					$group_taxed = go_session_is_registered('attendee_id') && $group_tax['group_tax'] == 'true' && $shipping_tax > 0 ? 'true' : 'false';
				} else {
					$group_taxed = (SHOW_PRICE_WITH_TAX == 'true' && $shipping_tax > 0) ? 'true' : 'false';
				}
				$order->info['tax'] += go_calculate_tax($order->info['shipping_cost'], $shipping_tax);
				$order->info['tax_groups']["$shipping_tax_description"] += go_calculate_tax($order->info['shipping_cost'], $shipping_tax);
				$order->info['total'] += go_calculate_tax($order->info['shipping_cost'], $shipping_tax);
				if ($group_taxed == 'true') {
					$order->info['shipping_cost'] += go_calculate_tax($order->info['shipping_cost'], $shipping_tax);
				}
			}
			$this->output[] = array('title' => $order->info['shipping_method'] . ':',
					'text' => $currencies->format($order->info['shipping_cost'], true, $order->info['currency'], $order->info['currency_value']),
					'value' => $order->info['shipping_cost']);
		}
	}

	function quote($method = '') {
		global $order, $shipping_weight;
		$this->quotes = array(
				'id' => $this->code, 
				'module' => $this->title,
				'methods' => array(array('id' => $this->code, 'title' => $this->description, 'cost' => 0)));
		if (go_not_null($this->icon)) $this->quotes['icon'] = go_picture($this->icon, $this->title);
		return $this->quotes;
	}

	function check() {
		if (!isset($this->_check)) {
			$check_query = go_db_query("select set_it_up_value from " . DB_TBL_SET_IT_UP . " where set_it_up_key = 'CONSTITUENT_SHIPPING_FREE_STATUS'");
			$this->_check = go_db_num_rows($check_query);
		}
		return $this->_check;
	}

	function keys() {
		return array('CONSTITUENT_SHIPPING_FREE_STATUS', 'CONSTITUENT_SHIPPING_FREE_SORT_ORDER', 'CONSTITUENT_SHIPPING_FREE_SHIPPING_OVER', 'CONSTITUENT_SHIPPING_FREE_DESTINATION');
	}
	function install() {
		go_db_query("insert into " . DB_TBL_SET_IT_UP . " (set_it_up_title, set_it_up_key, set_it_up_value, set_it_up_description, set_it_up_group_id, sort_order, set_function, date_added) values ('CONSTITUENT_SHIPPING_FREE_STATUS_TITEL', 'CONSTITUENT_SHIPPING_FREE_STATUS', 'true', 'CONSTITUENT_SHIPPING_FREE_STATUS_DESCRIPTION', '6', '1','go_cfg_select_option(array(\'true\', \'false\'), ', now())");
		go_db_query("insert into " . DB_TBL_SET_IT_UP . " (set_it_up_title, set_it_up_key, set_it_up_value, set_it_up_description, set_it_up_group_id, sort_order, date_added) values ('CONSTITUENT_SHIPPING_FREE_SORT_ORDER_TITEL', 'CONSTITUENT_SHIPPING_FREE_SORT_ORDER', '2', 'CONSTITUENT_SHIPPING_FREE_SORT_ORDER_DESCRIPTION', '6', '2', now())");
		go_db_query("insert into " . DB_TBL_SET_IT_UP . " (set_it_up_title, set_it_up_key, set_it_up_value, set_it_up_description, set_it_up_group_id, sort_order, set_function, date_added) values ('CONSTITUENT_SHIPPING_FREE_SHIPPING_TITEL', 'CONSTITUENT_SHIPPING_FREE_SHIPPING', 'false', 'CONSTITUENT_SHIPPING_FREE_SHIPPING_DESCRIPTION', '6', '3', 'go_cfg_select_option(array(\'true\', \'false\'), ', now())");
		go_db_query("insert into " . DB_TBL_SET_IT_UP . " (set_it_up_title, set_it_up_key, set_it_up_value, set_it_up_description, set_it_up_group_id, sort_order, use_function, date_added) values ('CONSTITUENT_SHIPPING_FREE_SHIPPING_OVER_TITEL', 'CONSTITUENT_SHIPPING_FREE_SHIPPING_OVER', '50', 'CONSTITUENT_SHIPPING_FREE_SHIPPING_OVER_DESCRIPTION', '6', '4', 'currencies->format', now())");
		go_db_query("insert into " . DB_TBL_SET_IT_UP . " (set_it_up_title, set_it_up_key, set_it_up_value, set_it_up_description, set_it_up_group_id, sort_order, set_function, date_added) values ('CONSTITUENT_SHIPPING_FREE_DESTINATION_TITEL', 'CONSTITUENT_SHIPPING_FREE_DESTINATION', 'national', 'CONSTITUENT_SHIPPING_FREE_DESTINATION_DESCRIPTION', '6', '5', 'go_cfg_select_option(array(\'national\', \'international\', \'both\'), ', now())");
	}
	function remove() {
		go_db_query("delete from " . DB_TBL_SET_IT_UP . " where set_it_up_key in ('" . implode("', '", $this->keys()) . "')");
	}
}
?>
