<?php
//user_action.php
include('db_con.php');
include('function.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'];
    $user_acc_no = $_SESSION['user_acc_no'];
}else{
	header('Location: login');
	die();
}
if(isset($_POST['btn_action']))
{
	$dated = date('Y-m-d');

	if($_POST['btn_action']=='save')
	{
    $date_time = date('Y-m-d h:i:s');
    $statement = $connect->prepare("
      INSERT INTO cashbook 
        (dated,date_time,user_id)
        VALUES (:dated, :date_time, :user_id)
    ");
    $statement->execute(
      array(       
       
          ':dated'             =>  $_POST['dated'],
          ':date_time'          =>  $date_time,       
          ':user_id'       =>  $user_id
          
      )
    );
     $statement = $connect->query("SELECT LAST_INSERT_ID()");
      $cid = $statement->fetchColumn();
      for($count=0; $count<$_POST["total_item"]; $count++)
      {
        $statement = $connect->prepare("
          INSERT INTO cashbook_detail 
          (cid,acc_no,narr,amount)
          VALUES(:cid, :acc_no, :narr, :amount)
        ");
        $statement->execute(
          array(
            ':cid'               =>  $cid,
            ':acc_no'              =>  trim($_POST["acc_no"][$count]),
            ':narr'              =>  trim($_POST["narr"][$count]),
            ':amount'          =>  trim($_POST["amount"][$count]),
         
           
            
          )
        );
      }

      $sql = "select sum(amount) from cashbook_detail where cid = '".$cid."'";
      $statement = $connect->query($sql);
      $amount = $statement->fetchColumn();
   
		$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 CB';
    $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'       =>  '',          
          ':user_id'       =>  $user_id,
           ':status'       =>  'Active'
        
      )
    );
     $statement = $connect->query("select max(jv_id) from jv");
      $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["amount"][$count]),  
            ':credit'          =>  0
            
          )
        );
      }

      $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'              => $user_acc_no,
            ':narr'              =>  'Cash Book Expense',
            ':debit'          =>  0,  
            ':credit'          =>  $amount
            
          )
        );

        echo 'success';
     
	}

	if($_POST['btn_action']=='update')
	{
    $cid = $_POST['cid'];
     $sql = "DELETE from cashbook_detail where cid = '".$cid."'";
     $statement = $connect->query($sql);
      for($count=0; $count<$_POST["total_item"]; $count++)
      {
        $statement = $connect->prepare("
          INSERT INTO cashbook_detail 
          (cid,acc_no,narr,amount)
          VALUES(:cid, :acc_no, :narr, :amount)
        ");
        $statement->execute(
          array(
            ':cid'               =>  $cid,
            ':acc_no'              =>  trim($_POST["acc_no"][$count]),
            ':narr'              =>  trim($_POST["narr"][$count]),
            ':amount'          =>  trim($_POST["amount"][$count]),
         
           
            
          )
        );
      }

      $sql = "select sum(amount) from cashbook_detail where cid = '".$cid."'";
      $statement = $connect->query($sql);
      $amount = $statement->fetchColumn();
   
   $sql = "select jv_id from jv where source = 'GL CB' and ref_id = '".$cid."'";
   $statement = $connect->query($sql);
   $jv_id = $statement->fetchColumn();

    $source = 'GL CB';
    
    $sql = "DELETE from jv_detail where jv_id = '".$jv_id."'";
    $statement = $connect->query($sql);
      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["amount"][$count]),  
            ':credit'          =>  0
            
          )
        );
      }

      $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'              => $user_acc_no,
            ':narr'              =>  'CashBook Expense',
            ':debit'          =>  0,  
            ':credit'          =>  $amount
            
          )
        );
      $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'       => 'Cashbook Expense',                      
          ':user_id'       =>  $user_id,
          ':status'       =>  'Active',
         
          ':jv_id'               =>  $jv_id
        )
      );

      $statement = $connect->prepare("
        UPDATE cashbook 
        SET 
        dated = :dated 
       
        WHERE cid = :cid
      ");
      
      $statement->execute(
        array(
          ':dated'             =>  $_POST["dated"],
       
          ':cid'               =>  $cid
        )
      );
     
   echo 'success';
	}
	
	if($_POST['btn_action'] == 'delete')
	{
		$cid = $_POST['cid'];
      $sql = "DELETE FROM cashbook WHERE cid = '".$cid."'";
            $statement = $connect->prepare($sql);
            $statement->execute(); 
        
        $sql = "DELETE FROM cashbook_detail WHERE cid = '".$cid."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); 
            $sql = "select jv_id from jv where source = 'GL CB' and ref_id = '".$cid."'";
            $statement = $connect->query($sql);
            $jv_id = $statement->fetchColumn();

		$sql = "DELETE FROM jv WHERE jv_id = '".$jv_id."'";
            $statement = $connect->prepare($sql);
            $statement->execute(); 
        
        $sql = "DELETE FROM jv_detail WHERE jv_id = '".$jv_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute();
		
	}
}
?>