<?php

// Get Hourly Project Information	
$query = 'SELECT *, q.pid AS pID, SUM(q.ptime) AS phours, q.prate AS pRateOLD, q.rateAmt AS pRate, GROUP_CONCAT(CAST(q.ptimes AS CHAR) SEPARATOR ",") AS times FROM (SELECT p.projectID AS pid, p.projectName AS pName, p.projectRate AS prate, p.rateHistory AS pRateHistory, c.clientName As cName, pt.timeID AS ptimes, pt.timeAmount AS pTime, pt.ppInv, pt.ppPaid, cr.* FROM projects p LEFT JOIN clients c ON (p.clientID = c.clientID) LEFT JOIN projectTime pt ON (p.projectID = pt.projectID) LEFT JOIN clientRates cr ON (p.currentRateID = cr.rateID) WHERE p.projectDisp=1 AND p.projectType=2 AND pt.timeDisp=1 AND pt.billable=1 AND c.subClientOf='. $pclientID;

if (!empty($pmtStatArray)) {
	if (in_array('invYes',$pmtStatArray))
		$query .= ' AND pt.ppInv=1';
	elseif (in_array('invNo',$pmtStatArray))
		$query .= ' AND pt.ppInv=0';
	elseif (in_array('payYes',$pmtStatArray))
		$query .= ' AND pt.ppPaid=1';
	elseif (in_array('payNo',$pmtStatArray))
		$query .= ' AND pt.ppPaid=0';
}
else
	$query .= ' AND pt.ppPaid=0';
	
if ( (!empty($dateFrom)) && (!empty($dateTo)) ) {
	$query .= ' AND ((pt.timeDate>="' . $dateFrom . '" AND pt.timeDate<="' . $dateTo . '")';
	//if ($prevUnpaid == 1) {
	if ($prevUnpaid == 1) {
		if ((!empty($dateFrom)) && (in_array('invNo',$pmtStatArray)))
			$query .= ' OR (pt.timeDate<"' . $dateFrom . '" AND pt.ppInv=0)';
		elseif ((!empty($dateFrom)) && (in_array('PayNo',$pmtStatArray)))
			$query .= ' OR (pt.timeDate<"' . $dateFrom . '" AND pt.ppPaid=0)';	
		else
			$query .= ' OR (pt.timeDate<"' . $dateFrom . '" AND pt.ppPaid=0)';
	}	
	$query .= ')';
}
else {
	$dateFrom = '';
	$dateTo = '';
}	
$query .= ') q GROUP BY q.pID ORDER BY q.cName, q.pName';
//echo '<p>'. $query .'</p>';
$result = $con->query($query);

?>