<?php 
if(isset($_GET['tableFakturHutang'])){

if(isset($_GET['lunas'])){
	$lunas="AND hutang_sisa=0";
}elseif(isset($_GET['belum_lunas'])){
	$lunas="AND hutang_sisa>0";
}elseif(isset($_GET['belum_dibayar'])){
	$lunas="AND hutang_dibayar=0";
}else{
	$lunas="";
}

doTableJSON($_GET['tableFakturHutang'],array(
"id",
"date",
"faktur",
"hutang",
"hutang_dibayar",
"hutang_sisa",
"tempo",
"supplier_id",
"total",
"grand_total"
),
"WHERE mode='pembelian' AND status='kredit' ".$lunas."");
}
else
if(isset($_GET['startDate']) AND isset($_GET['filterHutang'])){
	
if($_GET['status']!=''){	
	$status="AND status='".$_GET['status']."'";
}else{	$status='';}
if($_GET['user_id']!=''){	
	$user_id="AND user_id='".$_GET['user_id']."'";
}else{	$user_id='';}
if($_GET['pelanggan_id']!=''){	
	$pelanggan_id="AND pelanggan_id='".$_GET['pelanggan_id']."'";
}else{	$pelanggan_id='';}


$startDate=explode('/',$_GET['startDate']);	
$startDate=$startDate[2].'-'.$startDate[1].'-'.$startDate[0];	

$endDate=explode('/',$_GET['endDate']);	
$endDate=$endDate[2].'-'.$endDate[1].'-'.$endDate[0];	
	
doTableJSON($_GET['filterHutang'],array(
"id",
"date",
"faktur",
"hutang",
"hutang_dibayar",
"hutang_sisa",
"tempo",
"supplier_id",
"total",
"grand_total"
),"WHERE mode='pembelian' AND date>='".$startDate."' AND date<='".$endDate."'   ".$status." ".$user_id." ".$pelanggan_id." ");
}

