<?php
include('db_con.php');
 
  
  if (isset($_SESSION['user_id'])) {
    $user_type = $_SESSION['user_type'];
    $user_id = $_SESSION['user_id'];
    $user_name = $_SESSION['user_name'];
    $branch_id = $_SESSION['branch_id'];
    $branch_name = $_SESSION['branch_name'];
    $store_id = $_SESSION['store_id'];
    $store_name = $_SESSION['store_name'];
}else{
  header('Location: login');
  die();
}
if(isset($_POST['btn_action']))
{
 
  
  if($_POST['btn_action'] == 'add')
  {
    $dated = date('Y-m-d');
    $type_id = $_POST['type_id'];
    $item_cost = $_POST['item_cost'];
    $item_name = $_POST['item_name'];

    $sql = "select * from items where type_id='".$type_id."' and item_name = '".$item_name."'";
    $statement = $connect->prepare($sql);
    $statement->execute();
    $count = $statement->rowCount();
    if($count > 0)
    {
      return false;
    }else {
    
      $image = '';
    $query = "
    INSERT INTO items (dated,item_name,type_id,dimension,uom,min_qty,max_qty,location,ctn,item_cost, lp_rate,retail_price,list_price,op_stock,item_image,user_id,status)
        VALUES (:dated, :item_name,:type_id, :dimension,
           :uom, :min_qty, :max_qty, :location,:ctn,:item_cost, :lp_rate, :retail_price, :list_price,:op_stock, :item_image, :user_id, :status)
    ";  
    $statement = $connect->prepare($query);
    $statement->execute(
      array(
          
          ':dated'             =>  $dated,          
          ':item_name'          =>  trim($_POST["item_name"]),          
          ':type_id'       =>  $type_id,
          ':dimension'       =>  0,        
          ':uom'       =>  11,        
          ':min_qty'       =>  0,
          ':max_qty'       =>  0,
          ':location'       =>  '',
          ':ctn'       =>  1,
          ':item_cost'       =>  $item_cost,
          ':lp_rate'       =>  $item_cost,          
          ':retail_price'       =>  0,
          ':list_price'       =>  0,        
          ':op_stock'       =>  0,        
          ':item_image'  => $image,
          ':user_id'       =>  $user_id,
          ':status'       =>  'Active'
          )
    );
   
   
        $statement = $connect->query("SELECT LAST_INSERT_ID()");
        $item_id = $statement->fetchColumn();
        
    $new_item_no = $type_id .$item_id;
    $sql = "update items set item_no = '".$new_item_no."' where item_id = '".$item_id."'";
    $statement = $connect->prepare($sql);
    $statement->execute();
    
   
    $output['item_id'] = $item_id;
    }
  }
  echo json_encode($output);
}
 
?>