<?php
  include "include/dbcon.php";
  include "include/CORDER.php";
  include "include/CPRODUCT.php";
?>
<?php
  header('Content-Type: text/html; charset=utf-8');
?>
<?
  function spaceQtty($pval){
    if($pval<10){
	  $output = "&nbsp;" . $pval;
	}else{
	  $output = $pval;
	}
	return $output;
  }
  
  function spaceProd($pval,$pspace){
    $pstrlen = strlen($pval);
    if( $pstrlen>$pspace){
	  $output = substr($pval,0,$pspace);
	}else{
	  $strSpace = "";
	  for($i=$pstrlen;$i<$pspace;$i++){
	    $strSpace = $strSpace . "&nbsp;";
	  }
	  $output = "$pval" . $strSpace;
	}
	return $output;
  }
  
  function spacePrice($pval){
    if($pval<10){
	  $output = "&nbsp;&nbsp;" . sprintf("%.2f",$pval);
	}else{
	  if($pval<100){
	    $output = "&nbsp;" . sprintf("%.2f",$pval);
	  }else{
	    $output = sprintf("%.2f",$pval);
	  }
	}
	return $output;
  }
?>
<?php
  global $conn;
  
  $ptime = $_POST["ptime"];
  
  $myprod = new PRODUCT();
  $billno = $myprod->getInvoiceNum();
  $arrProduct = $myprod->getProducts(" WHERE id>0");
  $ctimestamp = 0;

  $mycart = new CART();
  $arrCart = $mycart->getCarts();
  $total = 0;
  foreach($arrCart as $tid=>$tcart){
  
    $ctimestamp = $tcart["ctimestamp"];
	
    $total = $total + ($arrProduct[$tcart["productid"]]["price"]*$tcart["qtty"]);

	$q = "INSERT INTO tb_orderitem VALUES('','$billno','".$tcart["productid"]."','".$tcart["qtty"]."','".$arrProduct[$tcart["productid"]]["price"]."')";
	//echo $q . "<br>";
//	mysqli_query($conn,$q);
	
	
	$content[] = spaceQtty($tcart["qtty"]) . " X " . 
	  spaceProd($arrProduct[$tcart["productid"]]["ename"],13) . " " . 
	  spacePrice($tcart["qtty"]* $arrProduct[$tcart["productid"]]["price"]);

  }
  
  $q2 = "INSERT INTO tb_order VALUES ('','$ctimestamp','".date("Y-m-d",$ctimestamp)."','$billno','$total')";
  //echo $q2;
//  mysqli_query($conn,$q2);
  
  $query = "TRUNCATE`tb_cart`";
//  mysqli_query($conn,$query);
  
 // $content[] = "&nbsp;LiJi FoodStuffs Trading";
 // $content[] = "&nbsp;&nbsp;&nbsp;&nbsp;Bill No: 20210001";
 // $content[] = "&nbsp;LiJi FoodStuffs Trading";
 // $content[] = "&nbsp;LiJi FoodStuffs Trading";
  
  ?>
  <span style="font-family:Lucida Console">
  &nbsp;LiJi FoodStuffs Trading<br />
  &nbsp;&nbsp;&nbsp;&nbsp;Bill No: 20210001<br /><br />
  <?= date("h:iA",$ptime) ?><?= spaceProd(" ",7) ?><?= date("d-m-Y",$ptime) ?><br />
  -------------------------<br />
  
  <?
  foreach($content as $tid=>$tcontent){
    echo $tcontent . "<br>";
  }
  ?>
  -------------------------<br />
  <?= spaceProd(" ",12) ?>Total : <?= spacePrice($total) ?><br /><br />
  <?= spaceProd(" ",8) ?>Thank You.<br>
  <?= spaceProd(" ",4) ?>Please come again.<br />
  
  </span>
  <?

  
?>
<?

  //$handle = printer_open("RONGTA 58mm Series Printer");
  $handle = printer_open("EPSONL360");
  printer_start_doc($handle, "mydoc");
  printer_start_page($handle);
  //printer_create_font ( string face, int height, int width, int font_weight, bool italic, bool underline, bool strikeout, int orientation )
  $font = printer_create_font("Lucida Console", 20,15,100, false, false, false, 0);
  printer_select_font($handle,$font);
  
  $texth = 0;
  foreach($content as $tid=>$tcontent){
    //echo $tcontent . "<br>";
	printer_draw_text($handle,$tcontent,0,$texth);
	$texth += 20;
  }
  printer_draw_text($handle,"  12345   7890123 567",0,$texth);
  
  printer_delete_font($font);

  printer_end_page($handle);
  printer_end_doc($handle);
  printer_close($handle);

?>
<?php
//  header("location: main.php");
?>