<?php

session_start();

$path_to_root = "";
include $path_to_root . 'database.php';

$db = new database();

$id_user = $_SESSION['user_id'];

$tanggal_pengambilan = date("d-m-Y", strtotime(strip_tags(trim($_POST['tanggal']))));
$tanggal_pesanan = date('d-m-Y');

if ($tanggal_pengambilan != '') {
	cek_sisa_stok_produk_order($db, $id_user);
	if (isset($_GET['total']) && $_GET['total']!="") {
		$total = $_GET['total'];

		$id_order = mt_rand(10000,99999);

		$sql = "INSERT INTO `order` (`id`, `id_user`, `bayar`, `bukti`, `status`, `tanggal_pesanan`, `tanggal_pengambilan`, `tanggal_pembayaran`, `tanggal_pengembalian`) VALUES ($id_order, $id_user, $total, 'kosong', 'belum bayar', '$tanggal_pesanan', '$tanggal_pengambilan', 'kosong', 'kosong')";

		if ($db->db_query($sql)) {
			$sql = "SELECT * FROM keranjang WHERE id_user=$id_user";

			$result = $db->db_query($sql);

			$data = $db->db_num_rows($result);
			$i = 1;

			while ($rows = $db->db_fetch_array($result)) {
				$id_barang = $rows['id_barang'];
				$quantity = $rows['quantity'];
				$lama_sewa = $rows['lama_sewa'];
				$id = $rows['id'];

				$sql_barang = "SELECT * FROM barang WHERE id_barang=$id_barang";

				$result_barang = $db->db_query($sql_barang);

				while ($rows_barang = $db->db_fetch_array($result_barang)) {
					$nama = $rows_barang['nama'];
					$harga = $rows_barang['harga'];
					$foto = $rows_barang['foto'];

					$sql_order_produk = "INSERT INTO `order_produk` (`id_order`, `id_barang`, `nama`, `harga`, `foto`, `quantity`, `lama_sewa`) VALUES ($id_order , $id_barang, '$nama', $harga, '$foto', $quantity, $lama_sewa)";

					if ($db->db_query($sql_order_produk)) {
						$sql_delete_keranjang = "DELETE FROM `keranjang` WHERE id=$id";

						if ($db->db_query($sql_delete_keranjang)) {
							if ($i == $data) {
								header("location:index.php?menu=order");
							} else {
								$i++;
							}
						}
					}
				}
			}
		} else {
			echo "gagal menambahkan order anda";
		}
	}
} else {
	header("location:index.php?menu=keranjang&alert=1");
}

function cek_sisa_stok_produk_order($db, $id_user)
{
	$sql = "SELECT a.quantity, a.id_barang, b.stok
		FROM keranjang a
		LEFT JOIN barang b ON b.id_barang = a.id_barang
		WHERE a.id_user = $id_user";

	$result_keranjang = $db->db_query($sql);

	while ($keranjang = $db->db_fetch_array($result_keranjang)) {
		$quantity = $keranjang['quantity'];
		$id_barang = $keranjang['id_barang'];
		$stok = $keranjang['stok'];

		$sisa_stok_produk = cek_sisa_stok($db, $id_barang, $stok);

		if ($quantity > $sisa_stok_produk) {
			header("location:index.php?menu=keranjang&alert=2");die;
		}
	}
}

function cek_sisa_stok($db, $id_produk, $stok) {
	$sql = 'SELECT b.quantity FROM `barang` a
		LEFT JOIN `order_produk` b ON b.id_barang = a.id_barang
		LEFT JOIN `order` c ON c.id = b.id_order
		WHERE a.id_barang = '.$id_produk.' AND (c.status = \'barang sudah diambil\' OR c.status = \'verifikasi pembayaran sukses\')';

	$result = $db->db_query($sql);
	$num_rows = $db->db_num_rows($result);
	$jumlah_sewa = 0;

	if ($num_rows > 0) {
		while ($sewa = $db -> db_fetch_array($result)) {
			$jumlah_sewa = $jumlah_sewa + $sewa['quantity'];
		}
		$sisa_stok = $stok - $jumlah_sewa;

		 return $sisa_stok;
	} else {
		return $stok;
	}
}

?>