<?php
/*
$Id: ot_tax.php,v 1.3 2002/12/06 02:08:11 christopheweber$

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

Copyright (c) 2002 osCommerce

Released under the GNU General Public License
*/

class ot_tax {
var $title, $output, $gst_title;

function ot_tax() {{
$this->code = 'ot_tax';
$this->title = MODULE_ORDER_TOTAL_TAX_TITLE;
	$this->title2 = GST;
	$this->title3 = PST;
$this->description = MODULE_ORDER_TOTAL_TAX_DESCRIPTION;
$this->enabled = ((MODULE_ORDER_TOTAL_TAX_STATUS == 'true') ? true : false);
$this->sort_order = MODULE_ORDER_TOTAL_TAX_SORT_ORDER;
	
	$this->output = array();
}
	}

function process() {
global $order, $currencies, $realsub, $customer_id;

	$flag=0;

	if (MODULE_ORDER_TOTAL_TAX_SHOW_GROUPS == 'true') {

	  reset($order->info['tax_groups']);
while (list($key, $value) = each($order->info['tax_groups'])) {

if (MODULE_ORDER_TOTAL_GST_STATUS == 'true') {

		if ( $flag != 1 ) {
		$orderquery1 = tep_db_query("select entry_zone_id from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . $customer_id . "'");
		$orderquery2 = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_id = '6'");
		$orderquery3 = tep_db_fetch_array($orderquery1);
		$orderquery4 = tep_db_fetch_array($orderquery2);
		$orderquery5 = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_id = '5'");
		$orderquery6 = tep_db_fetch_array($orderquery5);
		$orderquery7 = tep_db_query("select entry_country_id from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . $customer_id . "'");
		$orderquery8 = tep_db_fetch_array($orderquery7);
		
		$tax1 = ( $order->info['subtotal']+$order->info['shipping_cost'] ) * (MODULE_ORDER_TOTAL_GST_VALUE / 100);                 /* replace by your GST rate */
		
		
		if (MODULE_ORDER_TOTAL_GST_ZONE == 'Quebec: PST=(sub+GST)*x' ) {
		$tax2 = ( $order->info['subtotal']+ $tax1 ) * (MODULE_ORDER_TOTAL_PST_VALUE / 100);     /*  ............   PST rate */
		} else {
		$tax2 = ( $order->info['subtotal'] ) * (MODULE_ORDER_TOTAL_PST_VALUE / 100);     /*  ............   PST rate */
		}
		
		$taxdec1 = round($tax1, 2);
		$taxdec2 = round($tax2, 2);

		if ($orderquery8['entry_country_id']  == $orderquery6['configuration_value']) {   /* customer and store in the same country */

		$this->output[] = array('title' => $this->title2 . ' ( ' . MODULE_ORDER_TOTAL_GST_VALUE . '% ):',
'text' => $currencies->format($taxdec1, true, $order->info['currency'], $order->info['currency_value']),
'value' => $value);
		
		$flag=1;}
		
		
		if ($orderquery3['entry_zone_id']  == $orderquery4['configuration_value']) {	/* customer and store in the same zone */
		$this->output[] = array('title' => $this->title3  . ' ( ' . MODULE_ORDER_TOTAL_PST_VALUE . '% ):',
'text' => $currencies->format($taxdec2, true, $order->info['currency'], $order->info['currency_value']),
'value' => $order->info['tax']); }}
	}
		reset($order->info['tax_groups']);
	while (list($key, $value) = each($order->info['tax_groups'])) {
	if ($key > 0) {
$this->output[] = array('title' => $this->title . ' (' . $key . '%):',
'text' => $currencies->format($value, true, $order->info['currency'], $order->info['currency_value']),
'value' => $value); }}
}} else { 
		
		if (MODULE_ORDER_TOTAL_GST_STATUS == 'true') {

		$orderquery1 = tep_db_query("select entry_zone_id from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . $customer_id . "'");
	$orderquery2 = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_id = '6'");
		$orderquery3 = tep_db_fetch_array($orderquery1);
		$orderquery4 = tep_db_fetch_array($orderquery2);
		$orderquery5 = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_id = '5'");
		$orderquery6 = tep_db_fetch_array($orderquery5);
		$orderquery7 = tep_db_query("select entry_country_id from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . $customer_id . "'");
		$orderquery8 = tep_db_fetch_array($orderquery7);
		
		$tax1 = ( $order->info['subtotal']+$order->info['shipping_cost'] ) * (MODULE_ORDER_TOTAL_GST_VALUE / 100);                 /* replace by your GST rate */
		echo $order->info['shipping_cost']."BOOYA";
		if (MODULE_ORDER_TOTAL_GST_ZONE == 'Quebec: PST=(sub+GST)*x' ) {
		$tax2 = ( $order->info['subtotal']+ $tax1 ) * (MODULE_ORDER_TOTAL_PST_VALUE / 100);     /*  ............   PST rate */
		} else {
		$tax2 = ( $order->info['subtotal'] ) * (MODULE_ORDER_TOTAL_PST_VALUE / 100);     /*  ............   PST rate */
		}

		$taxdec1 = round($tax1, 2); 
		$taxdec2 = round($tax2, 2);

		if ($orderquery8['entry_country_id']  == $orderquery6['configuration_value']) {   /* customer and store in the same country */

		$this->output[] = array('title' => $this->title2 . ' ( ' . MODULE_ORDER_TOTAL_GST_VALUE . '% ):',
'text' => $currencies->format($taxdec1, true, $order->info['currency'], $order->info['currency_value']),
'value' => $value);

		if ($orderquery3['entry_zone_id']  == $orderquery4['configuration_value']) {	/* customer and store in the same zone */
		$this->output[] = array('title' => $this->title3  . ' ( ' . MODULE_ORDER_TOTAL_PST_VALUE . '% ):',
'text' => $currencies->format($taxdec2, true, $order->info['currency'], $order->info['currency_value']),
'value' => $order->info['tax']); }

	   }}

		if ($order->info['tax'] != 0) {
	$this->output[] = array('title' => $this->title . ':',
'text' => $currencies->format($order->info['tax'], true, $order->info['currency'], $order->info['currency_value']),
'value' => $order->info['tax']);
}
}
}

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

return $this->_check;
}

function keys() {
return array('MODULE_ORDER_TOTAL_TAX_STATUS', 'MODULE_ORDER_TOTAL_TAX_SORT_ORDER', 'MODULE_ORDER_TOTAL_TAX_SHOW_GROUPS', 'MODULE_ORDER_TOTAL_GST_STATUS', 'MODULE_ORDER_TOTAL_GST_VALUE', 'MODULE_ORDER_TOTAL_PST_VALUE', 'MODULE_ORDER_TOTAL_GST_ZONE');
	
	}

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 ('Display Tax', 'MODULE_ORDER_TOTAL_TAX_STATUS', 'true', 'Do you want to display the order tax value?', '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 ('Sort Order', 'MODULE_ORDER_TOTAL_TAX_SORT_ORDER', '2', 'Sort order of display.', '6', '2', 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 ('Display Tax Groups', 'MODULE_ORDER_TOTAL_TAX_SHOW_GROUPS', 'false', 'Do you want to display tax groups?', '6', '3', '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, set_function, date_added) values ('Display GST-PST Tax', 'MODULE_ORDER_TOTAL_GST_STATUS', 'true', 'Do you want to display the GST-PST tax?', '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 ('GST Value', 'MODULE_ORDER_TOTAL_GST_VALUE', '7', 'GST Value.', '6', '2', now())");
tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('PST Value', 'MODULE_ORDER_TOTAL_PST_VALUE', '7.5', 'PST Value.', '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 ('Mode of Calculation', 'MODULE_ORDER_TOTAL_GST_ZONE', 'Quebec: PST=(sub+GST)*x', 'How to calculate the PST?', '6', '4','tep_cfg_select_option(array(\'Quebec: PST=(sub+GST)*x\', \'Other: PST=sub*x\'), ', now())");       
}

function remove() {
$keys = '';
$keys_array = $this->keys();
$keys_size = sizeof($keys_array);
for ($i=0; $i<$keys_size; $i++) {
$keys .= "'" . $keys_array[$i] . "',";
}
$keys = substr($keys, 0, -1);

tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in (" . $keys . ")");
}
}
?>