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

// error_reporting(E_ALL);
// ini_set('display_errors', 1);

// Manifest Summary

$mShippingRef 		= $_GET['shippingreference'];
$mConnect   		= $_GET['connection'];
$mUser				= $_GET['user'];
$mPwd				= $_GET['pword'];
$mHost              = $_GET['host'];
$mDbName			= $_GET['dbase'];
$mCurrentUser       = $_GET['currentuser'];
$mOutput			= $_GET['output'];

include('../barcode/php-barcode.php');
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,'Manifest Summary',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');
	}  

// Text formatting for Barcode	
	function TextWithRotation($x, $y, $txt, $txt_angle, $font_angle=0)
    {
        $font_angle+=90+$txt_angle;
        $txt_angle*=M_PI/180;
        $font_angle*=M_PI/180;
    
        $txt_dx=cos($txt_angle);
        $txt_dy=sin($txt_angle);
        $font_dx=cos($font_angle);
        $font_dy=sin($font_angle);
    
        $s=sprintf('BT %.2F %.2F %.2F %.2F %.2F %.2F Tm (%s) Tj ET',$txt_dx,$txt_dy,$font_dx,$font_dy,$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt));
        if ($this->ColorFlag)
            $s='q '.$this->TextColor.' '.$s.' Q';
        $this->_out($s);
    }	
	
// Load data
	function LoadData($mConnection, $mShippingRef)
	{
	
// Read file lines
		$data = array();		
		$mDataQuery = "SELECT shipping_header.document_reference AS document_reference, shipping_header.trailer_reference AS trailer_reference FROM shipping_header WHERE shipping_header.document_reference='".$mShippingRef."'";
		if (!mysqli_query($mConnection,$mDataQuery))
		{
			print_r("FAIL-".mysql_error($mConnection)."-".$mDataQuery);
		}
		// mysql query construct
		$mData = mysqli_query($mConnection,$mDataQuery);
		$i = 0;
		while($mRow = mysqli_fetch_assoc($mData)) 
		{
			$data[] =($mRow);
			$i++;
		}
		mysqli_close($mConnection);
		return $data;
	}

// Simple table
	function BasicTable($header, $data)
	{
// Initial Line
		$this->ln(30);
// Column widths
		$w = array(35, 30);
// Header
		$i = 0;
		foreach($header as $col)
		{
			$this->Cell($w[$i],7,$col,1);
			$i++;
		}
		$this->Ln();
// Data
		foreach($data as $row)
		{
			$i = 0;
// Do Something.....
			$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);
	}
	
// Barcode table
	function BarcodeTable($header, $data, $shippingRef, $prefix)
	{
// date / time
		$date = date('d/m/Y h:i:s a', time());
// barcode settings
		$fontSize = 8;
		$marge    = 5;   // between barcode and hri in pixel
		$x        = 30;  // barcode center
		$y        = 70;  // barcode center
		$height   = 10;   // barcode height in 1D ; module size in 2D
		$width    = 0.25;    // barcode width in 1D ; not use in 2D
		$angle    = 0;   // rotation in degrees  
		$code     = 'VANTEC'; // barcode text;)
		$type     = 'code39';
		$black    = '000000'; // color in hex
// Headings
		$this->AddPage();
		$this->Text(10,50,"Shipping Reference: ".$shippingRef);
		$this->Text(100,50,"Report Date: ".$date);
		
		$this->SetFont('Arial','',$fontSize);
		$this->SetTextColor(0, 0, 0);
// Initial Line
		$this->ln(30);
// Column widths
		$w = array(108, 50);
// Header
		$i = 0;
		foreach($header as $col)
		{
			$this->Cell($w[$i],7,$col,0);
			$i++;
		}
		$this->Ln();
// Data
		$l = 0;
		foreach($data as $row)
		{
			$x = 30;
			foreach($row as $key=>$col)
			{
				$barCol = $col;
				if ($barCol != "")
				{
// Add prefix if required
					for($i=0;$i<count($prefix);$i++)
					{
						if (strtolower($key) == strtolower($prefix[$i]['fieldName']))
							$barCol = $prefix[$i]['prefix'].$col;
					}
				}
// Print Barcode
				$barcode = Barcode::fpdf($this, $black, $x, $y, $angle, $type, $barCol, $width, $height);
// Print Barcode Text (under barcode)
				$this->TextWithRotation($x, $y+8, $col, $angle);
				$i++;
				$x = $x+100;
			}
			$y = $y+20;
			$this->Ln();
			$l = $l+1;
// Full Page
			if ($l > 9)
			{
				$this->AddPage();
				$this->Text(10,50,"Shipping Reference: ".$shippingRef);
				$this->Text(100,55,"Report Date: ".$date);
// Header
				$i = 0;
				$this->ln(30);
				foreach($header as $col)
				{
					$this->Cell($w[$i],7,$col,0);
					$i++;
				}
				$this->Ln();
				$l = 0;
				$y = 70;
			}
		}
	}

}

function getPrefix($mHost, $mUser, $mPwd, $mDbName)
{
// Read Headers
	$prefix = array();
// mysql connection
	$mConnection=mysqli_connect($mHost,$mUser,$mPwd,$mDbName) or
		exit("FAIL-Connect failed: ".mysqli_connect_errno()." : ". mysqli_connect_error());
		
	$mDataQuery = "SELECT field_name AS fieldName, substr(prefix_name,1,1) AS prefix FROM default_setting";
// mysql query construct
	if(!mysqli_query($mConnection,$mDataQuery))
		exit("FAIL-".mysqli_error($mConnection));
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$prefix[] =($mRow);
		$i++;
	}
	mysqli_close($mConnection);
	return $prefix;
//    print_r($data.":".$mDataQuery.":".$i);
}

// Main Process
function mainProcess($mHost, $mUser, $mPwd, $mDbName,$mShippingRef,$mOutput)
{
	$pdf = new PDF();
// Column headings
	$header = array("Shipping Reference", "Trailer Reference",);

	if (!$mConnection=mysqli_connect($mHost,$mUser,$mPwd,$mDbName))
		$mConnection = $connection;

// Field Prefixes
	$prefix = getPrefix($mHost, $mUser, $mPwd, $mDbName);

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

	$data = $pdf->LoadData($mConnection, $mShippingRef);
	$pdf->SetFont('Arial','',12);

	$pdf->BarcodeTable($header,$data,$mShippingRef,$prefix);
	$pdf->Ln(5);

	if ($mOutput == "")
		$pdf->Output();
	else
	{
		$mPdf = "../../../vimsreports/manifestsummary".$mCurrentUser.".pdf";
		$pdf->Output($mPdf,$mOutput);
	}
}

	include("../../vimsconfig/phpConfig.php");
	$mConnection = defaultDbConnectionVal("DEFAULT");
	$mConnectionVals = explode(",",$mConnection);
	$mHost = $mConnectionVals[1];
	$mUser = $mConnectionVals[2];
	$mPwd = $mConnectionVals[3];
	$mDbName = $mConnectionVals[4];
	mainProcess($mHost, $mUser, $mPwd, $mDbName,$mShippingRef,$mOutput);
	
?>