<?php
	
	if (!empty($_REQUEST['pclientID'])) {
		//$content = ''; 
		$pclientID = $_REQUEST['pclientID'];
		$dateFrom = $_REQUEST['dateFrom'];
		$dateTo = $_REQUEST['dateTo'];
		$prevUnpaid = $_REQUEST['prevUnpaid'];
		if (!empty($_REQUEST['pmtStatus'])) {
			if (is_array($_REQUEST['pmtStatus'])) {
				$pmtStatArray = $_REQUEST['pmtStatus'];
				$pmtStat = implode(",",$_REQUEST['pmtStatus']);
			}
			else {
				$pmtStatArray = explode(",",$_REQUEST['pmtStatus']);
				$pmtStat = $_REQUEST['pmtStatus'];
			}
		}
		else {
			$pmtStatArray = array();
			$pmtStat = '';
		}
		$formHead = '<form name="billingUpdate" action="'. $phpself .'" method="post">
						<input type="hidden" name="op" value="' . $_REQUEST['op'] . '" />
						<input type="hidden" name="type" value="' . $_REQUEST['type'] . '" />
						<input type="hidden" name="pclientID" value="' . $_REQUEST['pclientID'] . '" />
						<input type="hidden" name="dateFrom" value="' . $_REQUEST['dateFrom'] . '" />
						<input type="hidden" name="dateTo" value="' . $_REQUEST['dateTo'] . '" />
						<input type="hidden" name="update" value="update" />';
		
		$rowBetween = '<tr>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
						</tr>';
		
		// Get Parent Client Information
		$query = 'SELECT * FROM clients WHERE clientID=' . $pclientID;
		$result = $con->query($query);
		while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
			global $clientName,$clientAbbr,$clientContact,$clientContact,$ratePercent,$rateHourly; 
			$clientName = $row['clientName'];
			$clientAbbr = $row['clientAbbr'];
			$clientContact = $row['clientContactName'];
			$clientContact = $row['clientContactEmail'];
			$ratePercent = $row['ratePercent'];
			$rateHourly = $row['rateHourly']; 
			$comRate = $row['rateHourly']; 
		}
		$repTotalInv = 0;
		$repTotalComish = 0;
		$content .= '<h2>Summary Billing Report: '. $clientName .'</h2>';
		$content .= '<p class="reportFilters">';
		if (!empty($dateFrom))
			$content .= 'From: ' . Date::convert($dateFrom, 'Y-m-d', 'm/d/Y');
		if (!empty($dateTo))
			$content .= ' - To: ' . Date::convert($dateTo, 'Y-m-d', 'm/d/Y');
		$content .= '</p>';
		
		$content .= '<table class="reportResults" cellspacing="0">';
		
		// Get Contract Project Information	
		$comishPercent = $ratePercent;
		include('summaryProjects.php');
		
		// Get Sub-Client Hourly Project Information
		$comishPercent = $ratePercent;
		include('summaryHourly.php');
		
		// Get Parent-Client Hourly Project Information
		$comishPercent = 1;
		include('summaryParentClient.php');
		
		// Get Recurring Fee Information
		$comishPercent = $ratePercent;
		include('summaryRecurring.php');
		
		// Get Expense Information
		$comishPercent = 1;
		include('summaryExpenses.php');
		
		$content .= '<tr class="totals">
						<th style="text-align: right">Totals:&nbsp;</th>
						<td rowspan="2" style="text-align: right">Invoiced:</td>
						<td style="text-align: left">&#36;' . number_format($repTotalInv, 2,'.','') . '&nbsp;</td>
						<td>&nbsp;</td>
						<td style="text-align: right">Commission:</td>
						<td style="text-align: right">&#36;' . number_format($repTotalComish, 2,'.','') . '&nbsp;</td>
					</tr>';
		$content .= '</table>'; 
		
	}
	else {
		$pClientID = '';
		$pClients = getPclients($pClientID,$con);
	
		$content .= '<h2>Summary Billing Report</h2>';
		$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">';
		if ($_SESSION['userAuth'] == 3) {
			$content .= '<tr>
							<th>Parent&nbsp;Client:</th>
							<td colspan="2">
								'. $_SESSION['clientName'] .'
								<input type="hidden" name="pclientID" value="'. $_SESSION['clientID'] .'" />
							</td>
						</tr>';
		}
		else {
			$content .= '	<tr>
								<th>Parent&nbsp;Client:</th>
								<td colspan="2">'. $pClients .'</td>
							</tr>';
		}
		$content .= '		<tr>
								<th>Date Range:</th>
								<td>
									<label>From Date</label><br>
									<input type="text" name="dateFrom" id="dateFrom" readonly="readonly" class="dateField" />
									<input type="button" value="select" onclick="displayDatePicker(\'dateFrom\', false, \'ymd\', \'-\');">
								</td>
								<td>
									<label>To Date</label><br>
									<input type="text" name="dateTo" id="dateTo" readonly="readonly" class="dateField" />
									<input type="button" value="select" onclick="displayDatePicker(\'dateTo\', false, \'ymd\', \'-\');">
								</td>
							</tr>';
		$content .= '	<tr>
							<th>Payment&nbsp;Status:</th>
							<td colspan="2">
								<select name="pmtStatus[]" multiple="multiple" size="4">
									<option value="invYes">Invoiced&nbsp;</option>
									<option value="invNo">Not Invoiced&nbsp;</option>
									<option value="payYes">Paid&nbsp;</option>
									<option value="payNo">Not Paid&nbsp;</option>
								</select>
							</td>
						</tr>';
		$content .= '	</table>
						<input type="submit" name="Submit" value="Generate Report" />
					</form>';
	}
?>