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

	$sel_br			=  $_REQUEST['sel_br'];

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

	
//	echo "`tatus is " .$sel_status;
	

//	echo "date from".$date_from;
	
	 

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

//	$sel_region		=  GetSQLValueString($sel_region, "text");


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

	$client_supp_id = $_REQUEST['supp_sel'];
	$sel_status = $_REQUEST['sel_status'];
	
	if ($sel_status == ""){
	
		$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 br_uid = ".$sel_br." and client_supp_id = ".$client_supp_id." order by  comp_name;";
	
	}else{
		
		$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 br_uid = ".$sel_br." and ".$_COOKIE['phxcrmclient_id']."_st.status in ".$sel_status." and client_supp_id = ".$client_supp_id." order by  comp_name;";
		
	}
//	echo "dave".$queryclient;
			$found = "no";
		
	//	echo $querydup;
		$resultclient=mysqli_query($conn, $queryclient);
		
		 $rowcount=mysqli_num_rows($resultclient);
		 
			
			$overall_ttl_fixed 		= 0;
			$overall_ttl_variable 	= 0;
			$overall_ttl_overall 	= 0;
			
	
	
	

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

			$st_id				= $rowclient['st_id'];
			$uid_client			= $rowclient['uid_client'];
			$comp_name			= $rowclient['comp_name'];
			$cont_name			= $rowclient['cont_name'];
			$work_phone			= $rowclient['work_phone'];

			$cell_phone			= $rowclient['cell_phone'];

			$work_ext			= $rowclient['work_ext'];
			$order_status 		= "ToCall";
			$job_status			=  $rowclient['status'];
			$c_status			=  $rowclient['c_status'];
			$c_pref_phone		=  $rowclient['c_pref_phone'];			
			$c_favcmts			=  $rowclient['c_favcmts'];	
			$sales_comments		=  $rowclient['sales_comments'];
			$client_order_no	=  $rowclient['client_order_no'];
			$sel_xero			=  $rowclient['sel_xero'];



			$last_st_id = 0;
							$ttl_100 = 0;
				$ttl_101 = 0;
				$ttl_102 = 0;
				$ttl_103 = 0;
				
				$ttl_variable = 0;
				$ttl_fixed = 0;
				$ttl_overall = 0;
				
								
				$queryttls = "select * from ".$_COOKIE['phxcrmclient_id']."_stl where inv_id = ".$st_id ;
				
				$resultsttls=mysqli_query($conn, $queryttls);

				while($rowsttls=mysqli_fetch_array($resultsttls)) { 
			
					$stl_r_type				= $rowsttls['stl_r_type'];
					$stl_line_amt				= $rowsttls['line_amount'];

					
						if ($stl_r_type == "Fixed"){
							$ttl_fixed = $ttl_fixed + $stl_line_amt;
						}else if ($stl_r_type == "Variable"){
							
							$ttl_variable = $ttl_variable + $stl_line_amt;
						}
					
					
					$ttl_overall = $ttl_overall +  $stl_line_amt;
				
					}
			
			//get last fully complete sales_cmts
			

			
//			echo "last_st_id".$last_st_id;
			
			
			$overall_ttl_fixed = $overall_ttl_fixed + $ttl_fixed;
			$overall_ttl_variable = $overall_ttl_variable + $ttl_variable;
			$overall_ttl_overall = $overall_ttl_overall + $ttl_overall;
			

			

		$total_line = "<table>
		<tr>
		<td style='text-align: right; width: 150px;'>$".number_format($ttl_fixed, 2)."</td>
		<td style='text-align: right; width: 150px;'>$".number_format($ttl_variable, 2)."</td>
		<td style='text-align: right; width: 150px;'>$".number_format($ttl_overall, 2)."</td>
		</tr>
		</table>";
		
			$order_status = '
			 <ul class="listview image-listview flush transparent">
            <li>
                <a class="item">

                    <div class="in">';
                        										

	$c_jc_name = $job_status;
include("../php/configi.php");

$queryjc="SELECT * FROM 1_job_codes2 order by jc_order";
//echo $querycy;
$resultjc=mysqli_query($conn, $queryjc);

//$st_id = 0;
$order_status = $order_status . '<select    class="form-control"   id = "sel_status" name="sel_status" style="width: 150px;"  onchange="javascript: change_st_status('.$st_id.', this.value)"  >';


 while($rowjc=mysqli_fetch_array($resultjc)) {
	 
	 $jc_id		 = $rowjc["jc_id"];
	 $jc_name	= $rowjc["jc_name"];

		$order_status = $order_status . '<option ';
		if ($rowjc['jc_name'] == $c_jc_name){$order_status = $order_status . ' selected ';} 
		$order_status = $order_status . 'value='.$rowjc["jc_id"].'>'.$jc_name.'</option>';



}


