<?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_water.payment_status = '" . $payment_status[$i] ."' ";
							}
							else {
								$listPaymentStatus .= "," . "'" . $payment_status[$i]."'";
								$listPaymentStatus_plain .= "," . $payment_status[$i];
								$sqlListPaymentStatus .= " or bills_water.payment_status = '" . $payment_status[$i] ."' ";
							}
						}
					}
					 " listBlock_plain " . $listBlock_plain;
					 " listPaymentStatus " . $listPaymentStatus;
					 " listPaymentStatus_plain " . $listPaymentStatus_plain;
					//$sqlListPaymentStatus = " bills_water.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_water.payment_status = " . $paymentStatusListing[$i] ." ";
							}
							else {
								$listPaymentStatus .= ",'" . $paymentStatusListing[$i]. "'";
								$sqlListPaymentStatus .= " or bills_water.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_water.issue_date) < '$statement_year' ";
						$cond_date_BF = " and payments.created_at < '$firstYearDate' ";
						$sqlDate = "  YEAR(bills_water.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_water.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_water.issue_date  >= '$dateFrom'  and bills_water.issue_date < '$date_cutOFF') ";
						$infoTarikhTunggakan = " DARI ".date("d/m/Y",strtotime($dateFrom)) . " SEHINGGA " . date("d/m/Y",strtotime($dateTo));
				}
				
				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"];
				
				
				echo $sqlWaterBills = "select  bills_water.*, tenants.tenant_name, properties.unit_no,tenants.status, tenants.rental_rate, locations.location_name,
								projects.project_name,bill_water_rate.id as waterrate_id, bill_water_rate.rate_amount, bill_water_rebet.id as waterrebet_id , bill_water_rebet.amount as rebetamount
								from bills_water
								inner join bill_water_rate on bills_water.bill_water_rate_id = bill_water_rate.id
								inner join bill_water_rebet on bills_water.rebet_id = bill_water_rebet.id 
								inner join tenants on bills_water.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_water.created_by =  users.id
								where locations.id = $location_id 
								and projects.id = $project_id and ($sqlDate) $sqlListBlock $sqlListPaymentStatus
								
								";
								
				$runWaterBills= mysqli_query($link, $sqlWaterBills);
				if($runWaterBills=== FALSE){
					die(mysqli_error($link));
				}

?>