	<?php
		date_default_timezone_set("Pacific/Auckland");

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  
  	include("configi.php");


  $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($conn, $theValue) : mysqli_escape_string($conn, $theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

	 include 'configi.php';	

	$date_from			=  $_REQUEST['date_from'];

	$sel_region			=  $_REQUEST['sel_region'];

	$sel_status			=  $_REQUEST['sel_status'];
	
		 if ($date_from == ""){
	
		echo "invalid date parameter";
		
		exit;
		
		 
	 }
	
	 if ($sel_region == ""){
	
		echo "invalid region parameter";
		
		exit;
		
		 
	 }
	 
	 	
	 if ($sel_status == ""){
	
		echo "invalid status parameter";
		
		exit;
		
		 
	 }
	
//	echo "`tatus is " .$sel_status;
	
	
	if ($sel_status == "All"){
			$sel_status = "";
	}else{
	
		$sel_status = " '".$sel_status."' ";

	}

	  $date_from2 = date("Y-m-d", strtotime($date_from));  

 
	$sel_region		=  GetSQLValueString($sel_region, "text");
	
	$ttl100 = 0;
	$ttl101 = 0;
	$ttl102 = 0;
	$ttl103 = 0;
	



	$i = 1;
	
	//echo "dave".$sel_status;

//	echo "notnot";
		$queryclient = "SELECT * from ".$_COOKIE['phxcrmclient_id']."_client INNER JOIN ".$_COOKIE['phxcrmclient_id']."_st ON ".$_COOKIE['phxcrmclient_id']."_client.uid_client = ".$_COOKIE['phxcrmclient_id']."_st.uid_client WHERE c_region = ".$sel_region." and (((".$_COOKIE['phxcrmclient_id']."_st.sales_date)='".$date_from2."')) and ".$_COOKIE['phxcrmclient_id']."_st.status  in ('Ready to Invoice','Invoiced to Xero')  order by c_status,comp_name;";
		

	
//	echo "dave".$queryclient;
			$found = "no";
		
	//	echo $querydup;
		$resultclient=mysqli_query($conn, $queryclient);
	


		while($queryclient=mysqli_fetch_array($resultclient)) { 

			$st_id				= $queryclient['st_id'];
			
			$querystl = "SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where inv_id = ".$st_id;
			
		//	echo "dave".$querystl;
	
	//	echo $querydup;
		$resultstl=mysqli_query($conn, $querystl);
	


		while($querystl=mysqli_fetch_array($resultstl)) { 

			$prodserv_uid				= $querystl['prodserv_uid'];
			$qty						= $querystl['qty'];
			
			if ($prodserv_uid == 100){

					$ttl100 = $ttl100 + $qty;

			}else if ($prodserv_uid == 101){

					$ttl101 = $ttl101 + $qty;

			}else if ($prodserv_uid == 102){

					$ttl102 = $ttl102 + $qty;

			}else if ($prodserv_uid == 103){

					$ttl103 = $ttl103 + $qty;

			}

		}


		// select count from stl where st_id
		
		
			
		}
		
	
	//output json
	
	
		
	$outputarray = array('ttl100' => $ttl100, 'ttl101' => $ttl101, 'ttl102' => $ttl102, 'ttl103' => $ttl103);

	$output2 = json_encode($outputarray);
	
	echo $output2;

	exit;
			
			?>