<?php
if (!isset($_SESSION)) {
	session_start();
}
ob_start();
$father = $_SESSION["user_id"];
if (!isset($_SESSION["user_id"])) {
	header("location:../logout.php");
}
include "../actions/dbPDO.php";
if (isset($_REQUEST["dd"])) {
	$myday = $_REQUEST["dd"];
}
$first_day  = date('Y-m-01', strtotime($myday));
$last_day 	= date('Y-m-t', strtotime($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>تارجت الأقساط</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="row" style="padding:0 ;">
			<div class="col-9" style="float:right;">
				<p style="font-weight:bold;font-size:10pt;"> تارجت الاقساط <?php echo " خلال شهر " . $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="form-row">
			<div class="col">
				<table class="table table-bordered">
					<thead>
						<tr>
							<th class="align-middle text-center" width="4%">م</th>
							<th class="align-middle text-center" width="9%">المبلغ</th>
							<th class="align-middle text-center" width="31%">اسم المستأجر</th>
							<th class="align-middle text-center" width="11%">الطراز</th>
							<th class="align-middle text-center" width="12%">لوحة رقم</th>
							<th class="align-middle text-center" width="7%">موديل</th>
							<th class="align-middle text-center" width="7%">رقم القسط</th>
							<th class="align-middle text-center" width="12%">تاريخ السداد</th>
							<th class="align-middle text-center" width="7%">عقد رقم</th>
						</tr>
					</thead>
					<tbody>
						<?php
						$output = '';
						$sql = $db->prepare("SELECT aksat_in.ak_id , aksat_in.ak_no , aksat_in.payed ,aksat_in.date_payed ,aksat_in.contract_id,customers.cust_name , cars.kind ,cars.model, concat(cars.letters,' ',cars.numbers) as PL
						FROM aksat_in , aksat_contract , customers , cars
						WHERE aksat_contract.contract_id = aksat_in.contract_id
						AND aksat_contract.carid = cars.carid
						AND aksat_contract.cust_id = customers.cust_id
						AND aksat_in.date_payed BETWEEN ? AND ?  ORDER BY aksat_in.date_payed,aksat_in.contract_id,aksat_in.ak_no");
						$sql->bindParam(1, $first_day);
						$sql->bindParam(2, $last_day);
						$sql->execute();
			if ($sql->rowCount() > 0) {
							$ss = 1;
							$tot_money = 0;
					while ($row = $sql->fetch()) {
								$output .= '<tr>
							<td>' . $ss . '</td>
	 						<td>' . $row['payed'] . '</td>
							<td>' . $row['cust_name'] . '</td>
							<td>' . $row['kind'] . '</td>
	 						<td>' . $row['PL'] . '</td>
	 						<td>' . $row['model'] . '</td>
							<td>' . $row['ak_no'] . '</td>
							<td>' . $row['date_payed'] . '</td>
							<td>' . $row['contract_id'] . '</td>
	 					</tr>';
								$ss = $ss + 1;
								$tot_money = $tot_money + $row['payed'];
							}
							$output .= '<tr><td colspan="9"></td></tr>
									<tr><td colspan="2"  class="text-left" >' . $tot_money . '</td>
										<td colspan="7" style="font-size:12px;">إجمالي إيرادات</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>