<?php
$PID = "bayar";
session_start();
require_once("../lib/dbconn.php");

/*echo "<pre>";
echo print_r($_POST);
echo "</pre>";*/

$cek1 = getFromTable("select count(id) from tbl_pembayaran");
$id = $cek1 + 1;

$cek2 = getFromTable("select count(no_booking)
					from tbl_pembayaran where tgl_bayar = CURRENT_DATE
					and jns_bayar = '".$_POST[jenis]."' ");
$cek3 = str_pad(((int) $cek2 + 1), 3, "0", STR_PAD_LEFT);
$tgl = date("d");
$bln = date("m");
$thn = date("y");

if($_POST[jenis] == "DEPOSIT"){
	$kwt = "DP-".$thn.$bln.$tgl.$cek3;
} else {
	$kwt = "BYR-".$thn.$bln.$tgl.$cek3;
}


$bayar = str_replace(",","",$_POST[bayar]);
$materai = str_replace(",","",$_POST[hrg_materai]);
$sisa1 = str_replace(",","",$_POST[sisa]);
$diskon = str_replace(",","",$_POST[diskon]);
$ppn = str_replace(",","",$_POST[tax]);
$servis = str_replace(",","",$_POST[js_serv]);

if ($_POST[sisa] <= 0 ){
	$sisa = $sisa1;
} else {
	$sisa = "0.00";
}

$ket = explode("-", $_POST[idx]);
if ($bayar > 0){
	//KALAU ADA YANG HARUS DIBAYAR ATAU SUDAH LUNAS KARENA DP 
	mysql_query("insert into tbl_pembayaran (
			id, tgl_bayar, wkt_bayar, jns_bayar, 
			cara_bayar, nm_pembayar, jml_bayar, diskon, 
			materai, sisa, user_input, no_booking, no_kwitansi,tax_ppn, servis, status_bayar, no_kartu) values (
			$id, CURRENT_DATE, CURRENT_TIME, '".$_POST[jenis]."',
			'".$_POST[cab]."', '".$_POST[nm_pembayar]."', $bayar, $diskon,
			$materai, $sisa, '".$_SESSION[nama_usr]."', '".$_POST[idx]."', '".$kwt."', $ppn, $servis, '".$_POST[stat]."', '".$_POST[no_kartu]."')");

}else{
//KALAU ADA SISA DP ?
mysql_query("insert into tbl_pembayaran (
			id, tgl_bayar, wkt_bayar, jns_bayar, 
			cara_bayar, nm_pembayar, jml_bayar, diskon, 
			materai, sisa, user_input, no_booking, no_kwitansi,tax_ppn, servis, status_bayar, no_kartu) values (
			$id, CURRENT_DATE, CURRENT_TIME, '".$_POST[jenis]."',
			'".$_POST[cab]."', '".$_POST[nm_pembayar]."', $bayar, $diskon,
			$materai, $sisa, '".$_SESSION[nama_usr]."', '".$_POST[idx]."', '".$kwt."', $ppn, $servis, '".$_POST[stat]."', '".$_POST[no_kartu]."')");


}



if($_POST[jenis] == "DEPOSIT"){
	
	$cek1 = getFromTable("select count(id) from tbl_transaksi");
	$kd = $cek1 + 1;
	$tgl = getFromTable("select CURRENT_DATE");
	$wkt = getFromTable("select CURRENT_TIME");
	if ($_POST[guest_type] == "002"){
		mysql_query("insert into tbl_transaksi (id, jns_trans, tgl_trans, tgl_entry,
					wkt_entry, no_booking, item_id, qty, harga, 
					diskon, jumlah, user_entry) values 
		            ('$kd', 'ROOM', '$tgl', '$tgl', 
		        	'$wkt', '".$_POST[idx]."', '".$_POST[id_kamar]."', 1, ".$_POST[harga].",
		        	".$_POST[harga].", 0, '".$_SESSION[nama_usr]."')");
	} else {
		mysql_query("insert into tbl_transaksi (id, jns_trans, tgl_trans, tgl_entry,
					wkt_entry, no_booking, item_id, qty, harga, 
					diskon, jumlah, user_entry) values 
		            ('$kd', 'ROOM', '$tgl', '$tgl', 
		        	'$wkt', '".$_POST[idx]."', '".$_POST[id_kamar]."', 1, ".$_POST[harga].",
		        	0, ".$_POST[harga].", '".$_SESSION[nama_usr]."')");
			        	
		        	
	}


	if ($_POST[p] == "input_dp"){
		//mysql_query("update tbl_booking set status_booking = 'booking', tgl_cekin = CURRENT_DATE where id_booking = '".$_POST[idx]."' ");
	} else {
		mysql_query("update tbl_booking set status_booking = 'cekin', tgl_cekin = CURRENT_DATE, 
		tgl_lahir = '".$_POST[tgl_lahir]."', no_ktp = '".$_POST[no_ktp]."', guest_type = '".$_POST[guest_type]."' where id_booking = '".$_POST[idx]."' ");
	}

	mysql_query("update tbl_room set stat_isi = 1, status_kmr = 'isi' where id = '".$_POST[id_kamar]."' ");
	 	
	unset($_SESSION["SELECT_KAMAR_CEKIN"]);
	unset($_SESSION["SELECT_NOMOR_CEKIN"]);
}


if ($ket[0] == "TRS"){
	$tag = getFromTable("select sum(jumlah) from tbl_transaksi where no_booking = '".$_POST[idx]."' ");
	$byr = getFromTable("select sum(jml_bayar + sisa) from tbl_pembayaran where jns_bayar in ('BAYAR') and no_booking = '".$_POST[idx]."' ");
	if ($tag = $byr){
		mysql_query("update tbl_pos_out set status_bayar = 'Y', no_kwitansi = '".$kwt."' where no_id = '".$_POST[idx]."' ");	
	}
	
	
} else {
	if($_POST[stat] == "002"){	
		mysql_query("update tbl_booking set status_booking = 'cekout', tgl_cekout = CURRENT_DATE where id_booking = '".$_POST[idx]."' ");
		mysql_query("update tbl_room set stat_isi = 0, status_kmr = 'kotor' where id = '".$_POST[id_kamar]."' ");
	}
}


if($_POST[jenis] == "BAYAR"){
	$dx = $_POST[jns];
	$s = count($_POST[jns]);
	for ($i = 0; $i <= $s-1; $i++) {
		mysql_query("update tbl_transaksi set status_bayar = 'Y', no_kwitansi = '".$kwt."' where id = ".$dx[$i]);
	}
	?>
<SCRIPT language="JavaScript">
	sWin = window.open('../includes/cetak_bayar.php?idx=<?php echo $_POST[idx];?>', 'xWin','top=0,left=0,width=750,height=550,menubar=no,scrollbars=yes');
	sWin.focus();  
</SCRIPT>
<?php
} else {
	?>
<SCRIPT language="JavaScript">
	sWin = window.open('../includes/cetak_dp.php?idx=<?php echo $_POST[idx];?>', 'xWin','top=0,left=0,width=750,height=550,menubar=no,scrollbars=yes');
	sWin.focus();  
</SCRIPT>
<?php
}


echo "<script language='JavaScript'>document.location='../index2.php?p=".$_POST[p]."&idx=".$_POST[idx]."'</script>";
?>