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

	require_once("../connection.php");
	$content 	= trim(file_get_contents("php://input"));
	$decoded 	= json_decode($content, true);
	
	$user=mysqli_real_escape_string($connect, $_POST['user']);
	$id_store=mysqli_real_escape_string($connect, $_POST['id_store']);
	$no_invoice=mysqli_real_escape_string($connect, $_POST['no_invoice']);
	$date=mysqli_real_escape_string($connect, $_POST['date']);
	$totalnominal=mysqli_real_escape_string($connect, $_POST['totalnominal']);
	

	$getusers	= mysqli_query($connect,"SELECT * FROM users WHERE phone_number='$user'");
	$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){

	mysqli_query($connect,"INSERT INTO spending_data(no_invoice,
								  date,
								  operator,
								  totalnominal,
								  id_store,
                                  user) 
                    VALUES('$no_invoice',
						   '$date',
						   '$u[phone_number]',
						   '$totalnominal',
						   '$id_store',
                           '$m[phone_number]')");
					
	$responses = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'success'
	);
   

	echo json_encode($responses);


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

    echo json_encode($json);	
	}
	?>