<?php 
/* APLIKASI PENJUALAN DPOS PRO
 *
 * Framework DPOS BISNIS berbasis PHP
 *
 * Developed by djavasoft.com
 * Copyright (c) 2018, Djavasoft Smart Technology
 *
 * @author	Mohamad Anton Arizal
 * @copyright	Copyright (c) 2008 Djavasoft. (https://djavasoft.com/)
 *
 *
*/

include'config.php';
function xdate($date){
	if(empty($date) || $date == '' || $date == null){
		return date('d-m-Y');
	}
	$date=explode('-',$date);
	if(count($date) >= 3){
		$date=$date[2].'-'.$date[1].'-'.$date[0];
		return $date;
	}
	return date('d-m-Y');
}
$myfile = fopen("app/page/setting_printer.php", "r") or die("Unable to open file!");
$data= fread($myfile,filesize("app/page/setting_printer.php"));
$set_print=json_decode($data, true);
fclose($myfile);
	$items=doTableArray("kasir_penjualan",array("kode_barang","nama_barang","harga","qty","total","faktur","satuan"),"WHERE status=2 AND faktur='".$_GET['faktur']."'");
	$faktur=doTableArray("faktur",array("date","pelanggan_id","total","pemasukan","diskon","voucher","dibayar","kembali","user_id","disc","pajak","pjk"),"where faktur='".$_GET['faktur']."'");
	
	// Validasi array faktur
	if(!is_array($faktur) || count($faktur) == 0 || !isset($faktur[0]) || !is_array($faktur[0])){
		echo '<div class="alert alert-danger">Data faktur tidak ditemukan</div>';
		exit;
	}
	
	$date= isset($faktur[0][0]) ? $faktur[0][0] : date('Y-m-d');
	$pelanggan_id= isset($faktur[0][1]) ? intval($faktur[0][1]) : 0;
	$total= isset($faktur[0][2]) ? floatval($faktur[0][2]) : 0;
	$pemasukan= isset($faktur[0][3]) ? floatval($faktur[0][3]) : 0;
	$diskon= isset($faktur[0][4]) ? floatval($faktur[0][4]) : 0;
	$voucher= isset($faktur[0][5]) ? floatval($faktur[0][5]) : 0;
	$dibayar= isset($faktur[0][6]) ? floatval($faktur[0][6]) : 0;
	$kembali= isset($faktur[0][7]) ? floatval($faktur[0][7]) : 0;
	$user_id_faktur= isset($faktur[0][8]) ? intval($faktur[0][8]) : 0;
	$userName= $user_id_faktur > 0 ? userName($user_id_faktur) : '-';
	$disc= isset($faktur[0][9]) ? floatval($faktur[0][9]) : 0;
	$pajak= isset($faktur[0][10]) ? floatval($faktur[0][10]) : 0;
	$pjk= isset($faktur[0][11]) ? floatval($faktur[0][11]) : 0;

	if(intval($pelanggan_id) !='' && intval($pelanggan_id)!=0){
		$supplier=doTableArray("pelanggan",array("nama_pelanggan"),"where id='".intval($pelanggan_id)."'");
		if(is_array($supplier) && count($supplier) > 0 && isset($supplier[0][0])){
			$nama_pelanggan=$supplier[0][0];
		}else{
			$nama_pelanggan='-';
		}
	}else{
		$nama_pelanggan='-';
	}
	
	?>
	<html moznomarginboxes="" mozdisallowselectionprint="" 
	style="margin-top:<?=$set_print["margin_top"];?>mm;margin-left:<?=$set_print["margin_left"];?>mm;"
	><head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>	<?php echo getPengaturan('nama_toko');?> - Cetak Nota        </title>
	<style type="text/css">
	pre{
		font-weight:bold;
	}

	@media print {
	@page  { 
	margin: 0mm;
	font-size:8px;
	}
	}

	</style>
	</head>
	<body onload="setTimeout(function(){window.print();}, 500);" cz-shortcut-listen="true" >
