<?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"])){
//include "../actions/dbPDO.php";
$start = $_REQUEST["start"];
				$end   = $_REQUEST["end"];

				$myday = date("Y-m-d");
				$mms  = DateTime::createFromFormat( 'm-Y', $myday );
				?>
<!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="javscript:window.close( window.print() )" -->
    <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:11pt;"> سندات صرف من <?php echo $start ; ?> إلي <?php echo $end ;?> </h6>
				</div>
				<div class="col-2">
				</div>
			</div>
						<div class="form-row">
							<div class="col">
			 			<table class="table table-bordered">
						<thead>
	              <tr>
	                   <th class="align-middle text-center" rowspan="2" width="7%">م</th>
	                   <th class="align-middle text-center" rowspan="2" width="7%">كود السند</th>
	                   <th class="align-middle text-center" colspan="2" width="18%">المبلغ</th>
										 <th class="align-middle text-center" rowspan="2" width="8%">رقم السند</th>
										 <th class="align-middle text-center" rowspan="2" width="10%">تاريخ السند</th>
										 <th class="align-middle text-center" rowspan="2" width="8%">رقم العقد</th>
										 <th class="align-middle text-center" rowspan="2" width="10%">من</th>
										 <th class="align-middle text-center" rowspan="2" width="10%">إلي</th>
										 <th class="align-middle text-center" rowspan="2" width="20%">ملاحظات</th>
										 <th class="align-middle text-center" rowspan="2" width="4%">فرع</th>
	              </tr>
								<tr>
									<th class="text-center" width="8%">بعد 2017</th>
									<th class="text-center" width="10%">قبل 2018</th>
								</tr>
	            </thead>
			 												<tbody >
<?php
			$output = '';
		$sr = 3864 ; $vat2018 = 0 ; $vat2017 = 0; $sum2018 =0 ; $sum2017 = 0 ;
		// OR date_out >'2017-12-31'
	$joker =$db->prepare("SELECT outvoice.out_id , outvoice.money_out , outvoice.printed_no , outvoice.sanad_out , outvoice.contract_id ,
		 contracts.date_in , outvoice.branch , outvoice.comm , contracts.date_out  from outvoice , contracts
		  where outvoice.contract_id = contracts.contract_id AND sanad_out BETWEEN ? AND ? ORDER BY out_id ");
								$joker->bindParam(1,$start);
								$joker->bindParam(2,$end);
								$joker->execute();
						if($joker->rowCount()>0)
											{
									while($bo = $joker->fetch())
																	{
																		$comm = "";
																$date_out  = 	$bo['date_out']; $sanad_out = $bo['sanad_out'] ;
																$date_in  = 	$bo['date_in'];
																$money_out = 	round(($bo['money_out']),2);
																if(($date_in >'2017-12-31')||(empty($date_in)) ){$vat2018 = $money_out ;}
																else{  $vat2017 = $money_out ;}
										$output .= '<tr>
																		<td>' . $sr . '</td>
																		<td>' . $bo['out_id'] . '</td>
																		<td>' . $vat2018 . '</td>
																		<td>' . $vat2017 . '</td>
																		<td>' . $bo['printed_no'] . '</td>
																		<td>' . $sanad_out . '</td>
																		<td>' . $bo['contract_id'] . '</td>
																		<td>' . $date_out . '</td>
																		<td>' . $date_in . '</td>
																		<td>' . $bo['comm'].$comm . '</td>
																		<td>' . $bo['branch'] . '</td>
																</tr>  ';
																				$sum2018 = $sum2018 + $vat2018 ;
																				$sum2017 = $sum2017 + $vat2017 ;
																				 $sr = $sr + 1;
																				 $vat2018 = 0 ; $vat2017 = 0;
																			}
																$output .= '<tr>
																								<td colspan="2" >إجمالي</td>
																								<td >' . $sum2018. '</td>
																								<td >' . $sum2017. '</td>
																						</tr>';
																 }
															 else
															 {	$output .= '<tr><td colspan="11">لا يوجد سجلات ...</td></tr>'; }

															 echo $output; ?>
															</tbody>
			 												 <tfoot></tfoot>
			 											 </table>
			</div>
			 </div>

</div><!-- نهاية المطاف -->
    </body>
</html>
<?php } ?>