$order_status = $order_status . '</select>
                    </div>
                </a>
            </li>
			</ul> ';
//$order_status = $order_status . '		
	//		<input style="width: 150px; margin-left: 15px;" id="client_order_no" type="text" onchange="javascript: update_field_clientorderno('.$st_id.',this.value)" class="form-control" value="'.htmlspecialchars($client_order_no).'">';
			
		//	$order_status = $order_status . '<span style="margin-left: 15px;">'.$last_sales_date_cmt.'</span>';

			
			$to_call =  str_replace(' ', '', $work_phone);


			$to_call = ltrim($to_call, 0);

			$to_call = ltrim($to_call);

			if ($work_ext == ""){
	
			}else{
				$to_call = $to_call;
			}

			$phone_url = '<a href= "tel:+64'.$to_call.'"><img style="height: 35px" src="../images/phone.jpg"></a>';
			
			$curdate = date("Y-m-d H:i:s");
			
			$report = $report . "<tr  >
			<td style='width: 100px' >
			<button id='openstafflogin' style='display: inline; margin-top:0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; height: 10px' type='button' class='btn btn btn-link' data-toggle='modal' onclick='get_contact_edit(".$uid_client.")' data-target='#DialogContEdit'>Edit Cust</button>
			</td>";
			
			//	$c_favcmts = "Notes:".trim($c_favcmts);
		//	$c_favcmts = "hello";


	$report = $report . "<td style='background-color: #aaf0c9; width: 270px;'><strong>".htmlspecialchars($comp_name)."</strong>
		

	
	</td>";

			
			$sales_cmts = "Previous Notes: ";

			
			$querystnotes = "SELECT * from ".$_COOKIE['phxcrmclient_id']."_st where uid_client = ".$uid_client." and st_id <> ".$st_id." order by sales_date desc";
	//	echo $querystnotes;
		

			$resultstnotes=mysqli_query($conn, $querystnotes);

			while($rowsnotes=mysqli_fetch_array($resultstnotes)) { 

				$note_sales_datetime				= $rowsnotes['sales_date'];
				$note_sales_comments				= $rowsnotes['sales_comments'];	

				$note_sales_datetime_format = date("d/m/y", strtotime($note_sales_datetime));
				
				if ($note_sales_comments == ""){
					
				}else{
				
				$sales_cmts = $sales_cmts . $note_sales_datetime_format . " " .$note_sales_comments . "<br>" ;
			
				}
			}
			
		
			

			$report = $report . "<td style='width: 220px'>".htmlspecialchars($cont_name)."
				 	</td>
			";


		
			
			$report = $report . "</td>";			
			$report = $report . "<td>";			


		$report = $report . "<button id='openstafflogin22' style='display: inline; margin-top:0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; height: 10px' type='button' class='btn btn btn-link' onclick='open_job(".$st_id.")' >Edit Job</button>";
					$report = $report . "</td>";			

			
	$report = $report . "<td style='width: 150px'>".$order_status."</td>";	
	
	$xero_url = '<a href= ""><img style="height: 75px" src="../images/xero.png"></a>
	<br>
	';

	$report = $report . "<td >".$total_line."</td>";
//	$report = $report . "<td style='width: 100px'>
//		<div class='custom-control custom-checkbox mb-1'>
 //                   <input type='checkbox' class='custom-control-input' id='customCheckb".$st_id."' onchange='javascript: alert(".$st_id.",".$sel_xero.")'>
  //                  <label class='custom-control-label' for='customCheckb".$st_id."'></label>
   //             </div>
//	
//	</td>";		
			$report = $report . "</tr>";
			
			$i = $i + 1;
			
		}
		
		
		//totals
		
		

	$report = $report . "</table>";
		
			echo $report;
			
								$overall_total_line = "<table>
		<tr>
		<td style='width: 600px'></td>
		<td style='text-align: right; width: 150px;'>$".number_format($overall_ttl_fixed, 2)."</td>
		<td style='text-align: right; width: 150px;'>$".number_format($overall_ttl_variable, 2)."</td>
		<td style='text-align: right; width: 150px;'>$".number_format($overall_ttl_overall, 2)."</td>
		</tr>
		</table>";
		
		
		echo $overall_total_line;
	
	exit;
	
		
		
	
	
			
			?>