else
if(isset($_GET['tableHutang'])){
	
$items=doTableArray("kasir_pembelian",array("kode_barang","nama_barang","harga","qty","total","faktur"),"WHERE faktur='".$_GET['faktur']."' AND status=2");
$faktur=doTableArray("faktur",array("date","supplier_id","pengeluaran","diskon","voucher","hutang","hutang_dibayar","hutang_sisa","tempo"),"where faktur='".$_GET['faktur']."' AND status='kredit'");

// 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');
if($date && $date != ''){
	$date = new DateTime($date);
	$date = $date->format('d-m-Y');
} else {
	$date = date('d-m-Y');
}

$tempo= isset($faktur[0][8]) ? $faktur[0][8] : '';
if($tempo && $tempo != '' && $tempo != '0'){
	$tempo = new DateTime($tempo);
	$tempo = $tempo->format('d-m-Y');
} else {
	$tempo = '-';
}

$supplier_id= isset($faktur[0][1]) ? intval($faktur[0][1]) : 0;
$pengeluaran= isset($faktur[0][2]) ? floatval($faktur[0][2]) : 0;
$diskon= isset($faktur[0][3]) ? floatval($faktur[0][3]) : 0;
$voucher= isset($faktur[0][4]) ? floatval($faktur[0][4]) : 0;
$hutang= isset($faktur[0][5]) ? floatval($faktur[0][5]) : 0;
$hutang_dibayar= isset($faktur[0][6]) ? floatval($faktur[0][6]) : 0;
$hutang_sisa= isset($faktur[0][7]) ? floatval($faktur[0][7]) : 0;

if(intval($supplier_id) !='' && intval($supplier_id)!=0){
	$supplier=doTableArray("supplier",array("nama_supplier"),"where id='".intval($supplier_id)."'");
	if(is_array($supplier) && count($supplier) > 0 && isset($supplier[0][0])){
		$nama_supplier=$supplier[0][0];
	}else{
		$nama_supplier='-';
	}
}else{
	$nama_supplier='-';
}

if($hutang == $hutang_dibayar){
	$lunas='<input id="lunas" type="button" class="btn btn-success btn-xs" value="Lunas">';
}else{
	$lunas='<input id="lunas"  type="button" class="btn btn-warning btn-xs" value="Belum Lunas">';
}
?>
<script>
$(document).ready(function() {
	var lunas = $('#lunas').val();
	
	if(lunas =='Lunas'){
		$("#bayarHutang").prop('disabled', true);
		
	}else{
		$("#bayarHutang").prop('disabled', false);

	}
	});	
</script>
<input id="getFaktur" style="display:none" value="<?php echo $_GET['faktur'];?>">
<div style="overflow-y: auto; height:430px; ">
<div id="printArea">
<div class="title" style="margin-bottom:10px;text-align:left">
<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:200px">Tanggal</td><td style="width:30px">:</td><td><?php echo $date;?></td>
<td style="width:200px">Supplier</td><td style="width:30px">:</td><td><?php echo $nama_supplier;?></td>
</tr>
<tr>
<td>Faktur Hutang</td><td>:</td><td><?php echo $_GET['faktur'];?></td>
<td>Status</td><td>:</td><td><?php echo $lunas;?></td>
</tr>
<tr>
<td>Jatuh Tempo</td><td>:</td><td><?php echo $tempo;?></td>
<td></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 style="width:200px" >Total</th>
	</tr>
  </thead>
  <tbody >
  <?php
  $i=1;
	foreach($items as $row){
	  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>'. currency($row[4]).'</td>';
	  echo '</tr>';
	  $i++; 	  
  }   
  ?>
  <tr><td colspan=3></td><td colspan=2><b>Total Hutang</b></td><td> <input class="form-control"  value="<?php echo  currency(intval ($hutang));?>"readonly></td></tr>
  <tr><td colspan=3></td><td colspan=2><b>Hutang Terbayar</b></td><td> <input class="form-control"  value="<?php echo  currency(intval ($hutang_dibayar));?>" readonly></td></tr>
  <tr><td colspan=3></td><td colspan=2><b>Sisa Hutang</b></td><td><input class="form-control"  value="<?php echo  currency(intval($hutang_sisa));?>"readonly></td></tr>
  </tbody>
  </table> 
<input id="faktur" value="<?php echo $_GET['faktur'];?>" hidden >
</div>
</div>
<?php
}
else
if(isset($_GET['bayarHutang'])){
$faktur=doTableArray("faktur",array("date","supplier_id","pengeluaran","diskon","voucher","hutang","hutang_dibayar","hutang_sisa","tempo"),"where faktur='".$_GET['faktur']."' AND status='kredit'");

// 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');
if($date && $date != ''){
	$date = new DateTime($date);
	$date = $date->format('d-m-Y');
} else {
	$date = date('d-m-Y');
}

$tempo= isset($faktur[0][8]) ? $faktur[0][8] : '';
if($tempo && $tempo != '' && $tempo != '0'){
	$tempo = new DateTime($tempo);
	$tempo = $tempo->format('d-m-Y');
} else {
	$tempo = '-';
}

$supplier_id= isset($faktur[0][1]) ? intval($faktur[0][1]) : 0;
$pengeluaran= isset($faktur[0][2]) ? floatval($faktur[0][2]) : 0;
$diskon= isset($faktur[0][3]) ? floatval($faktur[0][3]) : 0;
$voucher= isset($faktur[0][4]) ? floatval($faktur[0][4]) : 0;
$hutang= isset($faktur[0][5]) ? floatval($faktur[0][5]) : 0;
$hutang_dibayar= isset($faktur[0][6]) ? floatval($faktur[0][6]) : 0;
$hutang_sisa= isset($faktur[0][7]) ? floatval($faktur[0][7]) : 0;

if(intval($supplier_id) !='' && intval($supplier_id)!=0){
	$supplier=doTableArray("supplier",array("nama_supplier"),"where id='".intval($supplier_id)."'");
	if(is_array($supplier) && count($supplier) > 0 && isset($supplier[0][0])){
		$nama_supplier=$supplier[0][0];
	}else{
		$nama_supplier='-';
	}
}else{
	$nama_supplier='-';
}

$BH=explode('.',$_GET['faktur']);
$BH=$BH[2];
?>
	

<div style="width:500px">
<table class="table">
<input  style="display:none" class="form-control" type="text" id="user_id" value="<?php echo userID($_SESSION['user']);?>">
<input style="display:none" class="form-control" id="hutang_sisa_hide" value="<?php echo  intval ($hutang_sisa);?>"readonly>
  <tr><td colspan=3></td><td colspan=2><b>Faktur Pembayaran</b></td><td> <input  class="form-control" type="text" id="fakturHutang" value="<?php echo getFakturID('KSR-'.userID($_SESSION['user']),"BH").'-'.$BH;?>"></td></tr>
  <tr><td colspan=3></td><td colspan=2><b>Faktur Hutang</b></td><td> <input  class="form-control" type="text" id="fakturPembelian" value="<?php echo $_GET['faktur'];?>">
 </td></tr>
  <tr><td colspan=3></td><td colspan=2><b>Total Hutang</b></td><td> <input class="form-control" id="hutang" value="<?php echo  intval ($hutang);?>"readonly></td></tr>
  <tr><td colspan=3></td><td colspan=2><b>Hutang Terbayar</b></td><td> <input class="form-control" id="hutang_dibayar" value="<?php echo  intval ($hutang_dibayar);?>" readonly></td></tr>
  <tr><td colspan=3></td><td colspan=2><b>Sisa Hutang</b></td><td><input class="form-control" id="hutang_sisa" value="<?php echo  intval ($hutang_sisa);?>"readonly></td></tr>
  <tr><td colspan=3></td><td colspan=2><b>Pembayaran Tunai</b></td><td>  
  <div class="input-group">
  <input class="form-control" id="bayarTunai"  onkeyup="formatValue(this)">
  </div>
  <!-- Tombol Aksi Cepat Pembayaran -->
  <div style="margin-top:10px">
  <label style="font-size:12px;color:#666;margin-bottom:5px;display:block"><i class="fa fa-lightning-bolt"></i> Aksi Cepat:</label>
  <div class="btn-group" style="width:100%">
  <button type="button" class="btn btn-sm btn-success quick-pay-hutang" data-amount="10000" style="width:25%">10rb</button>
  <button type="button" class="btn btn-sm btn-success quick-pay-hutang" data-amount="20000" style="width:25%">20rb</button>
  <button type="button" class="btn btn-sm btn-success quick-pay-hutang" data-amount="50000" style="width:25%">50rb</button>
  <button type="button" class="btn btn-sm btn-success quick-pay-hutang" data-amount="100000" style="width:25%">100rb</button>
  </div>
  </div>
  </td></tr>
 </table>
</div>
<?php
}

