<style type='text/css'>
#th1{
background-color:blue;    
color:white;
font-weight:bold;
padding:5px;
font-size:8px;
}   

#td1{
background-color:black;    
color:white;
font-weight:bold;
padding:5px;
font-size:8px;
}   
   
#td2{
color:black;
padding:5px;
font-size:8px;
}   

#tdh{
color:black;
font-weight:bold;
font-size:8px; 
} 
</style>



 <?php
$nopos=$_GET['nopos'];
 ?>
 
 <script src="https://cdn.jsdelivr.net/npm/recta/dist/recta.js"></script>
<script>
//alert('tes');

  let format = new Intl.NumberFormat('id-ID', {
            style: 'currency',
            currency: 'IDR',
            minimumFractionDigits: 0,
        });

        const rupiah = (number)=>{
    return new Intl.NumberFormat("id-ID", {
      style: "currency",
      currency: "IDR"
    }).format(number);
  }

let xhr = new XMLHttpRequest();

    // Making our connection  
    let url = 'https://dialey.angsoft.my.id/posheru/printpos_1_ajax_test.php?nopos=<?php echo $nopos;?>';
    xhr.open("GET", url, true);

    // function execute after request is successful 
    xhr.onreadystatechange = function () {
       if (this.readyState == 4 && this.status == 200) {
        var myArr = JSON.parse(this.responseText);
        myFunction(myArr);
        var printer = new Recta('5999390299', '1811')
      
        // for (i=0;i<myArr['data'].length;i++)
        // {
        //     console.log(myArr['data'][i].item_code);
        // }
 console.log(rupiah(myArr['data'][0].harga_jual));
            printer.open().then(function () {
      
   
        printer.align('center')
        printer.text('------------------');
        printer.text(myArr['ntoko']);
        printer.bold(true)
        printer.text(myArr['atoko']);
        printer.bold(true)
        printer.text(myArr['hptoko']);
        printer.bold(true)
        printer.text(myArr['data'][0].notrans_link);
        printer.bold(true)
        printer.text('------------------');
      
         for (i=0;i<myArr['data'].length;i++)
        {
           // console.log(myArr['data'][i].item_code);
               
        printer.text(myArr['data'][i].item_desc);
        printer.bold(true)
        printer.text(myArr['data'][i].qty+'   '+rupiah(myArr['data'][i].harga_jual));
        printer.bold(true)
        printer.text('Sub Total : '+rupiah(myArr['data'][i].qty*myArr['data'][i].harga_jual));
     
       
        printer.bold(true)
     


        }
        printer.text('------------------');
        printer.bold(true)
        printer.text('Discount: '+rupiah(myArr['disc']));
        printer.bold(true)
        printer.text('Total : '+rupiah(myArr['total']));
        printer.bold(true)
        printer.text('------------------');
        printer.bold(true)
        printer.cut()
        printer.print()

    })
  
       
    }
       
    }
    // Sending our request 
    xhr.send();
    

    function myFunction(arr) {
    var out = "";
    var i;
    for(i = 0; i < arr.length; i++) {
        out += '<a href="' + arr[i].url + '">' +
        arr[i].display + '</a><br>';
    }
    document.getElementById("id01").innerHTML = out;
}

// fetch('https://dialey.angsoft.my.id/posheru/printpos_1_ajax.php?nopos=POS-1733371010348', {
//     method: 'POST'
// })
//    .then(response => response.json())
//    .then(json => console.log(JSON.stringify(json)))
   
  //console.log(json);

//   var printer = new Recta('1074241248', '1811')

 
//     printer.open().then(function () {
//       printer.align('center')
//         .text('Hello World !!')
//         .bold(true)
//         .text('This is bold text')
//         .bold(false)
//         .underline(true)
//         .text('This is underline text')
//         .underline(false)
//         .barcode('UPC-A', '123456789012')
//         .cut()
//         .print()
//     })
  

 </script>   

<p id="id01"></p>



<?php
 $nopos=$_GET['nopos'];
include 'dbconfig.php';
 
// Create connection


//echo $notrans;
 
$conn = mysqli_connect($servername, $username, $password, $dbname);
 
// Check connection
 
if (!$conn) {
 
    die("Connection failed: " . mysqli_connect_error());
 
}
 
$sql3="select * from toko_profile";
$res3 = mysqli_query($conn, $sql3);
$row3 = mysqli_fetch_row($res3);

$sql = "SELECT item_code,item_desc,harga_jual,qty,disc_val,custname FROM Trans1sub_test where notrans_link='".$nopos."'"." order by idno desc" ;

$sql2 = "SELECT sum(harga_jual*qty)-sum(disc_val) as totrp,sum(disc_val) as discount FROM Trans1sub_test where notrans_link='".$nopos."'"." order by idno desc" ;

$res2 = mysqli_query($conn, $sql2);
$row2 = mysqli_fetch_row($res2);
//customer from transaksi
$cust=mysqli_query($conn,"select custname,custid from Trans1sub_test where notrans_link='".$nopos."'");
$rowcust = mysqli_fetch_row($cust);
//customer from tbcustomer
$cust2=mysqli_query($conn,"select nohp,alamat from TbCustomer where custid='".$rowcust[1]."'");
$rowcust2 = mysqli_fetch_row($cust2);



$res = mysqli_query($conn, $sql);
echo "<table>";
echo "<tr><td id='tdh'>".$row3[2]."</td></tr>";
echo "<tr><td id='tdh'>".$row3[3]."</td></tr>";
echo "<tr><td id='tdh'>Contact Phone : ".$row3[4]."</td></tr>";
echo "</table>";
echo "<br>";
echo "<table border='1'>";
echo "<tr><th id='th1' colspan='5' align='center'>NO :".$nopos."</th></tr>";
echo "<tr><th id='th1' colspan='5' align='center'>".$rowcust[0]."/".$rowcust2[0]."</th></tr>";
echo "<tr><th id='th1'>Item Code</th><th id='th1'>Product</th><th id='th1'>Harga</th><th id='th1'>Qty</th><th id='th1'>Disc</th></tr>";

if($res === false) {
  echo "error while executing mysql: " . mysqli_error($conn);
}
 
 while($row = mysqli_fetch_row($res))
 
 {

echo "<tr><td id='td2'>".$row[0]."</td><td id='td2'>".$row[1]."</td><td align='right' id='td2'>".number_format($row[2],2)."</td><td align='right' id='td2'>".number_format($row[3],2)."</td><td align='right' id='td2'>".number_format($row[4],2)."</td></tr>";  
     
 }
 echo "<tr><td colspan='5' align='right' id='td1' >Total (IDR) : ".number_format($row2[0],2)." / Disc. : ".number_format($row2[1],2)."  </td></tr>";
 echo "<tr><td colspan='5' style='font-size:10px;'>Printing Date : ".date('d-M-Y')."</td></tr>";
 
   
   
 
 
mysqli_close($conn);


 
?>
