<?php

require_once __DIR__ . '/../magic/auth.php';

$auth = bdr_require_privilege(38, '../logout.php');
$user = $auth['user_id'];
$father = $auth['user_id'];
$branch = $auth['branch'];
$conn = $auth['conn'];
$db = $auth['conn'];

if(isset($_REQUEST["id"]))
	{
		$idd = $_REQUEST["id"] ;
  	$output2 = "";
		$output3 = "";
		$sql = $db->prepare("SELECT car_ment_v.carid , car_ment_v.kind , car_ment_v.numbers , car_ment_v.model , car_ment_v.req_id ,
     car_ment_v.qty , car_ment_v.tot_price , supp_mat_v.mat_name , supp_mat_v.invoice_no , supp_mat_v.mat_invoice_date ,
     supp_mat_v.item_name , supp_mat_v.store_name
     from supp_mat_v , car_ment_v
     where supp_mat_v.line_id = car_ment_v.line_in
     AND car_ment_v.req_id =?");
	$sql->bindParam(1,$idd);
	$sql->execute();
	if($sql->rowCount()>0){
    $summ = 0;
		$output2 = '<div class="table-responsive">
				 <table class="table" bordered="1">
					 <thead>
							<tr>
									 <th class="text-center" width="10%">إجمالى</th>
									 <th class="text-center" width="5%">الكمية</th>
									 <th class="text-center" width="25%">اسم الصنف</th>
									 <th class="text-center" width="10%">المخزن</th>
									 <th class="text-center" width="25%">المورد</th>
									 <th class="text-center" width="10%">الفاتورة</th>
									 <th class="text-center" width="15%">تاريخ الفاتورة</th>
								</tr>
							</thead>
							<tbody >';
	while($row = $sql->fetch())

	{
		 $output2 .= '<tr>
										<td class="text-center">' . $row['tot_price'] .'</td>
										<td class="text-center">' . $row['qty'] . '</td>
										<td class="text-center">' . $row['item_name'] . '</td>
										<td class="text-center">' . $row['store_name'] . '</td>
              			<td class="text-center">' . $row['mat_name'] . '</td>
              			<td class="text-center">' . $row['invoice_no'] . '</td>
										<td class="text-center">' . $row['mat_invoice_date'] . '</td>
								</tr> ';
       $summ 		= $summ + $row['tot_price'] ;
       $kind 		= $row['kind'];
	     $numm 		= $row['numbers'];
       $model 	= $row['model'];
       $req_id 	= $row['req_id'];
			}
$output2 .='<tr>
                <td colspan="2">'.$summ.'</td>
                <td> إجمالي التكلفة </td>
            </tr>';
$output3 .=' <h4 class="text-center" style="font-weight:bold;">سيارة من نوع '.$kind.' لوحة رقم '.$numm.' موديل '.$model.' رقم طلب الصيانة '.$req_id.'</h4>';
$output3 .= $output2 ;
	}
	else
	{	$output3 .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';	}
	header('Content-Type: application/vnd.ms-excel');
 	header('Content-Disposition: attachment; filename=مصروفات طلب الصيانة.xls');
 echo $output3;
}
?>
