	<?php
	/**
	* Created by PhpStorm.
	* User: ahmad
	* Date: 20/08/2019
	* Time: 11:48 AM
	* Location Laporan -> labarugi
	*/

	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']);
			$id_toko=mysqli_real_escape_string($connect, $_GET['id_store']);


	$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($id_toko==''){
		$query 		= "SELECT * FROM sales_data WHERE user='$m[phone_number]' AND date>='$tanggal_awal' and date<='$tanggal_akhir'";
		}else{
		$query 		= "SELECT * FROM sales_data WHERE user='$m[phone_number]' AND id_store='$id_toko' AND date>='$tanggal_awal' and date<='$tanggal_akhir'";
		}

	$hasil 		= mysqli_query($connect,$query);
	$jumlahnya 	= mysqli_num_rows($hasil);
	$sd 	= mysqli_fetch_array($hasil);

	if(mysqli_num_rows($hasil) > 0 ){
	if($id_toko==''){
	$jumlahterjual = mysqli_query($connect,"SELECT SUM(totalorder) as penjualan_kotor FROM sales_data WHERE user='$m[phone_number]' AND date>='$tanggal_awal' and date<='$tanggal_akhir'");
		}else{
	$jumlahterjual = mysqli_query($connect,"SELECT SUM(totalorder) as penjualan_kotor FROM sales_data WHERE user='$m[phone_number]' AND id_store='$id_toko' AND date>='$tanggal_awal' and date<='$tanggal_akhir'");
		}
    $j=mysqli_fetch_array($jumlahterjual);
	
	if($id_toko==''){
	$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'");
		}else{
	$pembatalandata = mysqli_query($connect,"SELECT SUM(totalorder) as pembatalan FROM sales_data WHERE user='$m[phone_number]' AND id_store='$id_toko' AND status='cancel' AND date>='$tanggal_awal' and date<='$tanggal_akhir'");
		}
    $bt=mysqli_fetch_array($pembatalandata);
	
	
	$querydiskon = mysqli_query($connect,"SELECT SUM(discount) as diskonnya, SUM(tax) as pajaknya, SUM(service_charge) as admin  FROM sales_data WHERE user='$m[phone_number]' AND status NOT IN ('cancel') AND id_store LIKE '%$id_toko%' AND date>='$tanggal_awal' and date<='$tanggal_akhir'");
	$qd=mysqli_fetch_array($querydiskon);
	
	if($bt['pembatalan']==''){
	$pembatalan=0;
	}else{
	$pembatalan= $bt['pembatalan'];
	}
	
	if($j['penjualan_kotor']==''){
	$penjualan_kotor=0;
	}else{
	$penjualan_kotor= $j['penjualan_kotor'];
	}
	
	if($qd['diskonnya']==''){
	$diskon=0;
	}else{
	$diskon= $qd['diskonnya'];
	}
	
	if($qd['pajaknya']==''){
	$pajak=0;
	}else{
	$pajak= $qd['pajaknya'];
	}
	
	if($qd['admin']==''){
	$admin=0;
	}else{
	$admin= $qd['admin'];
	}
	

	
	//menghitung hpp
    
	$querypembelian = mysqli_query($connect,"SELECT SUM(totalorder) as totalorder FROM purchasing_data WHERE user='$m[phone_number]' AND status NOT IN ('cancel') AND date>='$tanggal_awal' and date<='$tanggal_akhir'");
	$qp=mysqli_fetch_array($querypembelian);

	$querypenjualan = mysqli_query($connect,"SELECT SUM(totalorder) as totalorder FROM purchasing_data WHERE user='$m[phone_number]' AND status NOT IN ('cancel') AND date>='$tanggal_awal' and date<='$tanggal_akhir'");
	$qp2=mysqli_fetch_array($querypenjualan);
	
  
	$spa		=	$qp['totalorder'];
	$btud		=	$qp2['totalorder'];
  
	$hpp		=	$spa;
	$laba_kotor	=	$penjualan_kotor-$hpp;
	
	$penjualan_bersih=$penjualan_kotor-$pembatalan;

	
	
	$response = array(
    'status' => 'true',
    'errCode' => '01',
	'msg' => 'Success'
	);
	
	if($id_toko==''){
	$name_store = "All Store";
		}else{
	$store 	= mysqli_query($connect,"SELECT * FROM store WHERE user='$m[phone_number]' AND id_store='$id_toko'");
	$s		= mysqli_fetch_array($store);
	$name_store = $s['name_store'];
		}
	
	
	if($id_toko==''){
	$querynya = "SELECT * FROM sales WHERE user='$m[phone_number]' AND date>='$tanggal_awal' and date<='$tanggal_akhir' ORDER BY id_sales DESC";
		}else{
	$querynya = "SELECT * FROM sales WHERE user='$m[phone_number]' AND id_store='$id_toko' AND date>='$tanggal_awal' and date<='$tanggal_akhir' ORDER BY id_sales DESC";
		}
   
	$hasilnya = mysqli_query($connect,$querynya);
   
	$response["data"] = array();
  
  if($id_toko==''){
	$rata2 	= mysqli_query($connect,"SELECT SUM(totalprice) as totalharganya FROM sales WHERE user='$m[phone_number]' AND date>='$tanggal_awal' and date<='$tanggal_akhir'");
		}else{
	$rata2 	= mysqli_query($connect,"SELECT SUM(totalprice) as totalharganya FROM sales WHERE user='$m[phone_number]' AND id_store='$id_toko' AND date>='$tanggal_awal' and date<='$tanggal_akhir'");
		}
	$r		= mysqli_fetch_array($rata2);
   
   if($id_toko==''){
	$jml 	= mysqli_query($connect,"SELECT SUM(amount) as jumlahpenjualan FROM sales WHERE user='$m[phone_number]' AND date>='$tanggal_awal' and date<='$tanggal_akhir'");
		}else{
	$jml 	= mysqli_query($connect,"SELECT SUM(amount) as jumlahpenjualan FROM sales WHERE user='$m[phone_number]' AND id_store='$id_toko' AND date>='$tanggal_awal' and date<='$tanggal_akhir'");
		}
	$jm		= mysqli_fetch_array($jml);
   
	if($jm['jumlahpenjualan']==''){
	$terjual=0;
	}else{
	$terjual= $jm['jumlahpenjualan'];
	}
   
   if($id_toko==''){
	$jumlahdata 	= mysqli_query($connect,"SELECT * FROM sales WHERE user='$m[phone_number]' AND date>='$tanggal_awal' and date<='$tanggal_akhir' ORDER BY id_sales");
		}else{
	$jumlahdata 	= mysqli_query($connect,"SELECT * FROM sales WHERE user='$m[phone_number]' AND id_store='$id_toko' AND date>='$tanggal_awal' and date<='$tanggal_akhir' ORDER BY id_sales");
		}
	$count 		= mysqli_num_rows($jumlahdata);
	
	$operator 	= mysqli_query($connect,"SELECT * FROM users WHERE phone_number='$sd[operator]'");
    $o			= mysqli_fetch_array($operator);
   
   
   
	$rata_rata= $r['totalharganya']/$count;
  
	$info = array(
    'name_store' 	=> $name_store,
    'operator' 		=> $o['full_name'],
    'date' 			=> $tanggal_awal,
    'total_sales' 	=> $penjualan_bersih,
    'average' 		=> $rata_rata,
	'number_transaction' 	=> $jumlahnya
	);
  
  
	$laporan_penjualan = array();
	while($x = mysqli_fetch_array($hasilnya)){
	
	$barangnya 	= mysqli_query($connect,"SELECT * FROM product WHERE id_product='$x[id_product]'");
    $b			= mysqli_fetch_array($barangnya);
	

    $harga_beli	= $b["purchase_price"]*$terjual;
    $harga_jual	= $b["selling_price"]*$terjual;
    $raba_rugi			= $harga_jual-$harga_beli;
	
    $h['id_product'] 	= $x["id_product"];
    $h['name_product'] 	= "$b[name_product] ($x[no_invoice])";
    $h['amount'] 		= $x["amount"];
    $h['id_store'] 		= $x["id_store"];
    $h['totalprice'] 	= $x["totalprice"];
    $h['selling_price'] 	= $b["selling_price"];
    $h['unit'] 			= $b["unit"];
    array_push($laporan_penjualan, $h);
	}
  
	
  
  
	$response["data"] = array(
	'info' 				=> $info,
	'sales_report' => $laporan_penjualan
	);
  
	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);	
	}
	?>