<?php
//user_action.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']))
{
	$dated = date('Y-m-d');

	if($_POST['btn_action']=='save')
	{
		$sql = "SELECT MAX(jv_no) FROM jv WHERE branch_id = '".$branch_id."' and source ='GL Jv'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $jv_no = $statement->fetchColumn();
            settype($jv_no, "integer");
            $jv_no++;

    $source = 'GL jv';
    $statement = $connect->prepare("
      INSERT INTO jv 
        (jv_no,branch_id,dated,source,ref_id,description,user_id,status)
        VALUES (:jv_no, :branch_id, :dated, :source, :ref_id, :description, :user_id, :status)
    ");
    $statement->execute(
      array(
          ':jv_no'             =>  $jv_no,
          ':branch_id'             =>  $branch_id,
          ':dated'             =>  $_POST['dated'],
          ':source'          =>  $source,
          ':ref_id'          =>  $jv_no,
          ':description'       =>  $_POST["description"],
              
          ':user_id'       =>  $user_id,
           ':status'       =>  'Active'
      )
    );
     $statement = $connect->query("SELECT LAST_INSERT_ID()");
      $jv_id = $statement->fetchColumn();
      for($count=0; $count<$_POST["total_item"]; $count++)
      {
        $statement = $connect->prepare("
          INSERT INTO jv_detail 
          (jv_id,acc_no,narr,debit,credit)
          VALUES(:jv_id, :acc_no,  :narr, :debit, :credit)
        ");
        $statement->execute(
          array(
            ':jv_id'               =>  $jv_id,
            ':acc_no'              =>  trim($_POST["acc_no"][$count]),
            ':narr'              =>  trim($_POST["narr"][$count]),
            ':debit'          =>  trim($_POST["debit"][$count]),
            ':credit'           =>  trim($_POST["credit"][$count]),
           
            
          )
        );
      }
     
	}

	if($_POST['btn_action']=='update')
	{

      $jv_id = $_POST['jv_id'];
      $source = 'GL jv';
      
      $statement = $connect->prepare("
                DELETE FROM jv_detail WHERE jv_id = :jv_id
            ");
            $statement->execute(
                array(
                    ':jv_id'       =>      $jv_id
                    
                )
            );
      
      for($count=0; $count<$_POST["total_item"]; $count++)
      {
        
        $statement = $connect->prepare("
          INSERT INTO jv_detail 
          (jv_id,acc_no,narr,debit,credit) 
          VALUES (:jv_id, :acc_no, :narr, :debit, :credit)
        ");
        $statement->execute(
          array(
            ':jv_id'                 =>  $jv_id,
            ':acc_no'                =>  trim($_POST["acc_no"][$count]),
            ':narr'                =>  trim($_POST["narr"][$count]),
            ':debit'          =>  trim($_POST["debit"][$count]),
            ':credit'            =>  trim($_POST["credit"][$count])
           
                  
          )
        );
        //$result = $statement->fetchAll();
      }
      
      
      $statement = $connect->prepare("
        UPDATE jv 
        SET 
        dated = :dated, 
         branch_id = :branch_id, 
        source = :source,
        ref_id = :ref_id, 
        description = :description,
             
        user_id = :user_id,
        status = :status
        WHERE jv_id = :jv_id
      ");
      
      $statement->execute(
        array(
          ':dated'             =>  $_POST["dated"],
          ':branch_id'          =>  $branch_id,
          ':source'          =>  $source,
          ':ref_id'          =>  $jv_id,
          ':description'       =>  $_POST["description"],                      
          ':user_id'       =>  $user_id,
          ':status'       =>  'Active',
          ':jv_id'               =>  $jv_id
        )
      );
     
   
	}
	
	if($_POST['btn_action'] == 'delete')
	{
		$jv_id = $_POST['jv_id'];
		$sql = "DELETE FROM jv WHERE jv_id = '".$jv_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
        
        $sql = "DELETE FROM jv_detail WHERE jv_id = '".$jv_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no 
		
		
            
		
		//$result = $statement->fetchAll();	
		
	}
}
?>