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


	$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 no_telp='$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 datapenjualan WHERE operator='$u[no_telp]' AND status NOT IN ('cancel') AND tanggal>='$_GET[tanggal_awal]' and tanggal<='$_GET[tanggal_akhir]'");
	$n=mysqli_fetch_array($dataomset);
	

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


	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 toko WHERE id_toko='$u[id_toko]'");
	$t=mysqli_fetch_array($datatoko);
		
	$komisi=$n['total']*$x['komisi']/100;
	
	$total_gaji=$x['gaji_tetap']+$komisi+$x['tunjangan']-$x['potongan'];

    $h['id_toko'] 		= $t["id_toko"];
    $h['nama_toko'] 		= $t["nama_toko"];
	$h['alamat'] 			= $t["alamat"];
	$h['email'] 			= $t["email"];
    $h['no_telp'] 			= $t["nohp"];
    $h['nama_lengkap'] 		= $x["nama_lengkap"];
    $h['periode'] 			= "$_GET[tanggal_awal] s/d $_GET[tanggal_akhir]";
	$h['kehadiran'] 		= 0;
    $h['key'] 				= $x["id_session"];
    $h['bagian'] 			= $x["level"];
    $h['gaji_tetap'] 		= $x["gaji_tetap"];
    $h['total_penjualan'] 	= $omset;
    $h['komisi'] 			= $komisi;
    $h['tunjangan'] 		= $x["tunjangan"];
    $h['potongan'] 			= $x["potongan"];
    $h['total_gaji'] 		= $total_gaji;
    array_push($response["data"], $h);
	}
	echo json_encode($response);
	}else {
	$response = array(
    'status' 	=> 'true',
    'errCode' 	=> '02',
	'msg' 		=> 'tidak ada data'
	);
	echo json_encode($response);
	}


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

    echo json_encode($json);	
	}
	?>