<?php
	require_once __DIR__ . '/../magic/auth.php';

$auth = bdr_require_privilege(30, '../logout.php');
$user = $auth['user_id'];
$father = $auth['user_id'];
$branch = $auth['branch'];
$conn = $auth['conn'];
$db = $auth['conn'];

include "../op/co_wanted.php";
if(isset($_REQUEST["dd"])){ $myday = $_REQUEST["dd"];	}
				else{$myday = date("Y-m-d"); }
				$myday = date('Y-m-01', strtotime($myday));
				$first_day = date('Y-m-01',  strtotime($myday. ' - 2 month'));
				$last_day 	= date('Y-m-t', strtotime($myday));
				// $mms  = DateTime::createFromFormat( 'm-Y', $myday );
				$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.min.css"/>
      	 <link rel="stylesheet" href="../dist/css/day.css" />
		 <script src="../js/jquery.js"></script>
		 <script src="../dist/js/umd/popper.min.js"></script>
		<script src="../dist/js/bootstrap.min.js"></script>
	 </head>
	 <!-- onLoad="javscript:window.close( window.print() )" -->
    <body >
			<div class="container-fluid">
			<div class="row" style="padding:5 ;">
				<div class="col-xs-10"style="float:right;">
					<h6 style="font-weight:bold;padding:3px;font-size:11pt;"> حركة الكيلومتر لجميع السيارات خلال ثلاثة اشهر تنتهي في <?php echo $mms ; ?> </h6>
				</div>
				<div class="col-xs-2"style="float:right;">
				</div>
			</div>
				<div class="row" style="padding:2 ;">
				<div class="col-xs-12"style="float:right;">
			<?php
					$dsd =$db->prepare("SELECT carid , kind , model ,concat(letters,' ' ,numbers) as PL , color
					from cars WHERE carid in (SELECT DISTINCT carid from contracts where date_out <= ? AND date_in IS NULL OR date_in >= ? ) ORDER BY kind , model , numbers ");
					$dsd->bindParam(1,$last_day);
					$dsd->bindParam(2,$first_day);
					$dsd->execute();
				 if($dsd->rowCount()>0)
							{
							$master = 0 ; $qq = 1 ;
		 while($row = $dsd->fetch())
			{
				$carid 	= $row["carid"];
				$kind 		= $row["kind"];
				$model 	= $row["model"];
				$PL 			= $row["PL"];
				$color 	= $row["color"];
					echo "<h6 style='font-weight:bold;padding:3px;font-size:10pt;' >".$qq."~ ".$kind." لوحة رقم " .$PL. " اللون " .$color." " .$model."</h6>"   ;
				 ?>
		</div>
			</div>
			 	<div class="table-responsive">
			 		<table class="table table-bordered">
			 			<thead>
			 				<tr>
								<th class="align-middle text-center" width="3%"> م </th>
			 					<th class="align-middle text-center" width="9%">العقد </th>
			 					<th class="align-middle text-center" width="36%">اسم المستأجر </th>
			 					<th class="align-middle text-center" width="7%">أيام</th>
			 					<th class="align-middle text-center" width="7%">الكيلو المستخدم</th>
			 					<th class="align-middle text-center" width="6%">متوسط يومي</th>
								<th class="align-middle text-center" width="6%">كم زائد</th>
								<th class="align-middle text-center" width="6%">تكلفة كم</th>
			 					<th class="align-middle text-center" width="5%">حركة</th>
			 					<th class="align-middle text-center" width="7%">أيام وقوف</th>
			 					<th class="align-middle text-center" width="8%">الفرع</th>
			 				</tr>
			 			</thead>
			 		<tbody >
<?php
		$output = '';
		$sam = 0 ; $sr = 1 ; $km = 0 ; $date1 = "";
		$joker =$db->prepare("SELECT contracts.contract_id , contracts.km_out , contracts.date_out , contracts.date_in , contracts.km_in ,contracts.cust_id ,contracts.carid , customers.cust_name , branches.b_name
							  FROM contracts , customers , branches
							 WHERE contracts.cust_id = customers.cust_id AND contracts.branch = branches.branchID AND contracts.carid = ? AND contracts.date_out <= ? AND
							(contracts.date_in IS NULL OR  contracts.date_in >= ? ) order by contracts.date_out");
		$joker->bindParam(1,$carid);
		 $joker->bindParam(2,$last_day);
		$joker->bindParam(3,$first_day);
		$joker->execute();
	if($joker->rowCount()>0)
				{
			while($bo = $joker->fetch())
					{
						if($km > 0){$moved = $bo['km_out'] - $km ;}else{$moved = 0 ;}
						$km = $bo['km_in'] ;
						if(!empty($date1)){
							$date2 = new DateTime($bo['date_out']);
							$st_days = $date2->diff($date1)->format("%a");
						}
							else{$st_days = 0 ;}
						$date1 = new DateTime($bo['date_in']);
						$ss = $bo['contract_id'];
						 $wwc =   wanted($ss);
						 $dd = $wwc["days_qty"] ; $kk = $wwc["used_km"] ;
						 if($dd > 0){$avg = intval($kk / $dd) ;}else{$avg = $kk ;}
																		//  $cash = contract_month($ss,$first_day , $last_day);
		$output .= '<tr>
						<td>' . $sr . '</td>
						<td>' . $ss . '</td>
						<td>' . $bo['cust_name'] . '</td>
						<td>' .$dd . '</td>
						<td>' .$kk . '</td>
						<td>' . $avg . '</td>
						<td>' . $wwc['kilo_qty'] . '</td>
						<td>' . $wwc['kilos_cost'] . '</td>
						<td>' . $moved . '</td>
						<td>' .$st_days. ' </td>
						<td>' . $bo['b_name'] . '</td>
					</tr>  ';
			 $sam =  $sam + $wwc['kilos_cost'] ;
				$sr = $sr + 1;
		}
		$output .= '<tr>
						<td colspan="2">' . $sam. '</td>
						<td>إجمالي</td>
						</tr>';
					 }
			else {	$output .= '<tr><td colspan="11">لا يوجد سجلات ...</td></tr>';	 }
			echo $output; ?>
			</tbody>
			 <tfoot></tfoot>
		 </table>
			 	</div>
				<?php
				$master = $master + $sam; $qq = $qq +1 ;
				}
		echo "<h6 style='font-weight:bold;padding:3px;font-size:10pt;'> $master إجمالي إيرادات الكيلومتر </h6>";}
	?>
</div><!-- نهاية المطاف -->
					<script>
					$(document).ready(function(){

				});
	</script>
    </body>
</html>
