

<?php 


include('../conn.php');


if(isset($_POST['submit'])){
 
    $item_name=addslashes($_POST['item_name']);
    $item_names=addslashes($_POST['item_names']);
     $price=$_POST['price'];
    $unit=$_POST['unit'];
    $units=$_POST['units'];
    $unit_value=$_POST['unit_value'];
     $quantity=$_POST['quantity'];
     $datein=$_POST['datein'];
     $location=$_POST['location'];
     $by=$_POST['by'];
     $receipt_id=$_POST['receipt_id'];
     $item_id_new=$_POST['item_id_new'];
     $item_id=$item_name;
    
//setting new item when item does not exist
     if ($item_name=='nan') {
        $item_name=$item_names;
        $item_id=$item_id_new;
     }
     if ($unit=='nan') {
        $unit=$units;
     }


 



     $sql10 = "SELECT `item_name` FROM `inventory_cat` WHERE `item_id`='".$item_id."'";
     $result10 = $conn->query($sql10);
     $row10=mysqli_fetch_array($result10);
     
     if($row10 >= 1) {
        $item_name=$row10['item_name'];
     }

  
      





  
     
     $sql = "SELECT `item_id` FROM `inventory_cat` WHERE `item_id`='".$item_id."'";
     $result = $conn->query($sql);
     if($result->num_rows >= 1) {
      
     } else {
		// Image db insert sql
        $sql11 = "INSERT INTO `inventory_cat`(`item_id`, `item_name`, `by`, `date`) VALUES ('$item_id','$item_name','$by','$datein')";
		if(mysqli_query($conn,$sql11)){
		}
		else{
		  echo 'Error: '.mysqli_error($conn);
		}
 
     }

     

     $sql0 = "SELECT `unit` FROM `inventory_unit` WHERE `unit`='".$unit."'";
     $result0 = $conn->query($sql0);
     if($result0->num_rows >= 1) {
      
     } else {
		// Image db insert sql
        $sql1 = "INSERT INTO `inventory_unit`( `unit`, `date`, `by`) VALUES ('$unit','$datein','$by')";
		if(mysqli_query($conn,$sql1)){
		}
		else{
		  echo 'Error: '.mysqli_error($conn);
		}
 
     }

     $sql2 = "INSERT INTO `inventory`( `item_name`,`item_id`,`price`, `unit`, `unit_value`, `quantity`, `datein`, `item_location`, `receipt_id`,`by`) VALUES ('$item_name','$item_id','$price','$unit','$unit_value','$quantity','$datein','$location','$receipt_id','$by')";
     if(mysqli_query($conn,$sql2)){

   echo"<script>alert('Item added succefully'); window.location.href='../inventory'</script>";
     }
     else{
       echo 'Error: '.mysqli_error($conn);
     }



}

?>
 