<?php 
if($_GET['ukuran']=='58'){?>
<style type="text/css">
body{width: <?=$_GET['ukuran'];?>mm}
</style>
<p style="font-size:12px;font-family:arial;text-align:center">
<?php if(file_exists('images/system/logo.png') AND $set_print["show_logo"]==1)
{
echo  '<img src="images/system/logo.png?t='.rand().'" style="margin:3px;width:50px;height:50px"><br>';
} ?>
<b><?php echo getPengaturan('nama_toko');?></b><br><?php echo getPengaturan('alamat');?> <br>Telp. <?php echo getPengaturan('no_hp');?>

</p>
<pre>
---------------------------------
DATE	: <?php echo xDate($date);?> <?php echo date("H:i:s");?><br>FAKTUR	: <?php echo $_GET['faktur'];?><br>KASIR	: <?php echo $userName;?>
    <br>PELANGGAN : <?php echo 	$nama_pelanggan;?>
<br>---------------------------------<br> Harga	Qty Sat		Total<br>---------------------------------
<?php
$i=1;
if(is_array($items) && count($items) > 0){
	foreach($items as $row){
		if(!isset($row[1]) || !isset($row[2]) || !isset($row[3]) || !isset($row[4]) || !isset($row[6])){
			continue;
		}
		echo ''.$row[1].'<br>';
		echo ' '. currency($row[2]).'';
		echo ' x ';
		echo ''.$row[3].' '.$row[6].'  ';
		echo 'Rp.'. currency($row[4]).'<br>';
		$i++; 	  
	}
}
?>
---------------------------------
<?php if(intval($diskon)!=0 OR intval($diskon)!=''){?>Harga	:	Rp.<?php echo currency($total);?><br>Diskon	:	Rp.<?php echo currency($diskon);?>(<?php echo $disc;?>%)<?php } ?><?php if(intval($voucher)!=0 OR intval($voucher)!=''){?><br>Voucher	:	Rp.<?php echo currency($voucher);?><?php } ?><?php if(intval($pjk)!=0 OR intval($pjk)!=''){?><br>Pajak	:	Rp.<?php echo currency($pajak);?>(<?php echo $pjk;?>%)<?php } ?><br>Total	:	Rp.<?php echo currency($pemasukan);?><br>Bayar	:	Rp.<?php echo currency($dibayar);?><br>	-------------------------<br>Kembali	:	Rp.<?php echo currency($kembali);?><br>=================================
<center><?=$set_print["tulisan_footer"];?></center>
<BR>
</pre>
<?php } elseif($_GET['ukuran']=='80'){
/////////////////////////////////////// UKURAN 80MM ////////////////////////////////////////////////////////
?>
<style type="text/css">
body{width: <?=$_GET['ukuran'];?>mm}
</style>
<p style="font-size:12px;font-family:arial;text-align:center">
<?php if(file_exists('images/system/logo.png') AND $set_print["show_logo"]==1)
{
echo  '<img src="images/system/logo.png?t='.rand().'" style="margin:3px;width:50px;height:50px"><br>';
} ?>
<b><?php echo getPengaturan('nama_toko');?></b><br><?php echo getPengaturan('alamat');?> <br>Telp. <?php echo getPengaturan('no_hp');?>

</p>
<pre>
--------------------------------------
DATE	: <?php echo xDate($date);?> <?php echo date("H:i:s");?><br>FAKTUR	: <?php echo $_GET['faktur'];?><br>KASIR	: <?php echo $userName;?>
<br>PELANGGAN : <?php echo 	$nama_pelanggan;?>
<br>--------------------------------------<br> Harga	Qty Sat Total<br>--------------------------------------
<?php
$i=1;
if(is_array($items) && count($items) > 0){
	foreach($items as $row){
		if(!isset($row[1]) || !isset($row[2]) || !isset($row[3]) || !isset($row[4]) || !isset($row[6])){
			continue;
		}
		echo ''.$row[1].'<br>';
		echo ' '. currency($row[2]).'';
		echo ' x ';
		echo ''.$row[3].' '.$row[6].' ';
		echo 'Rp.'. currency($row[4]).'<br>';
		$i++; 	  
	}
}
?>
--------------------------------------
 <?php if(intval($diskon)!=0 OR intval($diskon)!=''){?>Harga		:	Rp.<?php echo currency($total);?><br> Diskon		:		Rp.<?php echo currency($diskon);?>(<?php echo $disc;?>%)<?php }?><?php if(intval($voucher)!=0 OR intval($voucher)!=''){?><br> Voucher		:		Rp.<?php echo currency($voucher);?><?php } ?><?php if(intval($pjk)!=0 OR intval($pjk)!=''){?><br> Pajak		:	Rp.<?php echo currency($pajak);?>(<?php echo $pjk;?>%)<?php } ?><br> Total		:	Rp.<?php echo currency($pemasukan);?><br> Bayar		:	Rp.<?php echo currency($dibayar);?><br>		----------------------<br> Kembali	:	Rp.<?php echo currency($kembali);?><br>======================================
<center><?=$set_print["tulisan_footer"];?></center>

<BR>
</pre>

<?php }elseif($_GET['ukuran']=='A4'){ ?>
<link href="<?php echo $CORE_URL;?>/assets/adminLTE/css/bootstrap.min.css" rel="stylesheet">

<div class="title" style="margin-bottom:10px;text-align:left;margin-top:10px;margin-left:10px">
<b><?php echo getPengaturan('nama_toko');?></b>
<br>
<small><?php echo getPengaturan('alamat');?> Telp/HP <?php echo getPengaturan('no_hp');?></small>
</div>
<table class="table">
<tr>
<td style="width:150px">Tanggal</td><td style="width:30px">:</td><td><?php echo $date;?> <?php echo date('H:i:s');?> </td>
<td >Pelanggan</td><td style="width:30px">:</td><td><?php echo $nama_pelanggan;?></td>
</tr>
<tr>
<td style="width:150px">Faktur Penjualan</td><td>:</td><td><?php echo $_GET['faktur'];?></td>
<td style="width:150px"></td><td></td><td></td>
</tr>
</table>

<table class="table " id="dataPenjualan" cellspacing="0">
  <thead>
	<tr>
	  <th style="width:50px">No</th>
	  <th>Kode</th>
	  <th>Nama Barang</th>
	  <th>Harga</th>
	  <th>Qty</th>
	  <th>Satuan</th>
	  <th >Total</th>
	</tr>
  </thead>
  <tbody >
  <?php
  $i=1;
	if(is_array($items) && count($items) > 0){
		foreach($items as $row){
			if(!isset($row[0]) || !isset($row[1]) || !isset($row[2]) || !isset($row[3]) || !isset($row[4]) || !isset($row[6])){
				continue;
			}
			echo '<tr>';
			echo '<td>'.$i.'</td>';
			echo '<td>'.$row[0].'</td>';
			echo '<td>'.$row[1].'</td>';
			echo '<td>'. currency($row[2]).'</td>';
			echo '<td>'.$row[3].'</td>';
			echo '<td>'.$row[6].'</td>';
			echo '<td>'. currency($row[4]).'</td>';
			echo '</tr>';
			$i++; 	  
		}
	}
  ?>
<?php if(intval($voucher) != 0 || intval($voucher)!=''){?>  <tr><td colspan=3></td><td colspan=2><b>Voucher</td><td> : <?php echo currency(intval($voucher));?></td></tr><?php } ?>
<?php if(intval($diskon) != 0 || intval($diskon)!=''){?>  <tr><td colspan=3></td><td colspan=2><b>Diskon</td><td> : <?php echo currency(intval($diskon));?></td></tr><?php } ?>
  <tr><td colspan=3></td><td colspan=3><b>Total</b></td><td> : <?php echo currency($pemasukan);?></td></tr>
  <tr><td colspan=3></td><td colspan=3><b>Dibayar</b></td><td> : <?php echo  currency(intval ($dibayar));?></td></tr>
  <tr><td colspan=3></td><td colspan=3><b>Kembali</b></td><td> : <?php echo  currency(intval ($kembali));?></td></tr>
  </tbody>
  </table> 
<?php } ?>
	</body></html>
