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