<?php
//open connection
include 'db_Connection.php';

/* start the session */
session_start();

if(!isset($_SESSION['username'])){ // Jika session username ada berarti dia sudah login   
  echo "<script>  window.location.href='../../login.php';</script>"; // Kita Redirect ke halaman welcome.php
}

if (isset($_SESSION['username'])) {       
    $data = mysqli_query($conn,"SELECT * FROM user WHERE status IN ('I','D') AND user_id ='".$_SESSION['username']."'");
    $cek_usrBaned = mysqli_num_rows($data);

    if($cek_usrBaned > 0){ // found the banned user       
      session_destroy(); 
      echo "<script>  window.location.href='../../login.php';</script>";
    }

} else {    

}

//update timestamp
//$cek_uid =$_SESSION['username'];  
$timezone = "Asia/Jakarta";
date_default_timezone_set($timezone);  
$datetime = new DateTime(date("Y-m-d H:i:s"));
$str_datetime = $datetime->format("Y-m-d H:i:s");
//$sql  = ("UPDATE usr_profile SET last_activity = '$str_datetime' WHERE user_id = '$cek_uid'");
//$query = mysqli_query($conn, $sql);

//get parameters
$data_post = $_POST['data_post'];

//get array value
$json = $data_post;
$array = json_decode($json, true);
$PO_Number = $array['PO_Number'];
$Principal_Code = $array['Principal_Code'];
$Principal_Name = $array['Principal_Name'];
$PO_Date = $array['PO_Date'];
$Payment_Type = $array['Payment_Type'];
$Amount = $array['Amount'];
$Percentage = $array['Percentage'];
//po_detail
$rowcount = $array['rowcount'];
$Product_Code = $array['Product_Code'];
$Product_Name = $array['Product_Name'];
$Product_Type = $array['Product_Type'];
$Description = $array['Description'];
$Price = $array['Price'];
$Quantity = $array['Quantity'];
$Total = $array['Total'];
$Subtotal = $array['Subtotal'];


// menghitung jumlah no PO yang ditemukan    
    $data = "SELECT * FROM purchase_order WHERE PO_Number ='$PO_Number'";
    $result = mysqli_query($conn, $data);
    $cek = mysqli_num_rows($result);  

//if($Payment_Type == "Cash"){
	//$Nominal_Amount = $array['Subtotal'];
//else{
	//$Nominal_Amount = $array['Amount'];	
	//}
//}	
    
