<?php
session_start();
ob_start();
$father = $_SESSION["user_id"];
if(!isset($_SESSION["user_id"])){header("location:../logout.php");}
	include "../actions/dbPDO.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 );
	$mmm = new DateTime($myday);
	$mms =  $mmm->format("m Y");
	$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="8%">م</th>
	 																<th class="text-center" width="16%">الطراز</th>
	 																<th class="text-center" width="16%">لوحة رقم</th>
	 																<th class="text-center" width="12%">موديل</th>
																	<th class="text-center" width="15%">المنصرف</th>
	 																<th class="text-center" width="33%">ملاحظات</th>
	 													 </tr>
	 												 </thead>
	 													 <tbody id="tstandby">
	 					<?php
	 							$output = '';

	 						$sql =$db->prepare("SELECT sum(item_line_out.tot_price) as price , ment_request.car_id , cars.kind , concat(cars.letters,' ',cars.numbers) as PL , cars.model FROM cars , ment_request , item_line_out
									WHERE item_line_out.req_id = ment_request.req_id AND ment_request.car_id = cars.carid
									AND item_line_out.branch = ?
									AND item_line_out.date_out between ? AND ?  GROUP BY ment_request.car_id 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 ;
	 																	while($row = $sql->fetch())
	 																		{
	 																				 $output .= '<tr>
																					 					<td>' . $ss . '</td>
	 																									 <td>' . $row['kind'] . '</td>
	 																									 <td>' . $row['PL'] . '</td>
	 																									 <td>' . $row['model'] . '</td>
																										 <td>' . $row['price'] . '</td>
	 																									 <td></td>
	 																								</tr>  '; $ss = $ss + 1 ; $money = $money + $row['price'] ;
	 																		}
																			$output .= '<tr><td colspan="6"></td></tr><tr>
	 																												<td colspan="4"> إجمالي </td>
		 																										 <td>'.$money.'</td>
	 																									 </tr>';
	 															 }
	 															 else
	 															 {
	 																		$output .= '<tr>
	 																												<td colspan="6" >لا يوجد سجلات ...</td>
	 																									 </tr>';
	 															 }
	 															 echo $output;
	 															 ?>
	 													 </tbody>
	 												 <tfoot></tfoot>
	 											 </table>
										</div>
							</div> <!-- end of table row -->
				</div><!-- main fluied container -->
    </body>
</html>
