	<?php
	/**
	* Created by PhpStorm.
	* User: ahmad
	* Date: 20/08/2019
	* Time: 12:00 AM
	* Location Histori -> Cari transaksi
	*/

	require_once("../connection.php");
	
	$key=mysqli_real_escape_string($connect, $_GET['key']);
	$cari=mysqli_real_escape_string($connect, $_GET['search']);



	$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){


	
	if($u['level']=='master'){
	$query = "SELECT * FROM sales_data WHERE user='$m[phone_number]' AND no_invoice LIKE '%$cari%' ORDER BY id_sales_data DESC";
		}else{
	$query = "SELECT * FROM sales_data WHERE operator='$u[phone_number]' AND no_invoice LIKE '%$cari%' ORDER BY id_sales_data DESC";
		}
		
	$hasil = mysqli_query($connect,$query);

	if(mysqli_num_rows($hasil) > 0 ){
	$response = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
	);
     
	$response["data"] = array();
	while($x = mysqli_fetch_array($hasil)){
    $h['id_pelanggan'] 	= $x["id_customer"];
    $h['no_invoice'] 	= $x["no_invoice"];
    $h['tanggal'] 		= $x["date"];
    $h['pembayaran'] 	= $x["payment"];
    $h['keterangan'] 	= $x["note"];
    $h['totalorder'] 	= $x["totalorder"];
    $h['totalbayar'] 	= $x["totalpay"];
    $h['kembalian'] 	= $x["changepay"];
    $h['status'] 		= $x["status"];
    array_push($response["data"], $h);
	}
  
	echo json_encode($response);
  
	}else {
	$response = array(
    'status' 	=> 'true',
    'errCode' 	=> '02',
	'msg' 		=> 'No data'
	);
	echo json_encode($response);
	}

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

    echo json_encode($json);	
	}
	?>