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

$idmember=addslashes(mysqli_real_escape_string($koneksi,$_GET['id']));
$kategori=addslashes(mysqli_real_escape_string($koneksi,$_GET['kat']));
$api=addslashes(trim($_GET['api']));
if(isset($_GET['api']))
{
    if($api=='AIzaSyARSSmIJjL5LuxZnrjkDnrD')
    {
		 
		 
		 $qry_batas_out=mysqli_query($koneksi,"SELECT * FROM `tb_set_out`  WHERE (kat='".$kategori."') AND (status='OPEN')");
					$N_batas_out=mysqli_num_rows($qry_batas_out);
					if($N_batas_out<>0)
					{
						$row_batas_out=mysqli_fetch_array($qry_batas_out);
						$batas_max=$row_batas_out['maximal'];
						$batas_kelompok=$row_batas_out['kelompok'];
						if($batas_kelompok=='Mingguan')
						{
							$Nhari=date("N")-1;
							$tgl_2=date('Y-m-d'); // menentukan tanggal awal
    						$tgl_1=date('Y-m-d', strtotime('-'.$Nhari.'days', strtotime($tgl_2))); // penjumlahan
							$tgl_1=$tgl_1.' 00:00:00'; 
							$tgl_2=$tgl_2.' 23:59:59';
						} else if($batas_kelompok=='Harian') {
							$tgl_1=date('Y-m-d');
							$tgl_1=$tgl_1.' 00:00:00'; 
							$tgl_2=$tgl_1.' 23:59:59';
						}
							$qry_cari_out=mysqli_query($koneksi,"SELECT sum(nominal) as total_belanja FROM `tb_rekening` WHERE (idanggota='".$idmember."') and (in_out='out') and (reff='".$kategori."') and (tgl BETWEEN '$tgl_1' AND '$tgl_2')");
					 
							$row_out=mysqli_fetch_array($qry_cari_out);
							$sudah_belanja=$row_out['total_belanja'];
							
							$saldo_belanja=$batas_max-$sudah_belanja;
					echo '[{"saldo":"'.$saldo_belanja.'"}]';
					}
		 
		 
		 
		
} //end api
}  //end get api      

?>