<?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");
				$mms  = DateTime::createFromFormat( 'm-Y', $myday );
				$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 .='<table class="table table-bordered">
				<thead>
				<tr>
					<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="8%">رقم الفاتورة</th>
					<th class="align-middle text-center" width="12%">تاريخ الفاتورة</th>
					 <th class="align-middle text-center" width="28%">اسم المورد</th>
					 <th class="align-middle text-center" width="20%">الرقم الضريبي</th>
					<th class="align-middle text-center" width="10%">تعليق</th>
					 <th class="align-middle text-center" width="8%">قبل الضريبة</th>	 
				</tr>
					</thead>
				<tbody >' ;

			$output2 = '';
			$a = 0 ; $b = 0;
			$joker =$db->prepare("SELECT material_in.invoice_no,material_in.net_cost , material_in.mat_invoice_date, material_in.vat_out , material_supp.mat_name , material_supp.vat_no 
						FROM material_in, `material_supp`
						 WHERE material_in.mat_supp_id = material_supp.mat_supp_id AND vat_out > 0
						AND mat_invoice_date BETWEEN ? and ? 
						ORDER BY material_supp.mat_supp_id , mat_invoice_date");
				$joker->bindParam(1,$start);
				$joker->bindParam(2,$end);
				$joker->execute();
			if($joker->rowCount()>0)
				{ $nn = 0;
				while($bo = $joker->fetch())
					{ $nn++; $net_cost =  $bo['net_cost'] ; $vat_out =  $bo['vat_out'] ;$nett = $net_cost + $vat_out ;
				$output2 .= '<tr>
								<td>' . $nn . '</td>
								<td>' . $nett . '</td>
								<td>' . $bo['invoice_no'] . '</td>
								<td>' . $bo['mat_invoice_date'] . '</td>
								<td>' . $bo['mat_name'] . '</td>
								<td>' . $bo['vat_no'] . '</td>
								<td></td>
								<td>' . $net_cost . '</td>
						</tr>  ';
									$a = $a + $nett ;
									$b = $b + $net_cost ;
									}
						$output2 .= '<tr><td colspan="9" ></td>';
						$output2 .= '<tr>
										<td colspan=(2) >' . $a. '</td>
										<td >إجمالي</td>
										<td colspan="2"></td>
										<td colspan="2" >إجمالي فواتير قبل الضريبة</td>
										<td colspan="2">'.$b. '</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;
		?>