if($cek > 0){	        	        
        $duplicate = "duplicate";    
        $res = array("result" => $duplicate);
        echo json_encode($res);
}
elseif($Payment_Type == "Cash"){
	
		for($i = 1; $i <= $rowcount; $i++){
			$sql  = ("INSERT INTO po_detail (PO_Number, Product_Code, Product_Name, Product_Type, Description, Purchase_Price, Quantity, Total)
			VALUES ('$PO_Number','$Product_Code[$i]','$Product_Name[$i]','$Product_Type[$i]','$Description[$i]','$Price[$i]','$Quantity[$i]','$Total[$i]')");
			$query = mysqli_query($conn, $sql);
			
			// cek stock barang    
			$data = "SELECT * FROM stock WHERE Product_Code ='$Product_Code[$i]'";
			$result = mysqli_query($conn, $data);
			$cek = mysqli_num_rows($result); 

			if($cek > 0){
				$sql  = ("UPDATE stock SET Quantity = Quantity + '$Quantity[$i]' WHERE Product_Code = '$Product_Code[$i]'");
				$query = mysqli_query($conn, $sql);
			}
			else{
				$sql  = ("INSERT INTO stock (Product_Code, Quantity)
				VALUES ('$Product_Code[$i]','$Quantity[$i]')");
				$query = mysqli_query($conn, $sql);
			}			
		}
			
		
		$sql  = ("INSERT INTO purchase_order (PO_Number, Principal_Code, Principal_Name, PO_Date, Payment_Type, Nominal_Amount, Percentage_Value, Status, PO_Amount, User_Id_Created, Date_Created)
		VALUES ('$PO_Number','$Principal_Code','$Principal_Name','$PO_Date','$Payment_Type','$Subtotal','$Percentage','Outstanding','$Subtotal','UserName','$str_datetime')");
        $query = mysqli_query($conn, $sql);

        $sukses = "sukses";    
        $res = array("result" => $sukses);
        echo json_encode($res);    
		}
	elseif($Payment_Type == "Downpayment" AND $Percentage == 0){
		for($i = 1; $i <= $rowcount; $i++){
			$sql  = ("INSERT INTO po_detail (PO_Number, Product_Code, Product_Name, Product_Type, Description, Purchase_Price, Quantity, Total)
			VALUES ('$PO_Number','$Product_Code[$i]','$Product_Name[$i]','$Product_Type[$i]','$Description[$i]','$Price[$i]','$Quantity[$i]','$Total[$i]')");
			$query = mysqli_query($conn, $sql);
			
			// cek stock barang    
			$data = "SELECT * FROM stock WHERE Product_Code ='$Product_Code[$i]'";
			$result = mysqli_query($conn, $data);
			$cek = mysqli_num_rows($result); 

			if($cek > 0){
				$sql  = ("UPDATE stock SET Quantity = Quantity + '$Quantity[$i]' WHERE Product_Code = '$Product_Code[$i]'");
				$query = mysqli_query($conn, $sql);
			}
			else{
				$sql  = ("INSERT INTO stock (Product_Code, Quantity)
				VALUES ('$Product_Code[$i]','$Quantity[$i]')");
				$query = mysqli_query($conn, $sql);
			}			
		}
			
		
		$sql  = ("INSERT INTO purchase_order (PO_Number, Principal_Code, Principal_Name, PO_Date, Payment_Type, Nominal_Amount, Percentage_Value, Status, PO_Amount, User_Id_Created, Date_Created)
		VALUES ('$PO_Number','$Principal_Code','$Principal_Name','$PO_Date','$Payment_Type','$Amount','$Percentage','Outstanding','$Subtotal','UserName','$str_datetime')");
        $query = mysqli_query($conn, $sql);

        $sukses = "sukses";    
        $res = array("result" => $sukses);
        echo json_encode($res);
		}
	else{
		for($i = 1; $i <= $rowcount; $i++){
			$sql  = ("INSERT INTO po_detail (PO_Number, Product_Code, Product_Name, Product_Type, Description, Purchase_Price, Quantity, Total)
			VALUES ('$PO_Number','$Product_Code[$i]','$Product_Name[$i]','$Product_Type[$i]','$Description[$i]','$Price[$i]','$Quantity[$i]','$Total[$i]')");
			$query = mysqli_query($conn, $sql);
			
			// cek stock barang    
			$data = "SELECT * FROM stock WHERE Product_Code ='$Product_Code[$i]'";
			$result = mysqli_query($conn, $data);
			$cek = mysqli_num_rows($result); 

			if($cek > 0){
				$sql  = ("UPDATE stock SET Quantity = Quantity + '$Quantity[$i]' WHERE Product_Code = '$Product_Code[$i]'");
				$query = mysqli_query($conn, $sql);
			}
			else{
				$sql  = ("INSERT INTO stock (Product_Code, Quantity)
				VALUES ('$Product_Code[$i]','$Quantity[$i]')");
				$query = mysqli_query($conn, $sql);
			}			
		}
			
		$AmountVal = ($Percentage / 100) * $Subtotal;
		$sql  = ("INSERT INTO purchase_order (PO_Number, Principal_Code, Principal_Name, PO_Date, Payment_Type, Nominal_Amount, Percentage_Value, Status, PO_Amount, User_Id_Created, Date_Created)
		VALUES ('$PO_Number','$Principal_Code','$Principal_Name','$PO_Date','$Payment_Type','$AmountVal','$Percentage','Outstanding','$Subtotal','UserName','$str_datetime')");
        $query = mysqli_query($conn, $sql);

        $sukses = "sukses";    
        $res = array("result" => $sukses);
        echo json_encode($res);		
	}
?>
