<?php
session_start();
ob_start();
$father = $_SESSION["user_id"];
if(!isset($_SESSION["user_id"])){header("location:../logout.php");}
	include "../actions/dbPDO.php";
	include "../op/month_history.php";
	if(isset($_REQUEST["dd"])){ $myday = $_REQUEST["dd"];	$branch = $_REQUEST["br"]; }
	$first_day = date('Y-m-01', strtotime($myday));
	$last_day 	= date('Y-m-t', strtotime($myday));
	$mms  = DateTime::createFromFormat( 'm-Y', $myday );
	$ac = $db->prepare(" SELECT b_name FROM  branches WHERE branchID =  ? LIMIT 1 ");
				$ac->bindParam(1,$branch) ;
				$ac->execute();
			if($ac->rowCount()>0){$day = $ac->fetch(PDO::FETCH_ASSOC);
						 $b_name = $day['b_name'];}
						?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
      	<title>حركة السيارات داخل فرع</title>
         <link rel="stylesheet" href="../dist/css/bootstrap.css"/>
      	 <link rel="stylesheet" href="../dist/css/day.css"media="print"/>
				 <script src="../js/jquery.js"></script>
 				<script src="../dist/js/umd/popper.min.js"></script>
 			 <script src="../dist/js/bootstrap.js"></script>
	 </head>
    <body onload="javascript: window.print(); setTimeout(window.close, 1000);" >
			<div class="container-fluid">
				<div class="form-row">
					<div class="col-9">
						<h6 style="font-weight:bold;font-size:10pt;" > .حركة السيارات داخل فرع <?php echo $b_name." خلال شهر ".$mms ;?></h6>
					</div>
					<div class="col-3" style="float:left;">
							<p style="font-size:8pt;float:left;" ><?php echo date("Y-m-d"); ?></p>
				 </div>
			 </div>
			<div class="form-row">
				<div class="col">
	 			  <table class="table table-bordered">
	 				<thead>
	 					<tr>
							<th class="text-center" width="6%">م</th>
	 						<th class="text-center" width="14%">الطراز</th>
	 						<th class="text-center" width="14%">لوحة رقم</th>
	 						<th class="text-center" width="10%">موديل</th>
							<th class="text-center" width="12%">استعداد</th>
	 						<th class="text-center" width="10%">مؤجرة</th>
	 						<th class="text-center" width="10%">صيانة</th>
							<th class="text-center" width="12%">إجمالي</th>
	 						<th class="text-center" width="12%">إيرادات</th>
	 					</tr>
	 				</thead>
	 		<tbody>
	<?php
	 		$output = '';
	 		$sql =$db->prepare("SELECT DISTINCT cars.carid ,cars.kind , concat(cars.letters,' ' ,cars.numbers) as PL , cars.model , cars.numbers 
						from cars , actions where cars.carid = actions.carid AND actions.branch =  ? AND actions.ac_date between ? AND ?
						ORDER BY cars.kind , cars.numbers ");
			$sql->bindParam(1,$branch);
	 		$sql->bindParam(2,$first_day);
			$sql->bindParam(3,$last_day);
	 			$sql->execute();
	 			if($sql->rowCount()>0)
	 			{ $ss = 1 ; $money = 0 ; $tot_st = 0; $tot_ment = 0 ; $tot_rent = 0 ;
	 				while($row = $sql->fetch())
	 					{
						$carid = $row['carid'];
						$month =  month_history($carid,$branch,$first_day,$last_day) ;
	 					 $output .= '<tr>
										<td>' . $ss . '</td>
	 									<td>' . $row['kind'] . '</td>
	 									<td>' . $row['PL'] . '</td>
	 									<td>' . $row['model'] . '</td>
										<td>' . $month['standby'] . '</td>
	 									<td>' . $month['at_rent'] . '</td>
										<td>' . $month['ment'] . '</td>
										<td>' . $month['tot_at_branch'] . '</td>
										<td>' . round($month['money'],2) . '</td>
	 								</tr>  '; $ss = $ss + 1 ; $money = $money + $month['money'] ;
			$tot_st = $tot_st+$month['standby']; $tot_ment = $tot_ment+$month['ment'] ; $tot_rent = $tot_rent+$month['at_rent'] ;
	 							}
			$gt = $tot_st + $tot_ment + $tot_rent ;
			$persent = ($tot_rent / $gt) * 100 ;
			$persent = number_format((float)$persent, 3, '.', '');
			$output .= '<tr><td colspan="9"></td></tr><tr>
	 									        <td colspan="4"> نسبة التشغيل '.$persent.' %</td>
										        <td>' . $tot_st . '</td>
		 	 							        <td>' . $tot_rent . '</td>
		 								        <td>' . $tot_ment . '</td>
		 								        <td>' . $gt . '</td>
		 								        <td>'.$money.'</td>
	 						                </tr>';
	 			}
	 			else{
	 				$output .= '<tr><td colspan="9">لا يوجد سجلات ...</td></tr>';
	 			 }
	 				echo $output;
	 					?>
	 				</tbody>
	 			<tfoot></tfoot>
	 		</table>
			</div>
		</div> <!-- end of table row -->
	</div><!-- main fluied container -->
</body>
</html>
