<?php

include('queries/sumQuery_Recurring.php');

if ($result->num_rows>0) { 
	$content .= 		$formHead .'
						<input type="hidden" name="updateTable" value="ongoingFeesPaid" />
						<input type="hidden" name="IDtype" value="feeID" />
						<input type="hidden" name="inID" value="ogfpmtID" />
						<tr><td colspan="8"><h3>Recurring Client Fees</h3></td></tr>
						<tr>
							<th class="tableHead">Client/Project</th>
							<th class="tableHead">&nbsp;</th>
							<th class="tableHead">Fee Date</th>
							<th class="tableHead" style="text-align: right">'. $amtInv .'</th>
							<th class="tableHead" style="text-align: center">'. $comishRate .'</th>
							<th class="tableHead" style="text-align: right">'. $comDue .'</th>
							<th class="tableHead" style="text-align: center">Inv</th>
							<th class="tableHead" style="text-align: center">Paid</th>
						</tr>';
	$odd = false; 
	$invTotal = 0; 
	$comissionTotal = 0;
	
	$idQuery = 'SELECT ogfpmtID FROM ongoingFeesPaid ORDER BY ogfpmtID DESC LIMIT 1'; 
	$idResult = $con->query($idQuery);
	while ($idRow=$idResult->fetch_array(MYSQLI_ASSOC)) {
		$lastPaymentRec = $idRow['ogfpmtID'];
	}
	//echo '<p>lastPaymentRec = '. $lastPaymentRec .'</p>';
	
	while ($row=$result->fetch_array(MYSQLI_ASSOC)) {
		// Shade Oposite Rows
		if (!empty($odd)) 
			$rowStyle = ' class="odd"';
		else 
			$rowStyle = '';
		
		$ppInvCK = '';
		$ppPaidCK = '';
		if (!empty($row['ppInv'])) {
			if ($row['ppInv'] == 1)
				$ppInvCK = ' checked';
		}
		if (!empty($row['ppPaid'])) {
			if ($row['ppPaid'] == 1)
				$ppPaidCK = ' checked';
			else 
				$ppPaidCK = '';
		}
		
		// Check for fee-specific Comission Rate
		$comishPercent = (($row['feeRate']>0) ? $row['feeRate'] : $comishPercent); 
		
		// Fee Date
		$feeDate = ''; 
		$invDate = ''; 
		if ($row['ogfpmtID'] != NULL) {
			$feeDate = Date::convert($row['invDate'], 'Y-m-d', 'M d, Y');
			$idDate = $row['invDate'];
			$pmtID = $row['ogfpmtID'];
		}
		elseif ((!empty($dateFrom)) && (!empty($dateTo))) { 
			if (($row['feeNextRec']>=$dateFrom) && ($row['feeNextRec']<=$dateTo)) {
				$feeDate = Date::convert($row['feeNextRec'], 'Y-m-d', 'M d, Y'); 
				$invDate = $row['feeNextRec'];
				$idDate = $row['feeNextRec'];
			}
			elseif (($row['feeLastRec']>=$dateFrom) && ($row['feeLastRec']<=$dateTo)) {
				$feeDate = Date::convert($row['feeLastRec'], 'Y-m-d', 'M d, Y'); 
				$invDate = $row['feeLastRec'];
				$idDate = $row['feeLastRec'];
			}
		}
		else {
			$feeDate = Date::convert($row['feeNextRec'], 'Y-m-d', 'M d, Y');
			$invDate = $row['feeNextRec'];
			$idDate = $row['feeNextRec'];
		}
		
		// Fee Payment ID
		if ($row['ogfpmtID'] != NULL)
			$pmtID = $row['ogfpmtID'];
		else
			$pmtID = $lastPaymentRec + 1;
		$itemID = $row['pID'] .'_' . $pmtID;
		
		// Get Current Fee paid Values
		if ($row['ogfpmtID'] != NULL)
			$currentValues = $row['ogfpmtID'] .','. $row['ogfeeID'] .','. $row['invDate'] .','. $row['pmtDate'] .','. $row['ppInvDate'] .','. $row['ppInv'] .','. $row['ppPaidDate'] .','. $row['ppPaid']; 
		else { 
			$lastPaymentRec = $lastPaymentRec + 1;
			$currentValues = $lastPaymentRec .','. $row['pID'] .','. $invDate .',0000-00-00,0000-00-00,0,0000-00-00,0';
		}	
		
		// Details
		$feeDetails = ''; 
		if ($row['feeDetails']) { $feeDetails .= $row['feeDetails']; }
		if ($row['feeDomain']) { $feeDetails .= ' - '. $row['feeDomain']; } 	
		if (!empty($feeDetails)) { $feeDetails = '<br><em>'. $feeDetails .'</em>'; }
							
		$comission = number_format($row['billAmount']*$comishPercent, 2,'.','');
		$content .= '<tr' . $rowStyle . '>
						<td>
							' . $row['clientName'] . ' - ' . $row['feeDesc'] .' ('. $row['feeRecDesc'] .') ' . $feeDetails . '
							<input type="hidden" name="proj'. $itemID .'" value="'. $itemID .'" />
							<input type="hidden" name="pmts'. $itemID .'" value="'. $row['ogfpmtID'] .'" />
							<input type="hidden" name="cvalues'. $itemID .'" value="'. $currentValues .'" />
						</td>
						<td>&nbsp;</td>
						<td>'. str_replace(' ', '&nbsp;', $feeDate) .'</td>
						<td style="text-align: right">&#36;' . number_format($row['billAmount'],2,'.','') . '&nbsp;</td>
						<td style="text-align: center">' . number_format($comishPercent*100) . '&#37;</td>
						<td style="text-align: right">&#36;' . number_format($comission,2,'.','') . '&nbsp;</td>
						<td style="text-align: center">
							<input type="hidden" name="invCurrent'. $itemID .'" value="'. $row['ppInv'] .'" />
							<input type="checkbox" name="invUpdate'. $itemID .'" value="1"'. $ppInvCK .' />
						</td>
						<td style="text-align: center">
							<input type="hidden" name="pmtCurrent'. $itemID .'" value="'. $row['ppPaid'] .'" />
							<input type="checkbox" name="pmtUpdate'. $itemID .'" value="1"'. $ppPaidCK .' />
						</td>
					</tr>';
						
		$invTotal = $invTotal+$row['billAmount'];
		$comissionTotal = $comissionTotal+$comission;
		
		if (!empty($odd))
			$odd = false;
		else  
			$odd = true;
	}
	$content .= '<tr class="totals">
					<th style="text-align: right">Totals:&nbsp;</th>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td style="text-align: right">&#36;' . number_format($invTotal, 2,'.','') . '&nbsp;</td>
					<td style="text-align: center">' . number_format($comishPercent*100) . '&#37;</td>
					<td style="text-align: right">&#36;' . number_format($comissionTotal, 2,'.','') . '&nbsp;</td>
				</tr>';
	$content .= '<tr><td colspan="8"><p style="text-align: right"><input type="submit" name="submit" value="Update Recurring" /></p></td></tr>';
	$content .= '</form>';
	$content .= $rowBetween;
	
	$repTotalInv = $repTotalInv+$invTotal;
	$repTotalComish = $repTotalComish+$comissionTotal;
}

?>