<?php

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

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

include "../op/number_text.php";
if (isset($_REQUEST["dd"])) {
	$myday = $_REQUEST["dd"];
} else {
	$myday = date("Y-m-d");
}
?>
<!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" />
	<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="javascript: window.print(); setTimeout(window.close, 1000);">
	<div class="container-fluid">
		<div class="form-row">
			<div class="col-10">
				<h6 style="font-size:10pt;font-weight:bold;"><?php echo $myday; ?> يومية الوسام المعتمد </h6>
			</div>
			<div class="col-2">
			</div>
		</div>
		<div class="form-row">
			<div class="col">
				<h6 style="font-size:10pt;font-weight:bold;"> تحويلات الراجحي </h6>
			</div>
		</div>
		<div class="form-row">
			<div class="col">
				<table class="table table-bordered table-striped ">
					<thead>
						<tr>
							<th class="text-center" width="8%">المبلغ</th>
							<th class="text-center" width="10%">الفرع</th>
							<th class="text-center" width="12%">التوقيع</th>
							<th class="text-center" width="8%">كود</th>
							<th class="text-center" width="41%">اسم العميل</th>
							<th class="text-center" width="10%">رقم العقد</th>
							<th class="text-center" width="10%">حالة العقد</th>
						</tr>
					</thead>
		<tbody>
		<?php
			$first_day = date('Y-m-01', strtotime($myday));
			$last_day 	= date('Y-m-t', strtotime($myday));
			$output = '';
		$sql = $conn->prepare("SELECT money_in , b_name ,branchID,  in_id , cust_name , contract_id , cont_status
								 FROM sanad_in  WHERE payMethod = 2 AND sanad_date = ? order by branchID ,in_id ");
						$sql->bindParam(1, $myday);
						$sql->execute();
						if ($sql->rowCount() > 0) {
							$sam = 0;
							while ($row = $sql->fetch()) {
								$cont = $row['cont_status'];
								switch ($cont) {
									case "1":
										$contt =  "ساري";
										break;
									case "2":
										$contt = "مغلق";
										break;
									case "3":
										$contt = "مديون";
										break;
								}
							$output .= '<tr>
											<td>' . $row['money_in'] . '</td>
											<td>' . $row['b_name'] . '</td>
											<td></td>
											<td>' . $row['in_id'] . '</td>
											<td>' . $row['cust_name'] . '</td>
											<td>' . $row['contract_id'] . '</td>
											<td>' . $contt . '</td>
										</tr>  ';
								$sam = $sam + $row['money_in'];
							}
						$output .= '<tr>
										<td>' . $sam . '</td>
										<td>إجمالي</td></tr>';
						} else {
							$output .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
						}
						echo $output;
						?>
					</tbody>
					<tfoot></tfoot>
				</table>
			</div><!-- home sanad in row -->
		</div><!-- end of banking money -->
		<div class="form-row">
			<div class="col">
				<h6 style="font-size:10pt;font-weight:bold;"> تحويلات الأهلي </h6>
			</div>
		</div>
		<div class="form-row">
			<div class="col">
				<table class="table table-bordered table-striped ">
					<thead>
						<tr>
							<th class="text-center" width="8%">المبلغ</th>
							<th class="text-center" width="10%">الفرع</th>
							<th class="text-center" width="12%">التوقيع</th>
							<th class="text-center" width="8%">كود</th>
							<th class="text-center" width="41%">اسم العميل</th>
							<th class="text-center" width="10%">رقم العقد</th>
							<th class="text-center" width="10%">حالة العقد</th>
						</tr>
					</thead>
		<tbody>
		<?php
			$first_day = date('Y-m-01', strtotime($myday));
			$last_day 	= date('Y-m-t', strtotime($myday));
			$output = '';
		$sql = $conn->prepare("SELECT money_in , b_name ,branchID,  in_id ,cust_name , contract_id , cont_status
								 FROM sanad_in  WHERE payMethod = 5 AND sanad_date = ? order by branchID ,in_id ");
						$sql->bindParam(1, $myday);
						$sql->execute();
						if ($sql->rowCount() > 0) {
							$sam = 0;
							while ($row = $sql->fetch()) {
								$cont = $row['cont_status'];
								switch ($cont) {
									case "1":
										$contt =  "ساري";
										break;
									case "2":
										$contt = "مغلق";
										break;
									case "3":
										$contt = "مديون";
										break;
								}
							$output .= '<tr>
											<td>' . $row['money_in'] . '</td>
											<td>' . $row['b_name'] . '</td>
											<td></td>
											<td>' . $row['in_id'] . '</td>
											<td>' . $row['cust_name'] . '</td>
											<td>' . $row['contract_id'] . '</td>
											<td>' . $contt . '</td>
										</tr>  ';
								$sam = $sam + $row['money_in'];
							}
						$output .= '<tr>
										<td>' . $sam . '</td>
										<td>إجمالي</td></tr>';
						} else {
							$output .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
						}
						echo $output;
						?>
					</tbody>
					<tfoot></tfoot>
				</table>
			</div><!-- home sanad in row -->
		</div><!-- end of banking money -->
		<div class="form-row">
			<div class="col">
				<h6 style="font-size:10pt;font-weight:bold;">سندات نقاط البيع </h6>
			</div>
		</div>
		<div class="form-row">
			<div class="col">
				<table class="table table-bordered table-striped ">
					<thead>
						<tr>
							<th class="text-center" width="8%">المبلغ</th>
							<th class="text-center" width="10%">الفرع</th>
							<th class="text-center" width="12%">التوقيع</th>
							<th class="text-center" width="8%">كود</th>
							<th class="text-center" width="41%">اسم العميل</th>
							<th class="text-center" width="10%">رقم العقد</th>
							<th class="text-center" width="10%">حالة العقد</th>
						</tr>
					</thead>
	<tbody >
		<?php
			$output = '';
			$sql = $conn->prepare("SELECT money_in , b_name , branchID, in_id , cust_name , contract_id , cont_status
								FROM sanad_in  WHERE payMethod = 3 AND sanad_date = ? ORDER BY branchID , in_id ");
			$sql->bindParam(1, $myday);;
			$sql->execute();
		if ($sql->rowCount() > 0) {
							$sam = 0;
							while ($row = $sql->fetch()) {
								$cont = $row['cont_status'];
								switch ($cont) {
									case "1":
										$contt =  "ساري";
										break;
									case "2":
										$contt = "مغلق";
										break;
									case "3":
										$contt = "مديون";
										break;
								}
								$output .= '<tr>
												<td>' . $row['money_in'] . '</td>
												<td>' . $row['b_name'] . '</td>
												<td></td>
												<td>' . $row['in_id'] . '</td>
												<td>' . $row['cust_name'] . '</td>
												<td>' . $row['contract_id'] . '</td>
												<td>' . $contt . '</td>
											</tr>  ';
								$sam = $sam + $row['money_in'];
							}
							$output .= '<tr>
											<td>' . $sam . '</td>
											<td>إجمالي</td>
										</tr>';
						} else {
							$output .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
						}
						echo $output;
						?>
					</tbody>
					<tfoot></tfoot>
				</table>
			</div><!-- home sanad in row -->
		</div><!-- end of cash points money -->

		<div class="form-row">
			<div class="col">
				<h6 style="font-size:10pt;font-weight:bold;">إجمالي إيرادات</h6>
			</div>
		</div>
		<div class="form-row">
			<div class="col">
				<table class="table table-bordered table-striped ">
					<thead>
						<tr>
							<th class="text-center" width="20%">الفرع</th>
							<th class="text-center" width="20%">الصافي</th>
							<th class="text-center" width="15%">تحويل للإدارة</th>
							<th class="text-center" width="15%">رصيد الصنوق</th>
							<th class="text-center" width="15%">إجمالي كاش</th>
							<th class="text-center" width="15%">إجمالي صرف</th>
						</tr>
					</thead>
			<tbody >
				<?php
					$output = '';
					$bb = $conn->prepare("SELECT branchID , b_name  from branches  where branchID > 1 order by branchID ");
					$bb->execute();
			if ($bb->rowCount() > 0) {
							$a = 0;
							$b = 0;
							$c = 0;
							$d = 0;
							$e = 0;
				while ($result = $bb->fetch()) {
								$branch = $result["branchID"];
								$b_name = $result["b_name"];
								$br_box = 0;
								$boxx = 0;
								$outt = 0;
								$inn = 0;
								$br_outt = 0;
								$br_money = 0;
								$cash = 0;
								// all net money for this day
						$sql = $conn->prepare("SELECT sum(money_in) as inn FROM sanad_in  WHERE sanad_date = ? AND branchID = ? ");
								$sql->bindParam(1, $myday);
								$sql->bindParam(2, $branch);
								$sql->execute();
						if ($sql->rowCount() > 0) {
							$row = $sql->fetch(PDO::FETCH_ASSOC);
							$inn   	= $row["inn"];
						}
								//  all box money for my day
					$sql = $conn->prepare("SELECT sum(money_box) as boxx FROM boxmoney  WHERE month_date = ? AND branch = ? ");
					$sql->bindParam(1, $myday);
					$sql->bindParam(2, $branch);
					$sql->execute();
				if ($sql->rowCount() > 0) {
						$row = $sql->fetch(PDO::FETCH_ASSOC);
						$boxx   	= $row["boxx"];
								}
								// box remain money
					$sql = $conn->prepare("SELECT sum(money_in) as br_money FROM sanad_in  WHERE payMethod = 1 AND branchID = ? AND sanad_date between  ? AND ? ");
					$sql->bindParam(1, $branch);
					$sql->bindParam(2, $first_day);
					$sql->bindParam(3, $last_day);
					$sql->execute();
				if ($sql->rowCount() > 0) {
					$row = $sql->fetch(PDO::FETCH_ASSOC);
					$br_money   		= $row["br_money"];
						}

				$sql = $conn->prepare("SELECT sum(money_box) as br_box FROM boxmoney  WHERE  branch = ? AND month_date between  ? AND ? ");
						$sql->bindParam(1, $branch);
						$sql->bindParam(2, $first_day);
					    $sql->bindParam(3, $last_day);
					$sql->execute();
				if ($sql->rowCount() > 0) {
					$row = $sql->fetch(PDO::FETCH_ASSOC);
					$br_box   		= $row["br_box"];
					}

								// all cash money for my day
			$sql = $conn->prepare("SELECT sum(money_in) as cash FROM sanad_in  WHERE payMethod = 1 AND sanad_date = ? AND branchID = ? ");
				$sql->bindParam(1, $myday);
				$sql->bindParam(2, $branch);
				$sql->execute();
			if ($sql->rowCount() > 0) {
				$row = $sql->fetch(PDO::FETCH_ASSOC);
				$cash   		= $row["cash"];
						}
								// all outvoice for my day
				$sql = $conn->prepare("SELECT sum(money_out) as outt FROM sanad_out  WHERE sanad_out = ? AND branch = ? ");
					$sql->bindParam(1, $myday);
					$sql->bindParam(2, $branch);
					$sql->execute();
				if ($sql->rowCount() > 0) {
					$row = $sql->fetch(PDO::FETCH_ASSOC);
					$outt   	= $row["outt"];
						}

				$sql = $conn->prepare("SELECT sum(money_out) as br_outt FROM sanad_out  WHERE branch = ? AND sanad_out between  ? AND ? ");
					$sql->bindParam(1, $branch);
					$sql->bindParam(2, $first_day);
					$sql->bindParam(3, $last_day);
					$sql->execute();
				if ($sql->rowCount() > 0) {
					$row = $sql->fetch(PDO::FETCH_ASSOC);
					$br_outt   		= $row["br_outt"];
					}

					$nett = $inn - $outt;
					$br_box = round(($br_box + $br_outt), 2);
					$box = round(($br_money - $br_box), 2);
					$output .= '<tr>
									<td>' . $b_name . '</td>
									<td>' . $nett . '</td>
									<td>' . $boxx . '</td>
									<td>' . $box . '</td>
									<td>' . $cash . '</td>
									<td>' . $outt . '</td>
								</tr>  ';
								$a = $a + $nett;
								$b = $b + $boxx;
								$c = $c + $box;
								$d = $d + $cash;
								$e = $e + $outt;
							}
							$output .= '<tr>
											<td>إجمالي الفروع</td>
											<td>' . $a . '</td>
											<td>' . $b . '</td>
											<td>' . $c . '</td>
											<td>' . $d . '</td>
											<td>' . $e . '</td>
									</tr>  ';
						} else {
							$output .= '<tr><td colspan="6">لا يوجد سجلات ...</td></tr>';
						}
						echo $output;
						?>
					</tbody>
					<tfoot></tfoot>
				</table>
			</div><!--  -->
		</div><!-- end branches money -->

		<div class="form-row">
			<div class="col">
				<h6 style="font-size:10pt;font-weight:bold;">حركة السيارات </h6>
			</div>
		</div>
		<div class="form-row">
			<div class="col">
				<table class="table table-bordered table-striped ">
					<thead>
						<tr>
							<th class="text-center" width="20%">الفرع</th>
							<th class="text-center" width="20%">إجمالي القوة </th>
							<th class="text-center" width="15%">سيارات مؤجرة</th>
							<th class="text-center" width="15%">وقوف</th>
							<th class="text-center" width="10%">ورشة</th>
							<th class="text-center" width="10%">خدمة</th>
							<th class="text-center" width="10%">هروب</th>
						</tr>
					</thead>
					<tbody >
		<?php
			$output = '';
			$bb = $conn->prepare("SELECT branchID , b_name  from branches  where branchID > 1 order by branchID ");
			$bb->execute();
		if ($bb->rowCount() > 0) {
			$a = 0;
			$b = 0;
			$c = 0;
			$d = 0;
			$e = 0;
			$f = 0;
	while ($result = $bb->fetch()) {
			$branch = $result["branchID"];
			$b_name = $result["b_name"];
			$br_box = 0;
			$boxx = 0;
			$outt = 0;
			$ff = 0;
		// all net money for yhis day
			$sql = $conn->prepare("SELECT count(carid) as inn FROM cars  WHERE  branch = ? ");
			$sql->bindParam(1, $branch);
			$sql->execute();
	if ($sql->rowCount() > 0) {
				$row = $sql->fetch(PDO::FETCH_ASSOC);
				$inn   						= $row["inn"];
			}
								//  all box money for my day
								$sql = $conn->prepare("SELECT count(carid) as boxx FROM contracts  WHERE cont_status = 1 AND branch = ? ");
								$sql->bindParam(1, $branch);
								$sql->execute();
								if ($sql->rowCount() > 0) {
									$row = $sql->fetch(PDO::FETCH_ASSOC);
									$boxx   						= $row["boxx"];
								}
								// box remain money
								$sql = $conn->prepare("SELECT count(carid) as br_money FROM cars  WHERE carstatus = 1 AND branch = ? ");
								$sql->bindParam(1, $branch);
								$sql->execute();
								if ($sql->rowCount() > 0) {
									$row = $sql->fetch(PDO::FETCH_ASSOC);
									$box   		= $row["br_money"];
								}
								// all cash money for my day
								$sql = $conn->prepare("SELECT count(carid) as cash FROM cars  WHERE carstatus = 3 AND branch = ?");
								$sql->bindParam(1, $branch);
								$sql->execute();
								if ($sql->rowCount() > 0) {
									$row = $sql->fetch(PDO::FETCH_ASSOC);
									$cash   						= $row["cash"];
								}
								// all outvoice for my day
								$sql = $conn->prepare("SELECT count(carid) as outt FROM cars  WHERE carstatus = 5 AND branch = ?");
								$sql->bindParam(1, $branch);
								$sql->execute();
								if ($sql->rowCount() > 0) {
									$row = $sql->fetch(PDO::FETCH_ASSOC);
									$outt   						= $row["outt"];
								}
				$sql = $conn->prepare("SELECT count(carid) as ff FROM cars  WHERE carstatus = 4 AND branch = ?");
					$sql->bindParam(1, $branch);
					$sql->execute();
				if ($sql->rowCount() > 0) {
					$row = $sql->fetch(PDO::FETCH_ASSOC);
					$ff   			= $row["ff"];
					}
						$output .= '<tr>
											<td>' . $b_name . '</td>
											<td>' . $inn . '</td>
											<td>' . $boxx . '</td>
											<td>' . $box . '</td>
											<td>' . $cash . '</td>
											<td>' . $outt . '</td>
											<td>' . $ff . '</td>
									</tr>  ';
								$a = $a + $inn;
								$b = $b + $boxx;
								$c = $c + $box;
								$d = $d + $cash;
								$e = $e + $outt;
								$f = $f + $ff;
							}
			$output .= '<tr>
							<td>إجمالي الفروع</td>
							<td>' . $a . '</td>
							<td>' . $b . '</td>
							<td>' . $c . '</td>
							<td>' . $d . '</td>
							<td>' . $e . '</td>
							<td>' . $f . '</td>
					</tr>  ';
		} else {
		$output .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
				}
		echo $output;
						?>
					</tbody>
					<tfoot></tfoot>
				</table>
			</div><!--  -->
		</div><!-- end branches money -->
	</div>
</body>

</html>