	<?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['first_date']);
	$tanggal_akhir=mysqli_real_escape_string($connect, $_GET['last_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" );
	$status=$status;
   
	$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(totalorder) as totalharganya FROM sales_data WHERE user='$m[phone_number]' AND date='$tanggalnya' AND status LIKE '%$status%' ");
		}else{
	$datapenjualanutama = mysqli_query($connect,"SELECT SUM(totalorder) as totalharganya FROM sales_data WHERE operator='$u[phone_number]' AND date='$tanggalnya' AND status LIKE '%$status%' ");
		}
	
	$s=mysqli_fetch_array($datapenjualanutama);
	
	if($s['totalharganya']==''){
	$totalharganya=0;
	}else{
	$totalharganya= $s['totalharganya'];
	}
	
	
	
	if($u['level']=='master'){
	$datapenjualan 	= "SELECT * FROM sales_data WHERE user='$m[phone_number]' AND date='$tanggalnya' AND status LIKE '%$status%' ORDER BY id_sales_data ASC";
		}else{
	$datapenjualan 	= "SELECT * FROM sales_data WHERE operator='$u[phone_number]' AND date='$tanggalnya' AND status LIKE '%$status%' ORDER BY id_sales_data ASC";
		}
		
	$hasilpenjualan = mysqli_query($connect,$datapenjualan);
	
	$detail = array();
	while($x = mysqli_fetch_array($hasilpenjualan)){
	    
	    $querydp = mysqli_query($connect,"SELECT * FROM db_table WHERE id_table='$x[id_table]'");
    $dp=mysqli_fetch_array($querydp);
	
	if($dp['name_table'] == null){
	$name_table="-";
	}else{
	$name_table= $dp['name_table'];
	}
	  
	
	$querydp = mysqli_query($connect,"SELECT * FROM sales_data WHERE no_invoice='$x[no_invoice]'");
    $dp=mysqli_fetch_array($querydp);
	
	$h['id_customer'] 		= $x["id_customer"];
    $h['no_invoice'] 		= $x["no_invoice"];
    $h['date'] 				= $x["date"];
    $h['payment'] 			= $x["payment"];
    $h['totalorder'] 		= $x["totalorder"];
    $h['totalpay'] 			= $x["totalpay"];
    $h['changepay'] 		= $x["changepay"];
    $h['status'] 			= $x["status"];
    $h['name_table'] 		= $name_table;
    array_push($detail, $h); 
    }
	
    $g['totalorderall'] 	= $totalharganya;
    $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);	
	}
	?>