<!DOCTYPE html>
<?php 
	session_start();
	@ob_start();
	include "../../config/koneksi2.php";
	include "../../config/fungsi_rupiah.php";
	include "../../config/fungsi_tanggal.php";

?>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Invoice | ePabrik</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">

  </head>
  <body onload="window.print();" style="font-family:'Sans-serif';font-size: x-large;">
  <?php
		function penyebut($nilai) {
					$nilai = abs($nilai);
					$huruf = array("", "Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh", "Delapan", "Sembilan", "Sepuluh", "Sebelas");
					$temp = "";
					if ($nilai < 12) {
						$temp = " ". $huruf[$nilai];
					} else if ($nilai <20) {
						$temp = penyebut($nilai - 10). " Belas";
					} else if ($nilai < 100) {
						$temp = penyebut($nilai/10)." Puluh". penyebut($nilai % 10);
					} else if ($nilai < 200) {
						$temp = " Seratus" . penyebut($nilai - 100);
					} else if ($nilai < 1000) {
						$temp = penyebut($nilai/100) . " Ratus" . penyebut($nilai % 100);
					} else if ($nilai < 2000) {
						$temp = " seribu" . penyebut($nilai - 1000);
					} else if ($nilai < 1000000) {
						$temp = penyebut($nilai/1000) . " Ribu" . penyebut($nilai % 1000);
					} else if ($nilai < 1000000000) {
						$temp = penyebut($nilai/1000000) . " Juta" . penyebut($nilai % 1000000);
					} else if ($nilai < 1000000000000) {
						$temp = penyebut($nilai/1000000000) . " Milyar" . penyebut(fmod($nilai,1000000000));
					} else if ($nilai < 1000000000000000) {
						$temp = penyebut($nilai/1000000000000) . " Trilyun" . penyebut(fmod($nilai,1000000000000));
					}     
					return $temp;
				}
			 
				function terbilang($nilai) {
					if($nilai<0) {
						$hasil = "minus ". trim(penyebut($nilai));
					} else {
						$hasil = trim(penyebut($nilai));
					}     		
					return $hasil;
				}
				
				
				
				
	  $inv = $_GET['id'];
	  $tgl = mysql_result(mysql_query("select billdate from jual where billno = $inv  "),0);
	  
	  $noord = "EE".substr($tgl,2,2).substr($tgl,5,2)." ".str_pad($inv, 4, "0", STR_PAD_LEFT);
	  $custid 	=  mysql_result(mysql_query("select supplier from jual where billno = $inv "),0);
	  $cust 	=  mysql_result(mysql_query("select nama from customer where id = $custid"),0);
	  $alamat	=  mysql_result(mysql_query("select alamat from customer where id = $custid"),0);
	  $salesman	=  mysql_result(mysql_query("select concat(nama, ' ' , telp) from salesman where id = (select salesman from jualcs where billno = $inv and ucode = '$ucode') "),0);
	  $billdate = tgl_ind($tgl);
	  
		$tot0 = mysql_result(mysql_query("select sum(qty * harga) from jualdetail where billno = $inv "),0);
		$diskon = mysql_result(mysql_query("select diskon from jualcs where billno = $inv "),0);
		$total = format_rupiah($tot0 - $diskon);
		
		$terbilang = terbilang($tot0 - $diskon)." Rupiah";
		
		$company = mysql_result(mysql_query("select nama from setup where id=1"),0);
		$companyadd = mysql_result(mysql_query("select alamat from setup where id=1"),0);
		?>
		<div class='col-xs-12 table-responsive'>
            <table width = '100%' frame='none' >
				<tr>
				<td width ='2%'></td> 
				<td width ='56%'>
					<b>&nbsp;</b>
					<table width = '100%' cellpadding="5px" cellspacing="0" style="border-collapse:collapse;">
						<tr>
							<td width ='100%' style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'><?php echo"$company ";?></td>
						</tr>
						<tr>
							<td width ='100%' style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'><?php echo"$companyadd";?></td>
						</tr>
					</table>
					<br>
					<table width = '100%'   border="2pt solid black" cellpadding="5px" cellspacing="0" style="border-collapse:collapse;">
						<tr>
							<td width ='100%' style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;' >BILL To<br><?php echo"$cust"?> <br> <?php echo"$alamat"?></td>
						</tr>
					</table>
				</td>
				<td width ='2%'></td>
				<td width ='38%'>
					<h3>SALES INVOICE</h3>
					<table width = '100%'   border="2pt solid black" cellpadding="5px" cellspacing="0" style="border-collapse:collapse;">
						<tr>
							<td width ='50%' style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>Invoice Date<br><?php echo"$billdate"?></td>
							<td width ='50%' style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>Invoice No.<br> <?php echo"$noord"?></td>
						</tr>
					</table>
					<br>
					<table width = '100%'   border="3px solid black" cellpadding="5px" cellspacing="0" style="border-collapse:collapse;"	>
						<tr>
							<td width ='50%' style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>Term<br><?php echo"$billdate"?></td>
							<td width ='50%' style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>Penjual<br> <?php echo"$salesman"?></td>
						</tr>
					</table>
				</td>
				<td width ='2%'></td>
				</tr>
			</table>
			 <table width = '100%' frame='none' >
				<tr>
					<td width ='2%'></td>
					<td width ='96%'>
						<table width = '100%'  border="3px solid black" border-collapse: collapse; cellpadding="5px" cellspacing="0" style="border-collapse:collapse;">
							<tr border="3px solid black">
								<td width ='10%' align='center' border="3px solid black" style="border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;">KODE</td>
								<td width ='46%' align='center' border="3px solid black" style="border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;">ITEM DESCRIPTION</td>
								<td width ='10%' align='center' border="3px solid black" style="border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;">QTY</td>
								<td width ='15%' align='center' border="3px solid black" style="border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;">UNIT PRICE</td>
								<td width ='15%' align='center' border="3px solid black" style="border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;">AMOUNT</td>
							</tr>
							<?php
								$hit = 0;
								$cek = mysql_query("Select * from jualdetail where billno = $inv order by billno desc, id asc");
								while ($r=mysql_fetch_array($cek)){	
									$hit = $hit + 1;
									$produk = mysql_result(mysql_query("select product_name from product where product_id = $r[code]"),0);
									$kode = mysql_result(mysql_query("select kode from product where product_id = $r[code]"),0);
									$uprice = format_rupiah($r['harga']);
									$qty = format_rupiah($r['qty']);
									$disc = format_rupiah($r['disc']);
									$tot = format_rupiah(($r['qty'] * ($r['harga'] -($r['harga'] * $r['disc'] / 100))));
									echo"<tr>
											<td width ='10%' align='center' style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>$kode</td>
											<td width ='46%'                style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>$produk</td>
											<td width ='10%' align='center' style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>$qty</td>
											<td width ='15%' align='right'  style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>$uprice</td>
											<td width ='15%' align='right'  style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>$tot</td>
										</tr>";
								}
								
								if ($hit <=5 ){
									for ($x = 0; $x <= 7 - $hit ; $x++) {
									  echo"<tr>
											<td width ='10%'                 style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>&nbsp;</td>
											<td width ='46%'                 style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>&nbsp;</td>
											<td width ='10%'  align='center' style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>&nbsp;</td>
											<td width ='15%'  align='right'  style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>&nbsp;</td>
											<td width ='15%'  align='right'  style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>&nbsp;</td>
										</tr>";
									} 
								}
							?>
							<tr>
								<td colspan = 3 				style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>Say : <?php echo"$terbilang";?></td>
								<td width ='15%'  align='right' style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'>Sub Total</td>
								<td width ='15%'  align='right' style='border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'><?php echo"$total";?></td>
							</tr>
							<tr>
								<td colspan=5  style='vertical-align: top;border-top:2pt solid black;border-left:2pt solid black; border-bottom:2pt solid black; border-right:2pt solid black;'> Description:<br><br></td>
								
							</tr>
							
						</table>
					</td>
					<td width ='2%'></td>
				</tr>
			  </table>
			  
			  <table width = '100%' frame='none' >
					<tr>
						<td width ='2%'></td>
						<td width="20%">Disiapkan Oleh <br><br><br><br>______________</td>
						<td width="20%">Disetujui Oleh <br><br><br><br>______________</td>
						<td width="20%">Diantar Oleh   <br><br><br><br>______________</td>
						<td width="20%">Diterima Oleh  <br><br><br><br>______________</td>
						<td width="18%">&nbsp;</td>
					</tr>
			  </table>
			
		</div>
	

  </body>
</html>
