	<?php
	/**
	* Created by PhpStorm.
	* User: ahmad
	* Date: 20/08/2019
	* Time: 11:48 AM
	* Location profil -> data akun home dan menu samping
	*/

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


	$getusers	= mysqli_query($connect,"SELECT * FROM users WHERE id_session='$key'");
	$u	   		= mysqli_fetch_array($getusers);
	$jumlah   	= mysqli_num_rows($getusers);
	$tanggal	= gmdate("Y-m-d", time()+60*60*7);
	
	
	$masterakun = mysqli_query($connect,"SELECT * FROM users WHERE phone_number='$u[master]'");
	$m=mysqli_fetch_array($masterakun);

	

	if ($jumlah > 0){

	$query = "SELECT * FROM users WHERE id_session='$u[id_session]'";
	$hasil = mysqli_query($connect,$query);


	$dataomset = mysqli_query($connect,"SELECT SUM(totalorder) as total FROM sales_data WHERE operator='$u[phone_number]' AND status NOT IN ('cancel') AND date>='$tanggal_awal' and date<='$tanggal_akhir'");
	$n=mysqli_fetch_array($dataomset);
	
	$dataabsen = mysqli_query($connect,"SELECT * FROM attendance WHERE staff='$u[phone_number]' AND name_attendance='login' AND date>='$tanggal_awal' AND date<='$tanggal_akhir'");
	$jumlahkehadiran = mysqli_num_rows($dataabsen);
	

	if($n['total']==''){
	$omset=0;
	}else{
	$omset= $n['total'];
	}
	
	if($jumlahkehadiran==''){
	$jumlahkehadirannya=0;
	}else{
	$jumlahkehadirannya= $jumlahkehadiran;
	}


	if(mysqli_num_rows($hasil) > 0 ){
	$response = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
	);
	$response["data"] = array();
	while($x = mysqli_fetch_array($hasil)){
		
	$datatoko = mysqli_query($connect,"SELECT * FROM store WHERE id_store='$u[id_store]'");
	$t=mysqli_fetch_array($datatoko);
		
	$komisi=$n['total']*$x['commission']/100;
	$total_gaji=$x['salary_fixed']+$komisi+$x['allowance']-$x['piece'];
 
    $h['id_store'] 			= $t["id_store"];
    $h['name_store'] 		= $t["name_store"];
	$h['address'] 			= $t["address"];
	$h['email'] 			= $t["email"];
    $h['no_telp'] 			= $t["nohp"];
    $h['full_name'] 		= $x["full_name"];
    $h['period'] 			= "$tanggal_awal s/d $tanggal_akhir";
    $h['presence'] 			= "$jumlahkehadirannya days";
    $h['key'] 				= $x["id_session"];
    $h['part'] 				= $x["level"];
    $h['salary_fixed'] 		= $x["salary_fixed"];
    $h['total_sales'] 		= $omset;
    $h['commission'] 		= $komisi;
    $h['allowance'] 		= $x["allowance"];
    $h['piece'] 			= $x["piece"];
    $h['total_salary'] 		= $total_gaji;
    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);	
	}
	?>