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

<style>
.my-report-scrollbar {
position: relative;
height: 100%;
width:100%;
overflow: auto;
}
.table-report-scroll-y {
display: block;
}
</style>
<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;
			
			
			
			
			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";
			
			
			$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 = ".$_SESSION["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));
					}
			
			//DISPLAY BEFORE SUBMIT START
			$statementby = $_GET["statementby"];
			IF($statementby != "" || $statementby != null){
				include("bill_types.php");
				if($statementby == 'statement_year'){
					$displayYear = "display:block";
					$displayMonthYear = "display:none";
					$displayDateFromTo = "display:none";
					$displayChooseDate = "display:none";
					$displayCheckBox = "display:block";
					$displayStatementOption = "display:none";
					
				
					  
					
				}
				else if($statementby == 'statement_monthyear'){
					$displayYear = "display:none";
					$displayMonthYear = "display:block";
					$displayDateFromTo = "display:none";
					$displayChooseDate = "display:none";
					$displayCheckBox = "display:block";
					$displayStatementOption = "display:none";
				}
				else if($statementby == 'statement_date'){
					$displayYear = "display:none";
					$displayMonthYear = "display:none";
					$displayDateFromTo = "display:block";
					$displayChooseDate = "display:none";
					$displayCheckBox = "display:block";
					$displayStatementOption = "display:none";
				}
				else if($statementby == 'statement_ChooseDate'){
					$displayYear = "display:none";
					$displayMonthYear = "display:none";
					$displayDateFromTo = "display:none";
					$displayChooseDate = "display:block";
					$displayCheckBox = "display:block";
					$displayStatementOption = "display:none";
				}
			}
			//END DISPLAY BEFORE SUBMIT
			
			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["searchStatement"])){
				//echo "submit!";
				$dateFrom = $_GET["dateFrom"];
				$dateTo = $_GET["dateTo"];
				$_SESSION["dateFrom"] = $dateFrom;
				$_SESSION["dateTo"] = $dateTo;
				
				$dateTo_cutOFF = date('Y-m-d', strtotime($dateTo. ' + 1 days'));	
				
				include("bill_types.php");
				
				
				$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"];
				$selectedTenantInfo = $_GET["tenantInfo"];
				$selected_cutOffPayment = $_GET["cutOffPayment"];
				$tenant_status = $_GET["tenant_status"];
				$_SESSION["aged_date"] = $selectedDate;
				$_SESSION["tenantInfo"] = $selectedTenantInfo;
				$_SESSION["cutOffPayment"] = $selected_cutOffPayment;
				$_SESSION["tenant_status"] = $tenant_status;
				
				
				$cond = "";
				$cond_project = "";
				$cond_location = "";
				$cond_ageingDate = "";
				$cond_tenantInfo = "";
				$cond_tenantStatus = "";
				$cond_staffID = "";
				$groupBY = "";
				$ageingDateInfo ="";
				$cond_selectedCutOffPayment = "";
				$bulanTerimaan = "";
				$project_id = $_SESSION["project_id"];
				$sqlDateADV = "";
				$date_statementOptionView = "";
				
				$sqlTenantStatus = "";
				if($tenant_status != ""){
					$sqlTenantStatus = " and tenants.status = '" .$tenant_status."' ";
				}
				
				
				$block = $_GET['block'];
				$sqlListBlock = "";
				$listBlock = "";
				$listBlock_plain = "";
				  if(!empty($block)) 
				  {
					 " COUNT BLOCK: ".$countBlock = count($block);
					for($i=0; $i < $countBlock; $i++)
					{
						($block[$i] . ",");
						if($listBlock == ""){
							$listBlock = "'" . $block[$i] ."'";
							$listBlock_plain = $block[$i];
						}
						else {
							$listBlock .= "," . "'" . $block[$i]."'";
							$listBlock_plain .= "," . $block[$i];
						}
					}
				  }
				IF($listBlock != ""){
					$sqlListBlock = " and properties.block in (" . $listBlock .") ";
				}
				$_SESSION["block"] = $block;
				
				
				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";
						
						$sqlIssueDate = "";
						
						$statement_year = $_GET["statement_year"];
						$_SESSION['statement_year']=$statement_year;
						$selected_cutOffPayment = $_GET["cutOffPayment"];
						$_SESSION["cutOffPayment"] = $selected_cutOffPayment;
						$dateEnd = date("Y-m-d",strtotime($statement_year . '-12-31')); //tarikh hujung tahun
						$dateStart =  date("Y-m-d",strtotime($statement_year . '-01-01')); //tarikh awal tahun
						$statement_year_cutOFF = date('Y-m-d', strtotime($dateEnd. ' + 1 days'));
						IF($selected_cutOffPayment == true){
							
							$cond_selectedCutOffPayment = " and YEAR(bills.created_at) < '$statement_year_cutOFF'";
						}
						
						
						
						//currentdate
						$currentdate = date("Y-m-d");
						$current_year = date("Y",strtotime($currentdate));
						echo "dateEnd_year:" . $dateEnd_year = date("Y",strtotime($dateEnd));
						if($current_year == $dateEnd_year){
							echo "DATE END:::: " .$dateEnd = date("Y-m-d");
						}
						
						$year_selected = date("Y",strtotime($dateStart));
						
						
						$cond_monthYear_BF = " and year(bills.issue_date) < '$statement_year' ";
						$cond_date_BF = " and bills.created_at < '$dateStart' ";
						//$sqlDate = "  YEAR(bills.created_at)  = $year_selected ";
						$sqlDate = "  bills.created_at  >= $dateStart and  bills.created_at <= '$dateEnd' ";
						//$sqlDateDeposit = "  YEAR(deposit_bills.created_at)  = $year_selected ";
						$sqlDateDeposit = "  deposit_bills.created_at)  >= '$dateStart' and deposit_bills.created_at)  <= '$dateEnd'  ";
						//$sqlDateDepositOthers = "  YEAR(deposit_bill_other_receipts.created_at)  = $year_selected ";
						$sqlDateDepositOthers = "  deposit_bill_other_receipts.created_at  >= '$dateStart' and deposit_bill_other_receipts.created_at  <= '$dateEnd' ";
						$sqlIssueDate = " AND  bills.issue_date  >= '$dateStart' and  bills.issue_date <= '$dateEnd'  ";
						$infoTarikhTunggakan = " TAHUN $statement_year";
						$sqlDateADV = " and bills.issue_date > '$dateEnd' ";
						$sqlDatePayments = " and bills.created_at >= '$dateStart' and bills.created_at < '$statement_year_cutOFF' ";
						$date_statementOptionView = $year_selected;
				}
				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";
						$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 bills.created_at < '$firstDay' ";
						
						$cond_selectedCutOffPayment = " and bills.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;
						
						$month = date("m",strtotime($date_monthYear));
						$year = date("Y",strtotime($date_monthYear));
						
						$date_monthYearDeposit = date("M Y",strtotime($lastdate));	
						
						$sqlDate = "  bills.issue_date  = '$date_monthYear' ";
						$sqlDateDeposit = " MONTH(deposit_bills.created_at)  = '$month' and YEAR(deposit_bills.created_at)  = '$year'";
						$sqlDateDepositOthers = " MONTH(deposit_bill_other_receipts.created_at)  = '$month' and YEAR(deposit_bill_other_receipts.created_at)  = '$year'";
						$infoTarikhTunggakan = " BULAN $payment_monthYear ";
						
						$date_statementOptionView = $date_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'));
						$dateOpening = date('Y-m-d', strtotime($dateFrom. ' - 1 days'));
						
						//]compare month year
						$monthYearFrom = date('m-Y',strtotime($dateFrom));
						$monthYearTo = date('m-Y',strtotime($dateTo));
						if($monthFrom == $monthTo){
							$bulanTerimaan = date('M Y', strtotime($dateFrom));
						}
						else{
							$bulanTerimaan = date("d/m/Y",strtotime($dateFrom)) . " SEHINGGA " . date("d/m/Y",strtotime($dateTo));
						}
						
						//compare year
						$tahunTerimaan = date('Y', strtotime($dateFrom));
						/*$yearFrom = date('Y',strtotime($dateFrom));
						$yearTo = date('Y',strtotime($dateTo));
						if($yearFrom == $yearTo){
							$tahunTerimaan = date('Y', strtotime($dateFrom));
						}
						else {
							
						}*/
						
						$firstDay = $dateFrom;
						$lastDay = $date_cutOFF;
						
						$cond_selectedCutOffPayment = " and bills.issue_date between '$firstDay' and '$lastDay' ";
						$sqlDate = " (bills.issue_date  >= '$dateFrom'  and bills.issue_date < '$date_cutOFF') ";
						$sqlDateDeposit = " (deposit_bills.created_at  >= '$dateFrom'  and deposit_bills.created_at < '$date_cutOFF') ";
						$sqlDateDepositOthers = " (deposit_bill_other_receipts.created_at  >= '$dateFrom'  and deposit_bill_other_receipts.created_at < '$date_cutOFF') ";
						$infoTarikhTunggakan = " DARI ".date("d/m/Y",strtotime($dateFrom)) . " SEHINGGA " . date("d/m/Y",strtotime($dateTo));
						$date_statementOptionView_from = $dateFrom;
						$date_statementOptionView_to = $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 = "";
						if($_GET["statement_ChooseDate"] != "" || $_GET["statement_ChooseDate"] != null){
							$chooseDate = date("Y-m-d",strtotime($_GET["statement_ChooseDate"]));
						}
						$_SESSION['chooseDate']=$chooseDate;
						$selected_cutOffPayment = $_GET["cutOffPayment"];
						$_SESSION["cutOffPayment"] = $selected_cutOffPayment;
						$date_cutOFF = date('Y-m-d', strtotime($chooseDate));	
						
						$cond_selectedCutOffPayment = " and bills.created_at = '$date_cutOFF'";
						$sqlDate = " bills.issue_date like '%$chooseDate%' ";
						$sqlDateDeposit = " deposit_bills.created_at like '%$chooseDate%' ";
						$sqlDateDepositOthers = " deposit_bill_other_receipts.created_at like '%$chooseDate%' ";
						$cutOffDate = $chooseDate;
						$infoTarikhTunggakan = " SEHINGGA ". date("d/m/Y",strtotime($chooseDate));
						
						$date_statementOptionView = $chooseDate;
				}
				else if($statementby == ""){
					$currentDate = date('Y-m-d');
					$cutOffDate = date('Y-m-d', strtotime($currentDate. ' + 1 days'));
					$cond_selectedCutOffPayment = " and bills.created_at < '$cutOffDate'";
					//$sqlDate = " payments.created_at = '$cutOffDate' ";
					$infoTarikhTunggakan = " TARIKH:  " . DATE("d/m/Y");
					
					$date_statementOptionView = $cutOffDate;
				}
				
				if($sqlDateDeposit != ""){
					$sqlDateDeposit = " and " . $sqlDateDeposit;
				}
				if($sqlDateDepositOthers != ""){
					$sqlDateDepositOthers  = " and " . $sqlDateDepositOthers;
				}
				
				if($report_type == "Deposit"){
					//check table deposit_bills (penyewa baru)
					 $sqlStatementDeposit_newRecord ="SELECT tenants.id as tenant_id, tenants.tenant_name, properties.unit_no, deposit_bills.id as deposit_id, 
															deposit_bills.deposit_bill_no as invoice_no, 
															deposit_bills.two_month_rental_due as two_month_rental_due, 
															deposit_bills.two_month_rental_amount as two_month_rental_amount_bill, 
															deposit_bills.utility_due as utility_due, 
															deposit_bills.utility_amount as utility_amount_bill, 
															deposit_bills.created_at as created_at, 
															deposit_bills.id as deposit_bill_id, 
															deposit_bills.deposit_bill_no as deposit_bill_no, 
															deposit_bills.payment_status, 
															concat(deposit_bills.deposit_bill_no,' deposit_bill ',deposit_bills.two_month_rental_amount) as info_two_months_amount  ,
															concat(deposit_bills.deposit_bill_no,' deposit_bill ',deposit_bills.utility_amount) as info_utility_amount  
															FROM deposit_bills 
                                                            inner JOIN tenants on deposit_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 
															where locations.id = " . $_SESSION["location_id"] ." and (deposit_bills.payment_status = 'unpaid' or deposit_bills.payment_status = 'partial') $sqlDateDeposit $sqlTenantStatus $sqlListBlock";
															
															
					
					
					$runStatementDeposit_newRecord = mysqli_query($link, $sqlStatementDeposit_newRecord);
					if($runStatementDeposit_newRecord === FALSE){
						die(mysqli_error($link));
					}
					$countStatementDeposit_newRecord = mysqli_num_rows($runStatementDeposit_newRecord);
					
					
					
					$colspanShow_header = 7;
					$colspanShow_bottom = 4;

					//deposit_payment_other_receipts table
					
					 $sqlDepositPaymentOthers ="SELECT tenants.id as tenant_id, tenants.tenant_name, properties.unit_no, deposit_bill_other_receipts.id as depositOthers_id, 
															deposit_bill_other_receipts.deposit_other_bill_no as invoice_no, 
															deposit_bill_other_receipts.yp_charges_due as yp_due, 
															deposit_bill_other_receipts.yp_charges_amount as yp_charges_amount, 
															deposit_bill_other_receipts.sst_charges_due as sst_due, 
															deposit_bill_other_receipts.sst_charges_amount as sst_charges_amount, 
															deposit_bill_other_receipts.stamping_charges_due as stamping_due, 
															deposit_bill_other_receipts.stamping_charges_amount as stamping_charges_amount, 
															deposit_bill_other_receipts.created_at as created_at, 
															deposit_bill_other_receipts.id as deposit_bill_other_id, 
															deposit_bill_other_receipts.deposit_other_bill_no as deposit_other_bill_no, 
															deposit_bill_other_receipts.payment_status, 
															concat(deposit_bill_other_receipts.deposit_other_bill_no,' deposit_other_bill ',deposit_bill_other_receipts.yp_charges_amount) as info_yp_paid  ,
															concat(deposit_bill_other_receipts.deposit_other_bill_no,' deposit_other_bill ',deposit_bill_other_receipts.sst_charges_amount) as info_sst_paid,  
															concat(deposit_bill_other_receipts.deposit_other_bill_no,' deposit_other_bill ',deposit_bill_other_receipts.stamping_charges_amount) as info_stamping_paid  
															FROM deposit_bill_other_receipts 
															inner JOIN tenants on deposit_bill_other_receipts.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 = " . $_SESSION["location_id"] ." and (deposit_bill_other_receipts.payment_status = 'unpaid' or deposit_bill_other_receipts.payment_status = 'partial')  $sqlDateDepositOthers $sqlTenantStatus $sqlListBlock";
					
					
					
					$runDepositPaymentOthers = mysqli_query($link, $sqlDepositPaymentOthers);
					if($runDepositPaymentOthers === FALSE){
						die(mysqli_error($link));
					}
					$countRowPaymentOthers = mysqli_num_rows($runDepositPaymentOthers);
					
				}
				else if($report_type == "Rental" || $report_type == "Water" ){
					if($statementby == "statement_year"){
						  "statement year $report_type";
							 
										
						echo  $sqlNotPaidList = "select tenants.id as tenant_id, tenants.tenant_name, properties.unit_no, bills.id as bill_id,
											bills.amount_due, bills.bill_amount, bills.payment_status, bills.issue_date, bills.bill_no, bills.created_at 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
											inner join projects on locations.project_id = projects.id
											where bills.bill_type_id in ($bill_types_id) and locations.id = ". $_SESSION['location_id'] ."   $sqlIssueDate $sqlTenantStatus $sqlListBlock
											and projects.id =  $project_id and (bills.payment_status = 'unpaid' or bills.payment_status = 'partial')  order by properties.id ASC
											";
					}
					else if($statementby == "statement_ChooseDate"){
						
						$sqlNotPaidList = " select tenants.id as tenant_id, tenants.tenant_name, properties.unit_no, bills.id as bill_id,bills.bill_no,
											bills.amount_due, bills.bill_amount, bills.payment_status, bills.issue_date, bills.created_at 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
											inner join projects on locations.project_id = projects.id 
											where bills.bill_type_id in ($bill_types_id)  and locations.id = ". $_SESSION['location_id'] ." 
											and projects.id = $project_id and (bills.payment_status = 'unpaid' or bills.payment_status = 'partial') and ($sqlDate)  $sqlTenantStatus $sqlListBlock
									
									";
					}
					else if($statementby == "statement_monthyear"){
						 
						$sqlNotPaidList = " select tenants.id as tenant_id, tenants.tenant_name, properties.unit_no, bills.id as bill_id,bills.bill_no,
											bills.amount_due, bills.bill_amount, bills.payment_status, bills.issue_date, bills.created_at 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
											inner join projects on locations.project_id = projects.id 
											where bills.bill_type_id in ($bill_types_id)  and locations.id = ". $_SESSION['location_id'] ."  
											and projects.id =  $project_id and (bills.payment_status = 'unpaid' or bills.payment_status = 'partial') and ($sqlDate) $sqlTenantStatus $sqlListBlock order by properties.id ASC";
					}
					else if($statementby = "statement_date"){
						
						  	
						$sqlNotPaidList = "select tenants.id as tenant_id, tenants.tenant_name, properties.unit_no, bills.id as bill_id,bills.bill_no,
											bills.amount_due, bills.bill_amount, bills.payment_status, bills.issue_date, bills.created_at 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
											inner join projects on locations.project_id = projects.id 
											where bills.bill_type_id in ($bill_types_id)  and locations.id = ". $_SESSION['location_id'] ."  
											and projects.id =  $project_id and (bills.payment_status = 'unpaid' or bills.payment_status = 'partial') and ($sqlDate) $sqlTenantStatus $sqlListBlock order by properties.id ASC";
					}
					else{
					     
						$sqlNotPaidList = " select tenants.id as tenant_id, tenants.tenant_name, properties.unit_no, bills.id as bill_id,bills.bill_no,
											bills.amount_due, bills.bill_amount, bills.payment_status, bills.issue_date, bills.created_at 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
											inner join projects on locations.project_id = projects.id 
											where bills.bill_type_id in ($bill_types_id)  and locations.id = ". $_SESSION['location_id'] ." 
											and projects.id = $project_id and (bills.payment_status = 'unpaid' or bills.payment_status = 'partial') and ($sqlDate)  $sqlTenantStatus $sqlListBlock order by properties.id ASC
											";
					}				
					$runNotPaidList= mysqli_query($link, $sqlNotPaidList);
					if($runNotPaidList=== FALSE){
						die(mysqli_error($link));
					}
				}
			}
			ECHO "SQL sqlNotPaidList::::". $sqlNotPaidList;
			
			?>
			
            
            <!--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-12">-->
									<div class="col-lg-12">
										<div class="card">
											<div class="card-header py-3">
												<h5 class="m-0 font-weight-bold text-primary">Search</h5>
											</div>
											<form action="<?php $_SERVER['PHP_SELF']; ?>" method="get" enctype="multipart/form data"  id="form_tenantStatement">
												<input type="hidden" name="projectLocation" value="<?php echo $_GET["projectLocation"]?>">
												<input type="hidden" name="menu" value="<?php echo $_GET["menu"]?>">
												<input type="hidden" name="submenu" value="<?php echo $_GET["submenu"]?>">
												<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>REPORT 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" onChange = "change(this.value)"  name="report_type" id="report_type" value="Deposit" required 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" onChange = "change(this.value)"  name="report_type" id="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" onChange = "change(this.value)"  name="report_type" id="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 class="row form-group">
														<div class="col col-md-3">
															<label class=" form-control-label"><b>STATEMENT OPTION</b></label>
														</div>
														<div class="col col-md-9">
															<div class="form-check" >
																<?php 
																	$page = "report_all_payments.php";
																	//include("statement_option.php"); 
																	?>
																<table width="100%">
																	<tr align="center">
																		<td width="30%">
																			<?php
																			
																			$_SESSION["statementby"] = $_GET["statementby"];
																					$_SESSION["statement_year"] = $_GET["statement_year"];
																					$_SESSION["statement_monthyear"] = $_GET["statement_monthyear"];
																					$_SESSION["dateFrom"] = $_GET["dateFrom"];
																					$_SESSION["dateTo"] = $_GET["dateTo"];
																					$_SESSION["statement_ChooseDate"] = $_GET["statement_ChooseDate"];?>
																			
																			<select name="statementby" id="statementby" onchange="statementby_change()" class="form-control" required>
																				<option value="">Select Statement 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 CUT OFF DATE</option>
																			</select>
																		</td>
																		<td id="statement_ByYear" style='<?php echo $displayYear ?>;'>
																			<?php //if($_SESSION["statement_year"])?>
																			<select required="" name="statement_year" id="statement_year" class="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>
																		</td>
																		<td id="statement_ByMonthYear" style='<?php echo $displayMonthYear  ?>;'>
																			<input required="" type="month" class="form-control" name="statement_monthyear" id="statement_monthyear" value="<?php echo $_SESSION["statement_monthyear"]?>" size="20"/>
																		</td>
																		<td id="statement_ByDate" style='<?php echo $displayDateFromTo ?>;' >
																		<b>	From: </b><input required="" id='dateFrom' name='dateFrom' type='date' size='10' value="<?php echo $_SESSION["dateFrom"]?>"> 
																		<b>	To:  </b><input  required="" id='dateTo' name='dateTo' type='date' size='10' value="<?php echo $_SESSION["dateTo"]?>">
																		</td>
																		<td id="statement_ByChooseDate" style='<?php echo $displayChooseDate ?>;' >
																			<input required="" id="statement_ChooseDate" name="statement_ChooseDate" class="form-control" type='date' size='10' value="<?php echo $_SESSION["statement_ChooseDate"]?>"> 
																		</td>
																	</tr>
																	<?php if($page != "report_all_payments.php"){?>
																	<tr >
																		<td>&nbsp;</td>
																		<td align="center" id="statement_cutOff" style='<?php  echo $displayCheckBox  ?>;font-size:12px' >
																			<div>
																				<input type="checkbox" id="cutOffPayment" name="cutOffPayment" <?php echo ($_SESSION["cutOffPayment"]) ? "checked" : "" ?> value="true">
																				<label for="cutOffPayment" style="font-size:16px"><b> View Ageing Record based on cut off payments same as selected Month / Year </b></label>
																			</div>
																		</td>
																	</tr>
																	<?php } ?>
																</table>
															</div>
														</DIV>
													</DIV>
													<div class="row form-group">
														<div class="col col-md-3">
															<label class=" form-control-label"><b>BLOCK</b></label>
														</div>
														<div class="col col-md-9">
															<div class="form-check" >
																<?php 
																	$sqlBlock = "select properties.block as block from properties inner join locations on properties.location_id = locations.id where locations.id = " . $_SESSION["location_id"] . " group by properties.block";
																	$runBlock = mysqli_query($link, $sqlBlock);
																	if($runBlock === FALSE){
																		die(mysqli_error($link));
																	}
																	while($resultBlock = mysqli_fetch_array($runBlock)){
																		$blocklist = $resultBlock["block"];
																		
																		?>
																		<div class="form-check-inline form-check">
																			<label for="inline-checkbox[]" class="form-check-label">
																				<input type="checkbox" id="inline-checkbox[]" class="checkbox" name="block[]" value="<?php echo $blocklist?>" class="form-check-input" ><b><font style="font-size: 14px"><?php echo strtoupper($blocklist)?></font></b>
																			</label>
																		</div>
																		<?php
																			//}
																	}
																	?>
															</div>
														</DIV>
													</DIV>
													<div class="row form-group">
														<div class="col col-md-3">
															<label class=" form-control-label"><b>TENANT STATUS</b></label>
														</div>
														<div class="col col-md-3">
															<div class="form-check">
																<div class="radio">
																	<label for="radioActive" class="form-check-label ">
																		<input type="radio" id="radioActive" name="tenant_status" value="enable" class="form-check-input" <?php echo $_SESSION["tenant_status"] == "enable" ? "checked" : "" ?>><font style="font-size: 14px">Active</font>
																	</label>
																</div>
																<div class="radio">
																	<label for="radioDeactive" class="form-check-label ">
																		<input type="radio" id="radioDeactive" name="tenant_status" value="disable" class="form-check-input" <?php echo $_SESSION["tenant_status"] == "disable" ? "checked" : "" ?>><font style="font-size: 14px">Deactive</font>  
																	</label>
																</div>
															</DIV>
														</div>
													</div>
												</div>	
												<div class="card-footer">		
													<div class="row form-group" >
														<div class="col col-md-9">
															<label for='text-input' class='form-control-label'>&nbsp;</label>
														</div>
														<div class="col col-sm-9">
															<button type="submit" name="searchStatement" onclick="submitFunc()" class="btn btn-primary btn-sm" >
																<i class="fa fa-dot-circle-o"></i> Submit
															</button>
															<button  onclick="resetFunc()"   name="resetStatement" class="btn btn-danger btn-sm">
																<i class="fa fa-ban"></i> Reset
															</button>
															 
														</div>
													</div>
												</div>
											</form>
										</div>												
									</div>
								</div>
								<div class="row form-group">
									<div class="col-lg-12" >
										<div class="card-header py-3">
											<h5 class="m-0 font-weight-bold text-primary">Showing Result
											
												<div align="center">
													<?php 
														if($statementby == "statement_year" || $statementby == "statement_monthyear" ||  $statementby == "statement_ChooseDate"){?>
														<button type="button"  onclick="printNotPaidList1date('<?php echo $report_type ?>','<?php echo $statementby ?>','<?php echo $date_statementOptionView?>','<?php echo $tenant_status?>')" class="btn btn-success btn-sm">PRINT</button>
														<button type="button"  onclick="exportNotPaidList1date('<?php echo $report_type ?>','<?php echo $statementby ?>','<?php echo $date_statementOptionView?>','<?php echo $tenant_status?>')" class="btn btn-success btn-sm">EXPORT CSV</button>
														<?php }else if($statementby == "statement_date"){?>
														<button type="button"  onclick="printNotPaidList2date('<?php echo $report_type ?>','<?php echo $statementby ?>','<?php echo $date_statementOptionView_from?>','<?php echo $date_statementOptionView_to?>','<?php echo $tenant_status?>')" class="btn btn-success btn-sm">PRINT</button>
														<button type="button"  onclick="exportNotPaidList2date('<?php echo $report_type ?>','<?php echo $statementby ?>','<?php echo $date_statementOptionView_from?>','<?php echo $date_statementOptionView_to?>','<?php echo $tenant_status?>')" class="btn btn-success btn-sm">EXPORT CSV</button>
													<?php } ?>
													
												</div>
											
											
											</h5>
										</div>
										
										<div class="card-body card-block">
										
										<?php 
											if($report_type == "Deposit"){
												include "not_paid_list_deposit.php";
											}
											else if($report_type == "Rental" || $report_type == "Water"){
											//	echo "TEST 12345!!";
												include "not_paid_list_rental_water.php";
											}
											else if($report_type == "other"){
												include "not_paid_list_other.php";
											}
										?>
									
										</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-->
