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

// Receipt Summary

// Input Parameters from URL
$mHaulierRef 		= $_GET['haulierreference'];
$mConnect   		= $_GET['connection'];
$mUser				= $_GET['user'];
$mPwd				= $_GET['pword'];
$mHost              = $_GET['host'];
$mDbName			= $_GET['dbase'];
$mCurrentUser       = $_GET['currentuser'];
$mOutput			= $_GET['output'];

require('../barcode/fpdf.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,'Receipt 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, $mHaulierRef)
	{
// 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 receipt_header.document_reference AS document_reference, receipt_header.customer_reference AS customer_reference, receipt_header.delivery_note AS delivery_note, part.part_number AS part_number,receipt_detail.serial_reference AS serial_reference, (receipt_detail.received_qty/part.conversion_factor) AS received_qty, CASE WHEN inventory_master.requires_inspection=1 THEN 'Y' ELSE '' END AS inspect, CASE WHEN inventory_master.requires_decant=1 THEN 'Y' ELSE '' END AS decant, product_type.product_type_code AS product_name, CASE WHEN part.requires_count =1 THEN 'Y' ELSE '' END AS count"; 
		$mDataQuery .= " FROM receipt_header LEFT JOIN receipt_body ON receipt_header.id=receipt_body.receipt_header_id LEFT JOIN part ON receipt_body.part_id=part.id LEFT JOIN receipt_detail ON receipt_body.id=receipt_detail.receipt_body_id LEFT JOIN inventory_master ON receipt_detail.part_number=inventory_master.part_number AND receipt_detail.serial_reference=inventory_master.serial_reference LEFT JOIN product_type ON inventory_master.product_type_id=product_type.id WHERE haulier_reference='".$mHaulierRef."'";
// mysql query construct
		if (!mysqli_query($mConnection,$mDataQuery))
			exit("FAIL-".mysqli_error($mConnection).":".$mdataQuery);
		$mData = mysqli_query($mConnection,$mDataQuery);
		$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(30, 30, 25, 30, 30, 20, 8, 9, 20, 11);
// Header
		$i = 0;
		foreach($header as $col)
		{
			$this->Cell($w[$i],7,$col,1);
			$i++;
		}
		$this->Ln();
// Data
		foreach($data as $row)
		{
			$i = 0;
			foreach($row as $col)
			{
				if ($i == 5)
					$this->Cell($w[$i],6,$col,1,0,'R');
				else
					$this->Cell($w[$i],6,$col,1);
				$i++;
			}
			$this->Ln();
		}
	}

// Better table
	function ImprovedTable($header, $data)
	{
// Column widths
		$w = array(10, 20, 40, 45, 40, 40, 40, 10);
// Header
		for($i=0;$i<count($header);$i++)
			$this->Cell($w[$i],7,$header[$i],1,0,'C');
		$this->Ln();
// Data
		foreach($data as $row)
		{
			$i = 0;
			foreach($row as $col)
			{
				$this->Cell($w[0],6,$col,'LR');
				$i++;
				if ($i > 3)
					$i = 0;
			}	
			$this->Ln();
		}
    // Closing line
		$this->Cell(array_sum($w),0,'','T');
	}
}

// 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('L');
// Column headings
$header = array("Receipt Ref.","Customer Ref.","Del.Note","Part Number","Serial Ref.","Scan Qty.","QA","DEC","Prod.Type","Count");

// 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, $mHaulierRef);
$pdf->SetFont('Arial','',8);

$pdf->AddPage();
$pdf->Text(10,50,"Haulier Reference: ".$mHaulierRef);
$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/receiptdetail".$mCurrentUser.".pdf";
	$pdf->Output($mPdf,$mOutput);
 }
//	$pdf->Output("../../vimsreports/receiptsummary.pdf",$mOutput);

?>