<?php
/*
  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd
*/
 // Check to ensure this file is included in Freeway!
defined('_FEXEC') or die(); 
// To Find the users wallet balance
	function tep_get_wallet_balance($customer_id){

		//total of amount uploads
		//echo "select sum(amount) as upload_total from ". TABLE_WALLET_UPLOADS. " where customers_id='" . $customer_id . "'  and payment_status>1";
		$upload_query=tep_db_query("select sum(amount) as upload_total from ". TABLE_WALLET_UPLOADS. " where customers_id='" . $customer_id . "'  and payment_status>1");
		$upload_result=tep_db_fetch_array($upload_query);
		$upload_total=$upload_result['upload_total'];

		// total of amount drawn
		//echo "select sum(amount) as drawn_total from ". TABLE_WALLET_HISTORY. " where customers_id='" . $customer_id . "'";
		$drawn_query=tep_db_query("select sum(amount) as drawn_total from ". TABLE_WALLET_HISTORY. " where customers_id='" . $customer_id . "'");
		$drawn_result=tep_db_fetch_array($drawn_query);
		$drawn_total=$drawn_result['drawn_total'];

		return $upload_total-$drawn_total;
	}

	function tep_get_wallet_min_balance($customers_id){
		$amount=(int) WALLET_MINIMUM_AMOUNT;
		$type=WALLET_MINIMUM_TYPE;
		if ($type=="P"){ // percentage of last 3 orders
			$order_sql="SELECT count(*) as orders_count,sum(value) as total from " . TABLE_WALLET_HISTORY . " wh, " . TABLE_ORDERS_TOTAL . " ot where wh.customers_id='" . $customers_id . "' " .
						" and wh.orders_id=ot.orders_id order by wh.drawn_date limit 3";
			$order_query=tep_db_query($order_sql);
			$order_result=tep_db_fetch_array($order_query);
			if ($order_result["orders_count"]>0 && $order_result["total"]>0){
				$final_amount=($order_result["total"]/$order_result["orders_count"])* WALLET_MINIMUM_AMOUNT/100;
			} else {
				$type="C";
			}
		}
		if ($type!="P") $final_amount=$amount;
		
		return $final_amount;
	}
	
	function tep_count_wallet_uploads() {
	    global $customer_id;

		$wallet_upload_query = tep_db_query("select count(*) as total from " . TABLE_WALLET_UPLOADS . " where customers_id = '" . (int)$customer_id . "'");
		$wallet_upload = tep_db_fetch_array($wallet_upload_query);

		return $wallet_upload['total'];
  }
  
    function tep_cfg_pull_down_minimum_type($id)
	{
		$name='configuration_value';
		$minimum_type_array = array(array('id' =>'C','text'=>WALLET_TYPE_CURRENCY),array('id'=>'P','text'=>WALLET_TYPE_PERCENTAGE));
		return tep_draw_pull_down_menu($name, $minimum_type_array, $id);	
	} 
	function tep_get_wallet_minimum_type($id){
		if ($id=="P") return WALLET_TYPE_PERCENTAGE;
		else return WALLET_TYPE_CURRENCY;
	}
?>
