<?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["dd"])){
$myday      = $_REQUEST['dd'] ; 
	  $start  = date('Y-m-01', strtotime($myday));
	  $end   = date('Y-m-t', strtotime($myday));
				$myday = date("Y-m-d");
				$mms  = DateTime::createFromFormat( 'm-Y', $myday );
			$output = '<div class="row" style="padding:10 ;">
				<div class="col-12">
					<h1 style="font-weight:bold;padding:3px;font-size:22px;">استحقاق من مؤسسة مناحي بجاد مناحي الغرمول البقمي للمقاولات العامة</h1>
				</div>
				</div>
				<div class="row" style="padding:10 ;">
				<div class="col-6">
				</div>
				<div class="col-6" style="float:left;">
					<h6 style="font-weight:bold;padding:3px;font-size:16px;">  في الفترة من '.$start.'  إلي '.$end.' </h6>
				</div>
			</div> ' ;
				$output .='<div class="form-row">
							<div class="col">
			 					<table class="table table-bordered">
			 						<thead>
										<tr>
											<th class="align-middle text-center" width="4%"> م </th>
											<th class="align-middle text-center" width="8%">رقم الفاتورة</th>
											<th class="align-middle text-center" width="31%">اسم المستخدم</th>
											<th class="align-middle text-center" width="8%">رقم العقد</th>
											<th class="align-middle text-center" width="8%">قيمة الفاتورة</th>
											<th class="align-middle text-center" width="8%">الضريبة</th>
											<th class="align-middle text-center" width="8%">بعد الضريبة</th>
											<th class="align-middle text-center" width="25%">تعليق</th>	
										</tr>
			 						</thead>
			 					<tbody >' ;
			$output2 = '';
			$sr = 1 ;		$a = 0 ; $b = 0; $e = 0;
			$cust_id = 15824;
				$joker =$db->prepare("SELECT vat.vat_id , vat.contract_id ,vat.net_cost , vat.vat_req , vat.after_vat ,
												customers.cust_name
									FROM contracts , customers , vat
								 WHERE vat.contract_id = contracts.contract_id AND
								  contracts.cust_id2 = customers.cust_id AND end BETWEEN ? AND ?
								  AND contracts.cust_id = $cust_id
								   ORDER BY vat_id ");
									 $joker->bindParam(1,$start);
									 $joker->bindParam(2,$end);
									 $joker->execute();
							 if($joker->rowCount()>0)
									{
								 while($bo = $joker->fetch())
											{
										$output2 .= '<tr>
											 			<td>' . $sr . '</td>
														<td>' . $bo['vat_id'] . '</td>
														<td>' . $bo['cust_name'] . '</td>
														<td>' . $bo['contract_id'] . '</td>
														<td>' . $bo['net_cost'] . '</td>
														<td>' . $bo['vat_req'] . '</td>
														<td>' . $bo['after_vat'] . '</td>
														<td></td>
													</tr>  ';
														$a = $a + $bo['net_cost'] ;
														$b = $b + $bo['after_vat'] ;
														$e = $e + $bo['vat_req'] ;
														$sr = $sr + 1;
											 }
									$output2 .= '<tr><td colspan="10" ></td>';
									$output2 .= '<tr>
													<td colspan="4" >إجمالي</td>
													<td >' . $a. '</td>
													<td >' . $e. '</td>
													<td >' . $b. '</td>
													<td ></td>
											</tr>';
									}
		else {	$output2 .= '<tr><td colspan="8">لا يوجد سجلات ...</td></tr>'; }
		$output = $output.$output2; }
		$output .='	</tbody><tfoot></tfoot></table></div></div>';
	header('Content-Type: application/vnd.ms-excel');
	header('Content-Disposition: attachment; filename=كشف البقمي'.$mms.'.xls');
	echo $output;
?>
