<?php
	if(isset($_GET['staff'])){$getStaffId = $_GET['staff'];}
	if(isset($_GET['monthYear'])){
		$getMonthYear = $_GET['monthYear'];
		$getMonth = date("m",strtotime($getMonthYear));
		$getYear = date("Y",strtotime($getMonthYear));
	}
?>
<!-- page content -->
<div class="right_col" role="main">
  <div class="">
	<div class="page-title">
	  <div class="title_left">
		<h3><?php if(isset($getStaffId)){echo "Report for ".getDetailByTable('staffs',$getStaffId)['s_nickname'];} ?></h3>
	  </div>

	  <div class="title_right">
		<div class="col-md-5 col-sm-5 col-xs-12 form-group pull-right top_search">
		  <div class="input-group">
			<input type="text" class="form-control" placeholder="Search for...">
			<span class="input-group-btn">
			  <button class="btn btn-default" type="button">Go!</button>
			</span>
		  </div>
		</div>
	  </div>
	</div>

	<div class="clearfix"></div>

	<div class="row">
		
	  <div class="col-md-12 col-sm-12 col-xs-12">
		<div class="x_panel">
		  <div class="x_title">
			<h2>Total Monthly Job Report </h2>
			  
			<ul class="nav navbar-right panel_toolbox">
			  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
			  </li>
			  <li class="dropdown">
				<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
				<ul class="dropdown-menu" role="menu">
				  <li><a href="#">Settings 1</a>
				  </li>
				  <li><a href="#">Settings 2</a>
				  </li>
				</ul>
			  </li>
			  <li><a class="close-link"><i class="fa fa-close"></i></a>
			  </li>
			</ul>
			<div class="clearfix"></div>
		  </div>
		  <div class="x_content">
			<table class="table table-hover">
			  <thead>
				<tr>
				  <th>#</th>
				  <th>Month</th>
				  <th>Total</th>
				  <th>Utilities</th>
				  <th>Salaries</th>
				  <th>Expenses</th>
				  <th>Outbound</th>
				  <th>Profit</th>
				  <th>%</th>
				</tr>
			  </thead>
			  <tbody>
				  <?php $no = 0;
					$sql = "SELECT DATE_FORMAT(rv_ci_date, '%Y-%m') as Months from reservation WHERE rv_status > 1 GROUP BY Months ORDER BY Months DESC";
				  	$result = mysqli_query($conn,$sql);
				  	while($row = mysqli_fetch_assoc($result)){
                        // if(filterRoomAdmin($row['r_id'],$_SESSION['admin'][0])==true){continue;};
						
				  ?>
				<tr>
				  <th><?php echo ++$no; ?></th>
				  <td><a href="?pagecontent=overall_report&collection=<?php echo $row['Months']; ?>"><?php echo date("F Y",strtotime($row['Months'])); ?></a></td>
				  <td><a href="?pagecontent=overall_report&total=y&monthYear=<?php echo $row['Months']; ?>"><?php $totalCollection = totalCollection($row['Months'])['totalPaid']; echo rmFormat($totalCollection); ?></a></td>
				  <td><a href="?pagecontent=overall_report&utilities=y&monthYear=<?php echo $row['Months']; ?>"><?php $totalUtilitiesAll = totalUtilitiesAll($row['Months'])['total'];  echo rmFormat($totalUtilitiesAll); ?></a></td>
				  <td><a href="?pagecontent=overall_report&salary=y&monthYear=<?php echo $row['Months']; ?>"><?php $totalSalaryAll = totalSalaryAll($row['Months'])['total']; echo rmFormat($totalSalaryAll); ?></a></td>
				  <td><?php $expenses = totalExpensesAll($row['Months'])['total']; echo rmFormat($expenses); ?></td>
				  <td><?php $totalOutbound = $totalUtilitiesAll+$totalSalaryAll+$expenses; echo rmFormat($totalOutbound); ?></td>
				  <td><?php $totalProfit = $totalCollection - $totalOutbound; echo rmFormat($totalProfit); ?></td>
				  <td><?php echo ($totalCollection != 0) ? round(($totalProfit * 100) / $totalCollection, 2) : 0; ?></td>
				</tr>
				  <?php $totalCollection = 0; $totalUtilitiesAll = 0; $totalSalaryAll = 0; $expenses = 0; $totalCollection = 0; $totalOutbound = 0;
					} ?>
			  </tbody>
			</table>

		  </div>
		</div>
	  </div>
	  <div class="clearfix"></div>
	</div>

	  <?php if(isset($_GET['total'])){ ?>
	<div class="row">
		
	  <div class="col-md-12 col-sm-12 col-xs-12">
		<div class="x_panel">
		  <div class="x_title">
			<h2>Report Daily Profit per Room for <?php echo date("F Y",strtotime($getMonthYear)); ?></h2>
			  
			<ul class="nav navbar-right panel_toolbox">
			  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
			  </li>
			  <li class="dropdown">
				<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
				<ul class="dropdown-menu" role="menu">
				  <li><a href="#">Settings 1</a>
				  </li>
				  <li><a href="#">Settings 2</a>
				  </li>
				</ul>
			  </li>
			  <li><a class="close-link"><i class="fa fa-close"></i></a>
			  </li>
			</ul>
			<div class="clearfix"></div>
		  </div>
		  <div class="x_content">
			<table class="table table-hover">
			  <thead>
				<tr>
				  <th>#</th>
				  <th>Room</th>
				  <th>Total</th>
				  <th>Utilities</th>
				  <th>Salaries</th>
				  <th>Expenses</th>
				  <th>Outbound</th>
				  <th>Profit</th>
				  <th>%</th>
				</tr>
			  </thead>
			  <tbody>
				  <?php 
				  	$no = 0;
				  	$totalCollectionAll = 0;
				  	$totalExpenses = 0;
				  	$totalOutboundAll = 0;
				  	$totalProfitAll = 0;
					
					$sql = "SELECT *, DATE_FORMAT(RV.rv_ci_date, '%Y-%m') AS Months FROM reservation AS RV LEFT JOIN rooms AS R ON RV.r_id=R.r_id WHERE MONTH(RV.rv_ci_date)=$getMonth AND YEAR(RV.rv_ci_date)=$getYear AND RV.rv_status > 1 AND RV.r_id!=0 AND R.r_profit_type=1 GROUP BY RV.r_id ORDER BY R.p_id ASC, R.r_no ASC";
				  	$result = mysqli_query($conn,$sql);
				  	$numRows = mysqli_num_rows($result);
				  	while($row = mysqli_fetch_assoc($result)){
						
				  ?>
				<tr>
				  <th><?php echo ++$no; ?></th>
				  <td><a href="?pagecontent=report_profit_room&room=<?php echo $row['r_id']; ?>&month=<?php echo $row['Months']; ?>-01"><?php echo getDetailByTable('properties',$row['p_id'])['pty_code']; ?> <?php echo getDetailByTable('rooms',$row['r_id'])['r_no']; ?></a></td>
				  <td><?php $totalCollectionByRoom = totalCollection($row['Months'],$row['r_id'])['totalPaid']; echo rmFormat($totalCollectionByRoom); ?></td>
				  <td><?php $totalUtilitiesByRoom = totalUtilitiesAll($row['Months'],$row['r_id'])['total']; echo rmFormat($totalUtilitiesByRoom); ?></td>
				  <td><?php $totalSalaryByRoom = round((totalSalaryAll($row['Months'])['total']/totalActiveRooms($getMonthYear)),2); echo rmFormat($totalSalaryByRoom); ?></td>
				  <td><?php $expenses = totalExpensesAll($row['Months'],$row['r_id'])['total'];  echo rmFormat($expenses); ?></td>
				  <td><?php $totalOutboundByRoom = ($totalUtilitiesByRoom + $totalSalaryByRoom + $expenses); echo rmFormat($totalOutboundByRoom); ?></td>
				  <td><?php $totalProfitByRoom = $totalCollectionByRoom - $totalOutboundByRoom; echo rmFormat($totalProfitByRoom); ?></td>
				  <td><?php echo round(($totalProfitByRoom*100)/$totalCollectionByRoom,2); ?></td>
				</tr>
				  <?php $totalCollectionAll += $totalCollectionByRoom;
						$totalUtilitiesAll += $totalUtilitiesByRoom;
						$totalSalaryAll += $totalSalaryByRoom;
						$totalExpenses += $expenses;
						$totalOutboundAll += $totalOutboundByRoom;
						$totalProfitAll += $totalProfitByRoom;
						//$totalCollection = 0; $totalRentalAll = 0; $totalUtilitiesAll = 0; $totalSalaryAll = 0; $expenses = 0; $totalCollection = 0; $totalOutbound = 0;
					} ?>
			  </tbody>
			  <tfoot>
				<tr>
				  <th>#</th>
				  <th></th>
				  <th><?php echo rmFormat($totalCollectionAll); ?></th>
				  <th><?php echo rmFormat($totalUtilitiesAll); ?></th>
				  <th><?php echo rmFormat($totalSalaryAll); ?></th>
				  <th><?php echo rmFormat($totalExpenses); ?></th>
				  <th><?php echo rmFormat($totalOutboundAll); ?></th>
				  <th><?php echo rmFormat($totalProfitAll); ?></th>
				  <th><?php echo round(($totalProfitAll*100)/$totalCollectionAll,2); ?>%</th>
				</tr>
			  </tfoot>
			</table>

		  </div>
		</div>
	  </div>
	  <div class="clearfix"></div>
	</div>

	
	<div class="row">
		
	  <div class="col-md-12 col-sm-12 col-xs-12">
		<div class="x_panel">
		  <div class="x_title">
			<h2>Report Housekeeping per Room for <?php echo date("F Y",strtotime($getMonthYear)); ?></h2>
			  
			<ul class="nav navbar-right panel_toolbox">
			  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
			  </li>
			  <li class="dropdown">
				<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
				<ul class="dropdown-menu" role="menu">
				  <li><a href="#">Settings 1</a>
				  </li>
				  <li><a href="#">Settings 2</a>
				  </li>
				</ul>
			  </li>
			  <li><a class="close-link"><i class="fa fa-close"></i></a>
			  </li>
			</ul>
			<div class="clearfix"></div>
		  </div>
		  <div class="x_content">
			<table class="table table-hover">
			  <thead>
				<tr>
				  <th>#</th>
				  <th>Room</th>
				  <th>Total</th>
				  <th>Cleaning Fee</th>
				  <th>Salaries</th>
				  <th>Expenses</th>
				  <th>Outbound</th>
				  <th>Profit</th>
				  <th>%</th>
				</tr>
			  </thead>
			  <tbody>
				  <?php
				  $no = 0;
				  $totalCollectionAll = 0;
				  $totalExpenses = 0; 
				  $totalOutboundAll = 0; 
				  $totalOutboundAll = 0; 
				  $totalProfitAll = 0; 
				  $totalCleaningFee = 0; 
				  $totalSalaryAll = 0;
					
					$sql = "SELECT *, DATE_FORMAT(RV.rv_ci_date, '%Y-%m') AS Months FROM reservation AS RV LEFT JOIN rooms AS R ON RV.r_id=R.r_id WHERE MONTH(RV.rv_ci_date)=$getMonth AND YEAR(RV.rv_ci_date)=$getYear AND RV.rv_status > 1 AND RV.r_id!=0 AND R.r_profit_type=2 GROUP BY RV.r_id ORDER BY R.p_id ASC, R.r_no ASC";
				  	$result = mysqli_query($conn,$sql);
				  	$numRows = mysqli_num_rows($result);
				  	while($row = mysqli_fetch_assoc($result)){
						
				  ?>
				<tr>
				  <th><?php echo ++$no; ?></th>
				  <td><a href="?pagecontent=report_profit_room&room=<?php echo $row['r_id']; ?>&month=<?php echo $row['Months']; ?>-01"><?php echo getDetailByTable('properties',$row['p_id'])['pty_code']; ?> <?php echo getDetailByTable('rooms',$row['r_id'])['r_no']; ?></a></td>
				  <td><?php $totalCollectionByRoom = totalCollection($row['Months'],$row['r_id'])['totalPaid']; echo rmFormat($totalCollectionByRoom); ?></td>
				  <td><?php $cleaningFee = cleaningFeeAll($row['Months'],$row['r_id'])['total'];  echo rmFormat($cleaningFee); ?></td>
				  <td><?php echo $totalSalaryByRoom; ?></td>
				  <td><?php $expenses = totalExpensesAll($row['Months'],$row['r_id'])['total'];  echo rmFormat($expenses); ?></td>
				  <td><?php $totalOutboundByRoom = ($totalSalaryByRoom + $expenses); echo rmFormat($totalOutboundByRoom); ?></td>
				  <td><?php $totalProfitByRoom = $totalCollectionByRoom - $totalOutboundByRoom; echo rmFormat($totalProfitByRoom); ?></td>
				  <td><?php echo round(($totalProfitByRoom*100)/$totalCollectionByRoom,2); ?></td>
				</tr>
				  <?php $totalCollectionAll += $totalCollectionByRoom;
						$totalCleaningFee += $cleaningFee;
						$totalSalaryAll += $totalSalaryByRoom;
						$totalExpenses += $expenses;
						$totalOutboundAll += $totalOutboundByRoom;
						$totalProfitAll += $totalProfitByRoom;
						//$totalCollection = 0; $totalRentalAll = 0; $totalUtilitiesAll = 0; $totalSalaryAll = 0; $expenses = 0; $totalCollection = 0; $totalOutbound = 0;
					} ?>
			  </tbody>
			  <tfoot>
				<tr>
				  <th>#</th>
				  <th></th>
				  <th><?php echo rmFormat($totalCollectionAll); ?></th>
				  <th><?php echo rmFormat($totalCleaningFee); ?></th>
				  <th><?php echo rmFormat($totalSalaryAll); ?></th>
				  <th><?php echo rmFormat($totalExpenses); ?></th>
				  <th><?php echo rmFormat($totalOutboundAll); ?></th>
				  <th><?php echo rmFormat($totalProfitAll); ?></th>
				  <th><?php echo round(($totalProfitAll*100)/$totalCollectionAll,2); ?>%</th>
				</tr>
			  </tfoot>
			</table>

		  </div>
		</div>
	  </div>
	  <div class="clearfix"></div>
	</div>

	

	
	<div class="row">
		
	  <div class="col-md-12 col-sm-12 col-xs-12">
		<div class="x_panel">
		  <div class="x_title">
			<h2>Report Profit Sharing per Room for <?php echo date("F Y",strtotime($getMonthYear)); ?></h2>
			  
			<ul class="nav navbar-right panel_toolbox">
			  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
			  </li>
			  <li class="dropdown">
				<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
				<ul class="dropdown-menu" role="menu">
				  <li><a href="#">Settings 1</a>
				  </li>
				  <li><a href="#">Settings 2</a>
				  </li>
				</ul>
			  </li>
			  <li><a class="close-link"><i class="fa fa-close"></i></a>
			  </li>
			</ul>
			<div class="clearfix"></div>
		  </div>
		  <div class="x_content">
			<table class="table table-hover">
			  <thead>
				<tr>
				  <th>#</th>
				  <th>Room</th>
				  <th>Total</th>
				  <th>Utilities</th>
				  <th>Salaries</th>
				  <th>Expenses</th>
				  <th>Outbound</th>
				  <th>Profit</th>
				  <th>Owner</th>
				  <th>Host</th>
				  <th>%</th>
				</tr>
			  </thead>
			  <tbody>
				  <?php 
				  	$no = 0; 
					$totalCollectionAll = 0; 
					$totalUtilitiesAll = 0; 
					$totalSalaryByRoom = 0; 
					$totalSalaryAll = 0; 
					$totalExpenses = 0; 
					$totalOutboundAll = 0; 
					$totalProfitAll = 0; 
					$totalOwnerProfitAll = 0; 
					$totalHostProfitAll = 0; 
					
					$sql = "SELECT *, DATE_FORMAT(RV.rv_ci_date, '%Y-%m') AS Months FROM reservation AS RV LEFT JOIN rooms AS R ON RV.r_id=R.r_id WHERE MONTH(RV.rv_ci_date)=$getMonth AND YEAR(RV.rv_ci_date)=$getYear AND RV.rv_status > 1 AND RV.r_id!=0 AND R.r_profit_type=3 GROUP BY RV.r_id ORDER BY R.p_id ASC, R.r_no ASC";
				  	$result = mysqli_query($conn,$sql);
				  	while($row = mysqli_fetch_assoc($result)){
						$getHostProfitSharingPercent = $row['r_profit_sharing'];
						$getOwnerProfitSharingPercent = (1.0 - $row['r_profit_sharing']);
						
				  ?>
				<tr>
				  <th><?php echo ++$no; ?></th>
				  <td><a href="?pagecontent=report_profit_room&room=<?php echo $row['r_id']; ?>&month=<?php echo $row['Months']; ?>-01"><?php echo getDetailByTable('properties',$row['p_id'])['pty_code']; ?> <?php echo getDetailByTable('rooms',$row['r_id'])['r_no']; ?></a></td>
				  <td><?php $totalCollectionByRoom = totalCollection($row['Months'],$row['r_id'])['totalPaid']; echo rmFormat($totalCollectionByRoom); ?></td>
				  <td><?php $totalUtilitiesByRoom = totalUtilitiesAll($row['Months'],$row['r_id'])['total']; echo rmFormat($totalUtilitiesByRoom); ?></td>
				  <td><?php $totalSalaryByRoom = round((totalSalaryAll($row['Months'])['total']/totalActiveRooms($getMonthYear)),2); echo rmFormat($totalSalaryByRoom); ?></td>
				  <td><?php $expenses = totalExpensesAll($row['Months'],$row['r_id'])['total'];  echo rmFormat($expenses); ?></td>
				  <td><?php $totalOutboundByRoom = ($totalUtilitiesByRoom + $totalSalaryByRoom + $expenses); echo rmFormat($totalOutboundByRoom); ?></td>
				  <td><?php $totalProfitByRoom = $totalCollectionByRoom - $totalOutboundByRoom; echo rmFormat($totalProfitByRoom); ?></td>
				  <td><?php $totalOwnerProfit = $totalProfitByRoom * $getOwnerProfitSharingPercent; echo rmFormat($totalOwnerProfit); ?></td>
				  <td><?php $totalHostProfit = $totalProfitByRoom * $getHostProfitSharingPercent; echo rmFormat($totalHostProfit); ?></td>
				  <td><?php echo round(($totalProfitByRoom*100)/$totalCollectionByRoom,2); ?></td>
				</tr>
				  <?php $totalCollectionAll += $totalCollectionByRoom;
						$totalUtilitiesAll += $totalUtilitiesByRoom;
						$totalSalaryAll += $totalSalaryByRoom;
						$totalExpenses += $expenses;
						$totalOutboundAll += $totalOutboundByRoom;
						$totalOwnerProfitAll += $totalOwnerProfit;
						$totalHostProfitAll += $totalHostProfit;
						$totalProfitAll += $totalProfitByRoom;
						} ?>
			  </tbody>
			  <tfoot>
				<tr>
				  <th>#</th>
				  <th></th>
				  <th><?php echo rmFormat($totalCollectionAll); ?></th>
				  <th><?php echo rmFormat($totalUtilitiesAll); ?></th>
				  <th><?php echo rmFormat($totalSalaryAll); ?></th>
				  <th><?php echo rmFormat($totalExpenses); ?></th>
				  <th><?php echo rmFormat($totalOutboundAll); ?></th>
				  <th><?php echo rmFormat($totalProfitAll); ?></th>
				  <th><?php echo rmFormat($totalOwnerProfitAll); ?></th>
				  <th><?php echo rmFormat($totalHostProfitAll); ?></th>
				  <th><?php echo round(($totalProfitAll*100)/$totalCollectionAll,2); ?>%</th>
				</tr>
			  </tfoot>
			</table>

		  </div>
		</div>
	  </div>
	  <div class="clearfix"></div>
	</div>

	  <?php } ?>

	  <?php if(isset($_GET['utilities'])){ ?>
	<div class="row">
		
	  <div class="col-md-12 col-sm-12 col-xs-12">
		<div class="x_panel">
		  <div class="x_title">
			<h2>Utilities Report for <?php echo date("F Y",strtotime($getMonthYear)); ?></h2>
			  
			<ul class="nav navbar-right panel_toolbox">
			  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
			  </li>
			  <li class="dropdown">
				<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
				<ul class="dropdown-menu" role="menu">
				  <li><a href="#">Settings 1</a>
				  </li>
				  <li><a href="#">Settings 2</a>
				  </li>
				</ul>
			  </li>
			  <li><a class="close-link"><i class="fa fa-close"></i></a>
			  </li>
			</ul>
			<div class="clearfix"></div>
		  </div>
		  <div class="x_content">
			<table class="table table-hover">
			  <thead>
				<tr>
				  <th>#</th>
				  <th>Room</th>
				  <th>TNB</th>
				  <th>Water</th>
				  <th>Internet</th>
				  <th>Gas</th>
				  <th>Indah Water</th>
				  <th>Rental</th>
				  <th>Maintaince Fee</th>
				  <th>Netflix</th>
				  <th>Total</th>
				</tr>
			  </thead>
			  <tbody>
				  <?php 
				  	$no = 0;
					$totalTnb = 0;
					$totalWater = 0;
					$totalInternet = 0;
					$totalGas = 0;
					$totalIndahWater = 0;
					$totalRental = 0;
					$totalMaintaince = 0;
					$totalNetflix = 0;
					
					$sql = "SELECT *, DATE_FORMAT(RV.rv_ci_date, '%Y-%m') as Months, R.r_id as roomId from reservation AS RV LEFT JOIN rooms AS R ON RV.r_id=R.r_id WHERE MONTH(RV.rv_ci_date)=$getMonth AND YEAR(RV.rv_ci_date)=$getYear AND RV.rv_status > 1 AND RV.r_id!=0 GROUP BY RV.r_id ORDER BY R.p_id ASC, R.r_no ASC";
				  	$result = mysqli_query($conn,$sql);
				  	$numRows = mysqli_num_rows($result);
				  	while($row = mysqli_fetch_assoc($result)){
						
				  ?>
				<tr>
				  <th><?php echo ++$no; ?></th>
				  <td><a href="?pagecontent=housekeeping_report&month=<?php echo $row['Months']; ?>"><?php echo getDetailByTable('properties',$row['p_id'])['pty_code']; ?> <?php echo getDetailByTable('rooms',$row['r_id'])['r_no']; ?></a></td>
				  <td><?php $tnb = totalUtilitiesAll($row['Months'],$row['r_id'],1)['total']; echo rmFormat($tnb); ?></td>
				  <td><?php $water = totalUtilitiesAll($row['Months'],$row['r_id'],2)['total']; echo rmFormat($water); ?></td>
				  <td><?php $internet = totalUtilitiesAll($row['Months'],$row['r_id'],3)['total']; echo rmFormat($internet); ?></td>
				  <td><?php $gas = totalUtilitiesAll($row['Months'],$row['r_id'],4)['total']; echo rmFormat($gas); ?></td>
				  <td><?php $indahWater = totalUtilitiesAll($row['Months'],$row['r_id'],5)['total']; echo rmFormat($indahWater); ?></td>
				  <td><?php $rental = totalUtilitiesAll($row['Months'],$row['r_id'],6)['total']; echo rmFormat($rental); ?></td>
				  <td><?php $maintaince = totalUtilitiesAll($row['Months'],$row['r_id'],7)['total']; echo rmFormat($maintaince); ?></td>
				  <td><?php $netflix = totalUtilitiesAll($row['Months'],$row['r_id'],8)['total']; echo rmFormat($netflix); ?></td>
				  <td><?php $totalUtilitiesByRoom = totalUtilitiesAll($row['Months'],$row['r_id'])['total']; echo rmFormat($totalUtilitiesByRoom); ?></td>
				</tr>
				  <?php 
				  		$totalTnb += $tnb;
						$totalWater += $water;
						$totalInternet += $internet;
						$totalGas += $gas;
						$totalIndahWater += $indahWater;
						$totalRental += $rental;
						$totalMaintaince += $maintaince;
						$totalNetflix += $netflix;
						$totalUtilitiesAll += $totalUtilitiesByRoom;
					} ?>
			  </tbody>
			  <tfoot>
				<tr>
				  <th>#</th>
				  <th></th>
				  <th><?php echo rmFormat($totalTnb); ?></th>
				  <th><?php echo rmFormat($totalWater); ?></th>
				  <th><?php echo rmFormat($totalInternet); ?></th>
				  <th><?php echo rmFormat($totalGas); ?></th>
				  <th><?php echo rmFormat($totalIndahWater); ?></th>
				  <th><?php echo rmFormat($totalRental); ?></th>
				  <th><?php echo rmFormat($totalMaintaince); ?></th>
				  <th><?php echo rmFormat($totalNetflix); ?></th>
				  <th><?php echo rmFormat($totalUtilitiesAll); ?></th>
				</tr>
				<tr>
				  <th>#</th>
				  <th></th>
				  <th colspan="8" style="text-align: right">Total without Rental</th>
				  <th><?php echo rmFormat($totalUtilitiesAll-$totalRental); ?></th>
				</tr>
			  </tfoot>
			</table>

		  </div>
		</div>
	  </div>
	  <div class="clearfix"></div>
	</div>
	  <?php } ?>

	  <?php if(isset($_GET['salary'])){ ?>
	<div class="row">
		
	  <div class="col-md-12 col-sm-12 col-xs-12">
		<div class="x_panel">
		  <div class="x_title">
			<h2>Utilities Report for <?php echo date("F Y",strtotime($getMonthYear)); ?></h2>
			  
			<ul class="nav navbar-right panel_toolbox">
			  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
			  </li>
			  <li class="dropdown">
				<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
				<ul class="dropdown-menu" role="menu">
				  <li><a href="#">Settings 1</a>
				  </li>
				  <li><a href="#">Settings 2</a>
				  </li>
				</ul>
			  </li>
			  <li><a class="close-link"><i class="fa fa-close"></i></a>
			  </li>
			</ul>
			<div class="clearfix"></div>
		  </div>
		  <div class="x_content">
			<table class="table table-hover">
			  <thead>
				<tr>
				  <th>#</th>
				  <th>Staff</th>
				  <th>Salary</th>
				  <th>Total</th>
				</tr>
			  </thead>
			  <tbody>
				  <?php 
				  	$no = 0;
					$totalTnb = 0;
					$totalAll = 0;
					$totalWater = 0;
					
					$sql = "SELECT * FROM staff_salary AS SS LEFT JOIN staffs AS S ON SS.s_id=S.s_id WHERE MONTH(SS.ss_monthyear)=$getMonth AND YEAR(SS.ss_monthyear)=$getYear";
				  	$result = mysqli_query($conn,$sql);
				  	$numRows = mysqli_num_rows($result);
				  	while($row = mysqli_fetch_assoc($result)){
						
				  ?>
				<tr>
				  <th><?php echo ++$no; ?></th>
				  <td><?php echo $row['s_nickname']; ?></td>
				  <td><?php $salaryAmount = $row['ss_amount']; echo rmFormat($salaryAmount); ?></td>
				  <td><?php $total = $salaryAmount; echo rmFormat($total); ?></td>
				</tr>
				  <?php 
				  		$totalTnb += $salaryAmount;
						$totalAll += $total;
					}
					?>
			  </tbody>
			  <tfoot>
				<tr>
				  <th>#</th>
				  <th></th>
				  <th><?php echo rmFormat($totalWater); ?></th>
				  <th><?php echo rmFormat($totalAll); ?></th>
				</tr>
			  </tfoot>
			</table>

		  </div>
		</div>
	  </div>
	  <div class="clearfix"></div>
	</div>
	  <?php } ?>
	  
  </div>
