<?php
ob_start();
session_name('CAKEPHP'); session_start();
include('exportconfig.php');
class db_query
{
	function getField($table, $condition, $val, $field)
	{
		global $db;
		$sql = mysqlI_query($db, "select " . $field . " from " . $table . " where " . $condition . " ='" . $val . "'");
		$r = mysqlI_fetch_array($sql);
		//echo "select ".$field." from ".$table." where ".$condition." ='".$val."'"; 
		return ($r[$field]);
	}
}
$s = new db_query();




$data = '';
$sql = "Select * from addmrn where id='" . $_REQUEST['mrnid'] . "'";
$result = mysqli_query($db, $sql);
$datarow = mysqli_fetch_array($result);
$data .= "<table width='100%' border='0' cellspacing='2' cellpadding='1' align='center'>";
$data .= "<tr>";
$data .= "<td><strong>";
$data .= "General details";
$data .= "</strong></td>";
$data .= "</tr>";
$data .= "<tr>";
$data .= "<th align='left'><strong>Mrn No. </strong></th>";
$data .= "<td>";
$data .= $datarow['MRNNo'];
$data .= "</td>";
$data .= "<th align='left'><strong>MRN Date </strong></th>";
$data .= "<td>";
$data .= $datarow['MRNDate'];
$data .= "</td>";
$data .= "</tr>";
$data .= "<tr>";
$data .= "<th align='left'><strong>Branch</strong></th>";
$data .= "<td>";
$site_sql = "Select locationname from locations where loccode ='" . $datarow['site'] . "'";
$site_res = mysqli_query($db, $site_sql);
$site_row = mysqli_fetch_array($site_res);
$data .= $site_row['locationname'];
$data .= "</td>";
$data .= "<th align='left'><strong>Store</strong></th>";
$data .= "<td>";
$store_sql = "select  StoreName from storetype where id='" . $datarow['StoreCode'] . "' ";
$store_result = mysqli_query($db, $store_sql);
$store_row = mysqli_fetch_array($store_result);
$data .= $store_row['StoreName'];
$data .= "</td>";
$data .= "</tr>";
$data .= "<tr>";

$data .= "<th align='left'><strong>Received Against</strong></th>";
$data .= "<td>";
$data .= $datarow['ReceivedAgainst'];
$data .= "</td>";
$data .= "<th align='left'><strong>Recevied From </strong></th>";
$data .= "<td>";
$vendor_sql = "select  vendor_name from suppliers where vendor_code='" . $datarow['ReceivedFrom'] . "' ";
$vendor_result = mysqli_query($db, $vendor_sql);
$vendor_row = mysqli_fetch_array($vendor_result);
$data .= $vendor_row['vendor_name'];
$data .= "</td>";
$data .= "</tr>";
$data .= "<tr>";
$data .= "<th align='left'><strong>PO/Challan/WO/ Ref.No.</strong></th>";
$data .= "<td>";
$data .= $datarow['PO_WO_MTO'];
$data .= "</td>";
$data .= "<th align='left'><strong>Order Date </strong></th>";
$data .= "<td>";
$data .= $datarow['OrderDate'];
$data .= "</td>";
$data .= "</tr>";
$data .= "</table>";
$data .= "<table>";
$data .= "<tr>";
$data .= "<td>&nbsp;";

$data .= "</td>";

$data .= "</tr>";

$data .= "</table>";
$data .= "<table width='100%' border='0' cellspacing='2' cellpadding='1' align='center'>";
$data .= "<tr>";
$data .= "<th align='left'><strong>Document Type</strong></th>";
$data .= "<td>";
$data .= $datarow['DocumentType'];
$data .= "</td>";

$data .= "<th align='left'><strong>No. </strong></th>";
$data .= "<td>";
$data .= $datarow['BillNo'];
$data .= "</td>";
$data .= "<th align='left'><strong> Voucher Date</strong></th>";
$data .= "<td>";
$data .= $datarow['BillDate'];
$data .= "</td>";
$data .= "<th align='left'><strong>Inwards Gate Pass No.</strong></th>";
$data .= "<td>";
$data .= $datarow['GetPassNo'];
$data .= "</td>";
$data .= "<th align='left'><strong>Inwards Date</strong></th>";
$data .= "<td>";
$data .= $datarow['Inwardsdate'];
$data .= "</td>";
$data .= "</tr>";
$data .= "<tr>";
$data .= "<th align='left'><strong>Transporter Name</strong></th>";
$data .= "<td>";
$data .= $datarow['Transp_name'];
$data .= "</td>";
$data .= "<th align='left'><strong>Vehicle No</strong></th>";
$data .= "<td>";
$data .= $datarow['VehicleNo'];
$data .= "</td>";
$data .= "<th align='left'><strong>Time In </strong></th>";
$data .= "<td>";
$data .= $datarow['TimeIn'];
$data .= "</td>";
$data .= "<th align='left'><strong>Time Out </strong></th>";
$data .= "<td>";
$data .= $datarow['Timeout'];
$data .= "</td>";
$data .= "</tr>";
$data .= "<tr>";


