<?php 
include("../config.php");
if(!empty($_POST)){
	$account_group       = mysqli_real_escape_string($conn, $_POST['account_group']);
	$descr       		 = mysqli_real_escape_string($conn, $_POST['descr']);
	$status              = '0';
	$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               = 'accounting_control_update';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);
  	 	$insert = "UPDATE account_control SET account_group='$account_group',descr='$descr',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               = 'accounting_control_add';
	$user_id            = $_SESSION['user_detail']['id'];
	$data               = serialize($_POST);
	$insert = "INSERT INTO `account_control`(`account_group`, `descr`, `status`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$account_group."','".$descr."','".$status."','".$company_id."','".$branch_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_control/list.php') );die();
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=account_control/add.php') );die();
} 