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

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

	$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);
	
	$today		= gmdate("Y-m-d 00:00:00", time()+60*60*7);
	$endtoday	= gmdate("Y-m-d 23:59:00", time()+60*60*7);
	


	if ($jumlah > 0){

	$query = "SELECT * FROM intial WHERE cashier_id='$u[phone_number]' AND create_at>='$today' AND create_at<='$endtoday'";
	$hasil = mysqli_query($connect,$query);
	
	$todaytr = date('Y-m-d');


	if(mysqli_num_rows($hasil) > 0 ){
  $response = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
   );
  $response["data"] = array();
  while($x = mysqli_fetch_array($hasil)){
	  
	  $datatransaksi = mysqli_query($connect,"SELECT SUM(totalorder) as totalod FROM sales_data 
											WHERE id_store='$u[id_store]' AND user='$u[phone_number]' AND shift='$x[code]' AND payment='cash'
											AND status NOT IN ('cancel')
											AND date>='$todaytr'");
	$dt=mysqli_fetch_array($datatransaksi);
	if($dt['totalod']==''){
	$sales_cash=0;
	}else{
	$sales_cash= $dt['totalod'];
	}
	
	$datatax = mysqli_query($connect,"SELECT SUM(tax) as totaltax FROM sales_data 
											WHERE id_store='$u[id_store]' AND user='$u[phone_number]' AND shift='$x[code]'
											AND status NOT IN ('cancel')
											AND date>='$todaytr'");
	$tx=mysqli_fetch_array($datatax);
	if($tx['totaltax']==''){
	$ppn=0;
	}else{
	$ppn= $tx['totaltax'];
	}
	
	$datasc = mysqli_query($connect,"SELECT SUM(service_charge) as totalsc FROM sales_data 
											WHERE id_store='$u[id_store]' AND user='$u[phone_number]' AND shift='$x[code]'
											AND status NOT IN ('cancel')
											AND date>='$todaytr'");
	$sc=mysqli_fetch_array($datasc);
	if($sc['totalsc']==''){
	$sc=0;
	}else{
	$sc= $sc['totalsc'];
	}
	
	$datareturn = mysqli_query($connect,"SELECT SUM(totalorder) as totaloc FROM sales_data 
											WHERE id_store='$u[id_store]' AND user='$u[phone_number]' AND shift='$x[code]'
											AND status = 'cancel'
											AND date>='$todaytr'");
	$dr=mysqli_fetch_array($datareturn);
	if($dr['totaloc']==''){
	$sales_return=0;
	}else{
	$sales_return= $dr['totaloc'];
	}
	
	$datacc = mysqli_query($connect,"SELECT SUM(totalorder) as totalnc FROM sales_data 
											WHERE id_store='$u[id_store]' AND user='$u[phone_number]' AND shift='$x[code]' AND payment='non cash'
											AND status NOT IN ('cancel')
											AND date>='$todaytr'");
	$cc=mysqli_fetch_array($datacc);
	if($cc['totalnc']==''){
	$sales_non_cash=0;
	}else{
	$sales_non_cash= $cc['totalnc'];
	}
	
	$datadc = mysqli_query($connect,"SELECT SUM(totalorder) as totaldebt FROM sales_data 
											WHERE id_store='$u[id_store]' AND user='$u[phone_number]' AND shift='$x[code]' AND payment='debt'
											AND status NOT IN ('cancel')
											AND date>='$todaytr'");
	$dc=mysqli_fetch_array($datadc);
	if($dc['totaldebt']==''){
	$sales_debt=0;
	}else{
	$sales_debt= $dc['totaldebt'];
	}
	  
	  $total_sales=$sales_cash+$ppn+$sc-$sales_return;
	  $datavariance = $sales_debt+$sales_non_cash+$x['cash_actual'];
	  $variance=($total_sales-$datavariance);
	  
	  if($x['flag']=="I"){
		$status= "INITIAL(I)";
	}else if($x['flag']=="S"){
		$status= "STOP(S)";
	}else if($x['flag']=="C"){
		$status= "CLOSE(C)";
	}
	  
    $h['id'] 	= $x["id"];
    $h['cash_actual'] = $x["cash_actual"];
    $h['name_initial'] = "$u[full_name] - $x[shift]";
    $h['name_cashier'] = $u["full_name"];
    $h['initial_date'] = $x["initial_date"];
    $h['cash_awal'] = $x["cash_awal"];
    $h['sales_cash'] = $sales_cash;
    $h['sales_return'] = $sales_return;
    $h['variance'] = $variance;
    $h['sales_debt'] = $sales_debt;
    $h['ppn'] = $ppn;
    $h['sc'] = $sc;
    $h['sales_non_cash'] = $sales_non_cash;
    $h['shift'] = $x["shift"];
    $h['status'] = $status;
    $h['flag'] = $x["flag"];
    $h['total_sales'] = $total_sales;
    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);	
	}
	?>