<?php 			//block - start
				$block = $_GET["block"];
				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];
						}
					}
				
				}
				else{
					
					$blockListing = "";
					if(strpos($block,",")){
					   "BLOCK EXPLODE: " .$blockListing = explode(",",$block);
					   "count block explode: " .$countBlock = count($blockListing);
					}
					else {
						$blockListing = $block;
						 "COUNT BLOCK: " .$countBlock = strlen($blockListing);
					}
					$sqlListBlock = "";
					$listBlock = "";
					  if(!empty($blockListing)) 
					  {
						for($i=0; $i < $countBlock; $i++)
						{
							($block[$i] . ",");
							if($listBlock == "")
								$listBlock = "'" . $blockListing[$i] . "'";
							else {
								$listBlock .= ",'" . $blockListing[$i]. "'";
							}
						}
					  }
				}
				
				 "LIST BLOCK: ".$listBlock;
				IF($listBlock != ""){
					$sqlListBlock = " and properties.block in (" . $listBlock .") ";
				}
				$_SESSION["block"] = $block;
				
				//block - end
				
				//payment_status - start
				 "PAYMENT STATUS: " .$payment_status = $_GET["payment_status"];
				if(!empty($payment_status)) 
				{
					 " COUNT payment_status: ".$countPaymentStatus = count($payment_status);
					IF($countPaymentStatus>1){
						for($i=0; $i < $countPaymentStatus; $i++)
						{
							($payment_status[$i] . ",");
							if($listPaymentStatus == ""){
								$listPaymentStatus = "'" . $payment_status[$i] ."'";
								$listPaymentStatus_plain = $payment_status[$i];
								$sqlListPaymentStatus = " bills.payment_status = '" . $payment_status[$i] ."' ";
							}
							else {
								$listPaymentStatus .= "," . "'" . $payment_status[$i]."'";
								$listPaymentStatus_plain .= "," . $payment_status[$i];
								$sqlListPaymentStatus .= " or bills.payment_status = '" . $payment_status[$i] ."' ";
							}
						}
					}
					 " listBlock_plain " . $listBlock_plain;
					 " listPaymentStatus " . $listPaymentStatus;
					 " listPaymentStatus_plain " . $listPaymentStatus_plain;
					//$sqlListPaymentStatus = " bills.payment_status = '$payment_status' ";
				
				}
				else{
					
					$paymentStatusListing = "";
					if(strpos($payment_status,",")){
					    "BLOCK EXPLODE: " .$paymentStatusListing = explode(",",$payment_status);
					    "count block explode: " .$countPaymentStatus = count($paymentStatusListing);
					}
					else {
						$paymentStatusListing = $payment_status;
						 "COUNT payment_status: " .$countPaymentStatus = strlen($paymentStatusListing);
					}
					$sqlListPaymentStatus = "";
					$listPaymentStatus = "";
					  if(!empty($paymentStatusListing)) 
					  {
						for($i=0; $i < $countPaymentStatus; $i++)
						{
							($payment_status[$i] . ",");
							if($listPaymentStatus == ""){
								$listPaymentStatus = "'" . $paymentStatusListing[$i] . "'";
								$sqlListPaymentStatus = " bills.payment_status = " . $paymentStatusListing[$i] ." ";
							}
							else {
								$listPaymentStatus .= ",'" . $paymentStatusListing[$i]. "'";
								$sqlListPaymentStatus .= " or bills.payment_status = " . $paymentStatusListing[$i] ." ";
							}
						}
					  }
				}
				
				 "LIST BLOCK: ".$listPaymentStatus;
				IF($sqlListPaymentStatus != ""){
					$sqlListPaymentStatus = " and ( $sqlListPaymentStatus ) ";
				}
				
				$_SESSION["payment_status"] = $payment_status;
				//payment_status - end

				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(bills.issue_date)  = $year_selected ";
						$sqlDateDeposit = "  YEAR(bill_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";
						$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;
						
						$date_monthYearDeposit = date("M Y",strtotime($lastdate));	
						
						$sqlDate = "  bills.issue_date  = '$date_monthYear' ";
						//$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 = " (bills.issue_date  >= '$dateFrom'  and bills.issue_date < '$date_cutOFF') ";
						$sqlDateDeposit = " (bill_deposits.created_at  >= '$dateFrom'  and bill_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 = " bills.created_at like '%$chooseDate%' ";
						$sqlDateDeposit = " bill_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");
				}
				
				$location_id = $_SESSION["location_id"];
				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) $sqlListBlock $sqlListPaymentStatus
									";
					$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" ){
					   
						 	$sqlRental = "select  bills.*, tenants.tenant_name, properties.unit_no,tenants.status, tenants.rental_rate,users.name, locations.location_name,
									projects.project_name, bill_types.bill_type_name
									from bills 
									
									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 bills.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) $sqlListBlock $sqlListPaymentStatus
									
									";
									
					$runRental= mysqli_query($link, $sqlRental);
					if($runRental=== FALSE){
						//die(mysqli_error("SQL DIE! Error for SQL: ".$sqlRental));
						die(mysqli_error($link));
					}
				}

?>