<?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/wanted.php";
if(isset($_REQUEST["dd"])){ $myday = $_REQUEST["dd"];	}
				else{$myday = date("Y-m-d"); }
				$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");
				?>
<!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" />
	 </head>
	 <!-- onload="javascript: window.print(); setTimeout(window.close, 1000);" -->
    <body >
			<div class="container-fluid">
			<div class="row" style="padding:5 ;">
				<div class="col-9"style="float:right;">
					<h6 style="font-weight:bold;padding:3px;font-size:10pt;">تقفيلة شهر <?php echo $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="row">
								<div class="col">

<?php
$dsd = $db->prepare("SELECT carid , kind , model ,concat(letters,' ' ,numbers) as PL , color from cars
		WHERE carid in(SELECT DISTINCT carid from car_in where sanad_date BETWEEN ? AND ? )
		OR carid in (SELECT DISTINCT carid from car_out where sanad_out BETWEEN ? AND ?)
	  OR 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,$first_day);
		 $dsd->bindParam(2,$last_day);
		 $dsd->bindParam(3,$first_day);
		 $dsd->bindParam(4,$last_day);
		 $dsd->bindParam(5,$last_day);
		 $dsd->bindParam(6,$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>
			 		<table class="table table-bordered">
			 					<thead>
			 							<tr>
			 								<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="5%">إلي</th>
			 								<th class="align-middle text-center" width="5%">أيام</th>
			 								<th class="align-middle text-center" width="5%">المدفوع</th>
			 								<th class="align-middle text-center" width="5%">إضافي</th>
			 								<th class="align-middle text-center" width="45%">بيان الإضافي</th>
			 								<th class="align-middle text-center" width="4%">خصم</th>
			 								<th class="align-middle text-center" width="5%">المطلوب</th>
			 								<th class="align-middle text-center" width="6%">الفرع</th>
			 						</tr>
			 			</thead>
			 		  <tbody >
<?php
						 $output = '';
						 $du = array();
						 $q = 0;
	 $sql6 =$db->prepare("SELECT contract_id from car_in where carid = ?  AND contract_id not in
		 (SELECT contract_id from contracts where carid = ? AND (date_out <= ? AND date_in IS NULL OR date_in >= ?)) AND sanad_date BETWEEN ? AND ? ");
						$sql6->bindParam(1,$carid);
						$sql6->bindParam(2,$carid);
						$sql6->bindParam(3,$last_day);
						$sql6->bindParam(4,$first_day);
						$sql6->bindParam(5,$first_day);
						$sql6->bindParam(6,$last_day);
					  $sql6->execute();
			if($sql6->rowCount()>0)while($row = $sql6->fetch())
					{$du[$q]=$row['contract_id']; $q = $q + 1 ;}
					$sql5 =$db->prepare("SELECT contract_id from car_out where carid = ?  AND contract_id not in
			 		 (SELECT contract_id from contracts where carid = ? AND (date_out <= ? AND date_in IS NULL OR date_in >= ? )) AND sanad_out BETWEEN ? AND ? ");
			 						$sql5->bindParam(1,$carid);
									$sql5->bindParam(2,$carid);
									$sql5->bindParam(3,$last_day);
									$sql5->bindParam(4,$first_day);
			 						$sql5->bindParam(5,$first_day);
			 						$sql5->bindParam(6,$last_day);
			 					  $sql5->execute();
			 			if($sql5->rowCount()>0)while($row = $sql5->fetch())
			 					{$du[$q]=$row['contract_id']; $q=$q+1 ;}
								$sam = 0 ; $cash = 0;  $wwc = 0;
									$du = join(",", $du);
									$sql =$db->prepare("SELECT contracts.contract_id , contracts.date_out , contracts.date_in , contracts.addtion_desc , contracts.deduction , branches.b_name from contracts , branches
										where  contracts.branch = branches.branchID AND contracts.contract_id in(?) order by contracts.date_out ");
								  $sql->bindParam(1,$du);
									 $sql->execute();
							if($sql->rowCount()>0)
										{
											while($row = $sql->fetch())
												{
														$last = $row['date_in'] ;
			                        if(!empty($last)){
			                            $last = date_create($row['date_in']);
			                        		$last = date_format($last,"m/d"); }
																	$contract_id = $row['contract_id'];

											   $wanted = new  Wanted($db,$contract_id) ;
											   $wwc = $wanted->wanted() ;
						                       $ww1 = $wwc["addtion_cost"] ;$ww2 = $wwc["violations"] ;$ww3 = $wwc["ins_claim"] ;
																	 $add = $ww1 + $ww2 + $ww3 ;
																	 $cash = $wanted->contract_month($first_day , $last_day);
											$output .= '<tr>
																	<td>' . $row['contract_id'] . '</td>
																	<td>' . $row['date_out'] . '</td>
																	<td>' .$last. '</td>
																	<td>' .$wwc["days_qty"] . '</td>
																	<td>'. $cash .'</td>
																	<td>' . $add . '</td>
																	<td style="font-size: 10px;"> سداد مديونية' . $row['addtion_desc'] . '</td>
																	<td>' . $row['deduction'] . '</td>
																	<td>' . $wwc["wanted"] . '</td>
																	<td>' . $row['b_name'] . '</td>
															</tr>  ';
																			 $sam = $sam + $cash ;
																		}
															 }
						$joker =$db->prepare("SELECT contracts.contract_id , contracts.date_out , contracts.date_in , contracts.addtion_desc , contracts.deduction , branches.b_name from contracts , branches
								where 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())
																	{
																		$last = $bo['date_in'] ;
																		if(!empty($last)){
																		$last = date_create($bo['date_in']);
																		$last = date_format($last,"m/d"); }
																		$contract_id = $bo['contract_id'];
																		$wanted = new  Wanted($db,$contract_id) ;
																		$wwc = $wanted->wanted() ;
																		 $ww1 = $wwc["addtion_cost"] ;$ww2 = $wwc["violations"] ;$ww3 = $wwc["ins_claim"] ;
																		 $add = $ww1 + $ww2 + $ww3 ;
																		 $cash = $wanted->contract_month($first_day , $last_day);
												$output .= '<tr>
																		<td>' . $contract_id . '</td>
																		<td>' . $bo['date_out'] . '</td>
																		<td>' .$last. '</td>
																		<td>' .$wwc["days_qty"] . '</td>
																		<td>'. $cash .'</td>
																		<td>' . $add . '</td>
																		<td style="font-size: 10px;">' . $bo['addtion_desc'] . '</td>
																		<td>' . $bo['deduction'] . '</td>
																		<td>' . $wwc["wanted"] . '</td>
																		<td>' . $bo['b_name'] . '</td>
																</tr>  ';
																				 $sam = $sam + $cash ;
																			}
																			$output .= '<tr>
																								<td>' . $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 src="../js/jquery.js"></script>
<script src="../dist/js/umd/popper.min.js"></script>
<script src="../dist/js/bootstrap.js"></script>
    </body>
</html>
