<?php
//error_reporting(0);
include('../koneksi.php');
if ($_SERVER["REQUEST_METHOD"] == "POST") {

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");

// Retrieve registrant data
    $register = substr($rekening, 0, 10);
    $pesan = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT a.nama AS nama, a.nohp AS nohp,b.plafond AS plafond,b.barang AS barang FROM registrasi a LEFT JOIN pinjaman b ON a.id=b.register WHERE id='$register'"));
    
        $nohp = $pesan['nohp'];
        $nama = $pesan['nama'];
        $plafond =number_format($pesan['plafond'],0);
        $barang = $pesan['barang'];
    $judul = "*INVOICE BIAYA PENITIPAN*";
    $tglpsn = $tgl;                
        // Set payment method
        $metodebayar = ($carabayar == "a") ? "Angsuran" : (($carabayar == "t") ? "Tebus" :"Jual");
        $jmlangsuran = number_format($jumlah, 0);
        $kantor = "*TOP GADAI " .strtoupper($_SESSION['kantor']."*");
        $rekeningpesan = $rekening;
        $ket2 = $keterangan;
$hasilket = preg_replace('/104.*$/', '', $ket2);
        $keteranganpesan = trim($hasilket);

$check_querya = mysqli_query($koneksi, "SELECT no_bukti FROM angsuran WHERE no_bukti='$no_bukti'");
$check_angs = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT (SUM(plafond) - SUM(pokok)) AS saldobd FROM angsuran WHERE rekening='$rekening'"));
    $cekbakidebet = $check_angs['saldobd'];
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;
    }
    if ($cekbakidebet <= 0) {
        echo "<script>alert('Angsuran sudah lunas.');</script>";
        echo "<script>window.location='../home.php?home=dbecb5ad09705de4976c6bdccf1e1971';</script>";
        exit;
    }

$query1 = 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' ")or die(mysqli_error($koneksi));
$query2 = mysqli_query($koneksi,"UPDATE pinjaman SET bakidebet='$bakidebet' where rekening='$rekening' ")or die(mysqli_error($koneksi));

$query3 = mysqli_query($koneksi,"INSERT INTO bukubesar SET no_bukti='$no_bukti',tanggal='$tgl',kode_akun='1.100.10',debet='$jumlah',kredit='0',keterangan='$keterangan',kode='$kode',username='$username' ")or die(mysqli_error($koneksi));
$query4 = mysqli_query($koneksi,"INSERT INTO bukubesar SET no_bukti='$no_bukti',tanggal='$tgl',kode_akun='1.120.10',debet='0',kredit='$pokok',keterangan='$keterangan',kode='$kode',username='$username' ")or die(mysqli_error($koneksi));
$query5 = mysqli_query($koneksi,"INSERT INTO bukubesar SET no_bukti='$no_bukti',tanggal='$tgl',kode_akun='4.410.10',debet='0',kredit='$margin',keterangan='$keterangan',kode='$kode',username='$username' ")or die(mysqli_error($koneksi));
$delete = mysqli_query($koneksi,"DELETE FROM bukubesar WHERE debet='0' AND kredit='0' ")or die(mysqli_error($koneksi));

$query6 = mysqli_query($koneksi, "INSERT INTO message SET number='$nohp',tgl='$tglpsn',judul='$judul', penErima='$nama', PenGirim='$kantor', rekening='$rekeningpesan',barangGadai='$barang',plafond='$plafond', nominal='$jmlangsuran', keterangan='$keteranganpesan', metodeBayar='$metodebayar', kodeFilTer='angsuran' ") 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));*/
// Success message and redirection
        echo "<script>alert('Data angsuran berhasil disimpan');</script>";
        echo "<script>window.location = '../home.php?home=dbecb5ad09705de4976c6bdccf1e1971';</script>";
} else {
    echo "<script>alert('Tombol belum diklik');</script>"; 
    echo "<script>window.location = '../home.php?home=dbecb5ad09705de4976c6bdccf1e1971';</script>"; 
}
?>
