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

$plan_id = $_POST['plan_id'];
$kode = $_POST['kode'];
$nolot = $_POST['nolot'];
$nick = $_POST['nick'];

$datasloc = mysqli_query ($koneksi, "SELECT product.SLoc
                              FROM plan
                              INNER JOIN product
                              ON plan.ProductID=product.ProductID
                                WHERE PlanID ='$plan_id'");
$row2= mysqli_fetch_array($datasloc);

if ($kode == '') {
    $data['error']['kode'] = 'Kode Product SSTPJ Tidak Boleh Kosong';
}
if ($nolot == '') {
    $data['error']['nolot'] = 'Nomor Lot STPJ Tidak Boleh Kosong';
}

$data1 = mysqli_query ($koneksi, " select QtyPlan from plan 
							where PlanID = $plan_id");
$row = mysqli_fetch_array ($data1);

if (empty($data['error'])) {
    	if($row2['SLoc']==='BOX2') {
    		$query = "insert INTO stpj SET
										PlanID = '$plan_id',
										Kode = '$kode',
										NoLot = '$nolot',
										Nick = '$nick',
										Qty = 15
										";

			mysqli_query($koneksi, $query)
			or die ("Gagal Perintah SQL".mysql_error());
    	} else {
		$query = "insert INTO stpj SET
										PlanID = '$plan_id',
										Kode = '$kode',
										NoLot = '$nolot',
										Nick = '$nick'
										";

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

echo json_encode($data);
header('Location:stpj.php?id='.$plan_id.'&qty='.$row['QtyPlan']);

?>

