<?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'];
}else{
	header('Location: login');
	die();
}
if(isset($_POST['btn_action']))
{
	if($_POST['btn_action'] == 'Add')
	{
		$dated = date('Y-m-d');
		 $sql = "SELECT IFNULL(max(v_id),0) FROM vendors ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $v_id = $statement->fetchColumn();
            if($v_id < 2016000)
            {
            	$v_id = 2016000;
            }
            $v_id++;
             $acc_no = $v_id;
		
		$query = "
		INSERT INTO vendors (v_id,dated,name,address,city,country,phone,email,web,gst,stn,ntn,op_balance,user_id,status)
        VALUES (:v_id, :dated, :name, :address, :city, :country, :phone, :email, :web, :gst, :stn, :ntn, :op_balance,:user_id, :status)
    ";	
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
          ':v_id'             =>  $v_id,
          ':dated'             =>  $dated,
          ':name'          =>  trim($_POST["v_name"]),
          ':address'          =>  trim($_POST["address"]),          
          ':city'       =>  trim($_POST["city"]),
          ':country'       =>  'Pakistan',
          ':phone'       =>  trim($_POST["phone"]),
          ':email'       =>  trim($_POST["email"]),
          ':web'       =>  trim($_POST["web"]),
          ':gst'       =>  trim($_POST["gst"]),
          ':stn'       =>  trim($_POST["stn"]),
          ':ntn'       =>  trim($_POST["ntn"]),
          ':op_balance' => trim($_POST["op_balance"]),           
          ':user_id'       =>  $user_id,         
          ':status'       =>  trim($_POST["status"])
          )
		);
		$query = "
		INSERT INTO accounts (acc_id,acc_no,dated,acc_name,acc_type,acc_parent,
		op_balance,user_id,status)
        VALUES (:acc_id, :acc_no, :dated, :acc_name, :acc_type, :acc_parent,
        :op_balance, :user_id, :status)
    ";	
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				':acc_id'       =>  $v_id,
				':acc_no'       =>  $acc_no,
				':dated'       => $dated,
				':acc_name'       => $_POST["v_name"],
				':acc_type'       =>  'CREDITOR',
				':acc_parent'       =>  201,
				':op_balance'       =>  0,
				':user_id'       =>  $user_id,
				':status'       =>  'Active'
				
        
          )
		);
		
		
		//$result = $statement->fetchAll();
		$op_balance = $_POST['op_balance'];

		if($op_balance <> 0)
		{

			$source = 'Vendor';
            $description = 'Opening Balance For ' .$_POST['v_name'];
            $ref_id = $v_id;

             $sql = "select IFNULL(max(jv_no),0) from jv where source = 'Vendor'";
		$statement=$connect->prepare($sql);
		$statement->execute();
		$jv_no = $statement->fetchColumn();
		$jv_no++;
		$dated = date('2021-07-01');

            
            $statement = $connect->prepare("
              INSERT INTO jv 
                (jv_no,branch_id,dated,source,ref_id,description,user_id)
                VALUES (:jv_no, :branch_id, :dated, :source, :ref_id, :description, :user_id)
    ");
    $statement->execute(
      array(
          ':jv_no'             =>  $jv_no,
          ':branch_id'             =>  $branch_id,
          ':dated'             =>  $dated,
          ':source'          =>  $source,
          ':ref_id'          =>  $ref_id,
          ':description'       =>  $description,
       
          ':user_id'       =>  $user_id
      )
    );
     $statement = $connect->query("SELECT LAST_INSERT_ID()");
      $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'              =>  1000000,
            ':debit'          =>  $op_balance,
            ':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'              =>  $acc_no,
            ':debit'          =>  0,
            ':credit'           =>  $op_balance
                        
          )
        );
		}
		if(isset($result))
		{
			echo 'New Vendor Added';
		}
	}
	if($_POST['btn_action'] == 'fetch_single')
	{
		$query = "
		SELECT * FROM vendors WHERE v_id = :v_id
		";
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				':v_id'	=>	$_POST["v_id"]
			)
		);
		$result = $statement->fetchAll();
		foreach($result as $row)
		{
			
			
			$output['dated']             = $row['dated'];
           $output['name']          =  $row['name'];
          $output['address']          =  $row['address'];
          $output['city']       =  $row["city"];
         // $output['country']       =  $row["country"];
          $output['phone']       =  $row["phone"];
          $output['email']       =  $row["email"];
          $output['web']       =  $row["web"];             
          $output['gst']       =  $row["gst"];  
          $output['stn']       =  $row["stn"];
          $output['ntn']       =  $row["ntn"];     
          $output['op_balance']       =  $row["op_balance"];        
          $output['status']       =  $row["status"];
          
		}
		echo json_encode($output);
	}
	if($_POST['btn_action'] == 'Edit')
	{
		$dated = date('2021-07-01'); 
		
			$query = "
			UPDATE vendors SET
				dated = '".$_POST["dated"]."', 
				name = '".$_POST["v_name"]."',
				address = '".$_POST["address"]."', 
				city = '".$_POST["city"]."',
				country = 'Pakistan',
				phone = '".$_POST["phone"]."',
				email = '".$_POST["email"]."',
				web = '".$_POST["web"]."',	
				gst = '".$_POST["gst"]."',	
				op_balance = '".$_POST["op_balance"]."',			
				user_id = '".$user_id."',				
				status = '".$_POST["status"]."'
				WHERE v_id = '".$_POST["v_id"]."'
			";
		
		$statement = $connect->prepare($query);
		$statement->execute();
		$acc_no = $_POST['v_id'];
		$v_id= $_POST['v_id'];
		$query = "
			UPDATE accounts SET
				
				acc_name = '".$_POST["v_name"]."'
				WHERE acc_no = '".$acc_no."'
			";
		
		$statement = $connect->prepare($query);
		$statement->execute();
		//$result = $statement->fetchAll();
		$sql = "select jv_id from jv where ref_id = '".$v_id."' and source ='Vendor'";
		$statement=$connect->prepare($sql);
		$statement->execute();
		$jv_id = $statement->fetchColumn(); 
			$sql = "delete from jv where ref_id = '".$v_id."' and source ='Vendor'";
		$statement=$connect->prepare($sql);
		$statement->execute();

		$sql = "delete from jv_detail where jv_id = '".$jv_id."'";
		$statement=$connect->prepare($sql);
		$statement->execute();
		
		$op_balance = $_POST['op_balance'];

		if($op_balance <> 0)
		{

			$source = 'Vendor';
            $description = 'Opening Balance For ' .$_POST['v_name'];
            $ref_id = $v_id;

            $sql = "select IFNULL(max(jv_no),0) from jv where source = 'Vendor'";
		$statement=$connect->prepare($sql);
		$statement->execute();
		$jv_no = $statement->fetchColumn();
		$jv_no++;
		$dated =date('2021-07-01'); 

            
            $statement = $connect->prepare("
              INSERT INTO jv 
                (jv_no,branch_id,dated,source,ref_id,description,user_id)
                VALUES (:jv_no, :branch_id, :dated, :source, :ref_id, :description, :user_id)
    ");
    $statement->execute(
      array(
          ':jv_no'             =>  $jv_no,
          ':branch_id'             =>  $branch_id,
          ':dated'             =>  $dated,
          ':source'          =>  $source,
          ':ref_id'          =>  $ref_id,
          ':description'       =>  $description,
       
          ':user_id'       =>  $user_id
      )
    );
     $statement = $connect->query("SELECT LAST_INSERT_ID()");
      $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'              =>  1000000,
            ':debit'          =>  $op_balance,
            ':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'              =>  $acc_no,
            ':debit'          =>  0,
            ':credit'           =>  $op_balance
                        
          )
        );
		}
		if(isset($result))
		{
			echo 'Vendor Details Edited';
		}
	}
	if($_POST['btn_action'] == 'delete')
	{
		$status = 'Inactive';
		if($_POST['status'] == 'Active')
		{
			$status = 'Inactive';
		}
		$query = "
		UPDATE vendors 
		SET status = :status 
		WHERE v_id = :v_id
		";
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				':status'	=>	$status,
				':v_id'		=>	$_POST["v_id"]
			)
		);	
		
		$acc_no = $_POST['v_id'];
		$query = "
		UPDATE accounts 
		SET status = :status 
		WHERE acc_no = :acc_no
		";
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				':status'	=>	$status,
				':acc_no'		=>	$acc_no
			)
		);
		$result = $statement->fetchAll();	
		if(isset($result))
		{
			echo 'Status change to ' . $status;
		}	
	}
}
?>