<?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();
}
$dated = date('Y-m-d');
if(isset($_POST['btn_action']))
{
	if($_POST['btn_action'] == 'fetch_single')
	{
		$query = "
		SELECT * FROM user_detail WHERE user_id = :user_id";
		
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				':user_id'	=>	$_POST['user_id']
			)
		);
		$result = $statement->fetchAll();
		foreach($result as $row)
		{				
			
           $output['user_email']          =  $row['user_email'];
           $output['user_name']          =  $row['user_name'];
           $output['user_pass']          =  $row['user_pass'];
           $output['user_type']          =  $row['user_type'];
         
           $output['status']          =  $row['status'];
         
          
		}
		echo json_encode($output);
	}
	if($_POST['btn_action'] == 'Add')
	{
		
		$dated = date('Y-m-d');
		$query = "
		INSERT INTO user_detail (dated,user_email,user_name,user_pass,user_type,branch_id,store_id,status)
        VALUES (:dated, :user_email, :user_name, :user_pass, :user_type, :branch_id, :store_id, :status)
    ";	
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
          
          ':dated'             =>  $dated,
          ':user_email'             =>  trim($_POST["user_email"]),
          ':user_name'          =>  trim($_POST["user_name"]),
          ':user_pass'          =>  $_POST["user_pass"],
          ':user_type'          =>  trim($_POST["user_type"]),
          ':branch_id'          =>  1,
           ':store_id'          =>  1,           
          ':status'       =>  trim($_POST["status"])
          
          )
		);
		$statement = $connect->query("SELECT LAST_INSERT_ID()");
      	$user_id = $statement->fetchColumn();
$sql = "insert into user_rights(user_id,doc_name)VALUES
('".$user_id."','rpt_svl_balance'),
('".$user_id."','rpt_svl'),
('".$user_id."','rpt_sale_target'),
('".$user_id."','production'),
('".$user_id."','inventory'),
('".$user_id."','items'),
('".$user_id."','items_new'),
('".$user_id."','items_edit'),
('".$user_id."','items_delete'),
('".$user_id."','items_inquiry'),
('".$user_id."','items_transfer'),
('".$user_id."','items_transfer_new'),
('".$user_id."','items_transfer_edit'),
('".$user_id."','items_transfer_delete'),
('".$user_id."','items_adjust'),
('".$user_id."','items_adjust_new'),
('".$user_id."','items_adjust_edit'),
('".$user_id."','items_adjust_delete'),
('".$user_id."','inventory_reports'),
('".$user_id."','inventory_reports_stockbystore'),
('".$user_id."','inventory_reports_item_ledger'),
('".$user_id."','inventory_reports_stockavailable'),
('".$user_id."','inventory_reports_stockavailable_value'),
('".$user_id."','inventory_reports_min_stock'),
('".$user_id."','production_delete'),
('".$user_id."','gl'),
('".$user_id."','gl_accounts'),
('".$user_id."','gl_accounts_new'),
('".$user_id."','gl_accounts_edit'),
('".$user_id."','gl_accounts_delete'),
('".$user_id."','gl_parent_accounts'),
('".$user_id."','gl_parent_accounts_new'),
('".$user_id."','gl_parent_accounts_edit'),
('".$user_id."','gl_parent_accounts_delete'),
('".$user_id."','jv'),
('".$user_id."','jv_new'),
('".$user_id."','jv_edit'),
('".$user_id."','jv_delete'),
('".$user_id."','jv_print'),
('".$user_id."','gl_reports'),
('".$user_id."','gl_reports_gl'),
('".$user_id."','gl_reports_cash_flow'),
('".$user_id."','gl_reports_trial_balance'),
('".$user_id."','gl_reports_sub_accounts'),
('".$user_id."','accounts_payable'),
('".$user_id."','vendors'),
('".$user_id."','vendors_new'),
('".$user_id."','vendors_edit'),
('".$user_id."','vendors_delete'),
('".$user_id."','sub_vendors'),
('".$user_id."','sub_vendors_new'),
('".$user_id."','sub_vendors_edit'),
('".$user_id."','sub_vendors_delete'),
('".$user_id."','purchase'),
('".$user_id."','purchase_new'),
('".$user_id."','purchase_edit'),
('".$user_id."','purchase_delete'),
('".$user_id."','purchase_print'),
('".$user_id."','purchase_return'),
('".$user_id."','purchase_return_new'),
('".$user_id."','purchase_return_edit'),
('".$user_id."','purchase_return_delete'),
('".$user_id."','purchase_return_print'),
('".$user_id."','accounts_payable_payments'),
('".$user_id."','accounts_payable_payments_new'),
('".$user_id."','accounts_payable_payments_edit'),
('".$user_id."','accounts_payable_payments_delete'),
('".$user_id."','accounts_payable_payments_print'),
('".$user_id."','accounts_payable_reports'),
('".$user_id."','accounts_payable_reports_vl'),
('".$user_id."','accounts_payable_reports_os'),
('".$user_id."','accounts_payable_reports_payments'),
('".$user_id."','accounts_payable_reports_purchase_report'),
('".$user_id."','accounts_receivable'),
('".$user_id."','customers'),
('".$user_id."','customers_new'),
('".$user_id."','customers_edit'),
('".$user_id."','customers_delete'),
('".$user_id."','cash_invoices'),
('".$user_id."','cash_invoices_new'),
('".$user_id."','cash_invoices_edit'),
('".$user_id."','cash_invoices_delete'),
('".$user_id."','cash_invoices_print'),
('".$user_id."','wholesale_invoices'),
('".$user_id."','wholesale_invoices_new'),
('".$user_id."','wholesale_invoices_edit'),
('".$user_id."','wholesale_invoices_delete'),
('".$user_id."','wholesale_invoices_print'),
('".$user_id."','return_wholesale'),
('".$user_id."','return_wholesale_new'),
('".$user_id."','return_wholesale_edit'),
('".$user_id."','return_wholesale_delete'),
('".$user_id."','return_wholesale_print'),
('".$user_id."','return_retail'),
('".$user_id."','return_retail_new'),
('".$user_id."','return_retail_edit'),
('".$user_id."','return_retail_delete'),
('".$user_id."','return_retail_print'),
('".$user_id."','accounts_receivable_payments'),
('".$user_id."','accounts_receivable_payments_new'),
('".$user_id."','accounts_receivable_payments_edit'),
('".$user_id."','accounts_receivable_payments_delete'),
('".$user_id."','accounts_receivable_payments_print'),
('".$user_id."','accounts_receivable_reports'),
('".$user_id."','accounts_receivable_reports_cl'),
('".$user_id."','accounts_receivable_reports_os'),
('".$user_id."','accounts_receivable_reports_payments'),
('".$user_id."','accounts_receivable_reports_cash_invoices'),
('".$user_id."','accounts_receivable_reports_sale_report_with_profit'),
('".$user_id."','accounts_receivable_reports_aging'),
('".$user_id."','accounts_receivable_reports_aging_invoices'),
('".$user_id."','accounts_receivable_reports_sales_man'),
('".$user_id."','accounts_receivable_reports_sale_invoices')";
 			$statement = $connect->prepare($sql);
            $statement->execute(); //
 $sql = "insert into app_options(user_id,app_name,app_value)VALUES
