<?php
	if (!empty($_POST['update'])) {
		unset($postArray);
		foreach ($_POST as $name => $value) {
			$$name = $value;
			if (substr($name,0,4) == 'proj') 
				$postArray[] = $value;
		}
		if (is_array($postArray)) {
			foreach ($postArray as $value) {
				$updateString = ''; 
				
				// Check for Invoice Updates
				$invcurVar = 'invCurrent'. $value;
				$invupVar = 'invUpdate'. $value;
				if (!isset($$invupVar))
					$$invupVar = 0;
				if ($$invcurVar != $$invupVar)
					$updateString .= 'ppInv='. $$invupVar .',';
				
				// Check for Payment Updates
				$pmtcurVar = 'pmtCurrent'. $value;
				$pmtupVar = 'pmtUpdate'. $value;
				if (!isset($$pmtupVar))
					$$pmtupVar = 0;
				if ($$pmtcurVar != $$pmtupVar)
					$updateString .= 'ppPaid='. $$pmtupVar .','; 
				$updateString = substr($updateString,0,strlen($updateString)-1);
	
				$projID = 'proj'. $value; 
				$pmtIDs = 'pmts'. $value;
				
				$query = ''; 
				if (!empty($updateString)) {
					if ($_REQUEST['updateTable'] == "ongoingFeesPaid") {
						$currentValues = 'cvalues'. $value;
						$cValues = $$currentValues;
						if (!empty($cValues)) {
							$cValues = explode(",",$cValues);
							list($ogfpmtID,$ogfeeID,$invDate,$pmtDate,$ppInvDate,$ppInv,$ppPaidDate,$ppPaid) = $cValues;
							if ($$invcurVar != $$invupVar) {
								if ($$invupVar == 1) {
									$ppInv = 1;
									$ppInvDate = $today;
								} elseif ($$invupVar == 0) {
									$ppInv = 0;
									$ppInvDate = '0000-00-00';
								}	
							}
							if ($$pmtcurVar != $$pmtupVar) {
								if ($$pmtupVar == 1) {
									$ppPaid = 1;
									$ppPaidDate = $today;
								} elseif ($$pmtupVar == 0) {
									$ppPaid = 0;
									$ppPaidDate = '0000-00-00';
								}
							}
							$query = 'REPLACE INTO '. $_REQUEST['updateTable'] .' (ogfpmtID,ogfeeID,invDate,pmtDate,ppInvDate,ppInv,ppPaidDate,ppPaid) VALUES ('. $ogfpmtID .','. $ogfeeID .',"'. $invDate .'","'. $pmtDate .'","'. $ppInvDate .'",'. $ppInv .',"'. $ppPaidDate .'",'. $ppPaid .')';
						}
					} elseif ($_REQUEST['updateTable'] == "expenses") {
						$query = 'UPDATE '. $_REQUEST['updateTable'] .' SET '. $updateString .' WHERE '. $_REQUEST['IDtype'] .'='. $$projID;
					} else {
						$query = 'UPDATE '. $_REQUEST['updateTable'] .' SET '. $updateString .' WHERE '. $_REQUEST['IDtype'] .'='. $$projID .' AND '. $_REQUEST['inID'] .' IN ('. $$pmtIDs .')'; 
					}	
					$result = $con->query($query);
				}
			}
		}
	}
	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 = '';
		}
		
		// Export Data
		$exportInputs = '';
		$exportString = '?';
		foreach($_REQUEST as $key => $value) {
			if (($key != "op")||($key != "type")) {
				$exportInputs .= '<input type="hidden" name ="'. $key .'" value="'. $value .'" />';
				$exportString .= $key .'='. $value .'&';
			}
		}
		$exportString = substr($exportString,0,strlen($exportString)-2); 
		
		$exportBtn = ' <form name="export" id="export" action="reports/expReports/summaryExport_1111.php" class="btn" method="post"><input type="hidden" name="excelOP" value="export" />'. $exportInputs .'<input type="submit" class="btn btn-default" value="Export to Excel" /></form> ';
		$emailBtn = ''; 
		//$emailBtn = ' <button class="btn btn-default" onclick="openLink(\'reports/emailReport.php'. $exportString .'\',\'Email Report\',\'400\',\'375\')">Email Report</button> ';
		$filters .= $exportBtn . $emailBtn;
				
		// Form Elements
		$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="prevUnpaid" value="' . $_REQUEST['prevUnpaid'] . '" />
						<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;
		//$adminBtns .= $exportBtn .' '. $emailBtn;
		$pageTitle = 'Summary Billing Report: '. $clientName;
		
		include('pages/inc/page-header.php');
		
		$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');
		
		$totalInv = (!empty($repTotalInv) ? number_format($repTotalInv, 2,'.','') : 'N/A');
		$totalComish = (!empty($repTotalComish) ? number_format($repTotalComish, 2,'.','') : 'N/A');
		
		$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;'. $totalInv .'&nbsp;</td>
						<td>&nbsp;</td>
						<td style="text-align: right">Commission:</td>
						<td style="text-align: right">&#36;'. $totalComish .'&nbsp;</td>
					</tr>';
		$content .= '</table>'; 
		
	} else {
		$pClientID = '';
		$pClients = getPclients($pClientID,$con);
	
		$pageTitle = 'Summary Billing Report';
		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">';
		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 datepicker" />
								</td>
								<td>
									<label>To Date</label><br>
									<input type="text" name="dateTo" id="dateTo" readonly="readonly" class="dateField datepicker" />
								</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 .= '	<tr>
							<th>Show Prev Unpaid:</th>
							<td colspan="2">
								<select name="prevUnpaid">
									<option value="1">Yes&nbsp;</option>
									<option value="0">No&nbsp;</option>
								</select>
							</td>
						</tr>';
		$content .= '	</table>
						<input type="submit" name="Submit" value="Generate Report" />
					</form>';
	}
	
	/*$docReady .= "$('#export').submit(function(e) {
		var url = 'reports/expReports/summaryExport_1111.php';
		$.ajax({
    		type: 'POST',
     		url: url,
     		data: $('#export').serialize(),
           	success: function(data) {
				if (data.returnReport !== '') {
					console.log(data.returnReport);
					var url = 'reports/expReports/' & data.report;
 					//window.open(url, '_blank');
				} else if (data.returnEmail !== '') {
					if (data.returnEmail === 'sent') {
						alert('Your Report has been Sent');
					} else {
						alert('Your Report has not been Sent.  Please try again.');
					}
				}
           	}
       	});
    	e.preventDefault();
	});";*/
	
?>