<?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
			 "location_id:". $_SESSION["location_id"];
			$sqlLocationSelected = "";
			$sqlLocationSelected_forOccupancy = "";
			if($_SESSION["projectLocation"] == ""){
				$total_invoice = 0;
				$total_payment = 0;
				$percent_tenanted = 0;
				$total_outstanding = 0;
			}
			else if($_SESSION["location_id"] != ""){
				//echo "LOCATION ID: ".$_SESSION["location_id"];
				$sqlLocationSelected = " and properties.location_id =  " .$_SESSION["location_id"];
				$sqlLocationSelected_forOccupancy = " where properties.location_id = " .$_SESSION["location_id"];
				//Invoice (Current Month - Rental)
				//total_invoice start
				//get current month
				$currentDate = date("Y-m-01");
				//search total invoice out for current month - tenant active only
				 $sqlTotalInvoice = "select sum(bill_amount) as total_invoice 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 tenants.status = 'enable' and bills.issue_date = '$currentDate' and bills.bill_type_id = 1 $sqlLocationSelected
									";
				$runTotalInvoice = mysqli_query($link, $sqlTotalInvoice);//or die ("Cannot execute program");

				if($runTotalInvoice === FALSE){
					die(mysqli_error($link));
				}
				$resultTotalInvoice = mysqli_fetch_assoc($runTotalInvoice);
				$total_invoice = $resultTotalInvoice["total_invoice"];
				//total invoice end
				
				//total payment start
				//search total payment receive for current month - knock off with invoice
				 $sqlTotalPayment = "select sum(amount_paid) as total_payment from payments 
									inner join bills on payments.bill_id = bills.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
									where bills.issue_date = '$currentDate' and bills.bill_type_id = 1 $sqlLocationSelected
									";
				$runTotalPayment = mysqli_query($link, $sqlTotalPayment);//or die ("Cannot execute program");

				if($runTotalPayment === FALSE){
					die(mysqli_error($link));
				}
				$resultTotalPayment = mysqli_fetch_assoc($runTotalPayment);
				$total_payment = $resultTotalPayment["total_payment"];
				//total payment end
				
				//occupancy start
				//tenanted = ada penyewa
				 $sqlOccupancy_tenanted = "select count(properties.id) as tenant_active, 
									count(properties.id) * 100 / (select count(*) from properties
															inner join locations on properties.location_id = locations.id
															inner join projects on locations.project_id = projects.id  $sqlLocationSelected_forOccupancy) 
									as percent_tenanted from properties
									inner join locations on properties.location_id = locations.id
									inner join projects on locations.project_id = projects.id
									where properties.status = 'disable' $sqlLocationSelected ";
				$runOccupancy_tenanted  = mysqli_query($link, $sqlOccupancy_tenanted);//or die ("Cannot execute program");

				if($runOccupancy_tenanted === FALSE){
					die(mysqli_error($link));
				}
				$resultOccupancy_tenanted = mysqli_fetch_assoc($runOccupancy_tenanted);
				$tenant_active = $resultOccupancy_tenanted["tenant_active"];
				$percent_tenanted = $resultOccupancy_tenanted["percent_tenanted"];
				//tenanted = ada penyewa end
				
				//total outstanding all start
				//get current month
				$currentDate = date("Y-m-01");
				//search total invoice out for current month - tenant active only
				 $sqlTotalOutstanding = "select sum(amount_due) as total_outstanding 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.issue_date < '$currentDate' and bills.bill_type_id = 1  $sqlLocationSelected
									";
				$runTotalOustanding = mysqli_query($link, $sqlTotalOutstanding);//or die ("Cannot execute program");

				if($runTotalOustanding === FALSE){
					die(mysqli_error($link));
				}
				$resultTotalOutstanding = mysqli_fetch_assoc($runTotalOustanding);
				$total_outstanding = $resultTotalOutstanding["total_outstanding"];
				//total outstanding end
				
			}
			?>
			
            
            <!--main content upper start-->
			<?php include "main-content-upper.php"?>
            <!--main content upper end-->
			<!--main content middle start-->
			<div class="row m-t-25">
				<div class="col-sm-6 col-lg-3">
					<div class="overview-item overview-item--c1">
						<div class="overview__inner">
							<div class="overview-box clearfix">
								<div class="icon">
									<i class="zmdi zmdi-calendar-note"></i>
								</div>
								<div class="text">
									<h2><?php echo 'RM'.number_format($total_invoice,2,'.',',') ?></h2>
									<span>Total Invoice (Current)</span>
								</div>
							</div>
							<div class="overview-chart">
								<canvas id="widgetChart1"></canvas>
							</div>
						</div>
					</div>
				</div>
				<div class="col-sm-6 col-lg-3">
					<div class="overview-item overview-item--c2">
						<div class="overview__inner">
							<div class="overview-box clearfix">
								<div class="icon">
									<i class="zmdi zmdi-money"></i>
								</div>
								<div class="text">
									<h2><?php echo 'RM'.number_format($total_payment,2,'.',',') ?></h2>
									<span>Total Payment (Current)</span>
								</div>
							</div>
							<div class="overview-chart">
								<canvas id="widgetChart2"></canvas>
							</div>
						</div>
					</div>
				</div>
				<div class="col-sm-6 col-lg-3">
					<div class="overview-item overview-item--c3">
						<div class="overview__inner">
							<div class="overview-box clearfix">
								<div class="icon">
									<i class="zmdi zmdi-city"></i>
								</div>
								<div class="text">
									<h2><?php echo number_format($percent_tenanted,2)?>%</h2>
									<span>Occupancy</span>
								</div>
							</div>
							<div class="overview-chart">
								<canvas id="widgetChart3"></canvas>
							</div>
						</div>
					</div>
				</div>
				<div class="col-sm-6 col-lg-3">
					<div class="overview-item overview-item--c4">
						<div class="overview__inner">
							<div class="overview-box clearfix">
								<div class="icon">
									<i class="zmdi zmdi-alert-circle-o"></i>
								</div>
								<div class="text">
									<h2><?php echo 'RM'.number_format($total_outstanding,2,'.',',')?></h2>
									<span>Total Outstanding</span>
								</div>
							</div>
							<div class="overview-chart">
								<canvas id="widgetChart4"></canvas>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-lg-6">
					<div class="au-card recent-report">
						<div class="au-card-inner">
							<h3 class="title-2">recent reports</h3>
							<div class="chart-info">
								<div class="chart-info__left">
									<div class="chart-note">
										<span class="dot dot--blue"></span>
										<span>products</span>
									</div>
									<div class="chart-note mr-0">
										<span class="dot dot--green"></span>
										<span>services</span>
									</div>
								</div>
								<div class="chart-info__right">
									<div class="chart-statis">
										<span class="index incre">
											<i class="zmdi zmdi-long-arrow-up"></i>25%</span>
										<span class="label">products</span>
									</div>
									<div class="chart-statis mr-0">
										<span class="index decre">
											<i class="zmdi zmdi-long-arrow-down"></i>10%</span>
										<span class="label">services</span>
									</div>
								</div>
							</div>
							<div class="recent-report__chart">
								<canvas id="recent-rep-chart"></canvas>
							</div>
						</div>
					</div>
				</div>
				<div class="col-lg-6">
					<div class="au-card chart-percent-card">
						<div class="au-card-inner">
							<h3 class="title-2 tm-b-5">char by %</h3>
							<div class="row no-gutters">
								<div class="col-xl-6">
									<div class="chart-note-wrap">
										<div class="chart-note mr-0 d-block">
											<span class="dot dot--blue"></span>
											<span>products</span>
										</div>
										<div class="chart-note mr-0 d-block">
											<span class="dot dot--red"></span>
											<span>services</span>
										</div>
									</div>
								</div>
								<div class="col-xl-6">
									<div class="percent-chart">
										<canvas id="percent-chart"></canvas>
									</div>
								</div>
							</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-->