('".$user_id."','dateup',3),
('".$user_id."','datedown',3)";
$statement = $connect->prepare($sql);
            $statement->execute(); //
if($_POST['user_type']=='admin')
{
             $sql = "update user_rights set doc_status = 'ON' where user_id ='".$user_id."'";
$statement = $connect->prepare($sql);
            $statement->execute(); //
}
		$sql = "select IFNULL(MAX(acc_id),1000) from accounts where acc_type='Asset'";
			$statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $acc_id = $statement->fetchColumn();
            if($acc_id < 1000){$acc_id=1000;}
            $acc_id++;
            $acc_parent= 101;
			$new_acc_no= $acc_parent .$acc_id;
		$query = "
		INSERT INTO accounts (dated,acc_id,acc_no,acc_name,acc_type,acc_parent,op_balance,user_id,status)
        VALUES (:dated, :acc_id,:acc_no, :acc_name, :acc_type, :acc_parent, :op_balance, :user_id, :status)
    ";	
    	
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
          
          ':dated'             =>  $dated,
           ':acc_id'          =>  $acc_id,
            ':acc_no'          =>  $new_acc_no,
           ':acc_name'          =>  trim($_POST["user_name"]),
          ':acc_type'          =>  'Asset',          
          ':acc_parent'       =>  101,
          ':op_balance'       =>  0,
          ':user_id'       =>  $user_id,
          ':status'       =>  trim($_POST["status"])          
          )
		);
		$query = "update user_detail set user_acc_no = '".$new_acc_no."' where user_id='".$user_id."'";
		$statement = $connect->prepare($query);
		$statement->execute();
		//$result = $statement->fetchAll();
		if(isset($result))
		{
			echo 'New User Added';
		}
		 
	}
	
	if($_POST['btn_action'] == 'Edit')
	{
		$dated = date('Y-m-d');
		
		
			$query = "
			UPDATE user_detail SET
				dated = '".$dated."',
				user_email = '".$_POST["user_email"]."',
				user_name = '".$_POST["user_name"]."',
				user_pass = '".$_POST["user_pass"]."',
				user_type = '".$_POST["user_type"]."',
				branch_id = 1,
				store_id = 1,									
				status = '".$_POST["status"]."'
				WHERE user_id = '".$_POST["user_id"]."'
			";
		
		$statement = $connect->prepare($query);
		$statement->execute();
		$sql = "select user_acc_no from user_detail where user_id = '".$_POST["user_id"]."'";
		$statement = $connect->prepare($sql);
		$statement->execute();
		$acc_no = $statement->fetchColumn();
		$query = "
			UPDATE accounts SET			
				acc_name = '".$_POST["user_name"]."'
				WHERE acc_no = '".$acc_no."'
			";
		
		$statement = $connect->prepare($query);
		$statement->execute();
		//$result = $statement->fetchAll();
		if(isset($result))
		{
			echo 'User Details Edited';
		}
	}
	if($_POST['btn_action'] == 'delete')
	{
		$status = 'Inactive';
		if($_POST['status'] == 'Active')
		{
			$status = 'Inactive';
		}
		$query = "
		UPDATE user_detail
		SET status = :status 
		WHERE user_id = :user_id
		";
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				':status'	=>	$status,
				':user_id'		=>	$_POST["user_id"]
			)
		);	
		//$result = $statement->fetchAll();	
		if(isset($result))
		{
			echo 'User Status change to ' . $status;
		}
	}
}
?>