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

$id = $_POST['plan_id'];
$date_plan = $_POST['date_plan'];
$mesin = $_POST['mesin'];
$product = $_POST['product'];
$material = $_POST['material'];
$qty = $_POST['qty'];
$prioritas = $_POST['prioritas'];
$poor = $_POST['poor'];
		if ($date_plan == '') {
			$data['error']['edit_date_plan'] = 'Tanggal Planning  tidak boleh kosong';
		}
		if ($mesin == '') {
			$data['error']['edit_mesin'] = 'Data mesin  tidak boleh kosong';
		}
		if ($product == '') {
			$data['error']['edit_product'] = 'Data Product Tidak Boleh Kosong';
		}
		if ($material == '') {
			$data['error']['edit_material'] = 'Data Material Tidak Boleh Kosong';
		}
		if ($qty == '') {
			$data['error']['edit_qty'] = 'Quantity Tidak Boleh Kosong';
		}
		if ($prioritas == '') {
			$data['error']['edit_prioritas'] = 'Priritas Tidak Boleh Kosong';
		}

		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 = "UPDATE plan SET
											DatePlan = '$date_plan',
											Week = '$week',
											MesinID = '$mesin',
											ProductID = '$product',
											MaterialID = '$material',
											QtyPlan = '$qty',
											Prioritas = '$prioritas',
											POOR = '$poor'
											WHERE PlanID = '$id'
											";

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

?>

