<?php
	if (!empty($_REQUEST['projects'])) {
		$pageTitle = 'Project Billing Report'; 
		$pageSubTitle .= '<p class="reportFilters">';
		$repFilter = ' WHERE p.projectType=1 AND p.projectDisp=1'; 
		if ($_SESSION['userAuth'] == 3) {
			$repFilter .= ' AND c.subClientOf="' . $_SESSION['clientID'] . '"';
		}
		
		if (is_array($_REQUEST['projects']))
			$projects = implode(',',$_REQUEST['projects']);
		else
			$projects = $_REQUEST['projects'];
		
		if ($projects == "all") 
			$pageTitle .= '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 pmt.invoiceDate>="'. $_REQUEST['dateFrom'] .'"';
			}
			if (!empty($_REQUEST['dateTo'])) {
				$pageSubTitle .= ' To '. Date::convert($_REQUEST['dateTo'], 'Y-m-d', 'M d, Y');
				$repFilter .= ' AND pmt.invoiceDate<="'. $_REQUEST['dateTo'] .'"';	
			}
			$pageSubTitle .= ': ';
		}
		
		// Get Invoice Status
		if ($_REQUEST['invoiceStatus'] != "all") {
			if ($_REQUEST['invoiceStatus'] == 0)
				$pageSubTitle .= ' Invoiced:'; 
			else
				$pageSubTitle .= ' Paid:'; 
			
			$invStat = $_REQUEST['invoiceStatus'];
			$repFilter .= ' AND pmt.paymentStatus='. $_REQUEST['invoiceStatus'];
		}
		else {
			$pageSubTitle .= ' Invoiced &amp; Not Invoiced:';
			unset($invStat);
		}
		$pageSubTitle .= '</p>';
		
		include('pages/inc/page-header.php');
		
		$query = 'SELECT DISTINCT p.projectID, p.projectName, c.clientName, pmt.* FROM projects AS p LEFT JOIN clients AS c ON (p.clientID = c.clientID) LEFT JOIN payments AS pmt ON (p.projectID = pmt.projectID) ' . $repFilter . ' ORDER BY c.clientName ASC, p.projectName';
		$result = $con->query($query);

		$tableInfo = ''; 

		if ($result->num_rows>0) {
			$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>Invoice Amount</th>
								<th>Invoice Date</th>
								<th>Invoice Status</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 = '500px';
			$tabTable = true;
			
			while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
				if ($row['paymentStatus'] == 1)
					$pmtStatus = "PAID";
				else 
					$pmtStatus = "INVOICED";
									
				$tableInfo .= '<tr>
								<td>' . $row['clientName'] . '</td>
								<td>' . $row['projectName'] . '</td>
								<td>&#36;' . number_format($row['paymentAmount'], 2,'.','') . '</td>
								<td>' . Date::convert($row['invoiceDate'], 'Y-m-d', 'm/d/Y') . '</td>
								<td>' . $pmtStatus . '</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 = 1; // Contract 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="2">'. $projects .'</td>
							</tr>
							<tr>
								<th>Date Range:</th>
								<td>
									<label>From Date</label><br>
									<input type="text" name="dateFrom" id="dateFrom" class="datepicker dateField" readonly="readonly" />
								</td>
								<td>
									<label>To Date</label><br>
									<input type="text" name="dateTo" id="dateTo" class="datepicker dateField" readonly="readonly" />
								</td>
							</tr>
							<tr>
								<th>Invoice Status:</th>
								<td colspan="2">
									<select name="invoiceStatus">
										<option value="all">All</option>
										<option value="0">Invoiced</option>
										<option value="1">Paid</option>
									</select>
								</td>
							</tr>
						</table>
						<input type="submit" name="Submit" value="Generate Report" />
					</form>';
	}
?>