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

// Picking Sheet

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

// 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[1],6,$row['part_number'],1);
			$this->Cell($w[2],6,$row['serial_reference'],1);
			$this->Cell($w[3],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);
	}

}

// Load data
function LoadData($mHost, $mUser, $mPwd, $mDbName, $mCustomerRef)
{
// 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 document_header.document_reference_code AS document_reference, document_header.tanum AS customer_reference, document_header.expected_delivery AS expected_delivery,document_header.rolling_no AS rolling_no";
	$mDataQuery .= ",document_body.wempf AS vehicle_reference";
	$mDataQuery .= " FROM document_header JOIN document_body on document_header.id=document_body.document_header_id";
	$mDataQuery .= " WHERE document_header.customer_reference='".$mCustomerRef."'";
	$mDataQuery .= " ORDER BY customer_reference, document_reference";
	if (!$mData = mysqli_query($mConnection,$mDataQuery))
		$data[0] = "FAIL-".mysqli_error($mConnection).":".$mDataQuery;
	
// mysql query construct

	$i = 0;
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$data[] =($mRow);
		$i++;
	}
	mysqli_close($mConnection);
	return $data;
//    print_r($data.":".$mDataQuery.":".$i);
}

// Barcode Properties
  
//  $fontSize = 10;
//  $marge    = 10;   // between barcode and hri in pixel
  $x        = 200;  // barcode center
  $y        = 150;  // barcode center
  $height   = 20;   // barcode height in 1D ; module size in 2D
//  $width    = 0.50;    // barcode height in 1D ; not use in 2D
  $angle    = 0;   // rotation in degrees
  $fontSize = 8;
  $marge    = 5;   // between barcode and hri in pixel
  $width    = 1.50;    // barcode width in 1D ; not use in 2D
//  $angle    = 0;   // rotation in degrees  
  
  $code     = 'VANTEC'; // barcode text;)
  $type     = 'code39';
  $black    = '000000'; // color in hex
  
if ($mCustomerRef != "")
	$code = $mCustomerRef;

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

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

$mCustomerRef 		= $_GET['customerreference'];
$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);

$data = LoadData($mHost, $mUser, $mPwd, $mDbName, $mCustomerRef);
// $pdf->SetFont('Arial','',$fontSize);

// Barcode
$barcodeData = Barcode::fpdf($pdf, $black, $x, $y, $angle, $type, array('code'=>$code), $width, $height);
// $pdf->SetTextColor(0, 0, 0);
 
// $pdf->AddPage();
$pdf->Text(10,50,"Customer Reference: ".$mCustomerRef);

$len = $pdf->GetStringWidth($barcodeData['hri']);
// Barcode::rotate(-$len / 2, ($barcodeData['height'] / 2) + $fontSize + $marge, $angle, $xt, $yt);
// $pdf->TextWithRotation($x + $xt, $y + $yt, $barcodeData['hri'], $angle);

$pdf->Text(100,70,"Report Date: ".$date);
// $pdf->BasicTable($header,$data);
// $pdf->Ln(5);

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

?>