	<?php
	/**
	* Created by PhpStorm.
	* User: ahmad
	* Date: 20/08/2019
	* Time: 11:48 AM
	* Location Histori -> Tab Daftar transaksi
	*/
	
	require_once("../connection.php");
	
	$key=mysqli_real_escape_string($connect, $_GET['key']);
	$tanggal_awal=mysqli_real_escape_string($connect, $_GET['start_date']);
	$tanggal_akhir=mysqli_real_escape_string($connect, $_GET['finish_date']);
	$status=mysqli_real_escape_string($connect, $_GET['status']);

	$getusers	= mysqli_query($connect,"SELECT * FROM users WHERE id_session='$key'");
	$u	   		= mysqli_fetch_array($getusers);
	$jumlah   	= mysqli_num_rows($getusers);
	
	$masterakun = mysqli_query($connect,"SELECT * FROM users WHERE phone_number='$u[master]'");
	$m=mysqli_fetch_array($masterakun);

	if ($jumlah > 0){ 
	
	$response = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
	);
   
	$begin = new DateTime( "$tanggal_awal" );
	$end   = new DateTime( "$tanggal_akhir" );
   
	$response["data"] = array();
	for($i = $begin; $i <= $end; $i->modify('+1 day')){
		
    $tanggalnya			= $i->format("Y-m-d");
    if($u['level']=='master'){
	$datapenjualanutama = mysqli_query($connect,"SELECT SUM(totalnominal) as nominalnya FROM spending_data WHERE user='$m[phone_number]' AND date='$tanggalnya'");
		}else{
	$datapenjualanutama = mysqli_query($connect,"SELECT SUM(totalnominal) as nominalnya FROM spending_data WHERE operator='$u[phone_number]' AND date='$tanggalnya'");
		}
		
	$s=mysqli_fetch_array($datapenjualanutama);
	
	if($s['nominalnya']==''){
	$nominalnya=0;
	}else{
	$nominalnya= $s['nominalnya'];
	}
	
	
	$datapengeluaran 	= "SELECT * FROM spending_data WHERE user='$m[phone_number]' AND date='$tanggalnya' ORDER BY id_spending_data ASC";
	$hasilpengeluaran = mysqli_query($connect,$datapengeluaran);
	
	$detail = array();
	while($x = mysqli_fetch_array($hasilpengeluaran)){
	  
	$dataop = mysqli_query($connect,"SELECT * FROM users WHERE phone_number='$x[operator]'");
	$o=mysqli_fetch_array($dataop);
	
    $h['no_invoice'] 		= $x["no_invoice"];
    $h['date'] 				= $x["date"];
    $h['nominal'] 			= $x["totalnominal"];
    $h['oleh'] 				= $o["full_name"];
    array_push($detail, $h);
    }
	
    $g['totalnominal'] 		= $nominalnya;
    $g['date'] 			= $tanggalnya;
    $g['detail'] 			= $detail;
    array_push($response["data"], $g);
	}
	echo json_encode($response);

	}else{
	
	$json = array(
    'status' 	=> 'true',
    'errCode' 	=> '00',
	'msg' 		=> 'User Not Found'
	);

    echo json_encode($json);	
	}
	?>