<?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["start"])){
				$first_day 	= $_REQUEST["start"];
				$last_day 	= $_REQUEST["end"];
				// $carid 		= $_REQUEST["carid"];
			}
						?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
      	<title><?php echo " ايرادات من ".$first_day." حتي ".$last_day ; ?></title>
         <link rel="stylesheet" href="../dist/css/bootstrap.css"/>
      	 <link rel="stylesheet" href="../dist/css/day.css"media="print"/>
	 </head>
	 <!-- onload="javascript: window.print(); setTimeout(window.close, 1500);" -->
    <body >
			<div class="container-fluid">
				<div class="form-row">
					<div class="col-9">
						<h6 style="font-weight:bold;font-size:10pt;" >إيرادات سيارة<?php echo " من ".$first_day." إلي ".$last_day ;?></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="5%"> م </th>
											<th class="text-center" width="15%">طراز</th>
											<th class="text-center" width="10%">لوحة رقم</th>
											<th class="text-center" width="8%">موديل</th>
	 										<th class="text-center" width="10%">إيرادات</th>
	 										<th class="text-center" width="10%">مصروفات</th>
											<th class="text-center" width="10%">صافي ايراد</th>
											<th class="text-center" width="10%">أقساط مسددة</th>
											<th class="text-center" width="10%">أقساط متبقة</th>
											<th class="text-center" width="12%">صافي السيارة</th>
	 									</tr>
	 								</thead>
	 							<tbody>
<?php
	 		$output = ''; $nn = 1; $inn = 0; $outt = 0; $ment = 0; $net = 0;
						 $mycar = $db->prepare("SELECT carid, concat(letters,' ',numbers) as PL,kind,model from cars WHERE branch > 1
						 			ORDER BY model , kind ");
						 $mycar->execute();
					 if($mycar->rowCount()>0)

					while ($row = $mycar->fetch()) {
						$carid  = $row['carid'];
						$PL  	= $row['PL'];
						$kind  	= $row['kind'];
						$model  = $row['model'];

						$tot_in = $db->prepare("SELECT sum(money_in) as inn from car_in WHERE carid = ? AND sanad_date between ? AND ? ");
								$tot_in->bindParam(1,$carid);
								$tot_in->bindParam(2,$first_day);
								$tot_in->bindParam(3,$last_day);
								$tot_in->execute();
						if($tot_in->rowCount()>0){$toty_in = $tot_in->fetch(PDO::FETCH_ASSOC);
								$total_in = $toty_in["inn"];
									} else { $total_in = 0 ;}
								$tot_out = $db->prepare("SELECT sum(money_out) as outt from car_out WHERE carid = ? AND sanad_out between ? AND ? ");
								$tot_out->bindParam(1,$carid);
								$tot_out->bindParam(2,$first_day);
								$tot_out->bindParam(3,$last_day);
								$tot_out->execute();
						if($tot_out->rowCount()>0){$toty_out = $tot_out->fetch(PDO::FETCH_ASSOC);
								$total_out = $toty_out["outt"];
							} else { $total_out = 0 ;}
							$tot_ment = $db->prepare("SELECT sum(tot_price) as ment from car_ment_v WHERE carid = ? AND date_out between ? AND ? ");
							$tot_ment->bindParam(1,$carid);
							$tot_ment->bindParam(2,$first_day);
							$tot_ment->bindParam(3,$last_day);
							$tot_ment->execute();
						if($tot_ment->rowCount()>0){$toty_ment = $tot_ment->fetch(PDO::FETCH_ASSOC);
							$total_ment = $toty_ment["ment"];
							// $net = round((($net/115)*100),2) ;
						} else { $total_ment = 0 ;}
							 $net = $total_in - $total_out ;
							 $net = round((($net/115)*100),2) ;
							 $nader = $net - $total_ment ;
	 					$output .= '<tr>
										<td>' . $nn . '</td>
	 									<td>' . $kind . '</td>
										<td>' . $PL . '</td>
										<td>' . $model . '</td>
	 									<td>' . $net . '</td>
	 									<td>' . $total_ment. '</td>
										<td>' . $nader. '</td>
	 									<td></td>
										<td></td>
										<td></td>
	 								</tr>  ';
									$nn++ ;
					}
	 					echo $output;
	 			?>
	 	</tbody>
	</table>
	</div>
	</div> <!-- end of table row -->
	</div><!-- main fluied container -->
	<script src="../js/jquery.js"></script>
	<script src="../dist/js/umd/popper.min.js"></script>
	<script src="../dist/js/bootstrap.js"></script>
    </body>
</html>
