<?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 "../actions/dbPDO.php";

include "../op/box.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>صندوق الشهر<?php echo $mms ; ?> </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="javascript: window.print(); setTimeout(window.close, 1000);">
	<div class="container-fluid">
		<br />
		<div class="form-row">
			<div class="col-10">
				<p style="font-size:13px;font-weight:bold;"> صندوق الفروع خلال شهر <?php echo $mms; ?></p>
			</div>
			<div class="col-2">
			</div>
		</div>
		<div class="form-row">
			<div class="col">
				<table class="table table-bordered table-striped ">
					<thead>
						<tr>
							<th class="align-middle text-center" width="4%">م</th>
							<th class="align-middle text-center" width="10%">الفرع</th>
							<th class="align-middle text-center" width="8%">إجمالي</th>
							<th class="align-middle text-center" width="8%">توريد كاش</th>
							<th class="align-middle text-center" width="8%">كاش</th>
							<th class="align-middle text-center" width="9%">مردود كاش</th>
							<th class="align-middle text-center" width="10%">مردود بنكي</th>
							<th class="align-middle text-center" width="9%">تحويل الراجحي</th>
							<th class="align-middle text-center" width="9%">نقاط بيع</th>
							<th class="align-middle text-center" width="9%">تحويل الأهلي</th>
							<th class="align-middle text-center" width="8%">فواتير</th>
							<th class="align-middle text-center" width="8%">رصيد الصندوق</th>
						</tr>
					</thead>
					<tbody id="tstandby">
						<?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; $d2 = 0;
							$e = 0;
							$f = 0;
							$r = 0;
							$g = 0;
							$h = 0;
							$qq = 1;
							while ($result = $bb->fetch()) {
								$branch = $result["branchID"];
								$b_name = $result["b_name"];
								$sam =  box($myday, $father, $branch);
								$output .= '<tr>
											  <td>' . $qq . '</td>
																<td>' . $b_name . '</td>
																<td>' . $sam["month_balance"] . '</td>
																<td>' . $sam["box_cash"] . '</td>
																<td>' . $sam["nt1"] . '</td>
																<td>' . $sam["out1"] . '</td>
																<td>' . $sam["out2"] . '</td>
																<td>' . $sam["nt2"] . '</td>
																<td>' . $sam["nt3"] . '</td>
																<td>' . $sam["nt5"] . '</td>
																<td>' . $sam["box_out"] . '</td>
																<td>' . $sam["box_balance"] . '</td>
															</tr>  ';
								$qq = $qq + 1;
								$a = $a + $sam["month_balance"];
								$b = $b + $sam["box_cash"];
								$c = $c + $sam["nt1"];
								$d = $d + $sam["out1"];
								$d2 = $d2 + $sam["out2"];
								$e = $e + $sam["nt2"];
								$f = $f + $sam["nt3"];
								$r = $r + $sam["nt5"];
								$g = $g + $sam["box_out"];
								$h = $h + $sam["box_balance"];
							}
							$output .= '<tr>
														 <td colspan="2" >إجمالي الفروع</td>
														 <td>' . $a . '</td>
														 <td>' . $b . '</td>
														 <td>' . $c . '</td>
														 <td>' . $d . '</td>
														 <td>' . $d2 . '</td>
														 <td>' . $e . '</td>
														 <td>' . $f . '</td>
														 <td>' . $r . '</td>
														 <td>' . $g . '</td>
														 <td>' . $h . '</td>
													</tr>  ';
						} else {
							$output .= '<tr><td colspan="12">لا يوجد سجلات ...</td></tr>';
						}
						echo $output;
						?>
					</tbody>
					<tfoot></tfoot>
				</table>
			</div><!--  -->
		</div><!-- end branches money -->
		<br />
		<hr />
		<br />
		<div class="form-row">
			<div class="col-10">
				<p style="font-size:13px;font-weight:bold;">إيرادات جميع الفروع خلال شهر <?php echo $mms; ?></p>
			</div>
			<div class="col-2">
			</div>
		</div>
		<div class="form-row">
			<div class="col">
				<table class="table table-bordered table-striped ">
					<thead>
						<tr>
							<th class="align-middle text-center" width="5%">م</th>
							<th class="align-middle text-center" width="15%">الفرع</th>
							<th class="align-middle text-center" width="13%">إجمالي</th>
							<th class="align-middle text-center" width="11%">كاش</th>
							<th class="align-middle text-center" width="8%">صرف</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="8%">نقاط بيع</th>
							<th class="align-middle text-center" width="10%">تحويل الأهلي</th>
							<th class="align-middle text-center" width="8%">فواتير</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; $c2 = 0;
							$d = 0;
							$e = 0;
							$r = 0;
							$f = 0;
							$g = 1;
							while ($result = $bb->fetch()) {
								$branch = $result["branchID"];
								$b_name = $result["b_name"];
								$sam =  box($myday, $father, $branch);
								// $output .= '<tr>
								// 	 						 <td>' . $g . '</td>
								// 							 <td>' . $b_name . '</td>
								// 							 <td>' . $sam["net"] . '</td>
								// 							 <td>' . $sam["bnt1"] . '</td>
								// 							 <td>' . $sam["bout"] . '</td>
								// 							 <td>' . $sam["bnt2"] . '</td>
								// 							 <td>' . $sam["bnt3"] . '</td>
								// 							 <td>' . $sam["bnt5"] . '</td>
								// 							 <td>' . $sam["box_out"] . '</td>
								// 						</tr>  ';
								// $g = $g + 1;
								$a = $a + $sam["net"];
								$b = $b + $sam["bnt1"];
								$c = $c + $sam["bout"];
								$c2 = $c2 + $sam["bout2"];
								$d = $d + $sam["bnt2"];
								$e = $e + $sam["bnt3"];
								$r = $r + $sam["bnt5"];
								$f = $f + $sam["box_out"];
							}
							$output .= '<tr>
											 <td colspan="2" >إجمالي الفروع</td>
											 <td>' . $a . '</td>
											 <td>' . $b . '</td>
											 <td>' . $c . '</td>
											 <td>' . $c2 . '</td>
											 <td>' . $d . '</td>
											 <td>' . $e . '</td>
											 <td>' . $r . '</td>
											 <td>' . $f . '</td>
										</tr>  ';
						} else {
							$output .= '<tr><td colspan="9">لا يوجد سجلات ...</td></tr>';
						}
						echo $output;
						?>
					</tbody>
					<tfoot></tfoot>
				</table>
			</div><!--  -->
		</div><!-- end branches money -->
	</div>
</body>
</html>