<?php
include "../koneksi.php";

$po_id = $_POST['po_id'];
$product_id = $_POST['product_id'];
$qty_po = $_POST['qty_po'];
$sample = $_POST['sample'];

if ($po_id == '') {
    $data['error']['po_id'] = 'Data PO tidak boleh kosong';
}
if ($product_id == '') {
    $data['error']['product_id'] = 'Data Product Tidak Boleh Kosong';
}
if ($qty_po == '') {
    $data['error']['qty_po'] = 'Quantity PO Tidak Boleh Kosong';
}

if ($sample == '1') {
	$balance_po = $qty_po;
} else {
$data2 = mysqli_query ($koneksi, " select QtyUnit from product WHERE ProductID=$product_id");
$row = mysqli_fetch_array ($data2);

$balance_po = floor($qty_po/$row['QtyUnit']);
}

if (empty($data['error'])) {
    
		$query = "insert INTO detail_po SET
										PO_ID = '$po_id',
										ProductID = '$product_id',
										QtyPO = '$qty_po',
										BalancePO = '$balance_po'
										";

		mysqli_query($koneksi, $query)
		or die ("Gagal Perintah SQL".mysql_error());
		
    $data['hasil'] = 'sukses';
    
} else {
    
    $data['hasil'] = 'gagal';
}

echo json_encode($data);
header('Location:rincianpo.php?id='.$po_id);

?>

