<?php

	include '../../config/koneksi.php';
	include '../../config/variabel_default.php';

	$siswa = $_POST['siswa'];
	$invoice = $_POST['invoice'];
	$statusBayar = '1';//success
	$ketBayar = str_replace('"','',$_POST['status']);//success
    $caraBayar = 'Transfer Midtrans';
    $idtransaksi = $_POST['idtransaksi'];
    $tipe = $_POST['tipe'];

    $noPayment = '';
    $idt = mysqli_fetch_array(mysqli_query($conn,"SELECT * FROM identitas LIMIT 1"));
    if ($tipe == 'gopay'){
        if ($idt['link'] == 'https://app.sandbox.midtrans.com/snap/snap.js'){
            $noPayment = 'https://api.sandbox.midtrans.com/v2/gopay/'.$idtransaksi.'/qr-code';
        }else{
            $noPayment = 'https://api.midtrans.com/v2/gopay/'.$idtransaksi.'/qr-code';
        }
    }elseif ($tipe == 'qris'){
        //shoppepay
        if ($idt['link'] == 'https://app.sandbox.midtrans.com/snap/snap.js'){
            $noPayment = 'https://api.sandbox.midtrans.com/v2/qris/shopeepay/sppq_'.$idtransaksi.'/qr-code';
        }else{
            $noPayment = 'https://api.midtrans.com/v2/qris/shopeepay/sppq_'.$idtransaksi.'/qr-code';
        }
    }
    
	$qPayment = mysqli_query($conn,"SELECT * FROM payment WHERE idSiswa='$siswa' AND paymentOrder=''");
    if (mysqli_num_rows($qPayment) <> 0){
    	while($pyt=mysqli_fetch_array($qPayment)) {
    		if ($noPayment <> ''){
                mysqli_query($conn,"UPDATE payment SET paymentOrder='$invoice', status='$ketBayar', noPayment='$noPayment' WHERE idSiswa='$siswa' AND idPayment='$pyt[idPayment]'");
            }else{
                mysqli_query($conn,"UPDATE payment SET paymentOrder='$invoice', status='$ketBayar' WHERE idSiswa='$siswa' AND idPayment='$pyt[idPayment]'");
            }
            
    		if ($pyt['jenisTagihan'] == 'Bulanan'){
    			mysqli_query($conn,"UPDATE tagihan_bulanan SET tglBayar='$waktu_sekarang', statusBayar='$statusBayar', inv='$invoice', caraBayar='$caraBayar' WHERE idSiswa='$siswa' AND idTagihanBulanan ='$pyt[idTagihan]'");
    		}else{
                mysqli_query($conn,"INSERT INTO tagihan_bebas_bayar(idTagihanBebas,tglBayar,jumlahBayar,ketBayar,caraBayar) VALUES ('$pyt[idTagihan]','$waktu_sekarang','$pyt[nominal]','$ketBayar','$caraBayar')");

                $tagBebas = mysqli_fetch_array(mysqli_query($conn,"SELECT * FROM tagihan_bebas WHERE idSiswa='$siswa' AND idTagihanBebas='$pyt[idTagihan]'"));
                $tagBebasBayar = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(jumlahBayar) as totalBayar FROM tagihan_bebas_bayar WHERE idTagihanBebas='$pyt[idTagihan]'"));
                if ($tagBebas['totalTagihan'] == $tagBebasBayar['totalBayar']){
                    $statusByr = $statusBayar;
                }elseif ($tagBebas['totalTagihan'] > $tagBebasBayar['totalBayar']){
                    $statusByr = '2';
                }else{
                    $statusByr = '0';
                }
                mysqli_query($conn,"UPDATE tagihan_bebas SET statusBayar='$statusByr' WHERE idSiswa='$siswa' AND idTagihanBulanan ='$pyt[idTagihan]'");
    		}
    	}
    }
 
?>
