<?php 
header("Access-Control-Allow-Headers: Authorization, Content-Type");
header("Access-Control-Allow-Origin: *");
$input = file_get_contents("php://input");
$post = json_decode($input, true);

if((isset($_SERVER['PHP_AUTH_USER'])) && (isset($_SERVER['PHP_AUTH_PW'])))
{
if(($_SERVER['PHP_AUTH_USER']=="Xh83!sA") && ($_SERVER['PHP_AUTH_PW']=="HshFla72nsFa,s==ds0adsd028-=29ndgala")){

$idmember=$post["idmember"];
$nominal=$post["total"];
$nama_anggota=$post["nama_anggota"];
$phone=$post["phone"];
$npsn=$post["npsn"];

include("confiq.php");
$koneksi=koneksi_tabungan($npsn);

$id_user=$post["id_user"];
$password=$post["password"];
$unit=$post["unit"];
$keterangan='Belanja di '.$unit.' dan biaya admin';

$pin=$post["pin"];
$task="34567fghjkl;FGHJK09865fghjs12345@#$@#$!@#";
$pin=md5($pin);
$pin=md5($pin.$task);
$fee=round($nominal*((5/2)/100));
$nominal_siswa=$nominal+$fee;
									//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");
    $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
								$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."', '-', '-','".$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){
									$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 Gagal';
										echo json_encode($MyJson);} 
									}
									}//end cek query qry_rek2
							}else {
							//respon jika gagal jurnal pembayaran siswa
								$MyJson['status']='01';		
								$MyJson['information']='Transaksi Gagal';
								echo json_encode($MyJson);  
								}
						}else {
							$MyJson['status']='01';		
							$MyJson['information']='Transaksi Gagal';
							echo json_encode($MyJson);  
							}
} //end api
}  //end get api      

?>