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

$father = $_SESSION["user_id"]; $br = "";
if(isset($_REQUEST["last"])){$last_day = $_REQUEST["last"];}
if(isset($_REQUEST["br"])){ $br = $_REQUEST["br"];}
	include "../op/co_wanted.php";
?>
<!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>
    <body>
			<div class="container-fluid">
			<div class="row" style="padding:5 ;">
				<div class="col-xs-10"style="float:right;">
					<h6 style="font-weight:bold;padding:3px;font-size:10pt;" > تقرير المديونيات حتي <?php echo$last_day ; ?> </h6>
				</div>
				<div class="col-xs-2"style="float:right;">
				</div>
			</div>
			<div class="table-responsive">
	         <table class="table table-bordered table-hover">
	           <thead>
	              <tr>
										<th class="text-center" width="5%"> م </th>
	                  <th class="text-center" width="8%"> رقم العقد </th>
	                  <th class="text-center" width="26%">الإسم</th>
	                  <th class="text-center" width="10%"> من </th>
	                  <th class="text-center" width="10%"> إلي </th>
										<th class="text-center" width="12%"> سيارة </th>
										<th class="text-center" width="10%">لوحة رقم</th>
	                  <th class="text-center" width="8%">قيمة العقد</th>
	                  <th class="text-center" width="8%">المدفوع</th>
										<th class="text-center" width="8%">خصم</th>
	                  <th class="text-center" width="8%">المستحق</th>
	              </tr>
	            </thead>
	              <tbody >
	                <?php
	                $output = '';
									if(empty($br)){
	                  $sql =$db->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , concat(cars.letters,' ', cars.numbers) as PL , cars.model ,
	                     contracts.contract_id , contracts.date_in , contracts.date_out   from customers , contracts , cars
	                     WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
	                       AND contracts.date_in <= ? and (close_at > ? OR close_at IS NULL) order by contracts.branch , contracts.date_in ,contracts.contract_id ");
										$sql->bindParam(1,$last_day);
										$sql->bindParam(2,$last_day);}
					else{
							$sql =$db->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , concat(cars.letters,' ', cars.numbers) as PL , cars.model ,
								 contracts.contract_id , contracts.date_in , contracts.date_out  from customers , contracts , cars
								 WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid AND contracts.branch = ?
									 AND contracts.date_in <= ? and (close_at > ? OR close_at IS NULL) order by contracts.branch ,contracts.date_in , contracts.contract_id");
								$sql->bindParam(1,$br);
								$sql->bindParam(2,$last_day);
								$sql->bindParam(3,$last_day);}
	              $sql->execute();
	      if($sql->rowCount()>0)
	           {$nn = 1 ; $tot_wanted = 0 ;
	      while($row = $sql->fetch())
	           { $ss   = $row['contract_id'];
	             $wwc  = wanted_at($ss,$last_day);
	             $ww   = $wwc["wanted"] ;
							 $after_vat =  $wwc["after_vat"] ;
							 $payed 		=  $wwc["payed"] ;
							 $total_cut =  $wwc["total_cut"] ;
								if($ww == 0){continue; }
	                   $output .= '<tr>
																		<td>' . $nn . '</td>
	                                  <td>' . $ss . '</td>
	                                  <td>' . $row['cust_name'] . '</td>
	                                  <td class="text-center" >' . $row['date_out'] . '</td>
											 						  <td class="text-center" >' . $row['date_in'] . '</td>
	                                  <td class="text-center" >' . $row['kind'] . '</td>
	                                  <td class="text-center" >' . $row['PL'] . '</td>
	                                  <td class="text-center" >' . $after_vat . '</td>
	                                  <td class="text-center" >' . $payed . '</td>
	                                  <td class="text-center" >' . $total_cut . '</td>
	                                  <td class="text-center" >' . $ww . '</td>
	                                </tr> ';$nn = $nn + 1 ;$tot_wanted = $tot_wanted + $ww ;
	                       }
									$output .= '<tr><td colspan="11"></td></tr>
															<tr>
												 				<td colspan="10" style="font-size:12px;" class="text-left">إجمالي المستحق حتي تاريخه</td>
																<td>' . $tot_wanted . '</td>
															</tr>';
	                  }
	              else { $output .= '<tr><td colspan="11">لا يوجد سجلات ...</td></tr>'; }
	                  echo $output;
	                  ?>
	              </tbody>
							<tfoot></tfoot>
	          </table>
	    </div>
		</div>
	 <script src="../js/jquery.js"></script>
	 <script src="../dist/js/umd/popper.min.js"></script>
	 <script src="../dist/js/bootstrap.min.js"></script>
	 <script>	javscript:window.close( window.print() ); </script>
    </body>
</html>
