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

	require_once("../connection.php");
	
	$key				=mysqli_real_escape_string($connect, $_POST['key']);
	$hargabeli			=mysqli_real_escape_string($connect, $_POST['purchase_price']);
	$hargajual			=mysqli_real_escape_string($connect, $_POST['selling_price']);
	$stok				=mysqli_real_escape_string($connect, $_POST['stock']);
	$nama_barang		=mysqli_real_escape_string($connect, $_POST['name_product']);
	$unit		=mysqli_real_escape_string($connect, $_POST['unit']);
	$id_kategori		=mysqli_real_escape_string($connect, $_POST['id_category']);
	$kodebarang			=mysqli_real_escape_string($connect, $_POST['codeproduct']);
	$minimalstok		=mysqli_real_escape_string($connect, $_POST['minimalstock']);
	$deskripsi			=mysqli_real_escape_string($connect, $_POST['description']);
	$online				=mysqli_real_escape_string($connect, $_POST['online']);
	$ada_stok			=mysqli_real_escape_string($connect, $_POST['have_stock']);
	$hargagrosir		=mysqli_real_escape_string($connect, $_POST['wholesale_price']);
	$minimal_pembelian	=mysqli_real_escape_string($connect, $_POST['minimum_purchase']);
	$tax				=mysqli_real_escape_string($connect, $_POST['tax']);
	$id_product			=mysqli_real_escape_string($connect, $_POST['id_product']);
	$alertstock			=mysqli_real_escape_string($connect, $_POST['alertstock']);

	$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);

	$databarang = mysqli_query($connect,"SELECT * FROM product WHERE codeproduct='$kodebarang' AND user='$m[phone_number]'");
	$b=mysqli_fetch_array($databarang);
	
	$databarang2 = mysqli_query($connect,"SELECT * FROM product WHERE id_product='$id_product' AND user='$u[phone_number]'");
	$i=mysqli_fetch_array($databarang2);
	
	$databarangold = mysqli_query($connect,"SELECT * FROM product WHERE codeproduct='$kodebarang'");
	$db=mysqli_fetch_array($databarangold);


	if ($jumlah > 0){
		
  include "../fungsi_thumb.php";
  
  $folder = date("m-d-y-H"); 
  
  $lokasi_file 		= $_FILES['img']['tmp_name'];
  $tipe_file   		= $_FILES['img']['type'];
  $nama_file   		= $_FILES['img']['name'];
  $acak        		= rand(1,99);
  $nama_file_unik	= $acak.$nama_file;
	
	if($kodebarang == $b['codeproduct']){
	
	 if (empty($lokasi_file)){
	   mysqli_query($connect,"UPDATE product SET name_product    	= '$nama_barang',
								  unit	 		= '$unit',
								  id_category	 		= '$id_kategori',
								  purchase_price		= '$hargabeli',
								  codeproduct	 		= '$kodebarang',
								  tax	 				= '$tax',
								  selling_price		 	= '$hargajual',
								  stock			 		= '$stok',
								  alertstock			= '$alertstock',
								  minimalstock	 		= '$minimalstok',
								  online	 	 		= '$online',
								  wholesale_price	 	= '$hargagrosir',
								  minimum_purchase	 	= '$minimal_pembelian',
								  have_stock	 	 	= '$ada_stok',
								  description		 	= '$deskripsi'
                            WHERE id_product 	 		= '$id_product'");
   }else{
	   
	    UploadImage($nama_file_unik);
	   mysqli_query($connect,"UPDATE product SET name_product    	= '$nama_barang',
								  unit	 		= '$unit',
								  id_category	 		= '$id_kategori',
								  purchase_price		= '$hargabeli',
								  codeproduct	 		= '$kodebarang',
								  tax	 				= '$tax',
								  selling_price		 	= '$hargajual',
								  stock			 		= '$stok',
								  alertstock			= '$alertstock',
								  minimalstock	 		= '$minimalstok',
								  online	 	 		= '$online',
								  wholesale_price	 	= '$hargagrosir',
								  minimum_purchase	 	= '$minimal_pembelian',
								  have_stock	 	 	= '$ada_stok',
								  description		 	= '$deskripsi',
								  folder		 		= '$folder',
								  img		 	 		= '$nama_file_unik'
                            WHERE id_product 	 		= '$id_product'");
	    
   }

	}else if($id_product == $i['id_product']){
	
	
	 if (empty($lokasi_file)){
	   mysqli_query($connect,"UPDATE product SET name_product    	= '$nama_barang',
								  unit	 		= '$unit',
								  id_category	 		= '$id_kategori',
								  purchase_price		= '$hargabeli',
								  codeproduct	 		= '$kodebarang',
								  tax	 				= '$tax',
								  selling_price		 	= '$hargajual',
								  stock			 		= '$stok',
								  alertstock			 		= '$alertstock',
								  minimalstock	 		= '$minimalstok',
								  online	 	 		= '$online',
								  wholesale_price	 	= '$hargagrosir',
								  minimum_purchase	 	= '$minimal_pembelian',
								  have_stock	 	 	= '$ada_stok',
								  description		 	= '$deskripsi'
                            WHERE id_product 	 		= '$id_product'");
   }else{
	    UploadImage($nama_file_unik);
	   mysqli_query($connect,"UPDATE product SET name_product    	= '$nama_barang',
								  unit	 		= '$unit',
								  id_category	 		= '$id_kategori',
								  purchase_price		= '$hargabeli',
								  codeproduct	 		= '$kodebarang',
								  tax	 				= '$tax',
								  selling_price		 	= '$hargajual',
								  stock			 		= '$stok',
								  alertstock			= '$alertstock',
								  minimalstock	 		= '$minimalstok',
								  online	 	 		= '$online',
								  wholesale_price	 	= '$hargagrosir',
								  minimum_purchase	 	= '$minimal_pembelian',
								  have_stock	 	 	= '$ada_stok',
								  description		 	= '$deskripsi',
								  folder		 		= '$folder',
								  img		 	 		= '$nama_file_unik'
                            WHERE id_product 	 		= '$id_product'");
	     
   }
	
	
	}else{
	
	mysqli_query($connect,"INSERT INTO product(name_product,
								  unit,
								  id_category,
								  codeproduct,
								  tax,
								  purchase_price,
								  selling_price,
								  stock,
								  alertstock,
								  minimalstock,
								  description,
								  have_stock,
								  img,
                                  user) 
                    VALUES('$nama_barang',
						   '$unit',
						   '$id_kategori',
						   '$db[kodebarang]',
						   '$tax',
						   '$hargabeli',
						   '$hargajual',
						   '$stok',
						   '$alertstock',
						   '$minimalstok',
						   '$deskripsi',
						   '$ada_stok',
						   '$db[img]',
                           '$m[phone_number]')");
	}
	
	$datastok = mysqli_query($connect,"SELECT * FROM stock WHERE id_product='$id_product'");
	$s=mysqli_fetch_array($datastok);
	$stoknya		=$stok;

	if($ada_stok == 1 ){	
	if(mysqli_num_rows($datastok) > 0 ){
	mysqli_query($connect,"UPDATE stock SET stock   = '$stoknya' WHERE id_product  = '$id_product'");
	}else{
	mysqli_query($connect,"INSERT INTO stock(id_product,
								  id_store,
								  stock,
                                  user) 
					VALUES('$id_product',
						   '$m[id_store]',
						   '$stok',
                           '$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);	
	}
	?>