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

$date_plan = $_POST['date_plan'];
$mesin = $_POST['mesin'];
$product = $_POST['product'];
$material = $_POST['material'];
$qty = $_POST['qty'];
$prioritas = $_POST['prioritas'];
$poor = $_POST['poor'];
$poor2 = $_POST['poor2'];

if ($date_plan == '') {
    $data['error']['date_plan'] = 'Tanggal Plan tidak boleh kosong';
}
if ($mesin == '') {
    $data['error']['mesin'] = 'Mesin Tidak Boleh Kosong';
}
if ($product == '') {
    $data['error']['product'] = 'Product Tidak Boleh Kosong';
}
if ($material == '') {
    $data['error']['material'] = 'Material Tidak Boleh Kosong';
}
if ($qty == '') {
    $data['error']['qty'] = 'Quantity Tidak Boleh Kosong';
}
if ($prioritas == '') {
    $data['error']['prioritas'] = 'Prioritas Tidak Boleh Kosong';
}
if ($poor2 == '') {
    $por = $poor;
} else {
	$por = $poor2;
}

function weekOfYear($date) {
    $weekOfYear = intval(date("W", $date));
    if (date('n', $date) == "1" && $weekOfYear > 51) {
        // It's the last week of the previos year.
        return 0;
    }
    else if (date('n', $date) == "12" && $weekOfYear == 1) {
        // It's the first week of the next year.
        return 53;
    }
    else {
        // It's a "normal" week.
        return $weekOfYear;
    }
}

$week = weekOfYear(strtotime($date_plan));

if (empty($data['error'])) {
    
		$query = "insert INTO plan SET
										DatePlan = '$date_plan',
										Week = '$week',
										MesinID = '$mesin',
										ProductID = '$product',
										MaterialID = '$material',
										QtyPlan = '$qty',
										Prioritas = '$prioritas',
										POOR = '$por'
										";

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

echo json_encode($data);


?>

