<?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'];
    $user_acc_no = $_SESSION['user_acc_no'];
}else{
	header('Location: login');
	die();
}
if(isset($_POST['btn_action']))
{
	if($_POST['btn_action']=='save')
	{
		$new_dated = $_POST['dated'];
             if($user_type=='user')
             {
            $sql = "select app_value from app_options where app_name='datedown' and user_id = '".$user_id."'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $datedown = $statement->fetchColumn();
            $sql = "select app_value from app_options where app_name='dateup' and user_id = '".$user_id."'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $dateup = $statement->fetchColumn();
             $sql = "SELECT date_sub(curdate(),interval '".$datedown."' day)";
              $statement = $connect->prepare($sql);
              $statement->execute(); // no need to add `$sql` here, you can take that out
              $pre_dated = $statement->fetchColumn();
    if($new_dated < $pre_dated){echo 'Invoice Can not be save in Previous date pre_dated is : '.$pre_dated .' Post Date : '.$new_dated ;return false;}
     $above_dated = Date('Y-m-d', strtotime("+".$dateup .' days'));
    if($new_dated > $above_dated){echo 'Invoice Can not be save in Above date Above dated is :'.$above_dated .' New Date is '. $new_dated;return false;}
        }
    for($count=0; $count<$_POST["total_item"]; $count++)
      {
       
     
    $sql = "SELECT IFNULL(MAX(rec_id),0) FROM rec_voucher ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $rec_id = $statement->fetchColumn();
            settype($rec_id, "integer");
            if($rec_id < 100000){$rec_id = 100000;}
            $rec_id++;
    
   
    $statement = $connect->prepare("
      INSERT INTO rec_voucher
        (rec_id,dated,cust_id,branch_id,paid_by,acc_no,chq_no,chq_date,inv_no,description,amount,
          pay_status,cl_date,user_id,status)
        VALUES (:rec_id, :dated,:cust_id,:branch_id,:paid_by,:acc_no, :chq_no,:chq_date,:inv_no,:description,
          :amount, :pay_status, :cl_date, :user_id, :status)
    ");
    $statement->execute(
      array(
          ':rec_id'             =>  $rec_id,
          ':dated'             =>  $_POST['dated'],
          ':cust_id'             =>  trim($_POST["cust_id"][$count]),
          ':branch_id'             =>  $branch_id,
          ':paid_by'          =>  'Cash',
          ':acc_no'          =>  $user_acc_no,
          
          ':chq_no'       =>  '',
          ':chq_date'          =>  $_POST["dated"],
           
          ':inv_no'          =>  '',          
          ':description'       =>  $_POST["description"][$count],
          ':amount'       =>  trim($_POST["amount"][$count]),          
          ':user_id'       =>  $user_id,
          ':pay_status'       =>  'Cleared',
          ':cl_date'       =>  $_POST["dated"],
          ':status'       =>  'Active'
      )
    );
   /////Creat JV////////
    $ref_id = $rec_id;
    
      
   
       $sql = "SELECT IFNULL(MAX(jv_no),0) FROM jv WHERE source = 'AR Pay' ";
            $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 = 'AR Pay';
           $description = "AR Payment Cash" .$ref_id ." - " .$_POST['description'][$count];
            $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'          =>  $ref_id,
          ':description'       =>  $description,
               
          ':user_id'       =>  $user_id,
          ':status'       => 'Active'
      )
    );
     $statement = $connect->query("select max(jv_id) from jv");
      $jv_id = $statement->fetchColumn();
    
       $statement = $connect->prepare("
          INSERT INTO jv_detail 
          (jv_id,acc_no,debit,credit)
          VALUES(:jv_id, :acc_no, :debit, :credit)
        ");
        $statement->execute(
          array(
            ':jv_id'               =>  $jv_id,
            ':acc_no'              =>  $user_acc_no,
         
            ':debit'          =>  $_POST['amount'][$count],
            ':credit'           =>  0
         
               
          )
        );
       $statement = $connect->prepare("
          INSERT INTO jv_detail 
          (jv_id,acc_no,debit,credit)
          VALUES(:jv_id, :acc_no, :debit, :credit)
        ");
        $statement->execute(
          array(
            ':jv_id'               =>  $jv_id,
            ':acc_no'              =>  $_POST['cust_id'][$count],
            
            ':debit'          =>  0,
            ':credit'           =>  $_POST['amount'][$count]
            
          
               
          )
        );
  ////// End JV//////
        echo 'success';
  }
	}

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

		$rec_id = $_POST['rec_id'];
		$ref_id = $rec_id;

   $statement = $connect->prepare("
        UPDATE rec_voucher
        SET dated = :dated,
        cust_id = :cust_id,
        branch_id = :branch_id,
        paid_by = :paid_by,
        acc_no = :acc_no,
       
        chq_no = :chq_no,
        chq_date = :chq_date,
    
        inv_no = :inv_no,
        description = :description,
        amount = :amount,      
        pay_status = :pay_status,
        cl_date = :cl_date,
        user_id = :user_id         
        WHERE rec_id = :rec_id
      ");
      $statement->execute(
        array(
          ':dated'             =>  $_POST["dated"],
          ':cust_id'          =>  trim($_POST["cust_id"]),
          ':branch_id'          =>  $branch_id,
          ':paid_by'          =>  'Cash',          
          ':acc_no'             =>  $user_acc_no,
          
          ':chq_no'             =>  '',
          ':chq_date'             =>  $_POST["dated"],
         
          ':inv_no'             =>  '',
          ':description'             =>  $_POST["description"],
          ':amount'             =>  trim($_POST["amount"]),         
          ':pay_status'             =>  'Cleared',
          ':cl_date'             =>  $_POST["dated"],
          ':user_id'             =>  $user_id,
          ':rec_id'           =>      $rec_id
        )
      );
   /////Creat JV////////
   
    
      
   $sql = "SELECT jv_id FROM jv WHERE ref_id = '".$ref_id."' and source ='AR Pay'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $jv_id = $statement->fetchColumn();
             $sql = "SELECT jv_no FROM jv WHERE jv_id = '".$jv_id."'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $jv_no = $statement->fetchColumn();
         
           /////Creat JV////////
   
    
               $sql = "SELECT user_id FROM jv WHERE jv_id = '".$jv_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $user_id = $statement->fetchColumn();
               $sql = "SELECT user_acc_no FROM user_detail WHERE user_id = '".$user_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $user_acc_no = $statement->fetchColumn();   
      
            $source = 'AR Pay';
           $description = 'AR Cash Payment ' .$ref_id .' - ' .$_POST['description'] ;
          
$statement = $connect->prepare("delete from jv_detail where jv_id = '".$jv_id."'");
$statement->execute();
    
       $statement = $connect->prepare("
          INSERT INTO jv_detail 
          (jv_id,acc_no,debit,credit)
          VALUES(:jv_id, :acc_no, :debit, :credit)
        ");
        $statement->execute(
          array(
            ':jv_id'               =>  $jv_id,
            ':acc_no'              =>  $user_acc_no,
         
            ':debit'          =>  $_POST['amount'],
            ':credit'           =>  0
         
               
          )
        );
       $statement = $connect->prepare("
          INSERT INTO jv_detail 
          (jv_id,acc_no,debit,credit)
          VALUES(:jv_id, :acc_no, :debit, :credit)
        ");
        $statement->execute(
          array(
            ':jv_id'               =>  $jv_id,
            ':acc_no'              =>  $_POST['cust_id'],
            
            ':debit'          =>  0,
            ':credit'           =>  $_POST['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'          =>  $ref_id,
          ':description'       =>  $description,
                      
          ':user_id'       =>  $user_id,
          ':status'       =>  'Active',
          ':jv_id'               =>  $jv_id
        )
      );
  ////// End JV//////
      echo 'success';
  
	}
	
	if($_POST['btn_action'] == 'delete')
	{
    $rec_id = $_POST['rec_id'];

		$sql = "DELETE FROM rec_voucher WHERE rec_id = '".$rec_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
       
		
		$sql = "SELECT jv_id FROM jv WHERE source = 'AR Pay' and ref_id = '".$rec_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $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();
            
		
		$result = $statement->fetchAll();	
		if(isset($result))
		{
			echo 'Deleted Receipt Voucher No. ' .$rec_id;
		}
		
	}
}
?>