	<?php 
	error_reporting(E_ALL & E_NOTICE);
	/**
	* Created by PhpStorm.
	* User: ahmad
	* Date: 20/08/2019
	* Time: 11:48 AM
	* Location Laporan -> mutasi kas
	*/

	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']);

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

	$query 		= "SELECT * FROM sales_data WHERE user='$m[phone_number]' AND date>='$tanggal_awa' and date<='$tanggal_akhir' ORDER BY id_datapenjualan DESC";
	$hasil 		= mysqli_query($connect,$query);
	$jumlahnya 	= mysqli_num_rows($hasil);

	if(mysqli_num_rows($hasil) > 0 ){
	
	
	$jumlahterjual = mysqli_query($connect,"SELECT SUM(totalorder) as penjualan_bersih FROM sales_data WHERE user='$m[phone_number]' AND date>='$tanggal_awal' and date<='$tanggal_akhir'");
    $j=mysqli_fetch_array($jumlahterjual);
	

	$pembatalandata = mysqli_query($connect,"SELECT SUM(totalorder) as pembatalan FROM sales_data WHERE user='$m[phone_number]' AND status='cancel' AND date>='$tanggal_awal' and date<='$tanggal_akhir'");
    $bt=mysqli_fetch_array($pembatalandata);
	

	if($bt['pembatalan']==''){
	$pembatalan=0;
	}else{
	$pembatalan= $bt['pembatalan'];
	}
	
	if($j['penjualan_bersih']==''){
	$penjualan_bersih=0;
	}else{
	$penjualan_bersih= $j['penjualan_bersih'];
	}
	
	$response = array(
    'status'	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
	);
   
	
   
	$response["data"] = array();
  
	$info = array(
    'penjualan_bersih' => $penjualan_bersih,
	'jumlah_transaksi' => $jumlahnya
	);
    
	$transaksi = array();
	while($y = mysqli_fetch_array($hasil)){
	  
	$querynya = "SELECT * FROM sales WHERE user='$m[phone_number]' AND no_invoice='$y[no_invoice]' AND date>='$tanggal_awal' and date<='$tanggal_akhir' ORDER BY id_sales DESC";
	$hasilnya = mysqli_query($querynya);
     
	$barang = array();
	while($x = mysqli_fetch_array($hasilnya)){
		
	$jml = mysqli_query($connect,"SELECT SUM(amount) as jumlahpenjualan FROM sales WHERE id_product='$x[id_product]' AND date>='$tanggal_awal' and date<='$tanggal_akhir'");
	$jm=mysqli_fetch_array($jml);
   
	if($jm['jumlahpenjualan']==''){
	$terjual=0;
	}else{
	$terjual= $jm['jumlahpenjualan'];
	}	
	
	
	$barangnya = mysqli_query($connect,"SELECT * FROM product WHERE id_product='$x[id_product]'");
    $b=mysqli_fetch_array($barangnya);
	
	if($b['nama_barang']==''){
	$nama_barang=0;
	}else{
	$nama_barang= $b['nama_barang'];
	}
	

    $harga_beli	= $x["purchase_price"]*$terjual;
    $harga_jual	= $x["selling_price"]*$terjual;
    $raba_rugi		= $harga_jual-$harga_beli;
    $margin= round($raba_rugi/$harga_beli*100);
	
	  
    $h['id_product'] 	= $x["id_product"];
    $h['name_product'] 	= $nama_barang;
    $h['qty'] 			= $terjual;
    $h['selling_price'] 	= $harga_jual;
    $h['purchase_price'] 	= $harga_beli;
    $h['laba'] 			= $raba_rugi;
    $h['margin'] 		= $margin;
    array_push($barang, $h);
  }
	  
    $omset	= $harga_jual-$harga_beli;
    $margin	= round($raba_rugi/$harga_beli*100);
	
	  
    $g['date'] 		= $y["date"];
    $g['no_invoice'] 	= $y["no_invoice"];
    $g['omset'] 		= $raba_rugi;
    $g['margin'] 		= $margin;
    $g['product'] 		= $product;
	
	
	
    array_push($transaksi, $g);
	}
  
  
  
	$response["data"] = array(
	'info' 		=> $info,
	'transaksi' => $transaksi
	);
  
	array_push($response["data"]);
	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);	
	}
?>