<?php
session_start();
ob_start();

// Panggil koneksi database.php untuk koneksi database
require_once "../../config/database.php";
// panggil fungsi untuk format tanggal
include "../../config/fungsi_tanggal.php";
// panggil fungsi untuk format rupiah
include "../../config/fungsi_rupiah.php";

$hari_ini = date("d-m-Y");

// ambil data hasil submit dari form
$userid   = $_SESSION['id_user'];
$id       = $_GET['id'];

$query22 = mysqli_query($mysqli, "SELECT *,(select nama from customer where id = jual.supplier) as namacustomer
											FROM jual WHERE billno=$id") 
                                    or die('Ada kesalahan pada query tampil : '.mysqli_error($mysqli));
while ($data22 = mysqli_fetch_assoc($query22)) {
	$tgl = tgl_ind($data22['billdate']);
	$customer = $data22['namacustomer'];
}
						

$jenis     = $_GET['jenis'];

$kategori  = $_GET['kategori'];

?>
<html xmlns="http://www.w3.org/1999/xhtml"> <!-- Bagian halaman HTML yang akan konvert -->
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Invoice Penjualan</title>
        <link rel="stylesheet" type="text/css" href="../../assets/css/laporanpdf.css" />
    </head>
    <body>
	 <!--
		<div style="background-image: url(http://www.tiarabakery.com/cab1/kop.jpg);background-repeat: no-repeat;  background-attachment: fixed; background-position: center;width:100%;">
			<br><br><br><br><br><br><br><br><br><br>
		</div>
	 -->	
        <div id="title">
		<?php

			echo"INVOICE";
		?>
        </div>

        <div id="isi" style='font-size:10pt;'>
             <table width="100%" border="0" cellpadding="0" cellspacing="0">
				<tr>
					<td height="20" >Tanggal</td><td width="20" align="center">:</td><td><?php echo $tgl;?></td>
					<td height="20" >Tanggal</td>
					<td height="20" >Tanggal</td><td width="20" align="center">:</td><td><?php echo $tgl;?></td>
				</tr>
				<tr>
					<td height="20" >Customer</td><td width="20" align="center">:</td><td><?php echo $customer;?></td>
					<td height="20" >Customer</td>
					<td height="20" >Customer</td><td width="20" align="center">:</td><td><?php echo $customer;?></td>
				</tr>
			 </table>
        </div>
		
	<?php
		$font=$_GET["font"];
		if ($font == ""){
			$style = "style='font-size:8pt;'";
		}
		else{
			$style = "style='font-size:".$font."pt;'";
		}
    
		echo"
        <div id='isi' $style>";
	?>
            <table width="100%" border="0.3" cellpadding="0" cellspacing="0">
                <thead style="background:#e8ecee">
                    <tr class="tr-title">
						<th height="20" width="40" align="center" valign="middle">No</th>
                        <th height="20" width="300" align="center" valign="middle">NAMA BARANG</th>
						<th height="20" width="50" align="center" valign="middle">QTY</th>
                        <th height="20" width="60" align="center" valign="middle">SATUAN</th>
						<th height="20" width="120" align="center" valign="middle">HARGA</th>
						<th height="20" width="120" align="center" valign="middle">TOTAL</th>
                    </tr>
                </thead>
                <tbody>
<?php
	$no    = 1;
		$query = mysqli_query($mysqli, "select * from jual
											inner join jualdetail
											on jual.billno = jualdetail.billno
											inner join product 
											on product.product_id = jualdetail.code
											inner join satuan 
											on satuan.id = product.satuan
											where jual.billno =$id
										") 
										or die('Ada kesalahan pada query tampil Transaksi : '.mysqli_error($mysqli));

    $count  = mysqli_num_rows($query);
    if($count == 0) {
        echo "  <tr>
                    <td width='20' height='8' align='center' valign='middle'></td>
                    <td width='80' height='8' align='center' valign='middle'></td>
					<td width='60' height='8' align='center' valign='middle'></td>

                </tr>";
    }
    // jika data tidak ada
    else {
        // tampilkan data
        while ($data = mysqli_fetch_assoc($query)) {
				
			$qty = number_format($data['qty'],0);
			$harga = number_format($data['harga'],0);
			$total = number_format($data['qty'] * $data['harga'],0);
            // menampilkan isi tabel dari database ke tabel di aplikasi
            echo "  <tr>
                        <td width='40' height='6'  style='$warna' align='center' valign='middle'>$no</td>
						<td width='300' height='6'  style='padding-left:5px;$warna' align='left' valign='middle'>$data[product_name]</td>
						<td width='50' height='6'  style='$warna' align='center' valign='middle'>$qty</td>
                        <td width='60' height='6'  style='$warna' align='center' valign='middle'> $data[satuan] </td>
						<td width='120' height='6'  style='padding-right:5px;$warna' align='right' valign='middle'> $harga </td>
						<td width='120' height='6'  style='padding-right:5px;$warna' align='right' valign='middle'> $total </td>
 
                    </tr>";
            $no++;
        }
		
	
		
    }
	
	
	for ($x = $no; $x <= 10; $x++) {
		echo "  <tr>
                        <td width='40' height='6'  style='$warna' align='center' valign='middle'>$no</td>
						<td width='300' height='6'  style='padding-left:5px;$warna' align='left' valign='middle'>&nbsp;</td>
						<td width='50' height='6'  style='$warna' align='center' valign='middle'>&nbsp;</td>
                        <td width='60' height='6'  style='$warna' align='center' valign='middle'> &nbsp; </td>
						<td width='120' height='6'  style='$warna' align='center' valign='middle'> &nbsp; </td>
						<td width='120' height='6'  style='$warna' align='center' valign='middle'> &nbsp; </td>
 
                    </tr>";
		$no++;
	} 
	
	
	
?>	
                </tbody>
            </table>
			
			
			<div >
               <?php echo $angkutan; ?>
            </div>
			
			
        </div>
    </body>
</html><!-- Akhir halaman HTML yang akan di konvert -->
<?php
$filename="PENJUALAN_".$id.".pdf"; //ubah untuk menentukan nama file pdf yang dihasilkan nantinya
//==========================================================================================================
$content = ob_get_clean();
$content = '<page style="font-family: freeserif">'.($content).'</page>';
// panggil library html2pdf
require_once('../../assets/plugins/html2pdf_v4.03/html2pdf.class.php');
try
{
    $html2pdf = new HTML2PDF('P','F4','en', false, 'ISO-8859-15',array(10, 10, 10, 10));
    $html2pdf->setDefaultFont('Arial');
    $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
    $html2pdf->Output($filename,'F');
	header('location:'.$filename);
}
catch(HTML2PDF_exception $e) { echo $e; }

?>