	<?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']);
	$no_invoice=mysqli_real_escape_string($connect, $_GET['no_invoice']);
	$timestamp = new DateTime(null, new DateTImeZone('Africa/Nairobi'));
	$tanggal = $timestamp->format('Y-m-d');
	$time = $timestamp->format('H:i');

	$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){
		
	$datapenjualan = mysqli_query($connect,"SELECT * FROM sales_data WHERE no_invoice='$no_invoice'");
	$db=mysqli_fetch_array($datapenjualan);
								 
	mysqli_query($connect,"UPDATE sales_data SET status       = 'finish'
                             WHERE no_invoice 	   = '$no_invoice'");
							 
	mysqli_query($connect,"UPDATE sales SET status       	   = 'finish'
                             WHERE no_invoice 	   = '$no_invoice'");
							 
							 

		mysqli_query($connect,"INSERT INTO history_job(no_invoice,
								  note,
								  operator,
								  status,
								  date,
                                  user) 
					VALUES('$no_invoice',
						   'Transaction $no_invoice Finish by $u[full_name]',
						   '$u[phone_number]',
						   'finish',
						   '$tanggal $time',
                           '$m[phone_number]')");
							 
	$json = array(
    'status' 		=> 'true',
    'errCode'	 	=> '01',
	'msg' 			=> 'Success'
	);

    echo json_encode($json);

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

    echo json_encode($json);	
	}
	?>