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



	$id_supplier						= $decoded["id_supplier"];
	$jatuh_tempo						= $decoded["due_date"];
	$total_order						= $decoded["total_order"];
	$key								= $decoded["key"];
	$tipe_pembayaran					= $decoded["payment_type"];

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

	$querydp = mysqli_query($connect,"SELECT id_purchasing_data FROM purchasing_data WHERE user='$m[phone_number]' ORDER BY id_purchasing_data DESC LIMIT 1");
	$dp=mysqli_fetch_array($querydp);
	
	$datatoko = mysqli_query($connect,"SELECT * FROM supplier WHERE id_supplier='$id_supplier'");
	$dt=mysqli_fetch_array($datatoko);


	if ($jumlah > 0){
	
	$date				= gmdate("dHi");
	
	$nama = $dt['name_supplier'];
	$arr = explode(' ', $nama);
	$singkatan = "";
	foreach($arr as $kata)
	{
	$singkatan .= substr($kata, 0, 1);
	}
	
	$kode= strtoupper($singkatan);
	
	$no_invoice			="$kode-$dp[id_purchasing_data]$date";
	$jumlah_pembayaran	=0;
    $kembalian			= $jumlah_pembayaran-$total_order;

	
	if($tipe_pembayaran==1){
		$pembayaran="cash";
				$status="paid off";

	}else if ($tipe_pembayaran==2){
		$pembayaran="debt";
				$status="paid off";

	}else if($tipe_pembayaran==3){
		$pembayaran="debt";
				$status="debt";

		
		
	mysqli_query($connect,"UPDATE supplier SET debt = '1'
                             WHERE id_supplier = '$id_supplier'");
					
	mysqli_query($connect,"INSERT INTO historydebtsupplier(id_supplier, user, status, no_invoice, nominal, date) 
                    VALUES('$id_supplier', '$m[phone_number]', 'debt', '$no_invoice', '$total_order', '$tanggal')");
		
	}
	
	for($x = 0; $x<count($decoded["product"]); $x++)
        {
	$id_product			= $decoded["product"][$x]["id_product"];
	$jumlah_barang		= $decoded["product"][$x]["amount_product"];

	$barang = mysqli_query($connect,"SELECT * FROM product WHERE id_product='$id_product'");
	$b=mysqli_fetch_array($barang);

	$totalharga	=$b['purchase_price']*$jumlah_barang;
	$stok		=$b['stock']+$jumlah_barang;

	mysqli_query($connect,"INSERT INTO purchase(id_product, amount, id_supplier, id_store, user, no_invoice, status, price, totalprice, date) 
                    VALUES('$id_product', '$jumlah_barang', '$id_supplier', '$u[id_store]', '$m[phone_number]', '$no_invoice', '$status', '$b[purchase_price]', '$totalharga', '$tanggal')");
  
			mysqli_query($connect,"UPDATE product SET stock   = '$stok' WHERE id_product  = '$id_product'");
			
			

	$datastok = mysqli_query($connect,"SELECT * FROM stock WHERE id_product='$id_product' AND id_store='$u[id_store]'");
	$s=mysqli_fetch_array($datastok);
    $stoknya		=$s['stock']+$jumlah_barang;
	
	if(mysqli_num_rows($datastok) > 0 ){
			mysqli_query($connect,"UPDATE stock SET stock   = '$stoknya' WHERE id_product  = '$id_product' AND id_store='$u[id_store]'");
	}else{
			 mysqli_query($connect,"INSERT INTO stock(id_product,
								  id_store,
								  stock,
                                  user) 
                    VALUES('$id_product',
						   '$u[id_store]',
						   '$stoknya',
                           '$m[phone_number]')");
	}
	
						
						   mysqli_query($connect,"INSERT INTO history_stock(id_product,
								  id_store,
								  stock,
								  detail,
								  status,
								  date,
                                  user) 
					VALUES('$id_product',
						   '$u[id_store]',
						   '$jumlah_barang',
						   'Add Stock (Wholesales)',
						   '0',
						   '$tanggal',
                           '$m[phone_number]')");
		}


	mysqli_query($connect,"INSERT INTO purchasing_data(id_supplier,
								  no_invoice,
								  date,
								  id_store,
								  payment,
								  totalorder,
								  status,
								  changepay,
								  due_date,
								  operator,
                                  user) 
                    VALUES('$id_supplier',
						   '$no_invoice',
						   '$tanggal',
						   '$u[id_store]',
						   '$pembayaran',
						   '$total_order',
						   '$status',
						   '$kembalian',
						   '$jatuh_tempo',
						   '$u[phone_number]',
                           '$m[phone_number]')");
  

	$responses = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
	);
   
	$responses["data"] = array();

  
	$responses["data"] = array(
	'invoice' => $no_invoice
	);
  
	array_push($responses["data"]);

	echo json_encode($responses);


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

    echo json_encode($json);	
	}
	?>