<?php
	if (!empty($_REQUEST['projects'])) {
		$pageTitle = 'Project Time Report'; 
		$pageSubTitle .= '<p class="reportFilters">';
		$repFilter = ' WHERE p.projectType=2 AND p.projectDisp=1 AND pt.timeDisp=1'; 
		if ($_SESSION['userAuth'] == 3) {
			$repFilter .= ' AND c.subClientOf="' . $_SESSION['clientID'] . '"';
		}
		
		if ($_REQUEST['billable'] != 'all')
			$repFilter .= ' AND pt.billable='. $billable;

		if (is_array($_REQUEST['projects']))
			$projects = implode(",",$_REQUEST['projects']);
		else
			$projects = $_REQUEST['projects'];
		
		if ($projects == "all")
			$pageSubTitle .= 'All Projects: ';
		else  
			$repFilter .= ' AND p.projectID IN ('. $projects .')';
		
		// Get Date Range
		if ((empty($_REQUEST['dateFrom']))&&(empty($_REQUEST['dateTo'])))
			$pageSubTitle .= ' All Time: ';
		else {
			if (!empty($_REQUEST['dateFrom'])) {
				$pageSubTitle .= ' From '. Date::convert($_REQUEST['dateFrom'], 'Y-m-d', 'M d, Y');
				$repFilter .= ' AND pt.timeDate>="'. $_REQUEST['dateFrom'] .'"';
			}
			if (!empty($_REQUEST['dateTo'])) {
				$pageSubTitle .= ' To '. Date::convert($_REQUEST['dateTo'], 'Y-m-d', 'M d, Y');
				$repFilter .= ' AND pt.timeDate<="'. $_REQUEST['dateTo'] .'"';	
			}
			$pageSubTitle .= ': ';
		}
		
		// Get Invoice Status
		if ($_REQUEST['invoiceStatus'] != "all") {
			if ($_REQUEST['invoiceStatus'] == 1)
				$pageSubTitle .= ' Invoiced:'; 
			else
				$pageSubTitle .= ' Not Invoiced:'; 
			
			$invStat = $_REQUEST['invoiceStatus'];
			$repFilter .= ' AND pt.timeTimeInvoiced='. $_REQUEST['invoiceStatus'];
		}
		else {
			$pageSubTitle .= ' Invoiced &amp; Not Invoiced:';
			unset($invStat);
		}
		$pageSubTitle .= '</p>';
		
		include('pages/inc/page-header.php');
			
		$query = 'SELECT *, SUM(q.ptime) AS phours, SUM(q.ptime)*prate AS pcharge FROM (SELECT p.projectID AS pid, p.projectName AS pname, p.projectRate AS prate, c.clientName As cname, pt.timeAmount As ptime, pt.billable As pBill FROM projects AS p LEFT JOIN clients AS c ON (p.clientID = c.clientID) LEFT JOIN projectTime AS pt ON (p.projectID = pt.projectID) ' . $repFilter . ') q GROUP BY q.pid ORDER BY q.cname ASC, q.pname';
		$result = $con->query($query);
		
		$tableInfo = ''; 

		if ($result) {
			$linkVars = '';
			foreach($_REQUEST as $name => $value) {
				if ($name == 'orderBy') {}
				elseif ($name == 'projects') {
					if (is_array($_REQUEST['projects']))
						$linkVars .= '&'. $name .'='. implode(',',$_REQUEST['projects']);
					else
						$linkVars .= '&'. $name .'='. $value;
				}
				else
					$linkVars .= '&'. $name .'='. $value;
			}
			$tableFieldNames = '<th>Client</th>
								<th>Project</th>
								<th>Hours</th>
								<th>Invoice Amount</th>
								<th>Billable?</th>';
			
			include('inc/data-tables/table-top.php');
			include('inc/data-tables/table-bottom.php');
			
			$sortColumns = 5;
			$noSortColumns = ''; // Column Number from left (column 1=0)
			$scrollHeight = 500;
			$tabTable = true;
			
			while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
				if ($row['pBill'] == 1) 
					$pBillable = "Yes";
				else
					$pBillable = "No";
				
				$tableInfo .= '<tr>
								<td>' . $row['cname'] . '</td>
								<td>' . $row['pname'] . '</td>
								<td>' . $row['phours'] . '</td>
								<td>&#36;' . number_format($row['pcharge'], 2,'.','') . '</td>
								<td>'. $pBillable .'</td>
							</tr>';
			}
			$content .= '<div class="row"><div class="col-sm-12">';
			$content .= $tableHeader . '<tbody>' . $tableInfo . '</tbody>'. $tableFooter;
			$content .= '</div></div>';
			include('inc/data-tables/dataTables.php');
		}
	} else {
		$projID = '';
		$projType = 2; // Hourly Projects
		$projects = getProjects($projID,$projType,$con);
	
		include('pages/inc/page-header.php');	

		$content .= '<form name="generateReport" action="' . $phpself . '" method="post">
						<input type="hidden" name="op" value="' . $_REQUEST['op'] . '" />
						<input type="hidden" name="type" value="' . $_REQUEST['type'] . '" />
						<table class="reportSelect">
							<tr>
								<th>Select Project(s):</th>
								<td colspan="3">'. $projects .'</td>
							</tr>
							<tr>
								<th>Date Range:</th>
								<td>
									<label>From Date</label><br>
									<input type="text" name="dateFrom" id="dateFrom" readonly="readonly" class="dateField datepicker" />
								</td>
								<td colspan="2">
									<label>To Date</label><br>
									<input type="text" name="dateTo" id="dateTo" readonly="readonly" class="dateField datepicker" />
								</td>
							</tr>
							<tr>
								<th>Invoice Status:</th>
								<td>
									<select name="invoiceStatus">
										<option value="all">All</option>
										<option value="1">Invoiced</option>
										<option value="0">Not Invoiced</option>
									</select>
								</td>
								<th>Billable</th> 
								<td>
									<select name="billable">
										<option value="all">All</option>
										<option value="1">Billable</option>
										<option value="0">Not Billable</option>
									</select>
								</td>
							</tr>
						</table>
						<input type="submit" name="Submit" value="Generate Report" />
					</form>';
	}
?>