<?
// Get Expense Information	
//$query = 'SELECT *, exp.expID as pID FROM expenses exp LEFT JOIN clients c ON (exp.clientID = c.clientID) LEFT JOIN projects p ON (exp.projectID = p.projectID) WHERE exp.expDisp=1 AND exp.expReimbursable=1 AND c.subClientOf='. $pclientID; 

$query = 'SELECT *, exp.expID as pID FROM expenses exp LEFT JOIN clients c ON (exp.clientID = c.clientID) LEFT JOIN projects p ON (exp.projectID = p.projectID) WHERE exp.expDisp=1 AND exp.expReimbursable=1 AND (c.subClientOf='. $pclientID .' OR c.clientID='. $pclientID .')'; 

if (!empty($pmtStatArray)) {
	if (in_array('invYes',$pmtStatArray))
		$query .= ' AND exp.ppInv=1';
	elseif (in_array('invNo',$pmtStatArray))
		$query .= ' AND exp.ppInv=0';
	elseif (in_array('payYes',$pmtStatArray))
		$query .= ' AND exp.ppPaid=1';
	elseif (in_array('payNo',$pmtStatArray))
		$query .= ' AND exp.ppPaid=0';
}
else
	$query .= ' AND exp.ppPaid=0';

if ( (!empty($dateFrom)) && (!empty($dateTo)) ) {
	$query .= ' AND ((exp.expDate>="' . $dateFrom . '" AND exp.expDate<="' . $dateTo . '")';
	if ($prevUnpaid == 1) {
		if ((!empty($dateFrom)) && (in_array('invNo',$pmtStatArray)))
			$query .= ' OR (exp.expDate<"' . $dateFrom . '" AND exp.ppInv=0)';
		elseif ((!empty($dateFrom)) && (in_array('PayNo',$pmtStatArray)))
			$query .= ' OR (exp.expDate<"' . $dateFrom . '" AND exp.ppPaid=0)';	
		else
			$query .= ' OR (exp.expDate<"' . $dateFrom . '" AND exp.ppPaid=0)';	
	}
	$query .= ')';
}
$query .= ' ORDER BY c.clientName, p.projectName';
//echo '<p>'. $query .'</p>'; 
$result = $con->query($query);
?>