<?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";

				$start = $_REQUEST["start"];
				$end   = $_REQUEST["end"];
				$myday = date("Y-m-d");
	$mmm = new DateTime($myday);
	$mms =  $mmm->format("m Y");
						?>
<!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="../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="row" style="padding:0 ;">
					<div class="col-9"style="float:right;">
						<p style="font-weight:bold;font-size:10pt;" > سيارات خدمة من   <?php echo $start." إلي ".$end ;?></p>
					</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="row" style="padding:0 ;">
					<div class="col">
	 				    <table class="table table-bordered">
	 						<thead>
	 								<tr>
										<th class="align-middle text-center" width="6%">م</th>
	 									<th class="align-middle text-center" width="14%">الطراز</th>
	 									<th class="align-middle text-center" width="12%">لوحة رقم</th>
	 									<th class="align-middle text-center" width="8%">موديل</th>
										<th class="align-middle text-center" width="12%">قسط السيارة </th>
	 									<th class="align-middle text-center" width="10%">ايام الخدمة</th>
	 									<th class="align-middle text-center" width="10%">نسبة القسط</th>
										<th class="align-middle text-center" width="28%">ملاحظات</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.kest ,cars.numbers , cars.target
				from cars , actions where cars.carid = actions.carid AND actions.carstatus =  5 AND actions.ac_date between ? AND ?
				ORDER BY cars.kind , cars.numbers ");
	 		$sql->bindParam(1,$start);
			$sql->bindParam(2,$end);
	 			$sql->execute();
	 			if($sql->rowCount()>0)
	 		 { $ss = 1 ; $tot_service_money = 0 ; $tot_net = 0 ;
	 				while($row = $sql->fetch())
	 					{
							$carid = $row['carid']; $target = 0 ;
							$service =  qrtService($carid,$start,$end) ;
							$kest = $row['kest'] ;
							$target = $row['target'];
							$service_money = intval(($kest * $service)/30.44 ) ; 
					// if($service_money > $target){$service_money = $tot_target ;}
	 						$output .= '<tr>
											<td>' . $ss . '</td>
	 										<td>' . $row['kind'] . '</td>
	 										<td>' . $row['PL'] . '</td>
	 										<td>' . $row['model'] . '</td>
											<td>' . $kest . '</td>
	 										<td>' . $service. '</td>
											<td>' .$service_money. '</td>
											<td ></td>
	 									</tr>  '; 
										$ss = $ss + 1 ;
								$tot_service_money = $tot_service_money + $service_money ;
	 						}
					$output .= '<tr><td colspan="6" style="font-size:12px;" class="text-left">إجمالي إيرادات الفرع</td>
									<td>' . $tot_service_money . '</td>
									<td></td>
	 						</tr>';
	 				}
	 				else
	 				{
	 					$output .= '<tr><td colspan="8">لا يوجد سجلات ...</td></tr>';
	 				 }
	 	 echo $output;
	 	 ?>
	 	</tbody>
	<tfoot></tfoot>
	</table>
	</div>
	</div> <!-- end of table row -->
	</div><!-- main fluied container -->
    </body>
</html>
