	<?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']);
							$totalharga=mysqli_real_escape_string($connect, $_GET['totalprice']);
							$id_diskon=mysqli_real_escape_string($connect, $_GET['id_discount']);


	$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){
		
	
	$datadiskon = mysqli_query($connect,"SELECT * FROM discount WHERE id_discount='$id_diskon'");
	$d=mysqli_fetch_array($datadiskon);
	
	$datatoko = mysqli_query($connect,"SELECT * FROM store WHERE id_store='$u[id_store]'");
	$t=mysqli_fetch_array($datatoko);
	
	if($d['type']=='prosentase'){
		$nominaldiskon=$totalharga*$d['nominal']/100;
	}else{
				$nominaldiskon=$d['nominal'];
	}
	$hargasetelahdiskon=$totalharga-$nominaldiskon;
	
	
    $nominalsc=$hargasetelahdiskon*$t['service_charge']/100;
	$total_service_charge=$hargasetelahdiskon+$nominalsc;
	
	$nominalpajak=$total_service_charge*$t['tax']/100;
	$total_bayar=$total_service_charge+$nominalpajak;


	$response = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
	);
	$response["data"] = array();
    $h['totalprice'] 			= $totalharga;
    $h['id_discount'] 			= $d["id_discount"];
    $h['name_discount'] 		= $d["name_discount"];
    $h['harga_setelah_diskon'] 	= $hargasetelahdiskon;
    $h['service_charge'] 		= $nominalsc;
    $h['total_service_charge'] 	= $total_service_charge;
    $h['tax'] 					= $nominalpajak;
    $h['totalpay'] 				= $total_bayar;
    array_push($response["data"], $h);
	
	echo json_encode($response);
	


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

    echo json_encode($json);	
	}
	?>