<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Struk Penjualan</title>
    <link rel="icon" type="image/png" href="logojud.png" />
    <style type="text/css">
        body {
            font-family: lucida;
            font-size: 8pt;
            color: #000;
        }
    </style>
    <?php
    $nuser = $this->session->userdata('ses_username');
    ?>
</head>

<body>
    <?php
    // Include autoloader 
    require_once 'dompdf/autoload.inc.php';
    require_once 'sendmessage.php';
    require_once 'movefile.php';

    // Reference the Dompdf namespace 
    use Dompdf\Dompdf;


    // untuk memasukkan gambar dalam tabelnya
    $path = base_url() . '/assets/img/logobw.png';
    $type = pathinfo($path, PATHINFO_EXTENSION);
    $data = file_get_contents($path);
    $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

    // promo
    if ($faktur->promo == '1') {
        $promo = 'Biasa';
    } elseif ($faktur->promo == '2') {
        $promo = 'Promo';
    }

    // layanan
    if ($faktur->layanan == '1') {
        $layanan = 'Dine In';
    } elseif ($faktur->layanan == '2') {
        $layanan = 'TakeAway';
    } elseif ($faktur->layanan == '3') {
        $layanan = 'DO';
    }
    $no_faktur=$faktur->notelp;
    $no_faktur_penjualan=$faktur->no_faktur_penjualan;
    // list produk untuk dimasukkan dalam tabel
    $listproduk = "";
    $total_item = 0;
    $subtotal = 0;
    foreach ($produk as $key) {
        if ($key->jenis == '2') {
            $jenis = "<td valign='bottom'><div style='font-size: 11px;font-family: courier,serif;'>" . $key->nm_barang . " Panas</div></td>";
        }
        if ($key->jenis == '3') {
            $jenis = "<td valign='bottom'><div style='font-size: 11px;font-family: courier,serif;'>" . $key->nm_barang . " Dingin</div></td>";
        }
        if ($key->jenis == '1') {
            $jenis = "<td valign='bottom'><div style='font-size: 11px;font-family: courier,serif;'>" . $key->nm_barang . "</div></td>";
        }

        if ($key->diskonpersen > 0 or $key->diskonrp > 0) {
            $disc = "<i> ~ Disc. " . $key->diskonpersen . "% (" . $key->diskonrp . " )</i></td>";
        } else {
            $disc = "";
        }

        $jumlah = "<td valign='bottom'><div style='font-size: 10px;align-text: right;' align='right'>" . $key->jumlah . "x</div></td>";
        $h = number_format($key->harga, 0, ',', '.');
        $harga = "<td valign='bottom'><div style='font-size: 11px;align-text: right;' align='right'>" . $h . "</div> </td>";
        $s = number_format($key->sub_total_jual, 0, ',', '.');
        $sub_total_jual = "<td valign='bottom'><div style='font-size: 11px;align-text: right;' align='right'>" . $s . "</div></td>";

        if ($key->diskonpersen > 0) {
            $disc2 = "
			<td><div style='font-size: 9px;font-family: courier,sans-serif;' valign='bottom'>
                ~ Disc. " . $key->diskonpersen . " % ( " . $key->diskonrp . " )
            </div></td>";
        } else {
            $disc2 = "";
        }

        $total_item += $key->jumlah;
        $subtotal += $key->sub_total_jual;

        $listprodukbaru = "<tr>$jenis" . $disc . $jumlah . $harga . $sub_total_jual . "</tr>" . "<tr>$disc2</tr>";

        $listproduk = $listproduk . $listprodukbaru;
    }

    $subtotal2 = number_format($subtotal, 0, ',', '.');
    $nppn = number_format($faktur->nppn, 0, ',', '.');
    $diskon = number_format($faktur->diskon, 0, ',', '.');
    $ongkir = number_format($faktur->ongkir, 0, ',', '.');
    $netto = number_format($faktur->netto, 0, ',', '.');
    $nbayar = number_format($faktur->nbayar, 0, ',', '.');
    $kembali = number_format($faktur->nkembali, 0, ',', '.');
    $almt_toko=$toko->almt_toko;
    $nm_toko= $toko->nm_toko;
    $tlp_toko=$toko->tlp_toko;
    $nomeja=$faktur->nomeja;
    $pinojol=$faktur->pinojol;
    $keter=$faktur->keter;

    // mulai generate pdf 
    $dompdf = new Dompdf();

    $html = "<table width='250' border='0' align='center'>
		<tr>
			<td colspan='4' align='center' ><a href='" . base_url('dashboard/') . "'><img  style='width:45%;height:55px;' src='" . $base64 . "' /></a></td>
		<!--	<font color='#00' size='+2' >" . $toko->nm_toko . "</font></a></td> -->
		</tr>
		<tr>
			<td colspan='4' align='center'><font color='#00' style='font-size: 10px;font-family: courier,serif;' >" . $almt_toko . "</font></td>
		</tr>
		<tr>
			<td colspan='4' align='center'><font color='#00' style='font-size: 10px;font-family: courier,serif;'>" . $tlp_toko . "</font></td>
		</tr>
		<tr>
			<td colspan='4'><hr /> </td>
		</tr>
		<tr>  
			<td>No." . $no_faktur . "</td>
			<td colspan='3' align='right'>" . $tgl  . "</td>
		</tr>
		<tr>
			<td>Kasir : " . $nuser . "</td>
			<td colspan='3' align='right'>" . $keter . "</td>			
		</tr>	
		<tr>
			<td>PIN Ojol : " . $pinojol . "</td>
			
			<td colspan='3' align='right'>" . $promo . "</td>			
		</tr>	
		<tr>
			<td>" . $layanan . "</td>
			<td colspan='3' align='right'>No Meja : " . $nomeja . "</td>
		</tr>	
		
		<tr>
			<td colspan='2'><hr size='1' /> </td>
		</tr>
    </table>

	<table width='250' border='0' align='center'>"
        . $listproduk . "
		<tr>
			<td colspan='4'><hr SIZE='1'  /> </td>
		</tr>
		<tr>
			<td>TOTAL</td>
            <td colspan='2'>" . $total_item . " ITEM</td>
            <td><div align='right'>" . $subtotal2 . "</div></td>
        </tr>
		<tr>
            <td align='right'>Pb1/Tax(10%)</td>
            <td></td>
            <td colspan='2'><div align='right'>" . $nppn . "</div></td>
		</tr>
        <tr>
            <td align='right'>Diskon Promo</td>
            <td></td>
            <td colspan='2'><div align='right'>" . $diskon . "</div></td>
		</tr>
		<tr>
            <td align='right'>Ongkos Kirim</td>
            <td></td>
            <td colspan='2'><div align='right'>" . $ongkir . "</div></td>
		</tr>
		<tr>
			<td></td>
			<td></td>
			<td colspan='4'><hr SIZE='1'  /> </td>
		</tr>
		<tr>
            <td align='right'>GRAND TOTAL</td>
            <td></td>
            <td colspan='2'><div align='right'>" . $netto . "</div></td>
		</tr>
		<tr>
            <td align='right'>Bayar</td>
            <td></td>
            <td colspan='2'><div align='right'>" . $nbayar . "</div></td>
		</tr>
		<tr>
			<td></td>	
			<td></td>
			<td colspan='4'><hr SIZE='1'  /> </td>
		</tr>
		<tr>
            <td align='right'>Kembali</td>
            <td></td>
            <td colspan='2'><div align='right'>" . $kembali . "</div></td>
		</tr>
		<tr>
            <td colspan='4' align='center'><hr SIZE='1'  />
            PRODUK ANAK NEGERI</td>
		</tr>
	
			<tr>	
				<td colspan='4' align='center'><i>CV Q-TA, Kediri Jawa Timur</i> </td>
			</tr><tr>	
				<td colspan='4' align='center'><i>Kritik & Saran : 081331479744</i></td>
			</tr><tr>	
				<td colspan='4' align='center'>TERIMA KASIH</td>
			</tr><tr>	
				<td colspan='4' align='center'><i>Selamat Datang Kembali</i></td>
			</tr>
	
	</table>";
    $dompdf->loadHtml($html);

    // Render HTML sebagai PDF 
    $dompdf->render();
    ob_end_clean();

  
    $dompdf->stream(); // untuk download, jika tidak perlu download bisa dikomen
    $pdf_name = $no_faktur; // nama file pdf
    $pdf = $dompdf->output();
    $file_location = $_SERVER['DOCUMENT_ROOT']."/struk/".$pdf_name.".pdf"; // lokasi file pdf akan disimpan (folder server)
  // dokumen root adalah tempat file ini disimpan
    file_put_contents($file_location,$pdf); // meletakkan file output ke folder server
    $kirimwa="D:\web\htdocs\rest\struk/".$pdf_name.".pdf";
    

  // untuk simpan di folder local yg dituju
  // $dompdf->stream() boleh dikomen atau tidak
  // dipaste di bawah $dompdf->stream()
    //$pdf = $dompdf->output();
    $namafile = $no_faktur . ".pdf"; // nama file yg didownload
	$to = "http:\\182.253.40.56:8282\uploaded/".$namafile; // folder yg dituju
	file_put_contents($to,$pdf); // meletakkan file output ke folder yg dituju
    $query2 = $this->db2->query("INSERT ignore INTO outbox (nobukti, wa_mode, wa_no, wa_text, wa_media, wa_file) VALUE ('$no_faktur_penjualan', '0', '$no_faktur', 'Terima Kasih','$kirimwa' , '')");
    
    //$dompdf->stream($namafile); // download file

//    sleep(2); // delay sambil menunggu download selesai
	//$to = movefile($namafile); // fungsi pindah file

    // menyimpan nama file pdfnya
    //$query2 = $this->db->query("INSERT ignore INTO outbox (nobukti, wa_mode, wa_no, wa_text, wa_media, wa_file) VALUE ('$no_faktur_penjualan', '0', '6282313191685', '$listproduk','' , '')");
    
    // proses kirim pdf melalui WA
    // sendmessage($no_faktur_penjualan);

    // $path = "C:\Users\Kuantum\Downloads/" . $namafile; // sesuaikan tempat download anda
    // unlink($path); // untuk menghapus file

    ?>

</body>

</html>