<?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"])){
$start = $_REQUEST["start"];
				$end   = $_REQUEST["end"];
				$myday = date("Y-m-d");
				$mmm   = new DateTime($myday);
				$mms   =  $mmm->format("Ymd");
				$comm ="";

			$output = '<div class="form-row" style="padding:5 ;">
				<div class="col-4">
					<h6 style="font-weight:bold;padding:3px;font-size:11px;">مؤسسة الوسام المعتمد فرع تأجير السيارات</h6>
				</div>
				<div class="col-4">
					<h6 style="font-weight:bold;padding:3px;font-size:11px;">الرقم الضريبي  ٣٠٠٠٩٨٥٣٢٥٠٠٠٠٣ </h6>
				</div>
				<div class="col-4" style="float:left;">
					<h6 style="font-weight:bold;padding:3px;font-size:10px;"> وارد القيمة المضافة من '.$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="4%">رقم الفاتورة</th>
													<th class="align-middle text-center" width="27%">اسم المستأجر/العميل</th>
													<th class="align-middle text-center" width="9%">تاريخ الإستحقاق</th>
													<th class="align-middle text-center" width="6%">رقم العقد</th>
													<th class="align-middle text-center" width="6%">قيمة الفاتورة</th>
													<th class="align-middle text-center" width="8%">الضريبة</th>
													<th class="align-middle text-center" width="5%">بعد الضريبة</th>
													<th class="align-middle text-center" width="20%">تعليق</th>
													<th class="align-middle text-center" width="6%">المستحق</th>
											</tr>
			 						</thead>
			 						<tbody >' ;

			$output2 = '';
			$sr = 1 ;		$a = 0 ; $b = 0; $c =0 ; $d = 0; $e = 0;
				$joker =$db->prepare("SELECT vat.vat_id , vat.contract_id ,vat.start , vat.end ,vat.duedate , vat.net_cost , vat.vat_req , vat.after_vat ,
												contracts.wanted , vat.addtion_desc, customers.cust_name
									FROM contracts , customers , vat
								 WHERE vat.contract_id = contracts.contract_id AND contracts.cust_id = customers.cust_id 
								 				AND duedate BETWEEN ? AND ?  
												ORDER BY vat_id ");
									 $joker->bindParam(1,$start);
									 $joker->bindParam(2,$end);
									 $joker->execute();
							 if($joker->rowCount()>0)
												 {
										 while($bo = $joker->fetch()){
											$duedate	= $bo['duedate']   ;
											$duedate = new DateTime($duedate);
            								$duedate =  $duedate->format("Y-m-d");
								if($bo['after_vat'] == 0){$comm = $bo['addtion_desc'] ; }
											 $output2 .= '<tr>
											 				<td>' . $sr . '</td>
															<td>' . $bo['vat_id'] . '</td>
															<td>' . $bo['cust_name'] . '</td>
															<td>' . $duedate . '</td>
															<td>' . $bo['contract_id'] . '</td>
															<td>' . $bo['net_cost'] . '</td>
															<td>' . $bo['vat_req'] . '</td>
															<td>' . $bo['after_vat'] . '</td>
															<td>' . $comm . '</td>
															<td>' . $bo['wanted'] . '</td>
												 </tr>  ';
																	$a = $a + $bo['net_cost'] ;
																    $b = $b + $bo['after_vat'] ;
																	$d = $d + $bo['wanted'] ;
																	$e = $e + $bo['vat_req'] ;
																	$sr = $sr + 1;
																	$comm ="";
																}
																$output2 .= '<tr><td colspan="10" ></td>';
																$output2 .= '<tr>
																				<td colspan="5" >إجمالي</td>
																					<td >' . $a. '</td>
																					<td >' . $e. '</td>
																					<td >' . $b. '</td>
																					<td ></td>
																					<td >' . $d. '</td>
																			</tr>';
																		}
																	else {	$output2 .= '<tr><td colspan="10">لا يوجد سجلات ...</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;
		?>
