function Simpan($MhswID, $TahunID, $md, $id)
{
    $BIPOTNamaID = $_REQUEST['BIPOTNamaID'] + 0;
    $Jumlah = $_REQUEST['Jumlah'] + 0;
    $Besar = $_REQUEST['Besar'] + 0;
    $Dibayar = $_REQUEST['Dibayar'] + 0;
    $Catatan = sqling($_REQUEST['Catatan']);
    // Simpan
    if ($md == 0) {
        $s = "update bipotmhsw\n      set Jumlah = '{$Jumlah}',\n          Besar  = '{$Besar}',\n          Dibayar = '{$Dibayar}',\n          Catatan = '{$Catatan}',\n          LoginEdit = '{$_SESSION['_Login']}',\n          TanggalEdit = now()\n      where BIPOTMhswID = '{$id}' ";
        $r = _query($s);
        HitungUlangBIPOTMhsw($MhswID, $TahunID);
        TutupScript($MhswID, $TahunID);
    } elseif ($md == 1) {
        $bn = GetFields('bipotnama', 'BIPOTNamaID', $BIPOTNamaID, '*');
        $s = "insert into bipotmhsw\n      (KodeID, PMBMhswID, MhswID, TahunID,\n      BIPOT2ID, BIPOTNamaID, Nama, TrxID,\n      Jumlah, Besar, Dibayar, Catatan,\n      LoginBuat, TanggalBuat)\n      values\n      ('" . KodeID . "', 1, '{$MhswID}', '{$TahunID}',\n      0, {$BIPOTNamaID}, '{$bn['Nama']}', {$bn['TrxID']},\n      {$Jumlah}, {$Besar}, {$Dibayar}, '{$Catatan}',\n      '{$_SESSION['_Login']}', now())";
        $r = _query($s);
        HitungUlangBIPOTMhsw($MhswID, $TahunID);
        TutupScript($MhswID, $TahunID);
    } else {
        die(ErrorMsg('Error', "Mode edit <b>{$md}</b> tidak dikenali.<br />\n    Hubungi Sysadmin untuk informasi lebih lanjut.\n    <hr size=1 color=silver />\n    Opsi: <input type=button name='Tutup' value='Tutup' onClick=\"window.close()\" />"));
    }
}
function Simpan($MhswID, $KHSID, $md, $BayarID)
{
    $RekeningID = sqling($_REQUEST['RekeningID']);
    $Bank = sqling($_REQUEST['Bank']);
    $BuktiSetoran = sqling($_REQUEST['BuktiSetoran']);
    $Keterangan = sqling($_REQUEST['Keterangan']);
    $Tanggal = "{$_REQUEST['Tanggal_y']}-{$_REQUEST['Tanggal_m']}-{$_REQUEST['Tanggal_d']}";
    $CNT = $_REQUEST['CNT'] + 0;
    $sisane = -1 * $_REQUEST['sisane'] + 0;
    $TTLBYR = $_REQUEST['TTLBYR'] + 0;
    // Cek jika tidak ada detailnya
    if ($sisane < $TTLBYR) {
        die(ErrorMsg('Error', "Penarikan tidak boleh lebih besar<br />\r\n      daripada total yang dapat ditarik<br />\r\n      Hubungi Sysadmin untuk informasi lebih lanjut.\r\n      <hr size=1 color=silver />\r\n      Opsi: <input type=button name='Tutup' value='Tutup' onClick=\"window.close()\" />"));
    }
    if ($CNT == 0) {
        die(ErrorMsg('Error', "Tidak ada detail biaya.<br />\r\n      Data tidak bisa disimpan.<br />\r\n      Hubungi Sysadmin untuk informasi lebih lanjut.\r\n      <hr size=1 color=silver />\r\n      Opsi: <input type=button name='Tutup' value='Tutup' onClick=\"window.close()\" />"));
    }
    // Cek dulu jumlah yg dibayarkan
    $jml = 0;
    for ($i = 1; $i <= $CNT; $i++) {
        $jml += $_REQUEST['BYR_' . $i] + 0;
    }
    if ($jml <= 0) {
        die(ErrorMsg('Error', "Jumlah yang Anda bayarkan: <font size=+1>{$jml}</font>.<br />\r\n      Tidak ada yang perlu dibayarkan.<br />\r\n      Hubungi Sysadmin untuk informasi lebih lanjut.\r\n      <hr size=1 color=silver />\r\n      Opsi: <input type=button name='Tutup' value='Tutup' onClick=\"window.close()\" /"));
    }
    $mhsw = GetFields('mhsw', "KodeID='" . KodeID . "' and MhswID", $MhswID, '*');
    $khs = GetFields('khs', 'KHSID', $KHSID, '*');
    // Oke, mulai simpan datanya
    // 1. Ambil nomer terakhir yang ada
    $BayarMhswID = GetNextBPM();
    // 2. Buat header bukti pembayaran
    $s = "insert into bayarmhsw\r\n    (BayarMhswID, TahunID, KodeID,\r\n    RekeningID, MhswID, TrxID, PMBMhswID,\r\n    Bank, BuktiSetoran, Tanggal,\r\n    Jumlah, Keterangan,\r\n    LoginBuat, TanggalBuat)\r\n    values\r\n    ('{$BayarMhswID}', '{$khs['TahunID']}', '" . KodeID . "',\r\n    '{$RekeningID}', '{$mhsw['MhswID']}', -1, 1,\r\n    '{$Bank}', '{$BuktiSetoran}', '{$Tanggal}',\r\n    {$jml}, '{$Keterangan}',\r\n    '{$_SESSION['_LoginBuat']}', now())";
    $r = _query($s);
    // 3. Simpan detailnya
    for ($i = 1; $i <= $CNT; $i++) {
        $_j = $_REQUEST['BYR_' . $i] + 0;
        if ($_j > 0) {
            // Simpan
            $id = $_REQUEST['BYRID_' . $i] + 0;
            $BIPOTNamaID = GetaField('bipotmhsw', "BIPOTMhswID", $id, 'BIPOTNamaID');
            // Simpan dulu detail pembayarannya
            $s = "insert into bayarmhsw2\r\n        (BayarMhswID, BIPOTMhswID, BIPOTNamaID, Jumlah,\r\n        LoginBuat, TanggalBuat)\r\n        values\r\n        ('{$BayarMhswID}', {$id}, '{$BIPOTNamaID}', " . -1 * $_j . ",\r\n        '{$_SESSION['_Login']}', now())";
            $r = _query($s);
            // Update detail biayanya
            $s = "update bipotmhsw\r\n        set Dibayar = Dibayar + " . -1 * $_j . "\r\n        where BIPOTMhswID = {$id}";
            $r = _query($s);
        }
    }
    HitungUlangBIPOTMhsw($MhswID, $khs['TahunID']);
    TutupScript($MhswID);
}
function Simpan($MhswID, $TahunID, $md, $id, $tagihanid)
{
    $BIPOTNamaID = $_REQUEST['BIPOTNamaID'] + 0;
    $Jumlah = $_REQUEST['Jumlah'] + 0;
    $Besar = $_REQUEST['Besar'] + 0;
    $Dibayar = $_REQUEST['Dibayar'] + 0;
    $Catatan = sqling($_REQUEST['Catatan']);
    $tagihanid = $_REQUEST['tagihanid'];
    //$cek_tagihan = GetaField("bipotmhswx", "MhswID='$MhswID' and TahunID='$TahunID' and NA='N' and TrxID", 1, "TagihanID", "order by BIPOTMhswID desc");
    $cek_sisa = GetaField("bipotmhsw", "NA='N' and TagihanID", $tagihanid, "sum(TrxID*Jumlah*Besar)");
    $trxBIPOTID = GetaField('mhsw', "MhswID", $MhswID, 'BIPOTID');
    $trxbipot = GetaField('bipot2', "BIPOTID = '" . $trxBIPOTID . "' and BIPOTNamaID", $BIPOTNamaID, "TrxID");
    //echo $trxbipot;
    if ($trxbipot < 0 && !empty($tagihanid) && $cek_sisa - $Jumlah * $Besar < 0) {
        die(ErrorMsg('Stop', "Potongan tidak boleh melebihi biaya.<br />\r\n    Hubungi Sysadmin untuk informasi lebih lanjut.\r\n    <hr size=1 color=silver />\r\n    Opsi: <input type=button name='Tutup' value='Tutup' onClick=\"window.close()\" />"));
    }
    // Simpan
    if ($md == 0) {
        $s = "update bipotmhsw set Jumlah = '{$Jumlah}', Besar  = '{$Besar}', Dibayar = '{$Dibayar}', Catatan = '{$Catatan}', LoginEdit = '{$_SESSION['_Login']}', TanggalEdit = now() where BIPOTMhswID = '{$id}' ";
        $r = _query($s);
        $s = "update bayarmhsw2 set Jumlah = '{$Dibayar}' where BIPOTMhswID = '{$id}' and BIPOTNamaID = '{$BIPOTNamaID}'";
        $r = _query($s);
        $BayarMhswID = GetaField('bayarmhsw2', "BIPOTMhswID = '{$id}' and BIPOTNamaID", $BIPOTNamaID, 'BayarMhswID');
        $TotalDibayar = GetaField('bayarmhsw2', "BIPOTMhswID = '{$id}' and NA = 'N' and BIPOTNamaID", $BIPOTNamaID, 'sum(Jumlah)') + 0;
        $s = "update bayarmhsw set Jumlah = {$TotalDibayar} where BayarMhswID = '{$BayarMhswID}'";
        $r = _query($s);
        HitungUlangBIPOTMhsw($MhswID, $TahunID);
        TutupScript($MhswID, $TahunID);
    } elseif ($md == 1) {
        $BIPOTID = GetaField('mhsw', "MhswID", $MhswID, 'BIPOTID');
        $bn = GetFields('bipotnama', 'BIPOTNamaID', $BIPOTNamaID, '*');
        $b2 = GetFields('bipot2', "BIPOTID = '{$BIPOTID}' and BIPOTNamaID", $BIPOTNamaID, '*');
        $s = "insert into bipotmhsw (KodeID, PMBMhswID, MhswID, TahunID,TagihanID,\r\n        BIPOT2ID, BIPOTNamaID, Nama, TrxID,\r\n        Jumlah, Besar, Dibayar, Catatan,\r\n        LoginBuat, TanggalBuat)\r\n      values ('" . KodeID . "', 1, '{$MhswID}', '{$TahunID}','{$tagihanid}',\r\n        '{$b2['BIPOT2ID']}', {$BIPOTNamaID}, '{$bn['Nama']}', {$bn['TrxID']},\r\n        {$Jumlah}, {$Besar}, {$Dibayar}, '{$Catatan}',\r\n        '{$_SESSION['_Login']}', now())";
        $r = _query($s);
        //22 juli 2013
        //jika insert tagihan
        //        $idt = mysql_insert_id();
        //        if ($bn[TrxID] == 1) {
        //            $s = "update bipotmhsw set TagihanID = '" . $idt . "' where BIPOTMhswID='" . $idt . "'";
        //            $r = _query($s);
        //        } else {
        //            //jika insert potongan
        //            $cek_tagihan = GetaField("bipotmhsw", "MhswID='$MhswID' and TahunID='$TahunID' and NA='N' and TrxID", 1, "TagihanID", "order by BIPOTMhswID desc");
        //            $s = "update bipotmhsw set TagihanID = '" . $cek_tagihan . "' where BIPOTMhswID='" . $idt . "'";
        //            $r = _query($s);
        //        }
        HitungUlangBIPOTMhsw($MhswID, $TahunID);
        TutupScript($MhswID, $TahunID);
    } else {
        die(ErrorMsg('Error', "Mode edit <b>{$md}</b> tidak dikenali.<br />\r\n    Hubungi Sysadmin untuk informasi lebih lanjut.\r\n    <hr size=1 color=silver />\r\n    Opsi: <input type=button name='Tutup' value='Tutup' onClick=\"window.close()\" />"));
    }
}
function ProsesBIPOT($MhswID, $TahunID)
{
    // Ambil data
    $mhsw = GetFields('mhsw', "KodeID='" . KodeID . "' and MhswID", $MhswID, "*");
    $khs = GetFields('khs', "KodeID = '" . KodeID . "' and TahunID = '{$TahunID}' and MhswID", $MhswID, "*");
    $khslalu = array();
    if ($khs[Sesi] > 1) {
        $sesilalu = $khs[Sesi] - 1;
        $khslalu = GetFields('khs', "KodeID = '" . KodeID . "' and Sesi = '{$sesilalu}' and MhswID", $mhsw[MhswID], "*");
        /* while(!empty($khslalu))
           {	if($khslalu['StatusMhswID'] != 'A')
           {	$sesilalu = $sesilalu-1;
           $khslalu = GetFields('khs', "KodeID = '".KodeID."' and Sesi = '$sesilalu' and MhswID", $MhswID, "*");
           }
           else
           {	break;
           }
           } */
    }
    // Ambil BIPOT-nya
    $s = "select * \r\n    from bipot2 \r\n    where BIPOTID = '{$mhsw['BIPOTID']}'\r\n      and Otomatis = 'Y'\r\n      and NA = 'N'\r\n    order by TrxID, Prioritas";
    $r = _query($s);
    while ($w = _fetch_array($r)) {
        $oke = true;
        // Apakah sesuai dengan status awalnya?
        $pos = strpos($w['StatusAwalID'], "." . $mhsw['StatusAwalID'] . ".");
        $oke = $oke && !($pos === false);
        // Apakah sesuai dengan status mahasiswanya?
        $pos = strpos($w['StatusMhswID'], "." . $khs['StatusMhswID'] . ".");
        $oke = $oke && !($pos === false);
        // Apakah grade-nya?
        if ($oke) {
            if ($w['GunakanGradeNilai'] == 'Y') {
                $pos = strpos($w['GradeNilai'], "." . $mhsw['GradeNilai'] . ".");
                $oke = $oke && !($pos === false);
            }
        }
        // Apakah Jumlah SKS Tahun ini mencukupi?
        if ($oke) {
            if ($w['GunakanGradeIPK'] == 'Y') {
                if ($khs['SKS'] < GetaField('gradeipk', "IPKMin <= {$mhsw['IPK']} and {$mhsw['IPK']} <= IPKMax and KodeID", KodeID, 'SKSMin')) {
                    $oke = false;
                } else {
                    $oke = true;
                }
            }
        }
        // Apakah Grade IPK-nya OK?
        if ($oke) {
            if ($w['GunakanGradeIPK'] == 'Y') {
                $pos = strpos($w['GradeIPK'], "." . GetaField('gradeipk', "IPKMin <= {$mhsw['IPK']} and {$mhsw['IPK']} <= IPKMax and KodeID", KodeID, 'GradeIPK') . ".");
                $oke = $oke && !($pos === false);
            }
        }
        // Apakah dimulai pada sesi ini?
        if ($oke) {
            if ($w['MulaiSesi'] <= $khs['Sesi'] or $w['MulaiSesi'] == 0) {
                $oke = true;
            } else {
                $oke = false;
            }
        }
        // Simpan data
        if ($oke) {
            // Cek, sudah ada atau belum? Kalau sudah, ambil ID-nya
            $ada = GetaField('bipotmhsw', "KodeID='" . KodeID . "' and MhswID = '{$mhsw['MhswID']}'\r\n        and PMBMhswID = 1 and NA = 'N'\r\n        and TahunID='{$khs['TahunID']}' and BIPOT2ID", $w['BIPOT2ID'], "BIPOTMhswID") + 0;
            // Cek apakah memakai script atau tidak?
            if ($w['GunakanScript'] == 'Y') {
                BipotGunakanScript($mhsw, $khs, $w, $ada, 1);
            } else {
                // Jika tidak ada duplikasi, maka akan di-insert. Tapi jika sudah ada, maka dicuekin aja.
                if ($ada == 0) {
                    // Simpan
                    $Nama = GetaField('bipotnama', 'BIPOTNamaID', $w['BIPOTNamaID'], 'Nama');
                    if ($w['PerMatakuliah'] == 'N') {
                        $s1 = "insert into bipotmhsw\r\n              (KodeID, COAID, PMBMhswID, MhswID, TahunID,\r\n              BIPOT2ID, BIPOTNamaID, Nama, TrxID,\r\n              Jumlah, Besar, Dibayar,\r\n              Catatan, NA,\r\n              LoginBuat, TanggalBuat)\r\n              values\r\n              ('" . KodeID . "', '{$w['COAID']}', 1, '{$mhsw['MhswID']}', '{$khs['TahunID']}',\r\n              '{$w['BIPOT2ID']}', '{$w['BIPOTNamaID']}', '{$Nama}', '{$w['TrxID']}',\r\n              1, '{$w['Jumlah']}', 0,\r\n              'Auto', 'N',\r\n              '{$_SESSION['_Login']}', now())";
                        $r1 = _query($s1);
                        //22 juli 2013
                        //jika insert tagihan
                        $idt = mysql_insert_id();
                        $bn = GetFields('bipotnama', 'BIPOTNamaID', $w[BIPOTNamaID], '*');
                        if ($bn[TrxID] == 1) {
                            $sx = "update bipotmhsw set TagihanID = '" . $idt . "' where BIPOTMhswID='" . $idt . "'";
                            $rx = _query($sx);
                        } else {
                            //jika insert potongan
                            $cek_tagihan = GetaField("bipotmhsw", "MhswID='{$mhsw['MhswID']}' and TahunID='{$khs['TahunID']}' and NA='N' and TrxID", 1, "TagihanID", "order by BIPOTMhswID desc");
                            $sx = "update bipotmhsw set TagihanID = '" . $cek_tagihan . "' where BIPOTMhswID='" . $idt . "'";
                            $rx = _query($sx);
                        }
                    } else {
                        // Ambil BIPOT Biaya Per Mata Kuliah
                        // mario
                        // 200813
                        $Jumlah2 = 0;
                        $COAID = '';
                        $sxx = "select k.MKKode, k.Nama, k.SKS, j.BiayaKhusus, j.Biaya, j.NamaBiaya, j.AdaResponsi\r\n                                  from krs k \r\n                                          left outer join jadwal j on k.JadwalID=j.JadwalID and j.KodeID='" . KodeID . "'\r\n                                          left outer join mk mk on mk.MKID=k.MKID and mk.KodeID='" . KodeID . "'\r\n                                  where k.MhswID='{$mhsw['MhswID']}' and k.TahunID='{$khs['TahunID']}' and mk.PraktekKerja='N' and k.KodeID='" . KodeID . "'";
                        $rxx = _query($sxx);
                        while ($wxx = _fetch_array($rxx)) {
                            $s1 = "select * from bipot2 where BIPOTID = '{$mhsw['BIPOTID']}' and Otomatis = 'Y' and (PerMataKuliah = 'Y' or PerLab = 'Y') and NA = 'N' order by TrxID, Prioritas";
                            $r1 = _query($s1);
                            while ($w1 = _fetch_array($r1)) {
                                $MsgList[] = '-----------------------------------------------------------------';
                                $MsgList[] = "Memproses {$w1['BIPOT2ID']}, Rp. {$w1['Jumlah']}";
                                $oke = true;
                                // Cek apakah mata kuliah ini dapat dikenakan biaya Lab
                                if ($w1['PerLab'] == 'Y') {
                                    if ($wxx['AdaResponsi'] == 'Y') {
                                        $oke = true;
                                    } else {
                                        $oke = false;
                                    }
                                } else {
                                    $oke = true;
                                }
                                // Apakah sesuai dengan status awalnya?
                                $pos = strpos($w1['StatusAwalID'], "." . $mhsw['StatusAwalID'] . ".");
                                $oke = $oke && !($pos === false);
                                $MsgList[] = "Sesuai dengan status awalnya ({$w1['StatusAwalID']} ~ {$mhsw['StatusAwalID']})? {$oke}";
                                // Apakah sesuai dengan status mahasiswanya?
                                $pos = strpos($w1['StatusMhswID'], "." . $khs['StatusMhswID'] . ".");
                                $oke = $oke && !($pos === false);
                                $MsgList[] = "Sesuai dengan status mahasiswanya ({$w1['StatusMhswID']} ~ {$khs['StatusMhswID']})? {$oke}";
                                // Apakah grade-nya?
                                if ($oke) {
                                    if ($w1['GunakanGradeNilai'] == 'Y') {
                                        $pos = strpos($w1['GradeNilai'], "." . $mhsw['GradeNilai'] . ".");
                                        $oke = $oke && !($pos === false);
                                        $MsgList[] = "Gunakan Grade Nilai? {$oke}";
                                    }
                                }
                                // Apakah Jumlah SKS Tahun lalu mencukupi?
                                if ($oke) {
                                    if ($w1['GunakanGradeIPK'] == 'Y') {
                                        $_SKS = GetaField('gradeipk', "IPKMin <= '{$khslalu['IPS']}' and '{$khslalu['IPS']}' <= IPKMax and KodeID", KodeID, 'SKSMin');
                                        if ($_SKS > $khslalu[SKS]) {
                                            $oke = false;
                                        } else {
                                            $oke = true;
                                        }
                                        $MsgList[] = "Jumlah SKS Tahun Mencukupi({$_SKS} ~ {$khslalu['SKS']})? {$oke}";
                                    }
                                }
                                // Apakah Grade IPK-nya OK?
                                if ($oke) {
                                    if ($w1['GunakanGradeIPK'] == 'Y') {
                                        if (!empty($khslalu)) {
                                            $_GradeIPK = GetaField('gradeipk', "IPKMin <= {$khslalu['IPS']} and {$khslalu['IPS']} <= IPKMax and KodeID", KodeID, 'GradeIPK');
                                            $pos = strpos($w1['GradeIPK'], "." . $_GradeIPK . ".");
                                            $oke = $oke && !($pos === false);
                                            $MsgList[] = "Grade IPK OK ({$_GradeIPK} ~ {$w1['GradeIPK']})? {$oke}";
                                        } else {
                                            $oke = false;
                                        }
                                    }
                                }
                                // Apakah dimulai pada sesi ini?
                                if ($oke) {
                                    if ($w1['MulaiSesi'] <= $khs['Sesi'] or $w1['MulaiSesi'] == 0) {
                                        $oke = true;
                                    } else {
                                        $oke = false;
                                    }
                                    $MsgList[] = "Mulai pada sesi ini ({$khs['Sesi']} ~ {$w1['MulaiSesi']})? {$oke}";
                                }
                                // Apakah ada setup berapa kali ambil?
                                if ($oke && $w1['KaliSesi'] > 0) {
                                    $_kali = GetaField('bipotmhsw', "MhswID='{$MhswID}' and NA='N' and PMBMhswID=1 and BIPOTNamaID='{$w1['BIPOTNamaID']}' and TambahanNama='{$wxx['MKKode']} - {$wxx['Nama']} - {$wxx['SKS']} SKS' and KodeID", KodeID, "count(BIPOTMhswID)") + 0;
                                    $oke = $_kali < $w1['KaliSesi'];
                                    $MsgList[] = "Berapa Kali Ambil - ({$_kali} ~ {$w1['KaliSesi']})? {$oke}";
                                }
                                if ($oke) {
                                    $MsgList[] = "ALL OK! GO FOR IT!";
                                }
                                // Simpan data
                                if ($oke) {
                                    $ada = GetaField('bipotmhsw', "KodeID='" . KodeID . "' and MhswID = '{$mhsw['MhswID']}' and NA = 'N' and PMBMhswID = 1 and TahunID='{$khs['TahunID']}' and BIPOTNamaID = '{$w1['BIPOTNamaID']}'\t\r\n                    and TambahanNama='{$wxx['MKKode']} - {$wxx['Nama']} - {$wxx['SKS']} SKS'\r\n                    and BIPOT2ID", $w1['BIPOT2ID'], "BIPOTMhswID") + 0;
                                    if ($ada == 0) {
                                        // Simpan
                                        //$COAID = $w1['COAID'];
                                        $Nama = GetaField('bipotnama', 'BIPOTNamaID', $w1['BIPOTNamaID'], 'Nama');
                                        if ($w1['PerSKS'] == 'Y') {
                                            $Jumlah = $wxx['SKS'];
                                        } else {
                                            $Jumlah = 1;
                                        }
                                        // mario
                                        // 200813
                                        $Besar = $w1['Jumlah'];
                                        $Jumlah2 += $Jumlah;
                                        $COAID = $w1[COAID];
                                        $BIPOT2ID = $w1[BIPOT2ID];
                                        $BIPOTNamaID = $w1[BIPOTNamaID];
                                        $TrxID = $w1[TrxID];
                                        /* $s2 = "insert into bipotmhsw (KodeID, COAID, PMBMhswID, MhswID, TahunID, BIPOT2ID, BIPOTNamaID, TambahanNama, Nama, TrxID, 
                                                                        Jumlah, Besar, Dibayar,Catatan, NA, LoginBuat, TanggalBuat)
                                                                        values ('" . KodeID . "', '$w1[COAID]', 1, '$mhsw[MhswID]', '$khs[TahunID]', '$w1[BIPOT2ID]', '$w1[BIPOTNamaID]', '" . $w['MKKode'] . " - " . $w['Nama'] . " - " . $w['SKS'] . " SKS', '$Nama', '$w1[TrxID]',
                                                                        '$Jumlah', '$Besar', 0, 'Auto', 'N', '$_SESSION[_Login]', now())";
                                                                        $r2 = _query($s2);
                                        
                                                                       */
                                    }
                                }
                                // if
                            }
                            // while
                        }
                        // while
                        // mario
                        // 200813
                        if ($Nama != '' && $Besar > 0) {
                            $s2 = "insert into bipotmhsw (KodeID, COAID, PMBMhswID, MhswID, TahunID, BIPOT2ID, BIPOTNamaID, TambahanNama, Nama, TrxID, \r\n                    Jumlah, Besar, Dibayar,Catatan, NA, LoginBuat, TanggalBuat)\r\n                  values ('" . KodeID . "', '{$COAID}', 1, '{$mhsw['MhswID']}', '{$khs['TahunID']}', '{$BIPOT2ID}', '{$BIPOTNamaID}', '', '{$Nama}', '{$TrxID}', \r\n                    '{$Jumlah2}', '{$Besar}', 0, 'Auto', 'N', '{$_SESSION['_Login']}', now())";
                            $r2 = _query($s2);
                            //22 juli 2013
                            //jika insert tagihan
                            $idt = mysql_insert_id();
                            $bn = GetFields('bipotnama', 'BIPOTNamaID', $BIPOTNamaID, '*');
                            if ($bn[TrxID] == 1) {
                                $sx = "update bipotmhsw set TagihanID = '" . $idt . "' where BIPOTMhswID='" . $idt . "'";
                                $rx = _query($sx);
                            } else {
                                //jika insert potongan
                                $cek_tagihan = GetaField("bipotmhsw", "MhswID='{$mhsw['MhswID']}' and TahunID='{$khs['TahunID']}' and NA='N' and TrxID", 1, "TagihanID", "order by BIPOTMhswID desc");
                                $sx = "update bipotmhsw set TagihanID = '" . $cek_tagihan . "' where BIPOTMhswID='" . $idt . "'";
                                $rx = _query($sx);
                            }
                        }
                    }
                    // if PerMatakuliah
                }
                // end $ada=0
            }
            // end if $ada
        }
        // end if $oke
    }
    // end while
    $jml = HitungUlangBIPOTMhsw($MhswID, $TahunID);
    return $jml;
}
function ProsesBIPOT($MhswID, $TahunID)
{
    // Ambil data
    $mhsw = GetFields('mhsw', "KodeID='" . KodeID . "' and MhswID", $MhswID, "*");
    $khs = GetFields('khs', "KodeID = '" . KodeID . "' and TahunID = '{$TahunID}' and MhswID", $MhswID, "*");
    // Ambil BIPOT-nya
    $s = "select * \r\n    from bipot2 \r\n    where BIPOTID = '{$mhsw['BIPOTID']}'\r\n      and Otomatis = 'Y'\r\n      and NA = 'N'\r\n    order by TrxID, Prioritas";
    $r = _query($s);
    while ($w = _fetch_array($r)) {
        $oke = true;
        // Apakah sesuai dengan status awalnya?
        $pos = strpos($w['StatusAwalID'], "." . $mhsw['StatusAwalID'] . ".");
        $oke = $oke && !($pos === false);
        // Apakah sesuai dengan status mahasiswanya?
        $pos = strpos($w['StatusMhswID'], "." . $khs['StatusMhswID'] . ".");
        $oke = $oke && !($pos === false);
        // Apakah grade-nya?
        if ($oke) {
            if ($w['GunakanGradeNilai'] == 'Y') {
                $pos = strpos($w['GradeNilai'], "." . $mhsw['GradeNilai'] . ".");
                $oke = $oke && !($pos === false);
            }
        }
        // Apakah Jumlah SKS Tahun ini mencukupi?
        if ($oke) {
            if ($w['GunakanGradeIPK'] == 'Y') {
                if ($khs['SKS'] < GetaField('gradeipk', "IPKMin <= {$mhsw['IPK']} and {$mhsw['IPK']} <= IPKMax and KodeID", KodeID, 'SKSMin')) {
                    $oke = false;
                } else {
                    $oke = true;
                }
            }
        }
        // Apakah Grade IPK-nya OK?
        if ($oke) {
            if ($w['GunakanGradeIPK'] == 'Y') {
                $pos = strpos($w['GradeIPK'], "." . GetaField('gradeipk', "IPKMin <= {$mhsw['IPK']} and {$mhsw['IPK']} <= IPKMax and KodeID", KodeID, 'GradeIPK') . ".");
                $oke = $oke && !($pos === false);
            }
        }
        // Apakah dimulai pada sesi ini?
        if ($oke) {
            if ($w['MulaiSesi'] <= $khs['Sesi'] or $w['MulaiSesi'] == 0) {
                $oke = true;
            } else {
                $oke = false;
            }
        }
        // Simpan data
        if ($oke) {
            // Cek, sudah ada atau belum? Kalau sudah, ambil ID-nya
            $ada = GetaField('bipotmhsw', "KodeID='" . KodeID . "' and MhswID = '{$mhsw['MhswID']}'\r\n        and PMBMhswID = 1\r\n        and TahunID='{$khs['TahunID']}' and BIPOT2ID", $w['BIPOT2ID'], "BIPOTMhswID") + 0;
            // Cek apakah memakai script atau tidak?
            if ($w['GunakanScript'] == 'Y') {
                BipotGunakanScript($mhsw, $khs, $w, $ada, 1);
            } else {
                // Jika tidak ada duplikasi, maka akan di-insert. Tapi jika sudah ada, maka dicuekin aja.
                if ($ada == 0) {
                    // Simpan
                    $Nama = GetaField('bipotnama', 'BIPOTNamaID', $w['BIPOTNamaID'], 'Nama');
                    $s1 = "insert into bipotmhsw\r\n            (KodeID, COAID, PMBMhswID, MhswID, TahunID,\r\n            BIPOT2ID, BIPOTNamaID, Nama, TrxID,\r\n            Jumlah, Besar, Dibayar,\r\n            Catatan, NA,\r\n            LoginBuat, TanggalBuat)\r\n            values\r\n            ('" . KodeID . "', '{$w['COAID']}', 1, '{$mhsw['MhswID']}', '{$khs['TahunID']}',\r\n            '{$w['BIPOT2ID']}', '{$w['BIPOTNamaID']}', '{$Nama}', '{$w['TrxID']}',\r\n            1, '{$w['Jumlah']}', 0,\r\n            'Auto', 'N',\r\n            '{$_SESSION['_Login']}', now())";
                    $r1 = _query($s1);
                }
                // end $ada=0
            }
            // end if $ada
        }
        // end if $oke
    }
    // end while
    $jml = HitungUlangBIPOTMhsw($MhswID, $TahunID);
    return $jml;
}
Exemple #6
0
function HapusBayar()
{
    $BayarMhswID = sqling($_REQUEST['BayarMhswID']);
    $MhswID = sqling($_REQUEST['MhswID']);
    $TahunID = sqling($_REQUEST['TahunID']);
    // Hapus header
    /*
          $s = "delete from bayarmhsw
          where BayarMhswID = '$BayarMhswID'
          and MhswID = '$MhswID'
          and KodeID = '".KodeID."' "; */
    $s = "update bayarmhsw\r\n    set NA = 'Y'\r\n    where BayarMhswID = '{$BayarMhswID}'\r\n      and MhswID = '{$MhswID}'\r\n      and KodeID = '" . KodeID . "' ";
    $r = _query($s);
    // Hapus detailnya
    $s = "update bayarmhsw2\r\n    set NA = 'Y'\r\n    where BayarMhswID = '{$BayarMhswID}' ";
    $r = _query($s);
    HitungUlangBIPOTMhsw($MhswID, $TahunID);
    BerhasilSimpan("?mnux={$_SESSION['mnux']}&gos=", 100);
}
function JalankanProses($_upJumlah, $_upProses)
{
    $arr = $_SESSION['_up_' . $_upProses];
    $dat = explode('|', $arr);
    // persentase
    $_sudah = $_upJumlah > 0 ? $_upProses / $_upJumlah * 100 : 0;
    $_sisa = 100 - $_sudah;
    // Parameter
    $BayarMhswID = 'BTN-' . $dat[23];
    $NamaTahun = $dat[0];
    $MhswID = $dat[3];
    $NamaMhsw = $dat[4];
    $ProdiID = $dat[6];
    $NamaProdi = $dat[7];
    $Angkatan = $dat[8];
    $RekeningID = "4201390002572";
    $Jumlah = $dat[10] + 0;
    $NamaBIPOT = $dat[11];
    $BuktiSetoran = $dat[23] . '|' . $dat[24];
    $Catatan = $dat[27];
    // Cek data mahasiswa
    $ketemu = false;
    $mhsw = GetFields('mhsw', "KodeID='" . KodeID . "' and MhswID", $MhswID, '*');
    if (!empty($mhsw)) {
        // *** Berarti account Mhsw
        $_MhswID = $MhswID;
        $_PMBID = $mhsw['PMBID'];
        $_PMBMhswID = 1;
        $ketemu = true;
    } else {
        // Cek apakah account PMB?
        $mhsw = GetFields('pmb', "KodeID='" . KodeID . "' and PMBID", $MhswID, '*');
        if (!empty($mhsw)) {
            // *** Account-nya PMB
            $_MhswID = '';
            $_PMBID = $mhsw['PMBID'];
            $_PMBMhswID = 0;
            $ketemu = true;
        } else {
            $ketemu = false;
        }
        // Tidak ketemu account-nya siapa
    }
    // Jika ketemu
    if ($ketemu) {
        // Prosesnya
        $thn = explode('-', $NamaTahun);
        $Tahun = $thn[0];
        $Semester = substr($thn[1], 0, 2) == 'GA' ? 1 : 2;
        $TahunID = $Tahun . $Semester;
        // Cek KHS
        $khs = GetFields('khs', "KodeID='" . KodeID . "' and MhswID='{$MhswID}' and TahunID", $TahunID, "*");
        if (empty($khs)) {
            // Jika tidak ketemu, maka insert KHS
            // Ambil Total SKS
            if ($khs['Sesi'] <= 1) {
                $MaxSKS = GetaField('prodi', "ProdiID='{$mhsw['ProdiID']}' and KodeID", KodeID, 'DefSKS') + 0;
            } else {
                $MaxSKS = GetaField('maxsks', "KodeID='" . KodeID . "' and NA = 'N'\r\n          and DariIP <= {$khs['IPS']} and {$khs['IPS']} <= SampaiIP and ProdiID", $khs['ProdiID'], 'SKS') + 0;
            }
            $_sesi = GetaField('khs', "MhswID='{$MhswID}' and KodeID", KodeID, "max(Sesi)") + 1;
            $s0 = "insert into khs\r\n        (TahunID, KodeID, ProgramID, ProdiID, MhswID,\r\n        StatusMhswID, Sesi, SKS,\r\n        MaxSKS, LoginBuat, TanggalBuat)\r\n        values\r\n        ('{$TahunID}', '" . KodeID . "', '{$mhsw['ProgramID']}', '{$mhsw['ProdiID']}', '{$MhswID}',\r\n        'A', {$_sesi}, 0,\r\n        {$MaxSKS}, '{$_SESSION['_Login']}', now())";
            $r0 = _query($s0);
            $KHSID = GetLastID();
            $khs = GetFields('khs', 'KHSID', $KHSID, '*');
        }
        // end KHS
        // Cek, apakah sudah dibayarkan sebelumnya atau belum?
        $ada = GetaField('bayarmhsw', "KodeID='" . KodeID . "' and BayarMhswID", $BayarMhswID, "count(BayarMhswID)") + 0;
        if ($ada > 0) {
            echo "<p style='text-align:center;background:red;color:yellow'><b>Sudah pernah dibayarkan.</b></p>";
            // Set status
            if ($khs['StatusMhswID'] == 'P') {
                $sa = "update khs \r\n          set StatusMhswID = 'A'\r\n          where KHSID = '{$khs['KHSID']}' ";
                $ra = _query($sa);
            }
        } else {
            include_once "../keu/bayarmhsw.lib.php";
            // Tambahkan di catatan pembayaran
            $s = "insert into bayarmhsw\r\n        (BayarMhswID, KodeID, TahunID, RekeningID, MhswID, PMBID, TrxID, PMBMhswID,\r\n        Bank, BuktiSetoran, Tanggal, Jumlah,\r\n        Keterangan, LoginBuat, TanggalBuat, NA)\r\n        values\r\n        ('{$BayarMhswID}', '" . KodeID . "', '{$TahunID}', '{$RekeningID}', '{$_MhswID}', '{$_PMBID}', 1, '{$_PMBMhswID}',\r\n        'BTN', '{$BuktiSetoran}', now(), {$Jumlah},\r\n        '{$Catatan}', '{$_SESSION['_Login']}', now(), 'N')";
            $r = _query($s);
            // Update summary
            $_StatusMhswID = $khs['StatusMhswID'] == 'P' ? 'A' : $khs['StatusMhswID'];
            HitungUlangBIPOTMhsw($MhswID, $TahunID);
        }
    } else {
        $_ketemu = "<div style='text-align:center; background: red; color: yellow'><b>Account tidak ditemukan...</b></div><br />";
    }
    // Tampilan proses
    $_Jumlah = number_format($Jumlah);
    echo "\r\n  <p align=center>\r\n  <font size=+1>{$_upProses}</font> <sup>~{$_upJumlah}</sup><br />\r\n    <img src='../img/B1.jpg' height=20 width=1 /><img src='../img/B2.jpg' height=20 width={$_sudah} /><img src='../img/B3.jpg' height=20 width={$_sisa} /><img src='../img/B1.jpg' height=20 width=1 />\r\n    <br />\r\n    {$_ketemu}\r\n    Tahun Akd: {$TahunID}<br />\r\n    NIM: {$MhswID} <br />\r\n    Nama: <b>{$NamaMhsw}</b><br />\r\n    Prodi: <b>{$NamaProdi}</b> <sup>{$ProdiID}</sup><br />\r\n    Rekening: <b>{$RekeningID}</b><br />\r\n    Jumlah: <b>{$_Jumlah}</b><br />\r\n    Catatan: <b>{$Catatan}</b><br />\r\n  </p>";
    // Next...
    $tmr = 10;
    $_SESSION['_upProses']++;
    echo <<<SCR
    <script>
    window.onload=setTimeout("window.location='../{$_SESSION['mnux']}.upload.php'", {$tmr});
    </script>
SCR;
}