<?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(acc_no),0) FROM accounts where acc_type='CREDITOR'";
            $statement = $connect->prepare($sql);
            $statement->execute();
            $acc_no = $statement->fetchColumn();
            if($acc_no < 2016001)
            {
            	$acc_no = 2016001;
            }
            $acc_no++;
         
		$sql = "select acc_name from accounts where acc_name ='".$_POST['v_name']."'";
		$statement = $connect->query($sql);
		$acc_name = $statement->fetchColumn();
		if($acc_name !== $_POST['v_name'])
		{
		$query = "
		INSERT INTO accounts (acc_no,dated,acc_name,acc_type,acc_parent,address,city,country,phone,email,web,op_balance,sm_id,user_id,status)
        VALUES (:acc_no, :dated, :acc_name, :acc_type, :acc_parent, :address, :city, :country, :phone, :email, :web,  :op_balance,:sm_id, :user_id, :status)
    ";	
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
          ':acc_no'             =>  $acc_no,
          ':dated'             =>  $dated,
          ':acc_name'          =>  trim($_POST["v_name"]),
          ':acc_type'          =>  'CREDITOR',
          ':acc_parent'          =>  201,
          ':address'          =>  trim($_POST["address"]),  
           
          ':city'       =>  trim($_POST["city"]),
          ':country'       =>  'Pakistan',
          ':phone'       =>  trim($_POST["phone"]),
          ':email'       =>  trim($_POST["email"]),
          ':web'       =>  trim($_POST["web"]),
     			':op_balance'       =>  trim($_POST["op_balance"]),
          
          
          
          ':sm_id'       =>  0, 
          ':user_id'       =>  $user_id,         
          ':status'       =>  trim($_POST["status"])
          )
		);
	
	
		}else{echo 'Vendor Name Already Exist!';return false;}
			echo 'New Vendor Added';
		
		
	}
	if($_POST['btn_action'] == 'fetch_single')
	{
		$query = "
		SELECT * FROM accounts WHERE acc_no = :acc_no
		";
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				':acc_no'	=>	$_POST["acc_no"]
			)
		);
		$result = $statement->fetchAll();
		foreach($result as $row)
		{
			
			
			
          $output['acc_name']          =  $row['acc_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['op_balance']       =  $row["op_balance"];  
          $output['status']       =  $row["status"];
          
		}
		echo json_encode($output);
	}
	if($_POST['btn_action'] == 'Edit')
	{
	
		$acc_no = $_POST['acc_no'];
		$ref_id = $acc_no;
			$query = "
			UPDATE accounts SET
		
				acc_name = '".$_POST["v_name"]."',
				address = '".$_POST["address"]."', 
				
				city = '".$_POST["city"]."',
				country = 'Pakistan',
				phone = '".$_POST["phone"]."',
				email = '".$_POST["email"]."',
				web = '".$_POST["web"]."',
				op_balance = '".$_POST["op_balance"]."',	
				sm_id = 0,
				user_id = '".$user_id."',				
				status = '".$_POST["status"]."'
				WHERE acc_no = '".$_POST["acc_no"]."'
			";
		
		$statement = $connect->prepare($query);
		$statement->execute();	
		
		$sql = "select jv_id from jv where ref_id = '".$ref_id."' and source = 'vendor'";
		$statement=$connect->prepare($sql);
		$statement->execute();
		$jv_id = $statement->fetchColumn();
			$sql = "delete from jv where ref_id = '".$acc_no."' 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 = $acc_no;
            $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 = '2021-01-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 
                        
          )
        );
		}
		
			echo 'Details Updated';
		
	}
	if($_POST['btn_action'] == 'delete')
	{
		$status = 'Inactive';
		if($_POST['status'] == 'Active')
		{
			$status = 'Inactive';
		}
	
	
		$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;
		}
	}
}
?>