$data .= "<th align='left'><strong>LR No</strong></th>";
$data .= "<td>";
$data .= $datarow['LRno'];
$data .= "</td>";
$data .= "<th align='left'><strong>LR Date</strong></th>";
$data .= "<td>";
$data .= $datarow['LRdate'];
$data .= "</td>";
$data .= "<th align='left'><strong>Total weight</strong></th>";
$data .= "<td>";
$data .= $datarow['TotalWeight'];
$data .= "</td>";
$data .= "</tr>";
$data .= "<tr>";
$data .= "<th align='left' colspan='2'><strong>Remarks</strong></th>";
$data .= "<td colspan='2'>";
$data .= $datarow['RemarksIfAny'];
$data .= "</td>";
$data .= "</tr>";
$data .= "</table>";
$data .= "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
$data .= "<tr>";
$data .= "<td><strong>";
$data .= "Item details";
$data .= "</strong></td>";
$data .= "</tr>";
$data .= "<tr>";
$data .= "<th><strong>S.No.</strong></th>";
$data .= "<th><strong>ITEM CODE </strong></th>";
$data .= "<th><strong>DESCRIPTION</strong></th>";
$data .= " <th><strong>MAKE</strong></th>";
$data .= " <th><strong>GRADE</strong></th>";
$data .= "<th scope='col'><strong>COLOR</strong></th>";
$data .= "<th scope='col'><strong>UOM</strong></th>";
$data .= "<th scope='col'><strong>PO QTY </strong></th>";
$data .= "<th scope='col'><strong>QTY AS PER BILL </strong></th>";
$data .= "<th scope='col'><strong>QTY ACCEPTED </strong></th>";
$data .= "<th scope='col'><strong>SHORT QUANTITY </strong></th>";
$data .= "<th scope='col'><strong>RATE</strong></th>";
$data .= "<th scope='col'><strong>TOTAL ITEM PRICE</strong></th>";
$data .= "<th scope='col'><strong>TAXABLE AMOUNT </strong></th>";
$data .= "<th scope='col'><strong>VAT AMOUNT</strong></th>";
$data .= "<th scope='col'><strong>TOTAL AMOUNT</strong></th>";

$data .= "<th scope='col'><strong>REMARKS</strong></th>";
$data .= "</tr>";

$qtysql = "Select * from addmrnqty where issue_id='" . $datarow['id'] . "'";
$qtyres = mysqli_query($db, $qtysql);

$i = 1;

while ($qtyrow = mysqli_fetch_array($qtyres)) {

	$datarow['id'];
	$itemsql = "select * from item_master where StockID='" . $qtyrow['itemcode'] . "'";
	$itemres = mysqli_query($db, $itemsql);
	$itemrow = mysqli_fetch_array($itemres);
	$data .= "<tr>";
	$data .= "<td>" . $i . "</td>";
	$data .= "<td>" . $qtyrow['itemcode'] . "</td>";
	$data .= "<td>" . $itemrow['item_name'] . "</td>";
	$data .= "<td>" . $makerow = $s->getField('product_manufacture', 'id', $itemrow['make'], 'manufacturer') . "</td>";
	$data .= "<td>" . $graderow = $s->getField('product_grade', 'id', $itemrow['grade'], 'grade') . "</td>";
	$data .= "<td>" . $colorrow = $s->getField('product_color', 'id', $itemrow['color'], 'color') . "</td>";
	$data .= "<td>" . $unitrow = $s->getField('unitsofmeasure', 'unitid', $itemrow['unit'], 'unitname') . "</td>";

	$data .= "<td>" . $qtyrow['POQty'] . "</td>";
	$data .= "<td>" . $qtyrow['qty_as_per_bill'] . "</td>";
	$data .= "<td>" . $qtyrow['reciveqty'] . "</td>";
	$data .= "<td>" . $qtyrow['short_accessid'] . "</td>";
	$data .= "<td>" . $qtyrow['itemprice'] . "</td>";
	$data .= "<td>" . $qtyrow['item_total_rate'] . "</td>";
	$data .= "<td>" . $qtyrow['taxamount_rate'] . "</td>";
	$data .= "<td>" . $qtyrow['val_after_disc'] . "</td>";
	$data .= "<td>" . $qtyrow['total_amt_with_tax'] . "</td>";

	$data .= "<td>" . $qtyrow['remarks'] . "</td>";

	$data .= "</tr>";
	$i++;
}

$data .= "</table>";


//$data.="";



$data;

$header = '';
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=MRN.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
