<?php
  include "include/dbcon.php";
  include "include/CORDER.php";
  include "include/CPRODUCT.php";
?>
<?php
//  header('Content-Type: text/html; charset=utf-8');
?>
<?php
  function spaceQtty($pval){
    if($pval<10){
	  $output = " " . $pval;
	}else{
	  $output = $pval;
	}
	return $output;
  }
  
  function spaceProd($pval,$pspace){

    $pstrlen = strlen($pval);

    if( $pstrlen>$pspace){
	  $output = "";
	}else{
	  $strSpace = "";
	  for($i=$pstrlen;$i<$pspace;$i++){
	    $strSpace = $strSpace . " ";
	  }
	  $output = " " . $strSpace;
	}
	return $output;
  }
  
  function spacePrice($pval){
    if($pval<10){
	  $output = "  " . sprintf("%.2f",$pval);
	}else{
	  if($pval<100){
	    $output = " " . 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;

?>
<?php
  $content[] = " LiJi FoodStuffs Trading";
  $content[] = "    Bill No: $billno";
  $content[] = " ";
  $content[] = date("h:iA",$ptime) . spaceProd(" ",7) . date("d-m-Y",$ptime);
  $content[] = "-------------------------";
?>
<?php

  $mycart = new CART();
  $arrCart = $mycart->getCarts();
  $total = 0;
  
  $checkorder = true;
  $i = 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>";
	if( mysqli_query($conn,$q) ){
	
	  $arrTempItemid[$i] = $tcart["productid"];
	  $arrTempQtty[$i] = $tcart["qtty"];
	
	}else{
	  $checkorder = false;
	}
	
	
	$content[] = spaceQtty($tcart["qtty"]) . " X " . 
	  substr($arrProduct[$tcart["productid"]]["ename"],0,14) .
	  spaceProd($arrProduct[$tcart["productid"]]["ename"],13) .  
	  spacePrice($tcart["qtty"]* $arrProduct[$tcart["productid"]]["price"]);

    $i++;

  }
  
  $q2 = "INSERT INTO tb_order VALUES ('','$ctimestamp','".date("Y-m-d",$ctimestamp)."','$billno','$total')";
  //echo $q2;
  if( mysqli_query($conn,$q2) ){
  }else{
    $checkorder = false;
  }
  
  if( $checkorder ){
	
	foreach($arrTempItemid as $tidx=>$tval){
	  $myprod->setProduct("id",$tval);
	  $myprod->deductProduct($arrTempItemid[$tidx],$arrTempQtty[$tidx]);
	}
	
    $query = "TRUNCATE`tb_cart`";
    mysqli_query($conn,$query);
	
  }else{
    
  }
  

?>
<?php
   $content[] = "-------------------------";
   $content[] = spaceProd(" ",11) . "Total : " . spacePrice($total);
   $content[] = "";
   $content[] = spaceProd(" ",8) . "Thank You.";
   $content[] = spaceProd(" ",4) . "Please come again.";
?>
<?php
  //$handle = printer_open("RONGTA 58mm Series Printer");
//  $handle = printer_open("Blu-ray-58");
//  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){
//	printer_draw_text($handle,$tcontent,0,$texth);
//	$texth += 20;
//  }
  
//  printer_delete_font($font);

//  printer_end_page($handle);
//  printer_end_doc($handle);
//  printer_close($handle);
?>
<?php
  header("location: main.php");
?>