<?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["dd"])){ $myday = $_REQUEST["dd"];	$branch = $_REQUEST["br"]; }
	$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;

	$mmm = new DateTime($myday);
	$mms =  $mmm->format("m Y");
	$ac = $db->prepare(" SELECT b_name FROM  branches WHERE branchID =  ? LIMIT 1 ");
				$ac->bindParam(1,$branch) ;
				$ac->execute();
			if($ac->rowCount()>0){$day = $ac->fetch(PDO::FETCH_ASSOC);
						 $b_name = $day['b_name'];}
						?>
<!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="row" style="padding:0 ;">
					<div class="col-9"style="float:right;">
						<p style="font-weight:bold;font-size:10pt;" > تارجت سيارات فرع  <?php echo $b_name." خلال شهر ".$mms ;?></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-md-12"style="float:right;">
	 											<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="14%">لوحة رقم</th>
	 																<th class="align-middle text-center" width="10%">موديل</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="12%">إجمالي إيراد</th>
	 																<th class="align-middle text-center" width="12%">صافي</th>
	 													 </tr>
	 												 </thead>
	 													 <tbody id="tstandby">
	 					<?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.branch =  ? AND actions.ac_date between ? AND ?
							ORDER BY cars.kind , cars.numbers ");
																$sql->bindParam(1,$branch);
	 															$sql->bindParam(2,$first_day);
																$sql->bindParam(3,$last_day);
	 															 $sql->execute();
	 																if($sql->rowCount()>0)
	 															 { $ss = 1 ; $tot_money = 0 ; $tot_target = 0 ; $tot_net = 0 ;
	 																	while($row = $sql->fetch())
	 																		{
																				$carid = $row['carid']; $target = 0 ;
																				$month =  month_history($carid,$branch,$first_day,$last_day) ;
																				$kest = $row['kest'] ;$tot = $month['tot_at_branch'] ; $money = $month['money'] ;
																				if($kest < 2500){$target = 2000 ;}else{$target = 3000 ;}
																				$net_target = intval(($target * $tot)/$days_qty ) ; $net = $money - $net_target ;
																				if($net_target > $target){$net_target = $tot_target ;}
	 																				 $output .= '<tr>
																					 					<td>' . $ss . '</td>
	 																									 <td>' . $row['kind'] . '</td>
	 																									 <td>' . $row['PL'] . '</td>
	 																									 <td>' . $row['model'] . '</td>
																										 <td>' . $target . '</td>
	 																									 <td>' . $tot. '</td>
																										 <td>' .$net_target. '</td>
																										 <td>' . $money . '</td>
																										 <td>' . $net. '</td>
	 																								</tr>  '; $ss = $ss + 1 ;
																									$tot_money = $tot_money + $money ;
																									$tot_target = $tot_target + $net_target ;
																									$tot_net = $tot_net + $net ;
	 																		}
																			$output .= '<tr>
	 																												<td colspan="6" style="font-size:12px;" class="text-left">إجمالي إيرادات الفرع</td>
																													<td>' . $tot_target . '</td>
																													<td>' . $tot_money . '</td>
																													<td>' . $tot_net . '</td>
	 																									 </tr>';
	 															 }
	 															 else
	 															 {
	 																		$output .= '<tr>
	 																												<td colspan="9">لا يوجد سجلات ...</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 branch = ? AND ac_date between ? AND ? )
																AND carid in( SELECT carid FROM sanad_in WHERE branchID = ? and sanad_date BETWEEN ? AND ?  )
																AND carid in( SELECT carid FROM sanad_out WHERE branch = ? and sanad_out BETWEEN ? AND ?  )
									 							ORDER BY kind , numbers ");
									 																$sql2->bindParam(1,$branch);
									 	 															$sql2->bindParam(2,$first_day);
									 																$sql2->bindParam(3,$last_day);
																									$sql2->bindParam(4,$branch);
									 	 															$sql2->bindParam(5,$first_day);
									 																$sql2->bindParam(6,$last_day);
																									$sql2->bindParam(7,$branch);
									 	 															$sql2->bindParam(8,$first_day);
									 																$sql2->bindParam(9,$last_day);
									 	 															 $sql2->execute();
									 	 																if($sql2->rowCount()>0)
									 	 															 {
									 	 																	while($row = $sql2->fetch())
									 	 																		{
									 																				$carid = $row['carid']; $target = 0 ;
									 																				$month =  month_history($carid,$branch,$first_day,$last_day) ;
									 																				$kest = $row['kest'] ;$tot = $month['tot_at_branch'] ; $money = $month['money'] ;
									 																				if($kest < 2500){$target = 2000 ;}else{$target = 3000 ;}
									 																				$net_target = 0 ; $net = $money ;
									 	 																				 $output .= '<tr>
									 																					 					<td>' . $ss . '</td>
									 	 																									 <td>' . $row['kind'] . '</td>
									 	 																									 <td>' . $row['PL'] . '</td>
									 	 																									 <td>' . $row['model'] . '</td>
									 																										 <td>' . $target . '</td>
									 	 																									 <td>' . $tot. '</td>
									 																										 <td>' .$net_target. '</td>
									 																										 <td>' . $money . '</td>
									 																										 <td>' . $net. '</td>
									 	 																								</tr>  '; $ss = $ss + 1 ;
									 																									$tot_money = $tot_money + $money ;
									 																									$tot_target = $tot_target + $net_target ;
									 																									$tot_net = $tot_net + $net ;
									 	 																		}
									 																			$output .= '<tr>
									 	 																												<td colspan="6" style="font-size:12px;" class="text-left">إجمالي إيرادات الفرع</td>
									 																													<td>' . $tot_target . '</td>
									 																													<td>' . $tot_money . '</td>
									 																													<td>' . $tot_net . '</td>
									 	 																									 </tr>';
									 	 															 }
									 	 															 else
									 	 															 {
									 	 																		$output .= '<tr>
									 	 																												<td colspan="9">لا توجد سداد مديونيات ...</td>
									 	 																									 </tr>';
									 	 															 }
	 															 echo $output;
	 															 ?>
	 													 </tbody>
	 												 <tfoot></tfoot>
	 											 </table>
										</div>
							</div> <!-- end of table row -->

				</div><!-- main fluied container -->
    </body>
</html>