<script>
function change(x){
	//document.getElementById('statementby').removeA
}
function submitFunc(){ //alert("SUBMIT BUTTON!!!");
	if(document.getElementById("report_type").value== ''){
		document.getElementById("report_type").setAttribute("required","required");
	}
	if(document.getElementById("statementby").value== 'statement_year'){
		
		document.getElementById("statement_year").setAttribute("required","required");
		document.getElementById("statement_monthyear").removeAttribute("required");
		document.getElementById("dateFrom").removeAttribute("required");
		document.getElementById("dateTo").removeAttribute("required");
		document.getElementById("statement_ChooseDate").removeAttribute("required");
		
		if(document.getElementById("statement_year").value =='')
			alert("Please select Year");
		else{
			//document.getElementById("statement_year").value ='';
			document.getElementById("statement_monthyear").value ='';
			document.getElementById("dateFrom").value ='';
			document.getElementById("dateTo").value ='';
			document.getElementById("statement_ChooseDate").value ='';
			document.getElementById("form_tenantStatement").submit();
		}
	}
	else if(document.getElementById("statementby").value== 'statement_monthyear'){
		
		document.getElementById("statement_year").removeAttribute("required");
		document.getElementById("statement_monthyear").setAttribute("required","required");
		document.getElementById("dateFrom").removeAttribute("required");
		document.getElementById("dateTo").removeAttribute("required");
		document.getElementById("statement_ChooseDate").removeAttribute("required");
		
		if(document.getElementById("statement_monthyear").value =='')
			alert("Please select Month/Year");
		else{
			document.getElementById("statement_year").value ='';
			//document.getElementById("statement_monthyear").value ='';
			document.getElementById("dateFrom").value ='';
			document.getElementById("dateTo").value ='';
			document.getElementById("statement_ChooseDate").value ='';
			document.getElementById("form_tenantStatement").submit();
		}
	}
	else if(document.getElementById("statementby").value== 'statement_date'){
		
		document.getElementById("statement_year").removeAttribute("required");
		document.getElementById("statement_monthyear").removeAttribute("required");
		document.getElementById("dateFrom").setAttribute("required","required");
		document.getElementById("dateTo").setAttribute("required","required");
		document.getElementById("statement_ChooseDate").removeAttribute("required");
		
		if(document.getElementById("dateFrom").value =='' || document.getElementById("dateTo").value =='')
			alert("Please choose date");
		else{
			document.getElementById("statement_year").value ='';
			document.getElementById("statement_monthyear").value ='';
			//document.getElementById("dateFrom").value ='';
			//document.getElementById("dateTo").value ='';
			document.getElementById("statement_ChooseDate").value ='';
			document.getElementById("form_tenantStatement").submit();
		}
	}
	else if(document.getElementById("statementby").value== 'statement_ChooseDate'){
		document.getElementById("statement_year").removeAttribute("required");
		document.getElementById("statement_monthyear").removeAttribute("required");
		document.getElementById("dateFrom").removeAttribute("required");
		document.getElementById("dateTo").removeAttribute("required");
		document.getElementById("statement_ChooseDate").setAttribute("required","required");
		
		if(document.getElementById("statement_ChooseDate").value =='')
			alert("Please select date");
		else{
			document.getElementById("statement_year").value ='';
			document.getElementById("statement_monthyear").value ='';
			document.getElementById("dateFrom").value ='';
			document.getElementById("dateTo").value ='';
			//document.getElementById("statement_ChooseDate").value ='';
			document.getElementById("form_tenantStatement").submit();
		}
	} 
}
function statementby_change(){
	
	var statementby = document.getElementById("statementby").value;
	
	//alert(statementby);
	 if(statementby != ""){
		//document.getElementById("statement_option").style.display ='block';
		if(statementby == "statement_year"){
			document.getElementById('statement_ByYear').style.display ='block';
			document.getElementById('statement_ByMonthYear').style.display ='none';
			document.getElementById('statement_ByDate').style.display ='none';
			document.getElementById('statement_ByChooseDate').style.display ='none';
			
		}
		else if(statementby == "statement_monthyear"){
			document.getElementById('statement_ByYear').style.display ='none';
			document.getElementById('statement_ByMonthYear').style.display ='block';
			document.getElementById('statement_ByDate').style.display ='none';
			document.getElementById('statement_ByChooseDate').style.display ='none';
			
		}
		else if(statementby === 'statement_date'){
			//alert("masuk >> " + statementby);
			document.getElementById('statement_ByYear').style.display ='none';
			document.getElementById('statement_ByMonthYear').style.display ='none';
			document.getElementById('statement_ByDate').style.display ='block';
			document.getElementById('statement_ByChooseDate').style.display ='none';
		}
		else if(statementby == "statement_ChooseDate"){
			document.getElementById('statement_ByYear').style.display ='none';
			document.getElementById('statement_ByMonthYear').style.display ='none';
			document.getElementById('statement_ByDate').style.display ='none';
			document.getElementById('statement_ByChooseDate').style.display ='block';
			
		} 
		
	} 
	
	
	
}
	function resetFunc(){ //alert();
		var selectStatement = document.getElementById("statementby").value;
		
		document.getElementById('statement_ByYear').style.display ='none';
		document.getElementById('statement_ByMonthYear').style.display ='none';
		document.getElementById('statement_ByDate').style.display ='none';
		document.getElementById('statement_cutOff').style.display ='none';
		document.getElementById('statement_ByChooseDate').style.display ='none';
		//selectStatement.selectedIndex = 0;
		selectStatement.value = "";
		//alert(selectStatement.selectedIndex);
		//document.getElementById("statementOption").reset();
		
		
		document.getElementById("radio1").checked = false;
		document.getElementById("radio2").checked = false;
		document.getElementById("radio3").checked = false;
		document.getElementById("form_tenantStatement").reset();
	}
</script>