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

$br = $_REQUEST["br"];
	include "../op/wanted.php";
if(isset($_REQUEST["dd"])){ $myday = $_REQUEST["dd"];	}
				else{$myday = date("Y-m-d"); }
				$first_day = date('Y-m-01', strtotime($myday));
				$last_day 	= date('Y-m-t', strtotime($myday));
				// $mms  = DateTime::createFromFormat( 'm-Y', $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.min.css"/>
      	 <link rel="stylesheet" href="../dist/css/day.css" />
	 </head>
    <body onload="javascript: window.print(); setTimeout(window.close, 1000);">
			<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 $mms ; ?> </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="8%"> عقد </th>
	                   <th class="text-center" width="30%">الإسم</th>
	                   <th class="text-center" width="6%">متأخرات</th>
	                   <th class="text-center" width="9%">الجوال</th>
	                   <th class="text-center" width="5%">ايام</th>
	                   <th class="text-center" width="12%">إقفال</th>
	                   <th class="text-center" width="10%">طراز</th>
	                   <th class="text-center" width="7%">رقم</th>
	                   <th class="text-center" width="7%">موديل</th>
	                   <th class="text-center" width="7%">إضافي</th>
	              </tr>
	            </thead>
	              <tbody id="myData">
	                <?php
	                $output = '';
				if(empty($br)){
	                  $sql =$db->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
	                     contracts.contract_id , contracts.date_in , contracts.days_qty , contracts.addtion_cost , contracts.violations , contracts.ins_claim
	                      from customers , contracts , cars
	                     WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
	                      AND contracts.cont_status > 2 AND contracts.date_in between ? AND ? order by contracts.date_in , contracts.contract_id   ");
										$sql->bindParam(1,$first_day);
										$sql->bindParam(2,$last_day);}
										else{
											$sql =$db->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
		                     contracts.contract_id , contracts.date_in , contracts.days_qty , contracts.addtion_cost , contracts.violations , contracts.ins_claim, contracts.wanted
		                      from customers , contracts , cars
		                     WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
		                      AND contracts.cont_status > 2 AND contracts.branch = ? AND contracts.date_in between ? AND ? order by contracts.date_in , contracts.contract_id ");
											$sql->bindParam(1,$br);
											$sql->bindParam(2,$first_day);
											$sql->bindParam(3,$last_day);}

	                  $sql->execute();
	                   if($sql->rowCount()>0)
	                  {
	                     while($row = $sql->fetch())
	                       {
							 $contract_id = $row['contract_id'];
								$wanted = new  Wanted($db,$contract_id) ;
								   $pay = $wanted->wanted() ;
								   $ww  = $pay["wanted"] ;
	                           $add = $row['addtion_cost']; $vio = $row['violations']; $ins = $row['ins_claim'];
	                           $ddd = $add + $vio + $ins ;
	                            $output .= '<tr>
	                                      <td>' . $contract_id . '</td>
	                                      <td>' . $row['cust_name'] . '</td>
	                                      <td>' . $ww . '</td>
	                                      <td>' . $row['phone'] . '</td>
	                                      <td class="text-center" >' . $row['days_qty'] . '</td>
	                                      <td class="text-center" >' . $row['date_in'] . '</td>
	                                      <td class="text-center" >' . $row['kind'] . '</td>
	                                      <td class="text-center" >' . $row['numbers'] . '</td>
	                                      <td class="text-center" >' . $row['model'] . '</td>
	                                      <td class="text-center" >' . $ddd . '</td>
	                                   </tr>  ';
	                       }
	                  }
	                  else
	                  {
	                       $output .= '<tr><td colspan="10">لا يوجد سجلات ...</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>
    </body>
</html>
