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

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

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 );
				?>
<!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="javascript: window.print(); setTimeout(window.close, 1000);">
			<div class="container-fluid">
			<div class="form-row">
				<div class="col align-items-center">
					<h6 class="text-center" style="font-size:14px;font-weight:bold;"> صندوق فرع <?php
					$dd = $db->prepare("SELECT b_name from branches where branchID = ?") ;
					$dd->bindParam(1,$branch) ;
					$dd->execute();
					if($dd->rowCount()>0){$result = $dd->fetch(PDO::FETCH_ASSOC);
								echo $result['b_name'];}
					?> خلال شهر  <?php echo $mms; ?></h6>
				</div>
			</div>
			<div class="form-row">
				<div class="col">
			<hr />
			<h6 style="font-weight:bold;font-size:13px;"> صندوق فرع <?php echo $result['b_name']; ?></h6>
		</div>
	</div>
			<div class="form-row align-items-center">
				<div class="col">
				<table class="table table-bordered table-striped ">
					<thead>
						<tr>
						    <th class="text-center" width="16%">إجمالي</th>
							<th class="text-center" width="16%">كاش</th>
							<th class="text-center" width="18%">تحويل الراجحي</th>
							<th class="text-center" width="16%">نقاط بيع</th>
							<th class="text-center" width="18%">تحويل الاهلي</th>
							<th class="text-center" width="16%">إيراد</th>
						</tr>
						</thead>
				<tbody>
						<?php
	$sam =  box($myday,$father,$branch);
		$output = '<tr>
						<td>' . $sam["nn"] . '</td>
						<td>' . $sam["n1"] . '</td>
						<td>' . $sam["n2"] . '</td>
						<td>' . $sam["n3"] . '</td>
						<td>' . $sam["n5"] . '</td>
						<td>داخلي</td>
				   </tr>
			<tr>
				<td>' . $sam["tt"] . '</td>
				<td>' . $sam["t1"] . '</td>
				<td>' . $sam["t2"] . '</td>
				<td>' . $sam["t3"] . '</td>
				<td>' . $sam["t5"] . '</td>
				<td>خارجي</td>
			</tr>
			<tr>
				<td>' . $sam["gr"] . '</td>
				<td>' . $sam["nt1"] . '</td>
				<td>' . $sam["nt2"] . '</td>
				<td>' . $sam["nt3"] . '</td>
				<td>' . $sam["nt5"] . '</td>
				<td>إجمالي</td>
			</tr>
			<tr><td colspan="6" class="text-center"></td></tr>
			<tr>
				<td>صرف</td>
				<td>' . $sam["out1"] . '</td>
				<td>مردود بنكي</td>
				<td>' . $sam["out2"] . '</td>
			</tr>
			<tr>
				<td>إيداعات كاش</td>
				<td>' . $sam["box_cash"] . '</td>
				
			</tr>
			<tr>
				<td>فواتير الفرع</td>
				<td>' . $sam["box_out"] . '</td>
			</tr>
			<tr>
				<td>رصيد الصندوق</td>
				<td>' . $sam["box_balance"] . '</td>
				<td colspan="2" class="text-left">صافي إجمالي ورادات الشهر</td>
				<td>' . $sam["month_balance"] . '</td>
			</tr>';
		echo $output;
		 ?>
				</tbody>
			<tfoot></tfoot>
		</table>
		</div>
			</div><!-- end first table form-form-row -->

<?php
  $emp1 = $db->prepare("SELECT DISTINCT user_id , realName from users
				where user_id in(SELECT DISTINCT father from invoice where branch = ? AND sanad_date BETWEEN ? AND ?)
				or user_id in(SELECT DISTINCT father from outvoice where branch = ? AND sanad_out BETWEEN ? AND ?)") ;
		$emp1->bindParam(1,$branch) ;
		$emp1->bindParam(2,$first_day) ;
		$emp1->bindParam(3,$last_day) ;
		$emp1->bindParam(4,$branch) ;
		$emp1->bindParam(5,$first_day) ;
		$emp1->bindParam(6,$last_day) ;
	  $emp1->execute();
 if($emp1->rowCount()>0)
	  {
	    while( $user = $emp1->fetch())
	    {$output ="";
				$emplyee = $user['realName']; $emp_id = $user['user_id'];

				 $output .= '<div class="form-row align-items-center">
						<div class="col">
						<h6 style="font-weight:bold;font-size:13px;"> موظف '.$emp_id.'#'.$emplyee.'</h6>
							</div>
					</div>
									<div class="form-row align-items-center">
										<div class="col">
														<table class="table table-bordered table-striped ">
																<thead>
																	 <tr>
																		<th class="text-center" width="11%">إجمالي</th>
																		<th class="text-center" width="11%">كاش</th>
																		<th class="text-center" width="16%">تحويل الراجحي</th>
																		<th class="text-center" width="14%">نقاط بيع</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 >';

										$emp = user_box($myday,$emp_id,$branch);
										 $output .= '<tr>
																	<td>' . $emp["month_balance"] . '</td>
																	<td>' . $emp["n1"] . '</td>
																	<td>' . $emp["n2"] . '</td>
																	<td>' . $emp["n3"] . '</td>
																	<td>' . $emp["n5"] . '</td>
																	<td>' . $emp["out1"] . '</td>
																	<td>' . $emp["out2"] . '</td>
																	<td>' . $emp["box_out"] . '</td>
																</tr>
																<tr>
																	 <td colspan="2">إجمالي صندوق الموظف</td>
																	 <td>إيداعات الشهر</td>
																	 <td>رصيد الصندوق</td>
																 </tr>
																 <tr>
																		<td colspan="2">' . $emp["net"] . '</td>
																		<td>' . $emp["box_cash"] . '</td>
																		<td>' . $emp["box_balance"] . '</td>
																	</tr> ';
								 $output .=' </tbody><tfoot></tfoot></table></div></div>' ;
							echo $output;
						 }}
				 ?>
          </div>
    </body>
</html>
