<?php
header("Access-Control-Allow-Headers: Authorization, Content-Type");
header("Access-Control-Allow-Origin: *");
include("confiq.php");
$npsn=$_GET["npsn"];
$koneksi=koneksi_tabungan($npsn);
$api=addslashes(trim($_GET['api']));
$idmember=noninjection($koneksi,$_GET["idmember"]);
$nominal=noninjection($koneksi,$_GET["total"]);
$nama=$_GET["nama"];
$nama_anggota= str_replace("'"," ",$nama);

$nama_anggota=addslashes($nama_anggota);

$phone=noninjection($koneksi,$_GET["phone"]);
$id_user=noninjection($koneksi,$_GET["id_user"]);
$unit=noninjection($koneksi,$_GET["unit"]);

if(isset($_GET['api']))
{
if($api=='AIzaSyARSSmIJjL5LuxZnrjkDnrD')
{
    $keterangan='Belanja di '.$unit.' dan biaya admin';
    $pin=noninjection($koneksi,$_GET["pin"]);
    $task="34567fghjkl;FGHJK09865fghjs12345@#$@#$!@#";
    $pin=md5($pin);
    $pin=md5($pin.$task);
    //FEE
	$koneksi0=open_connection2();
	$qry_fee=mysqli_query($koneksi0,"SELECT * FROM `tb_fee` WHERE npsn='".$npsn."' ");
	$n_fee=mysqli_num_rows($qry_fee);
	if($n_fee<>0)
	{
		$row_fee=mysqli_fetch_array($qry_fee);
		if($row_fee['fee_sdi']<>0)
		{  
			$fee=round($nominal*($row_fee['fee_sdi']/100));
  		} else {$fee=0;}

		if($row_fee['fee_sekolah']<>0)
		{  
			$fee_sekolah=round($nominal*($row_fee['fee_sekolah']/100));
  		} else {$fee_sekolah=0;}
	
	
	}else {
	$fee=round($nominal*((5/2)/100));
    $fee_sekolah=round($nominal*((75/10)/100));
	}
	// end fee
  	//  $nominal_siswa=$nominal+$fee;
  	$nominal_siswa=$nominal;

//cek saldo siswa	
//	$qry_rek=mysqli_query($koneksi,"SELECT * FROM `tb_rekening`  WHERE  (idanggota IN (SELECT idanggota FROM `tb_anggota` WHERE (idanggota='".$idmember."') AND (pin='".$pin."') )) order by id desc limit 1");

	$qry_rek=mysqli_query($koneksi,"SELECT * FROM `tb_rekening`  WHERE  (idanggota IN (SELECT idanggota FROM `tb_anggota` WHERE (idanggota='".$idmember."')  )) order by id desc limit 1");

	$row_rek=mysqli_fetch_array($qry_rek);
    if(mysqli_num_rows($qry_rek)<>0){
		$saldo_tmp=$row_rek['saldo'];
	    $saldo=$row_rek['saldo']-$nominal_siswa;
	} else {$saldo=0;$saldo_tmp=0;}
						
	if($saldo >= 0)
		{ 
								//jurnal pembayaran siswa
		//	echo "INSERT INTO `tb_rekening` (`idanggota`, `keterangan`, `in_out`, `nominal`, `saldo`, `reff`, `reff2`, `reff3`,`iduser`, `tgl`) VALUES ('".$idmember."', '".$keterangan."', 'out', '".$nominal_siswa."', '".$saldo."', '".$unit."', '-', '-','".$id_user."', current_timestamp());";
			$res=mysqli_query($koneksi,"INSERT INTO `tb_rekening` (`idanggota`, `keterangan`, `in_out`, `nominal`, `saldo`, `reff`, `reff2`, `reff3`,`iduser`, `tgl`) VALUES ('".$idmember."', '".$keterangan."', 'out', '".$nominal_siswa."', '".$saldo."', '".$unit."', '".$nama_anggota."', '-','".$id_user."', current_timestamp());");
					
				if($res){
									//jurnal kantin: cek saldo kantin
                    $qry_rek2=mysqli_query($koneksi,"SELECT * FROM `tb_rekening_kantin` WHERE idanggota='".$unit."' order by id desc limit 1");
					$row_rek2=mysqli_fetch_array($qry_rek2);
					if(mysqli_num_rows($qry_rek2)<>0){
					     $nominal=$nominal-($fee+$fee_sekolah);
						$saldo2=$row_rek2['saldo']+$nominal;
					} else {$saldo2=$nominal;}
									//jurnal kantin
									if($qry_rek2)
									{
									$res2=mysqli_query($koneksi,"INSERT INTO `tb_rekening_kantin` (`idanggota`, `keterangan`, `in_out`, `nominal`, `saldo`, `reff`, `reff2`, `reff3`,`iduser`, `tgl`) VALUES ('".$unit."', '".$keterangan."', 'in', '".$nominal."', '".$saldo2."', '".$idmember."', '".$nama_anggota."', '-','".$id_user."', current_timestamp());");
									if($res2){
									   //jurnal fee
									   //===============================
									    $qry_fee=mysqli_query($koneksi,"SELECT * FROM `tb_rekening_fee`  order by id desc limit 1");
									    if(mysqli_num_rows($qry_fee)<>0){
									        $row_fee=mysqli_fetch_array($qry_fee);
									        $saldofee=$row_fee['saldo']+$fee;
									    } else {$saldofee=$fee;}
									    if($qry_fee)
									    {
								            mysqli_query($koneksi,"INSERT INTO `tb_rekening_fee` (`idanggota`, `keterangan`, `in_out`, `nominal`, `saldo`, `reff`, `reff2`, `reff3`,`iduser`, `tgl`) VALUES ('".$unit."', '".$keterangan."', 'in', '".$fee."', '".$saldofee."', '".$idmember."', '".$nama_anggota."', '-','".$id_user."', current_timestamp());");
									    }
									    //===============================
									    
										//respon jika sukses
										$MyJson['status']='00';		
										$MyJson['information']='Transaksi Sukses';
										echo json_encode($MyJson); 
									}else {
									//respon jika gagal jurnal kantin
										//$res=mysqli_query($koneksi,"INSERT INTO `tb_rekening` (`idanggota`, `keterangan`, `in_out`, `nominal`, `saldo`, `reff`, `reff2`, `reff3`,`iduser`, `tgl`) VALUES ('".$idmember."', 'Pembatalan', 'in', '".$nominal."', '".$saldo_tmp."', '".$unit."', '-', '-','".$id_user."', current_timestamp());");
								
										if($res){$MyJson['status']='01';		
										$MyJson['information']='Transaksi Gagal3';
										echo json_encode($MyJson);} 
									}
									}//end cek query qry_rek2
							}else {
							//respon jika gagal jurnal pembayaran siswa
								$MyJson['status']='01';		
								$MyJson['information']='Transaksi Gagal2';
								echo json_encode($MyJson);  
								}
						}else {
							$MyJson['status']='01';		
							$MyJson['information']='Transaksi Gagal1';
							echo json_encode($MyJson);  
							}
} //end api
}  //end get api      

?>