elseif(isset($_GET['inputHutang'])){
	
$fakturPembelian=$_GET['fakturPembelian'];
$fakturHutang=$_GET['fakturHutang'];
$hutang=floatval($_GET['hutang']);
$hutang_sisa_old=floatval($_GET['hutang_sisa']);
$bayarTunai=floatval($_GET['bayarTunai']);
$user_id=$_GET['user_id'];

// Validasi pembayaran tidak boleh melebihi sisa hutang
if($bayarTunai > $hutang_sisa_old){
	$bayarTunai = $hutang_sisa_old;
}

// Hitung ulang hutang_sisa
$hutang_sisa = $hutang_sisa_old - $bayarTunai;
if($hutang_sisa < 0){
	$hutang_sisa = 0;
}

// Ambil data faktur untuk mendapatkan hutang_dibayar yang sudah ada
$fakturData=doTableArray("faktur",array("hutang_dibayar"),"where faktur='".$fakturPembelian."' AND status='kredit'");
$hutang_dibayar_lama = 0;
if(is_array($fakturData) && count($fakturData) > 0 && isset($fakturData[0][0])){
	$hutang_dibayar_lama = floatval($fakturData[0][0]);
}
$hutang_dibayar_baru = $hutang_dibayar_lama + $bayarTunai;

		doInsert("faktur",
		"
		faktur,pelanggan_id,tanggal,bulan,tahun,date,total,
		pemasukan,pengeluaran,voucher,diskon,grand_total,dibayar,kembali,
		mode,keterangan,status,hutang,hutang_dibayar,hutang_sisa,tempo,
		user_id,disc,pjk,pajak
		",
		"
		'".$fakturHutang."',
		'',
		'".date('d')."',
		'".date('m')."',
		'".date('Y')."',
		'".date('Y-m-d')."',
		'".$bayarTunai."',
		'0',
		'".$bayarTunai."',
		'0',
		'0',
		'".$hutang."',
		'".$bayarTunai."',
		'0',
		'bayar hutang',
		'Bayar Hutang : ".$fakturPembelian."',
		'tunai',
		'".$hutang."',
		'".$bayarTunai."',
		'".$hutang_sisa."',
		'0',
		'".$user_id."',
		'0',
		'0',
		'0'
		");

// Update faktur pembelian dengan hutang_dibayar baru dan hutang_sisa yang sudah dihitung ulang
doUpdate("faktur",
"
hutang_dibayar='".$hutang_dibayar_baru."',
hutang_sisa='".$hutang_sisa."'

",
"WHERE faktur='".$fakturPembelian."'"
);


}
else
if(isset($_GET['totalHutang'])){

// Gunakan hutang_sisa untuk menghitung total hutang yang masih tersisa
$kredit=sumData('faktur','hutang_sisa',"WHERE  mode='pembelian' AND status='kredit'");
if(!$kredit || $kredit == null){
	$kredit = 0;
}

	?>
	
<table class="table">
<tr>
<td><h3>Total Hutang</h3></td><td>  <h3><?php echo currency($kredit);?></h3></td>
</tr>
</table>
<?php
}
else
if(isset($_GET['totalHutangFilter'])){

if($_GET['status']!=''){	
	$status="AND status='".$_GET['status']."'";
}else{	$status='';}
if($_GET['user_id']!=''){	
	$user_id="AND user_id='".$_GET['user_id']."'";
}else{	$user_id='';}
if($_GET['pelanggan_id']!=''){	
	$pelanggan_id="AND pelanggan_id='".$_GET['pelanggan_id']."'";
}else{	$pelanggan_id='';}

$startDate=explode('/',$_GET['startDate']);	
$startDate=$startDate[2].'-'.$startDate[1].'-'.$startDate[0];	

$endDate=explode('/',$_GET['endDate']);
$endDate=$endDate[2].'-'.$endDate[1].'-'.$endDate[0];

// Gunakan hutang_sisa untuk menghitung total hutang yang masih tersisa
$kredit=sumData('faktur','hutang_sisa',"WHERE  mode='pembelian' AND status='kredit'  AND date>='".$startDate."' AND date<='".$endDate."' ".$status." ".$user_id." ".$pelanggan_id." ");
if(!$kredit || $kredit == null){
	$kredit = 0;
}

	?>
	
<table class="table">
<tr>
<td><h3>Total Hutang</h3></td><td> <h3><?php echo currency($kredit);?></h3></td>
</tr>
</table>
<?php
}
?>