</div>

<?php
			function totalCollection($date,$room = NULL){
				global $conn; $getMonth = date("m",strtotime($date)); $getYear = date("Y",strtotime($date));
				
				if(isset($room)){$sqlRoom = "AND r_id=$room";}else{$sqlRoom = "AND r_id!=0";}
				
				$result = mysqli_query($conn, "SELECT SUM(rv_total) AS totalPaid FROM reservation WHERE YEAR(rv_ci_date)=$getYear AND MONTH(rv_ci_date)=$getMonth AND rv_status > 1 $sqlRoom");
				
				return mysqli_fetch_assoc($result);
			}
		
			function totalRentalAll($date){
				global $conn; $getMonth = date("m",strtotime($date)); $getYear = date("Y",strtotime($date)); $total = 0;
				
				$result = mysqli_query($conn, "SELECT * FROM reservation AS RV LEFT JOIN rooms AS RM ON RV.r_id=RM.r_id WHERE YEAR(RV.rv_ci_date)=$getYear AND MONTH(RV.rv_ci_date)=$getMonth AND RV.rv_status > 1 GROUP BY RV.r_id");
				
				while($row = mysqli_fetch_assoc($result)){
					$total = $total + $row['r_rental'];
				};
				
				return $total;
			}
		
			function totalUtilitiesAll($date,$room = NULL,$type = NULL){
				global $conn; $getMonth = date("m",strtotime($date)); $getYear = date("Y",strtotime($date)); $total = 0;
				
				if(isset($room)){$sqlRoom = "AND r_id=$room";}else{$sqlRoom = "AND r_id!=0";}
				if(isset($type)){$sqlType = "AND u_type_id=$type";}else{$sqlType = "AND u_type_id!=0";}
				
				$result = mysqli_query($conn, "SELECT SUM(u_amount) AS total FROM utilities WHERE YEAR(u_date)=$getYear AND MONTH(u_date)=$getMonth AND u_type_id!=0 $sqlRoom $sqlType");

				return mysqli_fetch_assoc($result);
			}
		
			function totalSalaryAll($date){
				global $conn; $getMonth = date("m",strtotime($date)); $getYear = date("Y",strtotime($date)); $total = 0;
				
				$result = mysqli_query($conn, "SELECT SUM(ss_amount) AS total FROM staff_salary WHERE YEAR(ss_monthyear)=$getYear AND MONTH(ss_monthyear)=$getMonth");
				
				return mysqli_fetch_assoc($result);
			}
		
			function totalExpensesAll($date,$room = NULL){
				global $conn; $getMonth = date("m",strtotime($date)); $getYear = date("Y",strtotime($date)); $total = 0;
				
				if(isset($room)){$sqlRoom = "AND r_id=$room";}else{$sqlRoom = "";}
				
				$result = mysqli_query($conn, "SELECT SUM(re_amount) AS total FROM room_expenses WHERE YEAR(re_date)=$getYear AND MONTH(re_date)=$getMonth $sqlRoom AND re_initial=0");
				
				return mysqli_fetch_assoc($result);
			}
		
			function cleaningFeeAll($date,$room = NULL){
				global $conn; $getMonth = date("m",strtotime($date)); $getYear = date("Y",strtotime($date));
				
				
				if(isset($room)){$sqlRoom = "AND r_id=$room";}else{$sqlRoom = "AND r_id!=0";}
				
				$result = mysqli_query($conn, "SELECT SUM(rv_cleaning_fee) AS total FROM reservation WHERE YEAR(rv_ci_date)=$getYear AND MONTH(rv_ci_date)=$getMonth AND rv_status > 1 $sqlRoom");
				
				return mysqli_fetch_assoc($result);
			}
		
			function totalWorkedDays($staffId){
				global $conn;
				
				$result = mysqli_query($conn, "SELECT * FROM housekeep AS HK LEFT JOIN housekeep_staff AS HS on HK.hk_id=HS.hk_id WHERE HS.s_id=$staffId GROUP BY HK.hk_date");
				
				return mysqli_num_rows($result);
			}

			function totalActiveRooms($date){
				global $conn; $getMonth = date("m",strtotime($date)); $getYear = date("Y",strtotime($date));
				
				$result = mysqli_query($conn, "SELECT COUNT(r_id) AS total FROM reservation WHERE YEAR(rv_ci_date)=$getYear AND MONTH(rv_ci_date)=$getMonth AND rv_status > 1 AND r_id!=0 GROUP BY r_id");
				
				return mysqli_num_rows($result);
			}
		
		?>
<!-- /page content -->
