<?php include "headerLink.php"?>
<!DOCTYPE html>
<html lang="en">


<body class="animsition">
    <div class="page-wrapper">
        <!-- HEADER MOBILE-->
        <?php include "menuSideBar_mobile.php" ;?>
        <!-- END HEADER MOBILE-->
		
        <!-- MENU SIDEBAR-->
        <?php include "menuSideBar.php" ;?>
        <!-- END MENU SIDEBAR-->

        <!-- PAGE CONTAINER-->
        <div class="page-container">
            <!-- HEADER DESKTOP-->
			<?php include "headerPage.php"?>
            <!-- END HEADER DESKTOP-->
			
			
			
			<!-- MAIN CONTENT-->
			<?php
			
			$report_type=$_GET["report_type"];
			$_SESSION["report_type"] =  $report_type;
			
			$bill_types_id = "";
			if($report_type != "" || $report_type != null){
				 $sqlBillTypeID = "select id from bill_types where UPPER(bill_type_name) = UPPER('$report_type')";
				$runBillTypeID = mysqli_query($link, $sqlBillTypeID);
					if($runBillTypeID === FALSE){
						die(mysqli_error("SQL DIE! Error for SQL: ".$sqlBillTypeID));
					}
				$resultBillTypeID = mysqli_fetch_assoc($runBillTypeID);
				$bill_types_id = $resultBillTypeID["id"];
			}
			$bill_type_Outstanding = 0;

			if($location_id != ""){
				//echo "MASUK LOCATION ID: ".$location_id;
				if($location_id == 2)
					$bill_type_Outstanding = 4;
				else if($location_id == 3)
					$bill_type_Outstanding = 3;
				else if($location_id == 4)
					$bill_type_Outstanding = 5;
			}

			
			
			function dateDifference($start_date, $end_date)
			{
				// calulating the difference in timestamps 
				$diff = strtotime($start_date) - strtotime($end_date);
				 
				// 1 day = 24 hours 
				// 24 * 60 * 60 = 86400 seconds
				return ceil(abs($diff / 86400));
			}

			$selectedLocation = "";
			$selectedTenantInfo = "";
			$tenantInfo = "";
			$cutOffPayment = "";
			$tenant_status = "";
			$_SESSION["aged_date"] ="";
			$_SESSION["projectLocation"] = "";
			$_SESSION["tenantInfo"] = "";
			$_SESSION["cutOffPayment"] = "";
			$_SESSION["tenant_status"] = "";
			//$tenant_id = $_GET["tenant_id"];
			$sqlDate = "";
			$sqlDateDeposit = "";
			$_SESSION['projectLocation']=$_GET['projectLocation'];

			$_SESSION['statement_date']="";
			$_SESSION['statement_year']="";
			$_SESSION['statement_monthyear']="";
			$_SESSION['chooseDate']="";
			$_SESSION["statement_ChooseDate"] =  "";
			$_SESSION["statementby"] = "";
			$statementby = "";
			$statement_year = "";
			$statement_monthyear = "";
			$statement_date = "";
			$statement_ByChooseDate = "";
			$selected_cutOffPayment = "";
			$displayYear = "display:none";
			$displayMonthYear = "display:none";
			$displayDateFromTo = "display:none";
			$displayChooseDate = "display:none";
			$displayCheckBox = "display:none";
			$displayStatementOption = "display:none";

			$selectedLocation = $_GET["projectLocation"];
			if($selectedLocation != "" || $selectedLocation != null){
				$projectLocation = $_GET["projectLocation"];
				include "splitLocationName_project.php";
				$project_id = $project_id;
				$location_id = $location_id;
			}
				
				
				
			  $sqlYear = "select YEAR(bills.issue_date) AS year from bills 
						inner join tenants on bills.tenant_id =  tenants.id 
						inner join properties on tenants.property_id = properties.id
						inner join locations on properties.location_id = locations.id
						where locations.id = '$location_id'
						group by year having (year <= YEAR(CURDATE()) and year <> 0 ) order by year ASC";
			$runSqlYear = mysqli_query($link, $sqlYear);
			if($runSqlYear === FALSE){
				die(mysqli_error($link));
			}

			
			if(isset($_GET["reset"])){
				$_SESSION["dateFrom"] ="";
				$_SESSION["dateTo"] ="";
				$_SESSION['statement_date']="";
				$_SESSION['statement_year']="";
				$_SESSION['statement_monthyear']="";
				$_SESSION['chooseDate']="";
				$_SESSION["cutOffPayment"] =  "";
				$_SESSION["statement_ChooseDate"] =  "";
				$_SESSION["statementby"] = "";
				$_GET["statementby"]= "";
				$_GET["statement_year"] = "";
				$_GET["statement_monthyear"] = "";
				$_GET["statement_date"] = "";
				$_GET["statement_ByChooseDate"] = "";
				$_GET["selected_cutOffPayment"] = "";
				$_GET["dateFrom"] = "";
				$_GET["dateTo"] = "";
				$_GET["chooseDate"] = "";
				$_GET["cutOffPayment"] = "";
				$_GET["statement_ChooseDate"] = "";
				
				
				
				$_SESSION["aged_date"] ="";
				$_SESSION["projectLocation"] = "";
				$_SESSION["tenantInfo"] = "";
				$_SESSION["cutOffPayment"] = "";
				$_SESSION["cutOffPayment"] = "";
				$selectedDate = "";
				$selectedLocation = "";
				$selectedTenantInfo = "";
				$cutOffPayment = "";
				$tenant_status = "";
				 $displayYear = "display:none";
				$displayMonthYear = "display:none";
				$displayDateFromTo = "display:none";
				$displayChooseDate = "display:none";
				$displayCheckBox = "display:none";
				$statementby = "";
				$displayStatementOption = "display:none";
			}
			
			
			if(isset($_GET["submit"])){
				echo "submit!";
				$dateFrom = $_GET["dateFrom"];
				$dateTo = $_GET["dateTo"];
				$_SESSION["dateFrom"] = $dateFrom;
				$_SESSION["dateTo"] = $dateTo;
				
				$dateTo_cutOFF = date('Y-m-d', strtotime($dateTo. ' + 1 days'));	
				
				
				
				$statementby = $_GET["statementby"];
				$_SESSION['statementby'] = $statementby;
				$tenant_id = $_GET["tenant_id"];
				$cond_selectedCutOffPayment = "";
				$cond_monthYear_BF = "";
				$cond_date_BF = "";
				$selected_cutOffPayment = "";
				$statement_year_cutOFF ="";
				$infoTarikhTunggakan = "";
				
				
				$selectedDate = $_GET["aged_date"];
				$selectedLocation = $_GET["projectLocation"];
				$selectedTenantInfo = $_GET["tenantInfo"];
				$selected_cutOffPayment = $_GET["cutOffPayment"];
				$tenant_status = $_GET["tenant_status"];
				$_SESSION["aged_date"] = $selectedDate;
				$_SESSION["projectLocation"] = $selectedLocation;
				$_SESSION["tenantInfo"] = $selectedTenantInfo;
				$_SESSION["cutOffPayment"] = $selected_cutOffPayment;
				$_SESSION["tenant_status"] = $tenant_status;
				
				if($selectedLocation != "" || $selectedLocation != null){
					$projectLocation = $_GET["projectLocation"];
					include "splitLocationName_project.php";
					$project_id = $project_id;
					$location_id = $location_id;
				}
				
				
				$cond = "";
				$cond_project = "";
				$cond_location = "";
				$cond_ageingDate = "";
				$cond_tenantInfo = "";
				$cond_tenantStatus = "";
				$cond_staffID = "";
				$groupBY = "";
				$ageingDateInfo ="";
				$cond_selectedCutOffPayment = "";
				
				
				if($statementby == 'statement_year'){
						//all payments start from 1st Jan - 31 Dec
						$displayYear = "display:block";
						$displayMonthYear = "display:none";
						$displayDateFromTo = "display:none";
						$displayChooseDate = "display:none";
						$displayCheckBox = "display:block";
						
						$statement_year = $_GET["statement_year"];
						$_SESSION['statement_year']=$statement_year;
						$selected_cutOffPayment = $_GET["cutOffPayment"];
						$_SESSION["cutOffPayment"] = $selected_cutOffPayment;
						$cutOffDate = date("Y-m-d",strtotime($statement_year . '-12-31')); //tarikh hujung tahun
						$firstYearDate =  date("Y-m-d",strtotime($statement_year . '-01-01')); //tarikh awal tahun
						$statement_year_cutOFF = date('Y-m-d', strtotime($cutOffDate. ' + 1 days'));
						IF($selected_cutOffPayment == true){
							
							$cond_selectedCutOffPayment = " and YEAR(payments.created_at) < '$statement_year_cutOFF'";
						}
						
						$year_selected = date("Y",strtotime($firstYearDate));
						$cond_monthYear_BF = " and year(bills.issue_date) < '$statement_year' ";
						$cond_date_BF = " and payments.created_at < '$firstYearDate' ";
						$sqlDate = "  YEAR(payments.created_at)  = $year_selected ";
						$sqlDateDeposit = "  YEAR(deposits.created_at)  = $year_selected ";
						$infoTarikhTunggakan = " TAHUN $statement_year";
				}
				else if($statementby == 'statement_monthyear'){
						//statement by bills issue_date = monthyear selected
						$displayYear = "display:none";
						$displayMonthYear = "display:block";
						$displayDateFromTo = "display:none";
						$displayChooseDate = "display:none";
						$displayCheckBox = "display:block";
						echo "STATEMENT MONTH YEAR:: ".$statement_monthyear = $_GET["statement_monthyear"];
						$_SESSION['statement_monthyear']=$statement_monthyear;
						$selected_cutOffPayment = $_GET["cutOffPayment"];
						$_SESSION["cutOffPayment"] = $selected_cutOffPayment;
						$payment_monthYear = date("m/Y", strtotime($statement_monthyear));
						
						$firstDay = "";
						$lastDay = "";
						
						$firstDay = date("Y-m-01", strtotime($statement_monthyear));
						$lastDay = date("Y-m-t", strtotime($statement_monthyear));
						
						$cond_monthYear_BF = " and issue_date < '$firstDay ' ";
						$cond_date_BF = " and payments.created_at < '$firstDay' ";
						
						$cond_selectedCutOffPayment = " and payments.created_at between '$firstDay' and '$lastDay' ";
					
					$date_monthYear = date("Y-m-01",strtotime($statement_monthyear));
						$lastdate = $date_monthYear . "-01";
						$lastdate = date("Y-m-t", strtotime($lastdate));			
						$cutOffDate = $lastdate;
						$cutOffDate = date('Y-m-d', strtotime($lastdate. ' + 1 days'));
						
						$date_monthYearDeposit = date("M Y",strtotime($lastdate));	
						
						$sqlDate = "  payments.created_at  >= '$date_monthYear' and payments.created_at  < '$cutOffDate'";
						//$sqlDateDeposit = " deposits.one_month_date  = '$date_monthYearDeposit' ";
						$infoTarikhTunggakan = " BULAN $payment_monthYear ";
				}
				else if($statementby == 'statement_date'){
						//statement by payments on selected date (dateFrom - dateTo)
						$displayYear = "display:none";
						$displayMonthYear = "display:none";
						$displayDateFromTo = "display:block";
						$displayChooseDate = "display:none";
						$displayCheckBox = "display:block";
						$statement_date = $_GET["statement_date"];
						$dateFrom = date("Y-m-d",strtotime($_GET["dateFrom"]));
						$dateTo =  date("Y-m-d",strtotime($_GET["dateTo"]));
						$_SESSION['statement_date']=$statement_date;
						$_SESSION['dateFrom']=$dateFrom;
						$_SESSION['dateTo']=$dateTo;
						$date_cutOFF = date('Y-m-d', strtotime($dateTo. ' + 1 days'));	
						
						
						$firstDay = $dateFrom;
						$lastDay = $date_cutOFF;
						
						$cond_selectedCutOffPayment = " and payments.created_at between '$firstDay' and '$lastDay' ";
						$sqlDate = " (payments.created_at  >= '$dateFrom'  and payments.created_at < '$date_cutOFF') ";
						$sqlDateDeposit = " (deposits.created_at  >= '$dateFrom'  and deposits.created_at < '$date_cutOFF') ";
						$infoTarikhTunggakan = " DARI ".date("d/m/Y",strtotime($dateFrom)) . " SEHINGGA " . date("d/m/Y",strtotime($dateTo));
				}
				else if($statementby == 'statement_ChooseDate'){
						//statement by payments on selected date
						$displayYear = "display:none";
						$displayMonthYear = "display:none";
						$displayDateFromTo = "display:none";
						$displayChooseDate = "display:block";
						$displayCheckBox = "display:block";
						$chooseDate = date("Y-m-d",strtotime($_GET["chooseDate"]));
						$_SESSION['chooseDate']=$chooseDate;
						$selected_cutOffPayment = $_GET["cutOffPayment"];
						$_SESSION["cutOffPayment"] = $selected_cutOffPayment;
						$date_cutOFF = date('Y-m-d', strtotime($chooseDate. ' + 1 days'));	
						
						$cond_selectedCutOffPayment = " and payments.created_at < '$date_cutOFF'";
						$sqlDate = " payments.created_at like '%$chooseDate%' ";
						$sqlDateDeposit = " deposits.created_at like '%$chooseDate%' ";
						$cutOffDate = $chooseDate;
						$infoTarikhTunggakan = " SEHINGGA ". date("d/m/Y",strtotime($chooseDate));
				}
				else if($statementby == ""){
					$cutOffDate = date('Y-m-d', strtotime($chooseDate. ' + 1 days'));
					$cond_selectedCutOffPayment = " and payments.created_at < '$cutOffDate'";
					//$sqlDate = " payments.created_at = '$cutOffDate' ";
					$infoTarikhTunggakan = " TARIKH:  " . DATE("d/m/Y");
				}
				
				
				if($report_type == "Deposit"){
					    $sqlDeposit = "select deposits.*,bill_deposits.*, tenants.id as tenant_id, tenants.tenant_name, properties.unit_no  from deposits inner join 
									tenants on deposits.tenant_id = tenants.id
									inner join properties on tenants.property_id = properties.id
									inner join locations on properties.location_id = locations.id
									inner join projects on locations.project_id = projects.id 
									where locations.id = $location_id 
									and projects.id = $project_id
									and ($sqlDateDeposit) 
									";
					$runDeposit = mysqli_query($link, $sqlDeposit);
						if($runDeposit === FALSE){
						die(mysqli_error("SQL DIE! Error for SQL: ".$sqlDeposit));
					}
				}
				else if($report_type == "Rental" || $report_type == "Water" ){
					   echo $sqlRental = "select payments.id as payment_id, payments.receipt_no, payments.amount_paid, bills.*, tenants.tenant_name, properties.unit_no, tenants.rental_rate, bills.issue_date,  users.name, locations.location_name,
									projects.project_name, bill_types.bill_type_name, bills.bill_type_id
									from payments 
									inner join bills on payments.bill_id = bills.id
									inner join bill_types on bills.bill_type_id = bill_types.id
									inner join tenants on bills.tenant_id = tenants.id 
									inner join properties on tenants.property_id = properties.id
									inner join locations on properties.location_id = locations.id
									inner join projects on locations.project_id = projects.id
									inner join users on payments.created_by =  users.id
									where bills.bill_type_id in ($bill_types_id,$bill_type_Outstanding)  and locations.id = $location_id 
									and projects.id = $project_id and ($sqlDate) 
									
									";
									
					$runRental= mysqli_query($link, $sqlRental);
					if($runRental=== FALSE){
						//die(mysqli_error("SQL DIE! Error for SQL: ".$sqlRental));
						die(mysqli_error($link));
					}
				}
				
				
				
			}
			
			?>
			
            
            <!--main content upper start-->
			<?php include "main-content-upper.php"?>
            <!--main content upper end-->
			<!--main content middle start-->
			<div class="row" >
				
							<div class="card-body">
								<div class="row form-group">
									<div class="col col-md-3">
										<div class="card">
											<div class="card-header py-3">
												<h5 class="m-0 font-weight-bold text-primary">Search Payments</h5>
											</div>
											<form action="<?php $_SERVER['PHP_SELF']; ?>" method="get" enctype="multipart/form data" >
												<input type="hidden" name="projectLocation" value="<?php echo $_GET["projectLocation"]?>">
												<div class="card-body card-block">
													<input type="hidden" name="login" value="<?php echo $_GET["login"]?>">
													<div class="row form-group">
														<div class="col col-md-3">
															<label class=" form-control-label"><b>PAYMENT TYPE</b></label>
														</div>
														<div class="col col-md-9">
															<div class="form-check">
																<div class="radio">
																	<label for="radio1" class="form-check-label ">
																		<input type="radio" id="radio1" name="report_type" value="Deposit" class="form-check-input" <?php echo ($_SESSION["submenu"] == 'Deposit' || $_SESSION["report_type"] == 'Deposit' ? 'CHECKED' : '');?>> Deposit 
																	</label>
																</div>
																<div class="radio">
																	<label for="radio2" class="form-check-label ">
																		<input type="radio" id="radio2" name="report_type" value="Rental" class="form-check-input" <?php echo ($_SESSION["submenu"] == 'Rental' || $_SESSION["report_type"] == 'Rental' ? 'CHECKED' : '');?>> Rental   
																	</label>
																</div>
																<div class="radio">
																	<label for="radio3" class="form-check-label ">
																		<input type="radio" id="radio3" name="report_type" value="Water" class="form-check-input" <?php echo ($_SESSION["submenu"] == 'Water' || $_SESSION["report_type"] == 'Water' ? 'CHECKED' : '');?>> Water
																	</label>
																</div>
															</div>
														</div>
													</div>
													
														<div >
															<label class=" form-control-label"><b>PAYMENTS OPTION</b></label>
														</div>
														<div class="form-check" >
															<select id="statementby" name="statementby" class="form-control-sm form-control">
																<option>Select Option</option>
																<option value="statement_year"  <?php echo ($_SESSION["statementby"] ==  'statement_year')  ? "selected" : "" ?>>STATEMENT BY YEAR</option>
																<option value="statement_monthyear" <?php echo ($_SESSION["statementby"] ==  'statement_monthyear')  ? "selected" : "" ?>>STATEMENT BY MONTH AND YEAR</option>
																<option value="statement_date"  <?php echo ($_SESSION["statementby"] ==  'statement_date')  ? "selected" : "" ?>>STATEMENT BY DATE (BETWEEN TWO DATES)</option>
																<option value="statement_ChooseDate"  <?php echo ($_SESSION["statementby"] ==  'statement_ChooseDate')  ? "selected" : "" ?>>STATEMENT BY SELECTED DATE</option>
															</select>
														</div>
														<div class="form-check" >
															<div id="showstatement_year" class="myDiv" style=<?php echo $displayYear?>>
																<select name="statement_year" class="form-control-sm form-control">
																	<option value="">Please Select Year</option>
																	<?php while($resultYear = mysqli_fetch_array($runSqlYear)){?>
																			<option value="<?php echo $resultYear["year"]?>" <?php echo ($_SESSION["statement_year"] ==  $resultYear["year"])  ? "selected" : "" ?> ><?php echo $resultYear["year"]?></option>
																	<?php } ?>
																</select>
															</div>
															<div id="showstatement_monthyear" class="myDiv" style=<?php echo $displayMonthYear?>>
																<input type="month"  name="statement_monthyear" id="statement_monthyear" value="<?php echo $_SESSION["statement_monthyear"]?>" size="20" class="form-control-sm form-control"/>
															</div>
															<div id="showstatement_date" class="myDiv" style=<?php echo $displayDateFromTo?>>
																<label class=" form-control-label"><b>From</b></label>
																 <input id='dateFrom' name='dateFrom' type='date' size='10' value="<?php echo $_SESSION["dateFrom"]?>" class="form-control-sm form-control"> 
																 <label class=" form-control-label"><b>To</b></label>
																 <input id='dateTo' name='dateTo' type='date' size='10' value="<?php echo $_SESSION["dateTo"]?>" class="form-control-sm form-control">
															</div>
															<div id="showstatement_ChooseDate" class="myDiv" style=<?php echo $displayChooseDate?>>
																<label class=" form-control-label"><b> Choose Date </b></label>
																<?echo $_SESSION["statementby"]?><input id='chooseDate' name='chooseDate' type='date' size='10' value="<?php echo $_SESSION["chooseDate"]?>" class="form-control-sm form-control"> 
															</div>
														</div>
												</div>	
												<div class="card-footer">		
													<div class="row form-group" >
														<div class="col col-md-3">
															<label for='text-input' class='form-control-label'>&nbsp;</label>
														</div>
														<div class="col col-sm-9">
															<button class="btn btn-primary" type="submit" name="submit">Submit</button>
														</div>
													</div>
												</div>
											</form>
										</div>												
									</div>
									<div class="col col-md-9" >
										<div class="card-header py-3">
											<h5 class="m-0 font-weight-bold text-primary">Showing Result</h5>
										</div>
										<div class="card-body card-block">
										<?php 
											if($report_type == "Deposit"){
												include "payment_deposit.php";
											}
											else if($report_type == "Rental" || $report_type == "Water"){
												//echo "TEST!!";
												include "payment_rental.php";
											}
											/*else if($report_type == "Water"){
												include "invoice_water.php";
											}*/
										?>
									
										</div>
									</div>
								</div>
                        
							</div> 
						</DIV>
            <!--main content middle end-->
			<!--main content bottom start-->
				<?PHP include "main-content-bottom.php";?>
			<!--main content bottom end-->
			
            <!-- END MAIN CONTENT-->
            <!-- END PAGE CONTAINER-->
        </div>
    </div>
    <?PHP include "footerLink.php";?>
</body>
</html>
<!-- end document-->