<?php
error_reporting(0);
include('../koneksi.php');

if (isset($_GET['rekening']) AND isset($_GET['tgl'])){
$rekening = $_POST['rekening'];
}
$no_bukti     = "AG".$_SESSION['nokode'].date("YmdHis");
$username     = $_SESSION['username'];
$kode         = $_SESSION['kode'];
$rekening     = $_POST['rekening'];
$tgl          = date('Y-m-d',strtotime($_POST['tgl']));
$pokok        = str_replace(",", "", $_POST['pokok']);
$margin       = str_replace(",", "", $_POST['margin']);
$jumlah       = $pokok+$margin;
$carabayar   = $_POST['carabayar'];
$keterangan   = $_POST['keterangan'];
$bakidebet   = str_replace(",", "", $_POST['bakidebet'])-$pokok;
$tglnow = date("Y-m-d H:i:s");
$check_querya = mysqli_query($koneksi, "SELECT no_bukti FROM angsuran WHERE no_bukti='$no_bukti'");
if (mysqli_num_rows($check_querya) > 0) {
        echo "<script>alert('Transaksi sudah ada untuk rekening ini.');</script>";
        echo "<script>window.location='../home.php?home=dbecb5ad09705de4976c6bdccf1e1971';</script>";
        exit;
    }

$query = mysqli_query($koneksi,"insert into angsuran SET rekening='$rekening',no_bukti='$no_bukti',tgl='$tgl',plafond='0',pokok='$pokok',margin='$margin',carabayar='$carabayar',keterangan='$keterangan',username='$username',tglsystem='$tglnow' ")or die(mysqli_error($koneksi));
$query = mysqli_query($koneksi,"UPDATE pinjaman SET bakidebet='$bakidebet' where rekening='$rekening' ")or die(mysqli_error($koneksi));

$query = mysqli_query($koneksi,"insert into bukubesar SET id_jurnal='0', no_bukti='$no_bukti',tanggal='$tgl',kode_akun='1.100.10',debet='$jumlah',kredit='0',keterangan='$keterangan',kode='$kode',username='$username',tglsystem='$tglnow' ")or die(mysqli_error($koneksi));
$query = mysqli_query($koneksi,"insert into bukubesar SET id_jurnal='0',no_bukti='$no_bukti',tanggal='$tgl',kode_akun='1.120.10',debet='0',kredit='$pokok',keterangan='$keterangan',kode='$kode',username='$username',tglsystem='$tglnow' ")or die(mysqli_error($koneksi));
$query = mysqli_query($koneksi,"insert into bukubesar SET id_jurnal='0',no_bukti='$no_bukti',tanggal='$tgl',kode_akun='4.410.10',debet='0',kredit='$margin',keterangan='$keterangan',kode='$kode',username='$username',tglsystem='$tglnow' ")or die(mysqli_error($koneksi));
$delete = mysqli_query($koneksi,"DELETE FROM bukubesar WHERE debet='0' AND kredit='0' ")or die(mysqli_error($koneksi));
$register = substr($rekening,0,10);

$pesan = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT nama,nohp FROM registrasi WHERE id='$register' "));
$nohp= $pesan['nohp'];
$nama= $pesan['nama'];
$carabayar = "a"; 
$metodebayar = ($carabayar == "a") ? "Angsuran" : (($carabayar == "t") ? "Tebus" : "Jual");
$jmlangsuran = number_format($jumlah,0);
$kantor = "TOP GADAI ".strtoupper($_SESSION['kantor']);
$query4 = mysqli_query($koneksi,"INSERT INTO message SET number='$nohp',recipientName='$nama',senderName='$kantor',invoiceNumber='$rekening',storageFee='$jmlangsuran',notes='$keterangan',paymentMethod='$metodebayar' ")or die(mysqli_error($koneksi));

/*$pesan2 = "Terima Kasih... Anda Melakukan Gadai Rp.".number_format($plafond,0)." Semoga bisa membantu dan usahanya lancar";
$query4 = mysqli_query($koneksi,"INSERT INTO message SET number='$nohp',pesan='$pesan2' ")or die(mysqli_error($koneksi));*/
echo "Data Sukses Disimpan--!!!";



?>
