	<?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']);
	$ada_stok=mysqli_real_escape_string($connect, $_GET['have_stock']);
	$kodebarang=mysqli_real_escape_string($connect, $_GET['codeproduct']);

	$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){
	$databarang = mysqli_query($connect,"SELECT * FROM product WHERE codeproduct='$kodebarang' AND user='$m[phone_number]' AND showing='0' AND have_stock LIKE '%$ada_stok%'");
	$b=mysqli_fetch_array($databarang);

	
	$datastore = mysqli_query($connect,"SELECT * FROM store WHERE id_store='$u[id_store]'");
	$t=mysqli_fetch_array($datastore);
	
	if($kodebarang == $b['codeproduct']){
	$query = "SELECT * FROM product WHERE codeproduct='$kodebarang' AND user='$m[phone_number]'";
	$posisi=true;
	}else{
	$query = "SELECT * FROM product WHERE codeproduct='$kodebarang' AND user='0'";
	$posisi=false;
	}

	$hasil = mysqli_query($connect,$query);


	if(mysqli_num_rows($hasil) > 0 ){
  $response = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
   );
  $response["data"] = array();
  while($x = mysqli_fetch_array($hasil)){
	  
	$querykategori = "SELECT name_category FROM category WHERE id_category='$x[id_category]'";
	$hasilkategori = mysqli_query($connect,$querykategori);
	$k = mysqli_fetch_array($hasilkategori);
	
	$datastok = mysqli_query($connect,"SELECT * FROM stock WHERE id_product='$x[id_product]' AND id_store='$u[id_store]'");
	$s=mysqli_fetch_array($datastok);
	if($s['stock']==''){
	$stok=0;
	}else{
	$stok= $s['stock'];
	}
	if($t['decimals']=="No Decimal"){
	$selling_price = (int)$x['selling_price'];
	$purchase_price = (int)$x['purchase_price'];
	$wholesale_price = (int)$x['wholesale_price'];
	}
	else{
	$selling_price = $x['selling_price'];	
	$purchase_price = $x['purchase_price'];	
	$wholesale_price = $x['wholesale_price'];	
	}
	  $selling_prices=$selling_price+$x['tax']/100;
    $h['id_product'] 		= $x["id_product"];
    $h['posisi'] 			= true;
    $h['name_product'] 		= $x["name_product"];
    $h['unit'] 		= $x["unit"];
    $h['id_category'] 		= $x["id_category"];
    $h['name_category'] 	= $k["name_category"];
    $h['codeproduct'] 		= $x["codeproduct"];
    $h['tax'] 		= $x["tax"];
    $h['purchase_price'] 		= $purchase_price;
    $h['selling_price'] 		= $selling_prices;
    $h['stock'] 				= $stok;
    $h['minimalstock'] 		= $x["minimalstock"];
    $h['discount'] 			= $x["discount"];
	$h['online'] 		= $x["online"];
    $h['have_stock'] 		= $x["have_stock"];
    $h['wholesale_price'] 	= $wholesale_price;
    $h['minimum_purchase'] 	= $x["minimum_purchase"];
    $h['description'] 		= $x["description"];
    $h['img'] 			= "http://$_SERVER[HTTP_HOST]/geten/images/$x[folder]/small_$x[img]";
	$h['alertstock'] 	= $x["alertstock"];
    array_push($response["data"], $h);
  }
  echo json_encode($response);
	}else {
  $response = array(
    'status' 	=> 'true',
    'errCode' 	=> '02',
	'msg' 		=> 'no data'
   );
  echo json_encode($response);
	}


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

    echo json_encode($json);	
	}
	?>
