<?php
header('Cache-Control: no-cache, no-store, must-revalidate');   
header('Expires: 0');

// Shipping Summary

$mShippingRef 		= $_GET['shippingreference'];
$mTrailerRef 		= $_GET['trailerreference'];
$mConnect   		= $_GET['connection'];
$mUser				= $_GET['user'];
$mPwd				= $_GET['pword'];
$mHost              = $_GET['host'];
$mDbName			= $_GET['dbase'];
$mTableName 		= $_GET['table'];
$mFilter			= $_GET['filter'];
$mReturnFields		= $_GET['returnfields'];
$mCurrentUser       = $_GET['currentuser'];
$mOutput			= $_GET['output'];

$mFilter = str_replace("|AND|","&",$mFilter);
$mFilter = str_replace("/dbase/",$mDbName,$mFilter);

require('../barcode/fpdf.php');
// require(dirname(__FILE__) . '/../../vimsconfig/phpConfig.php');

class PDF extends FPDF
{
// Page header
	function Header()
	{
// Logo
		$this->Image('../../../images/vel_logo_large.jpg',10,6,50);
// Arial bold 15
		$this->SetFont('Arial','B',15);
// Move to the right
		$this->Cell(100);
// Title
		$this->Cell(50,10,'Shipping Detail',0,0,'C');
// Line break
		$this->Ln(20);
	}

// Page footer
	function Footer()
	{
// Position at 1.5 cm from bottom
		$this->SetY(-15);
// Arial italic 8
		$this->SetFont('Arial','I',8);
// date / time
		$date = date('d/m/Y h:i:s a', time());
// Page number
		$this->Cell(0,10,'Page '.$this->PageNo().'   Date '.$date,0,0,'C');
	}  
	
// Load data
	function LoadData($mHost, $mUser, $mPwd, $mDbName, $mShippingRef)
	{
// Read file lines
		$data = array();
// mysql connection
		$mConnection=mysqli_connect($mHost,$mUser,$mPwd,$mDbName) or
			exit("FAIL-Connect failed: ".mysqli_connect_errno()." : ". mysqli_connect_error());	
		$mDataQuery = "SELECT shipping_header.document_reference AS document_reference, shipping_header.trailer_reference AS trailer_reference, shipping_detail.part_number AS part_number, shipping_detail.serial_reference AS serial_reference, shipping_detail.qty AS qty";
		$mDataQuery .= " FROM shipping_header LEFT JOIN shipping_detail ON shipping_header.id=shipping_detail.shipping_header_id";
		$mDataQuery .= " WHERE shipping_header.document_reference='".$mShippingRef."'";
		$mDataQuery .= " ORDER BY document_reference,trailer_reference,part_number,serial_reference";
		
		if (!mysqli_query($mConnection,$mDataQuery))
		{
			print_r("FAIL-".mysql_error($mConnection)."-".$mDataQuery);
		}		
// mysql query construct
		$mData = mysqli_query($mConnection,$mDataQuery);
//		if (!$mData)
//			print_r("FAIL-".$mDataQuery."-".mysqli_num_rows($mData)."-".$mHost."-".$mUser."-".$mPwd."-".$mDbName);
		$i = 0;
		while($mRow = mysqli_fetch_assoc($mData)) 
		{
			$data[] =($mRow);
			$i++;
		}
		mysqli_close($mConnection);
		return $data;
//    print_r($data.":".$mDataQuery.":".$i);
	}

// Simple table
	function BasicTable($header, $data)
	{
// Initial Line
		$this->ln(30);
// Column widths
		$w = array(40,40, 40, 25);
// Header
		$i = 0;
		foreach($header as $col)
		{
			$this->Cell($w[$i],7,$col,1);
			$i++;
		}
		$this->Ln();
// Data
		foreach($data as $row)
		{
			$i = 0;
			//$this->Cell($w[0],6,$row['ran_order'],1);
			$this->Cell($w[0],6,$row['part_number'],1);
			$this->Cell($w[1],6,$row['serial_reference'],1);
			$this->Cell($w[2],6,$row['qty'],1,0,'R');
/*			
			foreach($row as $col)
			{
				if (($i==1))
					$this->Cell($w[$i],6,$col,1,0,'R');
				else
					$this->Cell($w[$i],6,$col,1);
				$i++;
			}
*/
			$this->Ln();
		}
		$this->Ln(5);
		$this->Cell(30,10,"Checked By",1);
		$this->Cell(60,10," ",1);
		$this->Ln();
		$this->Cell(30,10,"Date",1);
		$this->Cell(60,10," ",1);
	}

}

