<?php
    function terbilang($satuan) {
		$huruf = array("", "Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh", "Delapan", "Sembilan", "Sepuluh", "Sebelas");
		if ($satuan < 12)
		return " " . $huruf[$satuan];
		elseif ($satuan < 20)
		return terbilang($satuan - 10) . "Belas";
		elseif ($satuan < 100)
		return terbilang($satuan / 10) . " Puluh" . Terbilang($satuan % 10);
		elseif ($satuan < 200)
		return " seratus" . Terbilang($satuan - 100);
		elseif ($satuan < 1000)
		return terbilang($satuan / 100) . " Ratus" . Terbilang($satuan % 100);
		elseif ($satuan < 2000)
		return " seribu" . Terbilang($satuan - 1000);
		elseif ($satuan < 1000000)
		return terbilang($satuan / 1000) . " Ribu" . Terbilang($satuan % 1000);
		elseif ($satuan < 1000000000)
		return terbilang($satuan / 1000000) . " Juta" . Terbilang($satuan % 1000000);
		elseif ($satuan <= 1000000000)
		echo "Maaf Tidak Dapat di Prose Karena Jumlah Uang Terlalu Besar ";
	} 
	$terbilangs = "# ".strtoupper(terbilang($invoice['total']))." #";
	$q = "SELECT sum(total) totala FROM `additional` where id_invoice='".$invoice['id_invoice']."' and status='1'";
	$ccp = $this->db->query($q)->result_array();
	$total = "0";
	if(count($ccp)>0){
		$total= $ccp[0]['totala'];
	}	
	$bayar = "0";
	$q2 = "SELECT sum(total) bayar FROM `bayar` where id_invoice='".$invoice['id_invoice']."' and status='1'";
	$ccp2 = $this->db->query($q2)->result_array();
	if(count($ccp2)>0){
		$bayar= $ccp2[0]['bayar'];
	}
	$sisa = $total - $bayar;
	$terbilangs2 = "# ".strtoupper(terbilang($sisa))." #";
	
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Invoice</title> 
    <link href="<?=base_url()?>assets/webadmin/css/print.css" rel="stylesheet" />
</head>
<body>
	<?php $this->load->view('css_pdf.php')?>
	
	<table width='100%' cellpadding='0' cellspacing='0'>
		<tr>
			<td>
				<table width='100%'>
					<tr>
						<td width='50%'>
							<img src='<?=$_SERVER["DOCUMENT_ROOT"]?>/assets/webadmin/images/logo.png' width='150'>
							<!--<img src='<?=base_url()?>assets/webadmin/images/logo.png' width='150'>-->
						</td>
						<td align='right'> 
							<table width='100%'>
								<tr><td colspan='2' width='100'><h1>INVOICE</h1></td></tr> 
								<tr><td>Date </td><td> : <?=$invoice['tanggal']?></td></tr>
								<tr><td>Balance Due </td><td>: <b>Rp. <?=number_format($sisa)?></b></td></tr>
								<?php if($terbilangs2!="#   #"){?>
								<tr><td>&nbsp;</td><td>: <b><?=$terbilangs2?></b></td></tr>
								<?php }?>
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<br><br>
				<b>Bill To :</b><br>
				<b><?=$invoice['nama']?></b>
				<br><br>
			</td>
		</tr>
		<tr>
			<td>
				<table width='100%' cellpadding='0' cellspacing='0' class='table2'>
					<tr>
						<th width='10'>No</th>
						<th width=''>Item</th>
						<th width='10'>Qty</th>
						<th width=''>Rate</th>
						<th width=''>Amount</th>
					</tr>
					<?php
					$no=1;
					if(isset($tiket) && count($tiket) > 0): foreach($tiket as $row):  
					?>
					<tr>
						<td align='center'><?=$no?></td>
						<td align='center'><?=$row['deskripsi']?></td>
						<td align='center'><?=$row['qty']?></td>
						<td align='right'>Rp. <?=number_format($row['harga'])?></td>
						<td align='right'>Rp. <?=number_format($row['total'])?></td>
					</tr>
					<?php $no++; endforeach; endif;?>
					<tr>
						<td colspan='4' align='right'>Sub Total</td>
						<td align='right'><b>Rp. <?=number_format($total)?></b></td>
					</tr>
					<tr>
						<td colspan='4' align='right'>Amount Paid</td>
						<td align='right'><b>Rp. <?=number_format($bayar)?></b></td>
					</tr>
					<tr>
						<td colspan='4' align='right'>Total</td>
						<td align='right'><b>Rp. <?=number_format($sisa)?></b></td>
					</tr>
				</table>
				<?php if(isset($dbayar) && count($dbayar) >0):?>
				<div align='center'><h3>Note Pembayaran</h3><br>&nbsp;</div>
				<table width='100%' cellpadding='0' cellspacing='0' class='table2'>
					<tr>
						<th width='10'>No</th>
						<th width=''>Date</th>
						<th width='10'>Received</th>
						<th width=''>Description</th>
						<th width=''>Total</th>
					</tr>
					<?php
					$no=1;
					if(isset($dbayar) && count($dbayar) > 0): foreach($dbayar as $row):  
					?>
					<tr>
						<td align='center'><?=$no?></td>
						<td align='center'><?=$row['tanggal']?></td>
						<td align='center'><?=$row['dari']?></td>
						<td align='center'><?=$row['deskripsi']?></td>
						<td align='right'>Rp. <?=number_format($row['total'])?></td>
					</tr>
					<?php $no++; endforeach; endif;?>
				</table>
				<?php endif;?>
			</td>
		</tr>
		<tr>
			<td>
				<table width='100%' cellpadding='0' cellspacing='0' class='clearborder'>
					<tr>
						<td width='80%'> 
						</td>
						<td align='center'>
							<br>
							Bogor, <?=date("Y-m-d")?>
							<br><br><br><br><br>
							Hendry Ferdiansyah
						</td>
					</tr>
				</table>
			</td>
		</tr>		
	</table>
</body>
</html>