<?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'];

include "../op/wanted.php";
$start = $_REQUEST["start"];
			$end   = $_REQUEST["end"];
		?>
<!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" />
	 </head>
	 <!-- onload="javascript: window.print(); setTimeout(window.close, 1000);" -->
    <body >
			<div class="container-fluid">
			<div class="row" style="padding:5 ;">
				<div class="col-9"style="float:right;">
					<h6 style="font-weight:bold;padding:3px;font-size:10pt;"> فحص الفواتير مع العقود من <?php echo $start." إلي ".$end ; ?> </h6>
				</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="row">
		<div class="col">
				</div>
			 	</div>
			 		<table class="table table-bordered">
			 		<thead>
			 			<tr>
			 				<th class="align-middle text-center" width="5%">م</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="10%">اجمالي فواتير</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="12%">تاريخ العودة</th>
			 				<th class="align-middle text-center" width="10%">المدفوع</th>
							<th class="align-middle text-center" width="13%">ملاحظات</th>
							<th class="align-middle text-center" width="10%">المديونية</th>
			 			</tr>
			 		</thead>
			<tbody >
<?php
	$output = '';
	$sql =$db->prepare("SELECT contract_id , wanted,date_out,date_in , time_in
						 FROM `contracts`
					WHERE date_in BETWEEN ? and ? ORDER BY branch, date_in");
	$sql->bindParam(1,$start);
	$sql->bindParam(2,$end);
	$sql->execute();
 if($sql->rowCount()>0)
				{$money = 0 ; $no = 1; $falts = 0 ; $ww = 0;
					while($row = $sql->fetch())
							{
					 $contract_id = $row['contract_id'];
             		 $wanted = new  Wanted($db,$contract_id) ;
                	 $pay = $wanted->wanted() ;
					 $after_vat = $pay['after_vat'];
					 $totalVat = $wanted->totalVat();
					 $totalDeduction = $wanted->totalDeduction() ;
					 $falt = $after_vat - $totalVat ;
					$output .= '<tr>
									<td>'.$no.'</td>
									<td>' . $contract_id . '</td>
									<td>' . $after_vat . '</td>
									<td>' . $totalVat . '</td>
									<td>' .  $totalDeduction . '</td>
									<td>' .  $falt . '</td>
									<td>' . $row['date_in'] . '</td>
									<td>'. $pay['payed'] .'</td>
									<td></td>
									<td>'. $pay['wanted'] .'</td>
								</tr>  ';
			$money = $money + $row['wanted'] ;
			$falts = $falt + $falts;
			$ww = $ww + $row['wanted'] ;
			$no = $no +1 ;
		}
		$output .= '<tr><td colspan="10"></td></tr>
					<tr>
						<td colspan="2">شامل الضريبة</td>
						<td>' . $money. '</td>
						<td colspan="2">اجمالي انحراف</td>
						<td>' . $falts. '</td>
						<td></td>
						<td colspan="2">اجمالي مديونية</td>
						<td>' . $ww. '</td>
						</tr>';
			} else {$output .= '<tr><td colspan="10">لا يوجد سجلات ...</td></tr>';}
		 echo $output; ?>
			</tbody>
		<tfoot></tfoot>
	</table>
</div>
<script src="../js/jquery.js"></script>
<script src="../dist/js/umd/popper.min.js"></script>
<script src="../dist/js/bootstrap.js"></script>
</body>
</html>