<?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'];

}else{

	header('Location: login');

	die();

}

if(isset($_POST['btn_action']))



{



	if($_POST['btn_action'] == 'Add')

	{

		$acc_type = $_POST['acc_type'];

		if($acc_type=='Asset')

		{

			$sql = "select IFNULL(MAX(acc_no),100) from accounts_parent where acc_type='Asset'";

			$statement = $connect->prepare($sql);

            $statement->execute(); // no need to add `$sql` here, you can take that out

            $acc_no = $statement->fetchColumn();

            if($acc_no < 100){$acc_no=100;}

           



            

		}

		if($acc_type=='Liability')

		{

			$sql = "select IFNULL(MAX(acc_no),200) from accounts_parent where acc_type='Liability'";

			$statement = $connect->prepare($sql);

            $statement->execute(); // no need to add `$sql` here, you can take that out

            $acc_no = $statement->fetchColumn();

            if($acc_no < 200){$acc_no=200;}

           



            

		}

		if($acc_type=='Expense')

		{

			$sql = "select IFNULL(MAX(acc_no),300) from accounts_parent where acc_type='Expense'";

			$statement = $connect->prepare($sql);

            $statement->execute(); // no need to add `$sql` here, you can take that out

            $acc_no = $statement->fetchColumn();

           	if($acc_no < 300){$acc_no=300;}



            

		}



		if($acc_type=='Equity')

		{

			$sql = "select IFNULL(MAX(acc_no),400) from accounts_parent where acc_type='Equity'";

			$statement = $connect->prepare($sql);

            $statement->execute(); // no need to add `$sql` here, you can take that out

            $acc_no = $statement->fetchColumn();

           	if($acc_no < 400){$acc_no=400;}



            

		}

		if($acc_type=='Revenue')

		{

			$sql = "select IFNULL(MAX(acc_no),500) from accounts_parent where acc_type='Revenue'";

			$statement = $connect->prepare($sql);

            $statement->execute(); // no need to add `$sql` here, you can take that out

            $acc_no = $statement->fetchColumn();

           	if($acc_no < 500){$acc_no=500;}



            

		}



		$acc_no++;

		

		$dated = date('Y-m-d');

		

		$query = "

		INSERT INTO accounts_parent (dated,acc_no,acc_name,acc_type,user_id,status)

        VALUES (:dated, :acc_no, :acc_name, :acc_type, :user_id, :status)

    ";	

		$statement = $connect->prepare($query);

		$statement->execute(

			array(

          

          ':dated'             =>  $dated,

          ':acc_no'          =>  $acc_no,

          ':acc_name'          =>  trim($_POST["acc_name"]),

          ':acc_type'          =>  trim($_POST["acc_type"]),        

          ':user_id'       =>  $user_id,

          ':status'       =>  trim($_POST["status"])

          

          )

		);

		$result = $statement->fetchAll();

		if(isset($result))

		{

			echo 'New Account Added';

		}

	}

	if($_POST['btn_action'] == 'fetch_single')

	{

		$query = "

		SELECT * FROM accounts_parent 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['acc_type']          =  $row['acc_type'];                

          $output['status']       =  $row["status"];

          

		}

		echo json_encode($output);

	}

	if($_POST['btn_action'] == 'Edit')

	{

		$dated = date('Y-m-d');

		

			$query = "

			UPDATE accounts_parent SET

				dated = 	'".$dated."',

				acc_name = '".$_POST["acc_name"]."',

				acc_type = '".$_POST["acc_type"]."',				

				user_id = '".$user_id."',								

				status = '".$_POST["status"]."'

				WHERE acc_no = '".$_POST["acc_no"]."'

			";

		

		$statement = $connect->prepare($query);

		$statement->execute();

		$result = $statement->fetchAll();

		if(isset($result))

		{

			echo 'Account Details Edited';

		}

	}

	if($_POST['btn_action'] == 'delete')

	{

		$status = 'Inactive';

		if($_POST['status'] == 'Active')

		{

			$status = 'Inactive';

		}

		$query = "

		UPDATE accounts_parent

		SET status = :status 

		WHERE acc_no = :acc_no

		";

		$statement = $connect->prepare($query);

		$statement->execute(

			array(

				':status'	=>	$status,

				':acc_no'		=>	$_POST["acc_no"]

			)

		);	

		$result = $statement->fetchAll();	

		if(isset($result))

		{

			echo 'Account Status change to ' . $status;

		}

	}

}



?>