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

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

if(isset($_REQUEST["start"])){
$start = $_REQUEST["start"];
				$end   = $_REQUEST["end"];
				$myday = date("Y-m-d");
				$mmm   = new DateTime($myday);
				$mms   =  $mmm->format("m Y");
				$first =  new DateTime($start);
				$last  =  new DateTime($end);
				$mad = date('Y-m-d H:i:s',strtotime($end." +23 hour +59 minutes +59 seconds"));  
				$mado = new DateTime($mad);
				$mado = $mado->format('Y-m-d H:i:s'); 
				?>
<!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.min.css"/>
      	 <link rel="stylesheet" href="../dist/css/day.css" />
				 <script src="../js/jquery.js"></script>
				 <script src="../dist/js/umd/popper.min.js"></script>
			 	<script src="../dist/js/bootstrap.min.js"></script>
	 </head>
	 <!-- onload="javascript: window.print(); setTimeout(window.close, 1000);" -->
    <body>
			<div class="container-fluid">
			<div class="form-row" style="padding:5 ;">
				<div class="col-4">
					<h6 style="font-weight:bold;padding:3px;font-size:11px;">مؤسسة الوسام المعتمد فرع تأجير السيارات</h6>
				</div>
				<div class="col-4">
					<h6 style="font-weight:bold;padding:3px;font-size:11px;">الرقم الضريبي  ٣٠٠٠٩٨٥٣٢٥٠٠٠٠٣ </h6>
				</div>
				<div class="col-4" style="float:left;">
					<h6 style="font-weight:bold;padding:3px;font-size:10px;"> وارد القيمة المضافة من <?php echo $first->format('d-m-Y') ; ?> إلي <?php echo $last->format('d-m-Y') ;?> </h6>
				</div>
			</div>
					<div class="table-responsive">
			 					<table class="table table-bordered">
			 							<thead>
										 <tr>
										 	<th class="align-middle text-center" width="2%">م</th>
											<th class="align-middle text-center" width="5%">رقم الفاتورة</th>
			 								<th class="align-middle text-center" width="27%">اسم المستأجر</th>
											<th class="align-middle text-center" width="11%">تاريخ الإستحقاق</th>
											<th class="align-middle text-center" width="6%">قيمة الإيجار</th>
											<th class="align-middle text-center" width="5%">الضريبة</th>
			 								<th class="align-middle text-center" width="6%">بعد الضريبة</th>
											<th class="align-middle text-center" width="26%">تعليق</th>
			 								<th class="align-middle text-center" width="5%">المستحق</th>
											<th class="align-middle text-center" width="6%">رقم العقد</th>
			 							</tr>
			 							</thead>
			 						<tbody >
<?php
			$output = '';
	 $a = 0 ; $b = 0; $c =0 ; $d = 0; $e = 0;
						$joker =$db->prepare("SELECT vat.vat_id , vat.contract_id ,vat.start,vat.duedate , vat.end , vat.net_cost , vat.vat_req , vat.after_vat ,contracts.wanted , vat.comm, customers.cust_name
							 FROM contracts , customers , vat
							WHERE vat.contract_id = contracts.contract_id AND contracts.cust_id = customers.cust_id AND duedate BETWEEN ? AND ? ORDER BY vat_id ");
								$joker->bindParam(1,$start);
								$joker->bindParam(2,$mado);
								$joker->execute();
						if($joker->rowCount()>0){
									$nn = 1 ;
									while($bo = $joker->fetch()){
										
										$duedate	= $bo['duedate']   ;
										$duedate 	= new DateTime($duedate);
            							$duedate 	=  $duedate->format("Y-m-d");

										$output .= '<tr>
														<td>' . $nn . '</td>
														<td>' . $bo['vat_id'] . '</td>
														<td>' . $bo['cust_name'] . '</td>
														<td>' . $duedate . '</td>
														<td>' . $bo['net_cost'] . '</td>
														<td>' . $bo['vat_req'] . '</td>
														<td>' . $bo['after_vat'] . '</td>
														<td>' . $bo['comm'] . '</td>
														<td>' . $bo['wanted'] . '</td>
														<td>' . $bo['contract_id'] . '</td>
												</tr>  ';
														$a = $a + $bo['net_cost'] ;
														$b = $b + $bo['after_vat'] ;
														$d = $d + $bo['wanted'] ;
														$e = $e + $bo['vat_req'] ;
														$nn++;
												}
									$output .= '<tr><td colspan="10" ></td>';
									$output .= '<tr>
													<td colspan="4" >إجمالي</td>
													<td >' . $a. '</td>
													<td >' . $e. '</td>
													<td >' . $b. '</td>
													<td ></td>
													<td >' . $d. '</td>
											</tr>';
											 }
										else {	$output .= '<tr><td colspan="11">لا يوجد سجلات ...</td></tr>'; }
										echo $output; }?>
					</tbody>
			 			<tfoot></tfoot>
			 		</table>
			</div>
</div><!-- نهاية المطاف -->
    </body>
</html>