	<?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 sumary WHERE user='$m[phone_number]' AND create_at>='$tanggal_awal' and create_at<='$tanggal_akhir' ORDER BY id DESC";
		}else{
		$query 		= "SELECT * FROM sumary WHERE user='$m[phone_number]' AND id_store='$id_toko' AND create_at>='$tanggal_awal' and create_at<='$tanggal_akhir' ORDER BY id DESC";
		}
		

	$hasil 		= mysqli_query($connect,$query);
	
	$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($totaln == null){
		$total=0;
	}else{
		$total=$totaln;
	}
	
	$datatransaksi = mysqli_query($connect,"SELECT SUM(totalorder) as totalod FROM sales_data 
											WHERE id_store='$u[id_store]'
											AND status NOT IN ('cancel')
											AND date>='$tanggal_awal' AND date<='$tanggal_akhir'");
	$dt=mysqli_fetch_array($datatransaksi);
	if($dt['totalod']==''){
	$transaksi=0;
	}else{
	$transaksi= $dt['totalod'];
	}
	

	$info = array(
    'name_store' 	=> $name_store,
    'date' 			=> $tanggal_awal - $tanggal_akhir,
    'total' 		=> $transaksi
	);
  		
	$daily_report = array();
	while($x = mysqli_fetch_array($hasil)){
		
		
	$datastore 	= mysqli_query($connect,"SELECT * FROM store WHERE id_store='$x[id_store]'");
	$ds		= mysqli_fetch_array($datastore);
		
		
		if($id_toko==''){
		$datatransaksi = mysqli_query($connect,"SELECT SUM(totalorder) as totalod FROM sales_data 
											WHERE user='$x[cashier_id]' AND payment='cash'
											AND status NOT IN ('cancel')
											AND date='$x[create_at]'");
		}else{
		$datatransaksi = mysqli_query($connect,"SELECT SUM(totalorder) as totalod FROM sales_data 
											WHERE id_store='$id_toko' AND user='$x[cashier_id]' AND payment='cash'
											AND status NOT IN ('cancel')
											AND date='$x[create_at]'");	
			
		}
	$dt=mysqli_fetch_array($datatransaksi);
	if($dt['totalod']==''){
	$sales_cash=0;
	}else{
	$sales_cash= $dt['totalod'];
	}
	
	
	if($id_toko==''){
	$datadc = mysqli_query($connect,"SELECT SUM(totalorder) as totaldebt FROM sales_data 
											WHERE user='$x[cashier_id]' AND payment='debt'
											AND status NOT IN ('cancel')
											AND date='$x[create_at]'");
	}else{
		$datadc = mysqli_query($connect,"SELECT SUM(totalorder) as totaldebt FROM sales_data 
											WHERE id_store='$id_toko' AND user='$x[cashier_id]' AND payment='debt'
											AND status NOT IN ('cancel')
											AND date='$x[create_at]'");
			
		}										
											
	$dc=mysqli_fetch_array($datadc);
	if($dc['totaldebt']==''){
	$sales_debt=0;
	}else{
	$sales_debt= $dc['totaldebt'];
	}
	
	
	if($id_toko==''){
	$datadc = mysqli_query($connect,"SELECT SUM(totalorder) as totaldc FROM sales_data 
											WHERE user='$x[cashier_id]'
											AND status NOT IN ('cancel') AND note LIKE '%Debit Card%'
											AND date='$x[create_at]'");
	}else{
	$datadc = mysqli_query($connect,"SELECT SUM(totalorder) as totaldc FROM sales_data 
											WHERE id_store='$id_toko' AND user='$x[cashier_id]'
											AND status NOT IN ('cancel') AND note LIKE '%Debit Card%'
											AND date='$x[create_at]'");
			
		}											
											
	$dc=mysqli_fetch_array($datadc);
	if($dc['totaldc']==''){
	$sales_debit=0;
	}else{
	$sales_debit= $dc['totaldc'];
	}
	
	if($id_toko==''){
	$datacc = mysqli_query($connect,"SELECT SUM(totalorder) as totalcc FROM sales_data 
											WHERE user='$x[cashier_id]'
											AND status NOT IN ('cancel') AND note LIKE '%Credit Card%'
											AND date='$x[create_at]'");
											}else{
	$datacc = mysqli_query($connect,"SELECT SUM(totalorder) as totalcc FROM sales_data 
											WHERE id_store='$id_toko' AND user='$x[cashier_id]'
											AND status NOT IN ('cancel') AND note LIKE '%Credit Card%'
											AND date='$x[create_at]'");
			
		}
	$cc=mysqli_fetch_array($datacc);
	if($cc['totalcc']==''){
	$sales_credit_card=0;
	}else{
	$sales_credit_card= $cc['totalcc'];
	}
	
	if($id_toko==''){
	$dataovo = mysqli_query($connect,"SELECT SUM(totalorder) as totalovo FROM sales_data 
											WHERE user='$x[cashier_id]'
											AND status NOT IN ('cancel') AND note LIKE '%OVO%'
											AND date='$x[create_at]'");
																					}else{
	$dataovo = mysqli_query($connect,"SELECT SUM(totalorder) as totalovo FROM sales_data 
											WHERE id_store='$id_toko' AND user='$x[cashier_id]'
											AND status NOT IN ('cancel') AND note LIKE '%OVO%'
											AND date='$x[create_at]'");
			
		}
	$ov=mysqli_fetch_array($dataovo);
	if($ov['totalovo']==''){
	$ovo=0;
	}else{
	$ovo= $ov['totalovo'];
	}
	
	if($id_toko==''){
	$datagopay = mysqli_query($connect,"SELECT SUM(totalorder) as totalgopay FROM sales_data 
											WHERE user='$x[cashier_id]'
											AND status NOT IN ('cancel') AND note LIKE '%Gopay%'
											AND date='$x[create_at]'");
																					}else{
	$datagopay = mysqli_query($connect,"SELECT SUM(totalorder) as totalgopay FROM sales_data 
											WHERE id_store='$id_toko' AND user='$x[cashier_id]'
											AND status NOT IN ('cancel') AND note LIKE '%Gopay%'
											AND date='$x[create_at]'");
			
		}
	$ov=mysqli_fetch_array($datagopay);
	if($ov['totalgopay']==''){
	$gopay=0;
	}else{
	$gopay= $ov['totalgopay'];
	}
	
	if($id_toko==''){
	$datadana = mysqli_query($connect,"SELECT SUM(totalorder) as totaldana FROM sales_data 
											WHERE user='$x[cashier_id]'
											AND status NOT IN ('cancel') AND note LIKE '%Dana%'
											AND date='$x[create_at]'");
	}else{
	$datadana = mysqli_query($connect,"SELECT SUM(totalorder) as totaldana FROM sales_data 
											WHERE id_store='$id_toko' AND user='$x[cashier_id]'
											AND status NOT IN ('cancel') AND note LIKE '%Dana%'
											AND date='$x[create_at]'");
			
		}										
	$dn=mysqli_fetch_array($datadana);
	if($dn['totaldana']==''){
	$dana=0;
	}else{
	$dana= $dn['totaldana'];
	}
	$total_sales=$sales_cash+$sales_debt+$sales_credit_card+$sales_debit+$gopay+$ovo+$dana;
		
	$operator 	= mysqli_query($connect,"SELECT * FROM users WHERE phone_number='$x[cashier_id]'");
    $o			= mysqli_fetch_array($operator);

    $h['name_store'] 		= $ds["name_store"];
    $h['operator'] 			= $o["full_name"];
    $h['date'] 				= $x["create_at"];
    $h['sales_cash'] 		= $sales_cash;
    $h['sales_debt'] 		= $sales_debt;
    $h['sales_credit_card'] = $sales_credit_card;
    $h['sales_debit_card'] 	= $sales_debit;
    $h['gopay'] 			= $gopay;
    $h['ovo'] 				= $ovo;
    $h['dana'] 				= $dana;
    $h['sub_total'] 		= $total_sales;
    array_push($daily_report, $h);
	}
  
	
  
  
	$response["data"] = array(
	'info' 				=> $info,
	'daily_report' => $daily_report
	);
  
	array_push($response["data"]);
	echo json_encode($response);
	

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

    echo json_encode($json);	
	}
	?>