<?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["start"])) {
		$first_day 	= $_REQUEST["start"];
		$last_day 	= $_REQUEST["end"];
	} else {
		$myday = date("Y-m-d");
		$first_day = date('Y-m-01', strtotime($myday));
		$last_day 	= date('Y-m-t', strtotime($myday));
	}
	$date1 = new DateTime($first_day);
	$date2 = new DateTime($last_day);
	$days_qty   = $date2->diff($date1)->format("%a")+1;
						?>
<!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="javscript:window.close( window.print() )" >
			<div class="container-fluid">
				<div class="form-row">
					<div class="col-9">
						<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="form-row">
	<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="15%">الطراز</th>
	 				<th class="align-middle text-center" width="15%">لوحة رقم</th>
	 				<th class="align-middle text-center" width="8%">موديل</th>
					<th class="align-middle text-center" width="8%">القسط</th>
					<th class="align-middle text-center" width="8%">مؤجرة</th>
					<th class="align-middle text-center" width="8%">إستعداد</th>
					<th class="align-middle text-center" width="8%">صيانة</th>
	 				<th class="align-middle text-center" width="8%">إجمالي</th>
					<th class="align-middle text-center" width="8%">إيرادات</th>
	 				<th class="align-middle text-center" width="8%">صافي</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
							from cars , actions where cars.carid = actions.carid AND actions.ac_date between ? AND ?
							ORDER BY cars.kind , cars.numbers ");
	 									$sql->bindParam(1,$first_day);
										$sql->bindParam(2,$last_day);
	 									$sql->execute();
	 									if($sql->rowCount()>0)
	 							    { $ss = 1 ; $tot_money = 0 ; $tot_kest = 0 ; $tot_net = 0 ;
											$tot_rent = 0; $tot_standy = 0 ; $tot_ment = 0; $mg_tot = 0;
	 						while($row = $sql->fetch())
	 									{
											$carid = $row['carid']; $branch = 0 ;
										  $month =  month_history($carid,$branch,$first_day,$last_day) ;
											$kest = $row['kest'] ;$tot = $month['tot_at_branch'] ; $money = $month['money'] ;
											$standby = $month["standby"] ; $at_rent = $month["at_rent"] ; $ment = $month["ment"] ;
											$net = $money - $kest ;
	 					$output .= '<tr>
												  <td>' . $ss . '</td>
	 												<td>' . $row['kind'] . '</td>
	 												<td>' . $row['PL'] . '</td>
	 												<td>' . $row['model'] . '</td>
													<td>' . $kest . '</td>
	 												<td>' . $at_rent. '</td>
													<td>' . $standby. '</td>
													<td>' . $ment. '</td>
													<td>' . $tot. '</td>
													<td>' . $money . '</td>
													<td>' . $net. '</td>
	 											</tr>  '; $ss = $ss + 1 ;
	$tot_rent = $tot_rent + $at_rent; $tot_standy =$tot_standy + $standby; $tot_ment =$tot_ment + $ment ;
									$mg_tot = $mg_tot + $tot ;
										$tot_money = $tot_money + $money ;
										 $tot_kest = $tot_kest + $kest ;
											$tot_net = $tot_net + $net ;
	 																		}
		$persent = ($tot_rent /$mg_tot) * 100 ;
		$persent = number_format((float)$persent, 3, '.', '');
									$output .= '<tr><td colspan="11"></td></tr><tr>
	 															<td colspan="4" style="font-size:12px;"> نسبة التشغيل '.$persent.' %</td>
																<td>' . $tot_kest . '</td>
																<td>' . $tot_rent. '</td>
																<td>' . $tot_standy. '</td>
																<td>' . $tot_ment. '</td>
																<td>' . $mg_tot. '</td>
																<td>' . $tot_money . '</td>
																<td>' . $tot_net. '</td>
	 														</tr>';
	 													}
	 										 else
	 												 {
	 									$output .= '<tr>
	 																<td colspan="11">لا يوجد سجلات ...</td>
	 															</tr>';
	 															 }
		$sql2 =$db->prepare("SELECT DISTINCT carid ,kind , concat( letters,' ' ,numbers) as PL , model , kest
					 from cars where carid not in( SELECT carid from actions WHERE ac_date between ? AND ? )
					 AND carid in( SELECT carid FROM sanad_in WHERE sanad_date BETWEEN ? AND ?  )
					 AND carid in( SELECT carid FROM sanad_out WHERE sanad_out BETWEEN ? AND ?  )
					 ORDER BY kind , numbers ");
									$sql2->bindParam(1,$first_day);
									$sql2->bindParam(2,$last_day);
								  $sql2->bindParam(3,$first_day);
									$sql2->bindParam(4,$last_day);
									$sql2->bindParam(5,$first_day);
									$sql2->bindParam(6,$last_day);
									$sql2->execute();
							 if($sql2->rowCount()>0)
							  {
						while($row = $sql2->fetch())
							 {
								$carid = $row['carid']; $branch = 0;
								$month =  month_history($carid,$branch,$first_day,$last_day) ;
							$kest = 0 ;$tot = 0 ; $money = $month['money'] ;
							$net = $money - $kest ;
							$output .= '<tr>
														<td>' . $ss . '</td>
														<td>' . $row['kind'] . '</td>
														<td>' . $row['PL'] . '</td>
														<td>' . $row['model'] . '</td>
														<td>' . $kest . '</td>
														<td>0</td>
														<td>0</td>
														<td>0</td>
														<td>0</td>
														<td>' . $money . '</td>
														<td>' . $net. '</td>
													</tr>  '; $ss = $ss + 1 ;
											$tot_money = $tot_money + $money ;
											$tot_net = $tot_net + $net ;
								}
							$output .= '<tr>
									 	 				<td colspan="9" style="font-size:12px;" class="text-left"> إجمالي الإيرادات بعد سداد المديونيات </td>
									 					<td>' . $tot_money . '</td>
														<td>' . $tot_net . '</td>
									 	 			</tr>';
							}
						else
							{
								$output .= '<tr>
									 	 				 <td colspan="11"> لا توجد سداد مديونيات...</td>
									 	 				</tr>';
							}
	 			echo $output;
	 															 ?>
	 													 </tbody>
	 												 <tfoot></tfoot>
	 											 </table>
										</div>
							</div> <!-- end of table row -->
				</div><!-- main fluied container -->
    </body>
</html>
