<?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']))
{

	$dated = date('Y-m-d');
	
	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(); 
            $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(); 
            $dateup = $statement->fetchColumn();
             $sql = "SELECT date_sub(curdate(),interval '".$datedown."' day)";
              $statement = $connect->prepare($sql);
              $statement->execute(); 
              $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(pay_id),0) FROM pay_voucher ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $pay_id = $statement->fetchColumn();
            settype($pay_id, "integer");
            if($pay_id < 200000){$pay_id = 200000;}
            $pay_id++;
    
   
    $statement = $connect->prepare("
      INSERT INTO pay_voucher
        (pay_id,dated,v_id,branch_id,paid_by,acc_no,chq_no,chq_date,inv_no,description,amount,
          pay_status,cl_date,user_id,status)
        VALUES (:pay_id, :dated,:v_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(
          ':pay_id'             =>  $pay_id,
          ':dated'             =>  $_POST['dated'],
          ':v_id'             =>  trim($_POST["v_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 = $pay_id;
    
      
   
       $sql = "SELECT IFNULL(MAX(jv_no),0) FROM jv WHERE source = 'AP 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 = 'AP Pay';
           $description = 'AP Payment ' .$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'          => 0,
            ':credit'           =>   $_POST['amount'][$count]
                        
          )
        );
       $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['v_id'][$count],
            
            ':debit'          =>  $_POST['amount'][$count],
            ':credit'           =>  0
            
          
               
          )
        );
    }
  ////// End JV//////
    echo 'success';
	}

	if($_POST['btn_action']=='update')
	{
		$pay_id = $_POST['pay_id'];
		$ref_id = $pay_id;
       
   $statement = $connect->prepare("
        UPDATE pay_voucher
        SET dated = :dated,
        v_id = :v_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 pay_id = :pay_id
      ");
      $statement->execute(
        array(
          ':dated'             =>  $_POST["dated"],
          ':v_id'          =>  trim($_POST["v_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,
          ':pay_id'           =>      $pay_id
        )
      );
   /////Creat JV////////
   
    
      
   $sql = "SELECT jv_id FROM jv WHERE ref_id = '".$ref_id."' and source ='AP 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 = 'AP Pay';
           $description = 'AP 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'          =>  0,
            ':credit'           =>  $_POST['amount']
         
               
          )
        );
       $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['v_id'],
            
            ':debit'          =>  $_POST['amount'],
            ':credit'           =>  0
            
          
               
          )
        );
         $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')
	{
		$pay_id = $_POST['pay_id'];
		$sql = "DELETE FROM pay_voucher WHERE pay_id = '".$pay_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 = 'AP Pay' and ref_id = '".$pay_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 Payment Voucher No. ' .$pay_id;
		}
		
	}
}
?>