	<?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';	

	$uid_client			=  $_REQUEST['uid_client'];

	
		 if ($uid_client == ""){
	
		echo "invalid  parameter";
		
		exit;
		
		 
	 }
	




	$i = 1;

	
		$queryclient = "SELECT * from ".$_COOKIE['phxcrmclient_id']."_client where uid_client = ".$uid_client;
	

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

			
	
	
	

	$report = "<table id='maintable' class='table table-striped'>";
	$report = "<table id='maintable' class='table table-striped'>";
		while($rowclient=mysqli_fetch_array($resultclient)) { 


			$comp_name			= $rowclient['comp_name'];

		

			$last_st_id = 0;
							$ttl_100 = 0;
				$ttl_101 = 0;
				$ttl_102 = 0;
				$ttl_103 = 0;
			
			//get last fully complete sales_cmts
			
			
			echo "<table class='table'>";
			echo "<tr>";
			echo "<td>";

				echo "<h1>" .  htmlspecialchars($comp_name). "</h1>";
			
			echo "</td>";
			echo "</tr>";

					echo "</table>";	
					
								echo "<table class='table'>";
								
								
									echo "<tr>";
			echo "<td>";

				echo "Sales ID:";
			
			echo "</td>";
						echo "<td>";

				echo "Date:";

			
			echo "</td>";
						echo "<td>";

				echo "Status:";
			
			echo "</td>";
									echo "<td>";

				echo "Client order no:";
			
			echo "</td>";
												echo "<td>";

								echo "Sales Comments:";
			
			echo "</td>";
															echo "<td>";

								echo "Slice:";
			
			echo "</td>";
															echo "<td>";

								echo "Kit:";
			
			echo "</td>";
															echo "<td>";

								echo "Bunch:";
			
			echo "</td>";
															echo "<td>";

								echo "Loose:";
			
			echo "</td>";
			echo "</tr>";
								
								
			$querystall = "select * from  ".$_COOKIE['phxcrmclient_id']."_st where uid_client = ".$uid_client." order by sales_date desc;";
			
	//		echo $querystall . "<br>";
			
			$resultstall=mysqli_query($conn, $querystall);

			while($rowstall=mysqli_fetch_array($resultstall)) { 

				$st_id						= $rowstall['st_id'];
				$status						= $rowstall['status'];			
				$client_order_no			= $rowstall['client_order_no'];	
				$sales_comments				= $rowstall['sales_comments'];	
				$sales_date					= $rowstall['sales_date'];
				
				$sales_date					= date("d/m/Y", strtotime($sales_date));
				
							$ttl_100 = 0;
					$ttl_101 = 0;
			$ttl_102 = 0;
			$ttl_103 = 0;
			

				
				
					$queryslastdetailstl = "select prodserv_uid, qty from 101_stl where inv_id = ".$st_id ;
					
//					echo $queryslastdetailstl;

	//			echo "<br>";
				
					$resultslastdetailstl=mysqli_query($conn, $queryslastdetailstl);

					while($rowslastdetailstl=mysqli_fetch_array($resultslastdetailstl)) { 
			
						$stl_prodserv_uid				= $rowslastdetailstl['prodserv_uid'];
						$stl_qty						= $rowslastdetailstl['qty'];
					
						if ($stl_prodserv_uid == "100"){
							$ttl_100 = $ttl_100 + $stl_qty;
						}
					
						if ($stl_prodserv_uid == "101"){
							$ttl_101 = $ttl_101 + $stl_qty;
						}			
					
						if ($stl_prodserv_uid == "102"){
							$ttl_102 = $ttl_102 + $stl_qty;
						}					

						if ($stl_prodserv_uid == "103"){
							$ttl_103 = $ttl_103 + $stl_qty;
						}					
					
				
					}


	echo "<tr>";
			echo "<td>";

				echo  htmlspecialchars($st_id);
			
			echo "</td>";
						echo "<td>";

				echo  htmlspecialchars($sales_date);
			
			echo "</td>";
						echo "<td>";

				echo  htmlspecialchars($status);
			
			echo "</td>";
									echo "<td>";

				echo  htmlspecialchars($client_order_no);
			
			echo "</td>";
												echo "<td>";

				echo  htmlspecialchars($sales_comments);
			
			echo "</td>";
																		echo "<td>";

								echo $ttl_100;
			
			echo "</td>";
															echo "<td>";

								echo $ttl_102;
			
			echo "</td>";
															echo "<td>";

								echo $ttl_101;
			
			echo "</td>";
															echo "<td>";

								echo $ttl_103;
			
			echo "</td>";
			echo "</tr>";

				
			}
			
			
			echo "<table>";


				
				
				}
				
				
				
				exit;
		