// Return Default mySql Connection
function defaultDbConnection()
{
// get default System Settings from xml file	
	$mSystem = "DEFAULT";
// Get System Definition XML from URL
	$mData = file_get_contents("../../vimsconfig/systemsettings.xml");
// Parse XML to XML Object Array
	$xml = simplexml_load_string($mData, 'SimpleXMLElement', LIBXML_NOCDATA);
// Split-out System Definitions from XML 'fields' Nodes
	$i = 0;

	for ($i = 0; $i <= 150; $i++) 
	{			
		$mSystemId = "";
		$mPath = "";
		$mDbHost = "";
		$mDbUser = "";
		$mDbPwd = "";
		$mDbase = "";
		$mMqServer = "";
		$mCompany = "";
		if (("".$xml->systems->system[$i]->systemid."" == "") || ("".$xml->systems->system[$i]->systemid."" == false) || ("".$xml->systems->system[$i]->systemid."" == false))
			break;
		
		$mSystemId = "".$xml->systems->system[$i]->systemid."";
		if (($mSystemId == "") || ($mSystemId == false))
			break;
		
// Store System Settings from "trandef/systemsettings.xml"		
		if (strToUpper($mSystem) == strToUpper($mSystemId))
		{
			$mPath = "".$xml->systems->system[$i]->path."";
			$mDbHost = "".$xml->systems->system[$i]->dbhost."";
			$mDbUser = "".$xml->systems->system[$i]->dbuser."";
			$mDbPwd = "".$xml->systems->system[$i]->dbpwd."";
			$mDbase = "".$xml->systems->system[$i]->dbase."";
			break;
		}
		if (($mSystemId == "") || ($mSystemId == false))
			break;
	}

//	$mConnection = mysqli_connect($mDbHost,$mDbUser,$mDbPwd,$mDbase)or die("FAIL-Failed to connect to Database: " . mysqli_connect_error().":".$mSystem);
	$mDbDefaults = array();
	$mDbDefaults[0] = $mDbHost;
	$mDbDefaults[1] = $mDbUser;
	$mDbDefaults[2] = $mDbPwd;
	$mDbDefaults[3] = $mDbase;
	$mDbDefaults[4] = $mPath;
	return $mDbDefaults;
}

$pdf = new PDF();
// Column headings
$header = array("Part Number","Serial Ref.","Quantity");

// Data loading
if (($mHost == "") || ($mHost == null))
{
	$mDbDefaults = defaultDbConnection();
	$mHost = $mDbDefaults[0];
	$mUser = $mDbDefaults[1];
	$mPwd = $mDbDefaults[2];
	$mDbName = $mDbDefaults[3];
	$mPath = $mDbDefaults[4];
}

// date / time
	$date = date('d/m/Y h:i:s a', time());

$data = $pdf->LoadData($mHost, $mUser, $mPwd, $mDbName, $mShippingRef);
$pdf->SetFont('Arial','',12);

$pdf->AddPage();
$pdf->Text(10,50,"Shipping Reference: ".$mShippingRef." / ".($data[0]['trailer_reference']));
$pdf->Text(100,50,"Report Date: ".$date);
$pdf->BasicTable($header,$data);
$pdf->Ln(5);

// $pdf->AddPage();
// $pdf->ImprovedTable($header,$data);
 if ($mOutput == "")
	 $pdf->Output();
 else
 {
	$mPdf = "../../../vimsreports/shippingsummary".$mCurrentUser.".pdf";
	$pdf->Output($mPdf,$mOutput);
 }
//	$pdf->Output("../../vimsreports/receiptsummary.pdf",$mOutput);

?>