<?php
	if (!empty($_REQUEST['disp'])) {
	
		$repFilter = ' WHERE c.clientID>0'; 
		if ($_SESSION['userAuth'] == 3) {
			$repFilter .= ' AND c.subClientOf="' . $_SESSION['clientID'] . '"';
		}
		if ($_REQUEST['disp'] == "all") {
			$pageTitle = 'All Recurring Fees';
		} elseif ($_REQUEST['disp'] == 'dateRange') {
			$firstDay = $_REQUEST['dateFrom'];
			$lastDay = $_REQUEST['dateTo'];
			$repFilter .= ' AND (((feeNextRec>="' . $firstDay . '") AND (feeNextRec<="' . $lastDay . '")) OR ((feeLastRec>="' . $firstDay . '") AND (feeLastRec<="' . $lastDay . '")))'; 
			$firstDay = Date::convert($firstDay, 'Y-m-d', 'M d, Y');
			$lastDay = Date::convert($lastDay, 'Y-m-d', 'M d, Y');
			$pageTitle = 'Upcoming Recurring Fees from '. $firstDay .' to '. $lastDay;
		} elseif (($_REQUEST['disp'] == 'thisMonth')||($_REQUEST['disp'] == 'nextMonth')||($_REQUEST['disp'] == 'next2Months')||($_REQUEST['disp'] == 'next3Months')||($_REQUEST['disp'] == 'next6Months')||($_REQUEST['disp'] == 'next9Months')||($_REQUEST['disp'] == 'next12Months')) {
			$thisMonth = date('m');
			$thisYear = date('Y');
			if ($_REQUEST['disp'] == 'thisMonth') {
				$month = date('m');
				$year = date('Y');
				$firstDay = $year . '-' . $month . '-01';
			} elseif ($_REQUEST['disp'] == 'nextMonth') {
				$updateMonth = addMonths($today, 1);
				list($year, $month, $day) = preg_split('/[\/]|[-]+/', $updateMonth);
				$firstDay = $year . '-' . $month . '-01';
			} else {
				$addAmt = $_REQUEST['disp'];
				$addAmt = str_replace("next", "", $addAmt);	
				$addAmt = str_replace("Months", "", $addAmt);
				$updateMonth = addMonths($today, $addAmt);
				list($year, $month, $day) = preg_split('/[\/]|[-]+/', $updateMonth);
				$firstDay = $thisYear . '-' . $thisMonth . '-01';
			}
			$lastDay = lastday($month, $year);
			$repFilter .= ' AND (((feeNextRec>="' . $firstDay . '") AND (feeNextRec<="' . $lastDay . '")) OR ((feeLastRec>="' . $firstDay . '") AND (feeLastRec<="' . $lastDay . '")))'; 
			$firstDay = Date::convert($firstDay, 'Y-m-d', 'M d, Y');
			$lastDay = Date::convert($lastDay, 'Y-m-d', 'M d, Y');
			$pageTitle = 'Upcoming Recurring Fees from '. $firstDay .' to '. $lastDay;
		}
		$pageSubTitle .= '<p class="inactive">* Records in grey are currently &quot;Inactive&quot;.</p>';
		
		include('pages/inc/page-header.php');
		
		$query = 'SELECT *, (ogf.feeAmt * fr.feeRecCalc) as billAmount FROM ongoingFees AS ogf LEFT JOIN clients AS c ON (ogf.clientID = c.clientID) LEFT JOIN feeDesc AS fd ON (ogf.feeDescID = fd.feeDescID) LEFT JOIN feeRec AS fr ON (ogf.feeRecID = fr.feeRecID) ' . $repFilter . ' ORDER BY c.clientName ASC';
		$result = $con->query($query);
		if ($result) {
			
			$tableFieldNames = '<th>Client Name</th>
								<th>Fee Type</th>
								<th>Fee Start</th>
								<th>Monthly</th>
								<th>Billing Plan</th>
								<th>Bill Amount</th>
								<th>Next Bill Date</th>
								<th>Last Bill Date</th>';
			
			include('inc/data-tables/table-top.php');
			include('inc/data-tables/table-bottom.php');
			
			$sortColumns = 8;
			$noSortColumns = ''; // Column Number from left (column 1=0)
			$scrollHeight = '500px';
			$tabTable = true;
			
			$tableInfo = ''; 
			
			while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
				if ($row['feeActive'] == 0)
					$rowStyle = ' class="inactive"';
				else
					$rowStyle = '';
				
				if ($row['feeLastRec'] == '0000-00-00')
					$feeLast =  $row['feeStartDate'];
				else 
					$feeLast = $row['feeLastRec'];
				$feeNext = $row['feeNextRec']; 

				$feeNext = Date::convert($feeNext, 'Y-m-d', 'm/d/Y');
				$feeLast = Date::convert($feeLast, 'Y-m-d', 'm/d/Y');
				$feeStart = Date::convert($row['feeStartDate'], 'Y-m-d', 'm/d/Y');
				
				$tableInfo .= '<tr' . $rowStyle . '>
								<td>' . $row['clientName'] . '</td>
								<td>' . $row['feeDesc'] . '</td>
								<td>' . $feeStart . '</td>
								<td>&#36;' . $row['feeAmt'] . '</td>
								<td>' . $row['feeRecDesc'] . '</td>
								<td>&#36;' . number_format($row['billAmount'], 2,'.','') . '</td>
								<td>' . $feeNext . '</td>
								<td>' . $feeLast . '</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 {
	
		/*$docReady .= "$('#dateRange').hide();
			$('select').change(function () {
			
			// Report Type Select Change
			if ($('select#disp').val() == 'dateRange')
				$('#dateRange').show();
			else
				$('#dateRange').hide();
			});";*/
	
		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>Report Option</th>
								<td>
									<select name="disp" id="disp">
										<option value="">&lt; select &gt;</option>
										<option value="all">All Ongoing Fees</option>
										<option value="dateRange">Specific Date Range</option>
										<option value="thisMonth">This Month\'s Fees</option>
										<option value="nextMonth">Next Month\'s Fees</option>
										<option value="next2Months">Next 2 Month\'s Fees</option>
										<option value="next3Months">Next 3 Month\'s Fees</option>
										<option value="next6Months">Next 6 Month\'s Fees</option>
										<option value="next9Months">Next 9 Month\'s Fees</option>
										<option value="next12Months">Next 12 Month\'s Fees</option>
									</select>
								</td>
							</tr>
							<tr id="dateRange">
								<th>Date Range</th>
								<td>
									<strong>From: </strong> 
									<input type="text" name="dateFrom" class="dateField datepicker">
									&nbsp;&nbsp;
									<strong>To: </strong>
									<input type="text" name="dateTo" class="dateField datepicker">
								</td>
							</tr>
						</table>
						<input type="submit" name="Submit" value="Generate Report" />
					</form>';
	}
?>