<?php
session_start();

require_once("../../../../../class/connectdb.php");
$con = new DbConnect;
$con = $con->connect();

/* Change to the correct path if you copy this example! */
require __DIR__ . '/../../autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;

/**
 * Install the printer using USB printing support, and the "Generic / Text Only" driver,
 * then share it (you can use a firewall so that it can only be seen locally).
 *
 * Use a WindowsPrintConnector with the share name to print.
 *
 * Troubleshooting: Fire up a command prompt, and ensure that (if your printer is shared as
 * "Receipt Printer), the following commands work:
 *
 *  echo "Hello World" > testfile
 *  copy testfile "\\%COMPUTERNAME%\Receipt Printer"
 *  del testfile
 */
 
$items = [];
$transaction_id = $_POST['transaction_id'];

//this will be copied to the printing script
$transaction_id = $_POST['transaction_id'];
$payment_type = $_POST['payment_type'];

$query = mysqli_query($con, "UPDATE labtesttill SET payment_type = '$payment_type', payment_status = 1 WHERE transaction_id = '$transaction_id' ");  //Update payment method

if($query == true){
	$sql = mysqli_query($con, "SELECT * FROM labtesttill WHERE transaction_id = '$transaction_id' ");  //Select the test for this transaction to print
    while($row = mysqli_fetch_array($sql)){
		$test_name = $row['test_name'];
		$qty = $row['qty'];
		$price = $row['price'];
		$qty = $row['qty'];
		$item = new item($test_name . ' X' . $qty, $price * $qty);
		array_push($items, $item);
	}
}

try {
    // Enter the share name for your USB printer here
    //$connector = null;
    $connector = new WindowsPrintConnector("XP-80C");

    /* Print a "Hello world" receipt" */
    $printer = new Printer($connector);
    
	/* Information for the receipt */
	/* $items = array(
	     new item("Example item #1", "4.00"), 
		 new item("Another thing", "3.50"), 
		 new item("Something else", "1.00"), 
		 new item("A final item", "4.45")
	); */
	
	//$subtotal = new item('Subtotal', '12.95');
	$tax = new item('Tax', '0.00');
	$total = new item('Total', number_format($net_qty_price), true);
	
	/* Date is kept the same for testing */
	// $date = date('l jS \of F Y h:i:s A');
	$date = date('l jS \of F Y h:i:s A');;
	/* Start the printer */
	
	/* Name of shop */
	$printer->selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
	$printer->text("Trustee Labouratory.\n");
	$printer->selectPrintMode();
	$printer->text("50D Glover Road Ikoyi.\n");
	$printer->feed();
	
	//The till user
	$printer->selectPrintMode();
	$printer->text('Staff: ' . $_SESSION['username']);
	$printer->feed();
	
	/* Title of receipt */
	$printer->setEmphasis(true);
	$printer->text("SALES INVOICE\n");
	$printer->setEmphasis(false);
	/* Items */
	$printer->setJustification(Printer::JUSTIFY_LEFT);
	$printer->setEmphasis(true);
	$printer->text(new item('', 'N'));
	$printer->setEmphasis(false);
	foreach ($items as $item) {
		$printer->text($item);
	}
	$printer->setEmphasis(true);
	$printer->text($subtotal);
	$printer->setEmphasis(false);
	$printer->feed();
	/* Tax and total */
	$printer->text($tax);
	$printer->selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
	$printer->text($total);
	$printer->selectPrintMode();
	/* Footer */
	$printer->feed(2);
	$printer->setJustification(Printer::JUSTIFY_CENTER);
	$printer->text("Thank you for your patronage\n");
	$printer->text("Contact us: 09088776677\n");
	$printer->feed(2);
	$printer->text($date . "\n");
	/* Cut the receipt and open the cash drawer */
	$printer->cut();
	$printer->pulse();
	$printer->close();
	/* A wrapper to do organise item names & prices into columns */
	
		
		/* Close printer */
		$printer -> close();
	} catch (Exception $e) {
		echo "Couldn't print to this printer: " . $e -> getMessage() . "\n";
	}
	
class item{
		private $name;
		private $price;
		private $nairaSign;
		public function __construct($name = '', $price = '', $nairaSign = false)
		{
			$this->name = $name;
			$this->price = $price;
			$this->nairaSign = $nairaSign;
		}
		public function __toString()
		{
			$rightCols = 10;
			$leftCols = 38;
			if ($this->nairaSign) {
				$leftCols = $leftCols / 2 - $rightCols / 2;
			}
			$left = str_pad($this->name, $leftCols);
			$sign = $this->nairaSign ? 'N ' : '';
			$right = str_pad($sign . $this->price, $rightCols, ' ', STR_PAD_LEFT);
			return "{$left}{$right}\n";
		}
}