<?php 
include("../config.php");
if(!empty($_POST)){
	$voucher_no          = accountCode($conn);
	$act_code          	 = mysqli_real_escape_string($conn, strtoupper($_POST['act_code']));
	$ac_name         	 = mysqli_real_escape_string($conn, strtoupper($_POST['ac_name']));
	$account_group       = mysqli_real_escape_string($conn, strtoupper($_POST['account_group']));
	$cr_open_bal       	 = mysqli_real_escape_string($conn, strtoupper($_POST['open_bal']));
	$dr_open_bal       	 = mysqli_real_escape_string($conn, strtoupper($_POST['open_bal']));
	$mode       		 = mysqli_real_escape_string($conn, $_POST['mode']);
	$company_id          = $_SESSION['company_id'];
	$branch_id           = $_SESSION['branch_id'];
	$created_datetime    = date('Y-m-d H:i:s');
	$created_id          = $_SESSION['user_detail']['id'];
	$updated_datetime    = date('Y-m-d H:i:s');
	$updated_id          = $_SESSION['user_detail']['id'];

	if($_POST["id"] != ''){
  	 	$type               = 'account_update';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);

  	 	if($_POST['mode'] == 'cr'){
	      $cr_open_bal         = $_POST['open_bal'];
	      $insert = "UPDATE act_master SET act_code='$act_code',ac_name='$ac_name',account_group='$account_group',dr_open_bal='0.00',cr_open_bal='$cr_open_bal',mode='$mode',branch_id='$branch_id',company_id='$company_id',updated_id='$updated_id',updated_datetime='$updated_datetime' WHERE id='".$_POST['id']."'";
	    }else{
	      $dr_open_bal         = $_POST['open_bal'];
	      $insert = "UPDATE act_master SET act_code='$act_code',ac_name='$ac_name',account_group='$account_group',dr_open_bal='$dr_open_bal',cr_open_bal='0.00',mode='$mode',branch_id='$branch_id',company_id='$company_id',updated_id='$updated_id',updated_datetime='$updated_datetime' WHERE id='".$_POST['id']."'";
	    }
		$result = mysqli_query($conn,$insert) or die( mysqli_error($conn) );
		$activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    	$acttivity_res = mysqli_query($conn,$activiti);

  }else{
	$type               = 'account_add';
	$user_id            = $_SESSION['user_detail']['id'];
	$data               = serialize($_POST);
	if($_POST['mode'] == 'cr'){
      $insert = "INSERT INTO `act_master`(`act_code`, `ac_name`, `account_group`, `cr_open_bal`, `mode`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$act_code."','".$ac_name."','".$account_group."','".$cr_open_bal."','".$mode."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    }else{
      $insert = "INSERT INTO `act_master`(`act_code`, `ac_name`, `account_group`, `dr_open_bal`, `mode`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$act_code."','".$ac_name."','".$account_group."','".$dr_open_bal."','".$mode."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    }

	$result = mysqli_query($conn,$insert);
	
	$activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
	$acttivity_res = mysqli_query($conn,$activiti);

	
  }
  echo json_encode( array('response' => 1, 'msg' => 'DATA SAVED SUCCESSFULLY....!!!', 'alert' => 1, 'redirect' => 'index.php?page=account_mst/list.php') );die();
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=account_mst/add.php') );die();
} 