	<?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']);
	$tanggal	= gmdate("Y-m-d", time()+60*60*7);

	$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 transfer_data WHERE no_invoice='$no_invoice'");
	$db=mysqli_fetch_array($datapenjualan);
								 
	mysqli_query($connect,"UPDATE transfer_data SET status       = 'finish'
                             WHERE no_invoice 	   = '$no_invoice'");
							 
	mysqli_query($connect,"UPDATE transfer SET status       	   = 'finish'
                             WHERE no_invoice 	   = '$no_invoice'");
							 
	$query = "SELECT * FROM transfer WHERE no_invoice = '$no_invoice'";
	$hasil = mysqli_query($connect,$query);						 
	
	while($x = mysqli_fetch_array($hasil)){
		
	$datastok = mysqli_query($connect,"SELECT * FROM stock WHERE id_product='$x[id_product]' AND id_store='$x[id_store]'");
	$s=mysqli_fetch_array($datastok);
    $stoknya		=$s['stock']+$x['amount'];
	
	
	$dataop = mysqli_query($connect,"SELECT * FROM users WHERE phone_number='$db[operator]'");
	$y=mysqli_fetch_array($dataop);
	
	
	$datastok2 = mysqli_query($connect,"SELECT * FROM stock WHERE id_product='$x[id_product]' AND id_store='$y[id_store]'");
	$s2=mysqli_fetch_array($datastok2);
    $stoknya2		=$s2['stock']-$x['amount'];
	
	if(mysqli_num_rows($datastok) > 0 ){
			mysqli_query($connect,"UPDATE stock SET stock   = '$stoknya' WHERE id_product  = '$x[id_product]' AND id_store='$x[id_store]'");
			mysqli_query($connect,"UPDATE stock SET stock   = '$stoknya2' WHERE id_product  = '$x[id_product]' AND id_store='$y[id_store]'");

	}else{
			 mysqli_query($connect,"INSERT INTO stock(id_product,
								  id_store,
								  stock,
                                  user) 
                    VALUES('$x[id_product]',
						   '$x[id_store]',
						   '$stoknya',
                           '$m[phone_number]')");
						   
						   
					
	}
	
	
						
						   mysqli_query($connect,"INSERT INTO history_stock(id_product,
								  id_store,
								  stock,
								  detail,
								  status,
								  date,
                                  user) 
					VALUES('$x[id_product]',
						   '$y[id_store]',
						   '$x[amount]',
						   'Transfer Stock $no_invoice',
						   '1',
						   '$tanggal',
                           '$m[phone_number]')");
						   
						
						   mysqli_query($connect,"INSERT INTO history_stock(id_product,
								  id_store,
								  stock,
								  detail,
								  status,
								  date,
                                  user) 
					VALUES('$x[id_product]',
						   '$x[id_store]',
						   '$x[amount]',
						   'Receive  Stock  $no_invoice',
						   '0',
						   '$tanggal',
                           '$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);	
	}
	?>