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

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

$myday = $_REQUEST['day'];
if(empty($myday)){
	$myday = date("Y-m-d");
	}

?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="robots" content="index,follow">
	<link rel="icon" href="../images/perfect.ico">
	<title>يومية دفعات إيجارية</title>
	<link rel="stylesheet" href="../dist/css/bootstrap.css" />
	<link rel="stylesheet" href="../css/font-awesome.min.css">
	<link rel="stylesheet" href="../css/myCar.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>
	<div class="container-fluid">
		<br />
		<div class="form-row align-items-center">
			<div class="col-6">
				<h5 style="font-weight:bold;" > يومية دفعات إيجارية  </h5>
			</div>
			<div class="col-6">
				<h5 style="font-weight:bold;" > <?php echo $myday; ?></h5>
			</div>
		</div>
		<br />			
	</div>
	<div class="container-fluid">
		<div class="form-row align-items-center">
			<div class="col">
				<label class="control-label " style="font-weight:bold;">سندات دفعات إيجار </label>
			</div>
		</div>
		<br />
		<div class="form-row">
			<table class="table table-bordered table-hover">
				<thead>
					<tr>
						<th class="text-center" width="4%">م</th>
						<th class="text-center" width="8%">المبلغ</th>
						<th class="text-center" width="30%">الإسم</th>
						<th class="text-center" width="10%">رقم العقد</th>
						<th class="text-center" width="10%">رقم الدفعة</th>
						<th class="text-center" width="6%">موظف</th>
						<th class="text-center" width="16%">سداد</th>
						<th class="text-center" width="16%">تعليق</th>
					</tr>
				</thead>
		<tbody>
		<?php
		$cash = 0 ;
		$rajhi = 0;
		$neqat = 0;
		$ahli  =0;
		$output = '';
			$sql = $db->prepare("SELECT aksat_in.ak_id , aksat_in.payed, aksat_in.contract_id ,aksat_in.payMethod , aksat_in.father, aksat_in.ak_no,
					aksat_in.comm ,customers.cust_name
					FROM `aksat_in`  , customers , aksat_contract
					WHERE aksat_in.contract_id = aksat_contract.contract_id
					AND   aksat_contract.cust_id = customers.cust_id
					AND   (aksat_in.comm != 'مرحل' OR aksat_in.comm IS NULL )
					AND date_payed = ? ORDER BY payMethod");
					$sql->bindParam(1, $myday);
					$sql->execute();
					if ($sql->rowCount() > 0) {
						$no = 0 ;
						while ($row = $sql->fetch()) {
							$no++;
							$bb = $row['payMethod'];
							$payed = $row['payed'];
							if (!empty($bb)) {
								switch ($bb) {
									case "1":
										$pay =  "كاش";
										$cash = $cash + $payed;
										break;
									case "2":
										$pay = "تحويل الراجحي";
										$rajhi = $rajhi + $payed;
										break;
									case "3":
										$pay = "نقاط بيع";
										$neqat = $neqat + $payed;
										break;
									case "5":
										$pay = "تحويل الأهلي";
										$ahli = $ahli + $payed;
										break;
								}
							} else {
								$pay = "";
							}
							$output .= '<tr><td>' . $no . '</td>
													<td>' . $payed . '</td>
													<td>' . $row['cust_name'] . '</td>
													<td>' . $row['contract_id'] . '</td>
													<td>' . $row['ak_no'] . '</td>
													<td>' . $row['father'] . '</td>
													<td>' . $pay . '</td>
													<td>' . $row['comm'] . '</td>
											</tr>';
						}
					} else {
						$output .= '<tr><td colspan="8">لا يوجد سجلات ...</td></tr>';
					}
					echo $output;
					?>
				</tbody>
				<tfoot></tfoot>
			</table>
		</div>
<br />
<hr/>
<div class="form-row align-items-center">
			<div class="col">
				<label class="control-label " style="font-weight:bold;">جزء من دفعة إيجار </label>
			</div>
		</div>
		<br />
		<div class="form-row">
			<table class="table table-bordered table-hover">
				<thead>
					<tr>
						<th class="text-center" width="4%">م</th>
						<th class="text-center" width="8%">المبلغ</th>
						<th class="text-center" width="30%">الإسم</th>
						<th class="text-center" width="10%">رقم العقد</th>
						<th class="text-center" width="10%">رقم الدفعة</th>
						<th class="text-center" width="6%">موظف</th>
						<th class="text-center" width="16%">سداد</th>
						<th class="text-center" width="16%">تعليق</th>
					</tr>
				</thead>
		<tbody>
<?php
	$output = '';
		$sql = $db->prepare("SELECT aksat_payed.ak_id , aksat_payed.money_in, aksat_payed.contract_id ,aksat_payed.payMethod , aksat_payed.father, aksat_payed.ak_no, customers.cust_name
					FROM aksat_payed  , customers , aksat_contract
					WHERE aksat_payed.contract_id = aksat_contract.contract_id
					AND   aksat_contract.cust_id = customers.cust_id
					AND date_payed = ? ORDER BY payMethod");
		$sql->bindParam(1, $myday);
		$sql->execute();
	if ($sql->rowCount() > 0) {
			$no = 0 ;
		while ($row = $sql->fetch()) {
				$no++;
				$bb = $row['payMethod'];
				$money_in = $row['money_in'] ;
			if (!empty($bb)) {
				switch ($bb) {
					case "1":
						$pay =  "كاش";
						$cash = $cash + $money_in;
						break;
					case "2":
						$pay = "تحويل الراجحي";
						$rajhi = $rajhi + $money_in;
						break;
					case "3":
						$pay = "نقاط بيع";
						$neqat = $neqat + $money_in;
						break;
					case "5":
						$pay = "تحويل الأهلي";
						$ahli = $ahli + $money_in;
						break;
					}
				} else {
					$pay = "";
					}
		$output .= '<tr><td>' . $no . '</td>
						<td>' . $money_in . '</td>
						<td>' . $row['cust_name'] . '</td>
						<td>' . $row['contract_id'] . '</td>
						<td>' . $row['ak_no'] . '</td>
						<td>' . $row['father'] . '</td>
						<td>' . $pay . '</td>
						<td></td>
					</tr>';
		}

		} else {
				$output .= '<tr><td colspan="8">لا يوجد سجلات ...</td></tr>';
					}
					$output .= '<tr><td colspan="8"></td></tr>';
					$output .="<tr><td colspan='2'>$cash</td><td > إجمالي كاش </td></tr>" ;
					$output .="<tr><td colspan='2'>$neqat</td><td > إجمالي نقاط بيع </td></tr>" ;
					$output .="<tr><td colspan='2'>$rajhi</td><td > إجمالي تحويل راجحي </td></tr>" ;
					$output .="<tr><td colspan='2'>$ahli</td><td > إجمالي تحويل أهلي </td></tr>" ;
					echo $output;
					?>
				</tbody>
				<tfoot></tfoot>
			</table>
		</div>
	</div><!-- container-fluid -->
	<script> javascript: window.print();  </script>
	<!-- setTimeout(window.close, 2500); -->
</body>
</html>