<?php 
include("../config.php");
if(!empty($_POST)){
	$module         	= mysqli_real_escape_string($conn, $_POST['module']);
	$branch       		= mysqli_real_escape_string($conn, $_POST['branch']);
	$start_sr_no    	= mysqli_real_escape_string($conn, $_POST['start_sr_no']);
	$end_sr_no      	= mysqli_real_escape_string($conn, $_POST['end_sr_no']);
	$company_id     	= mysqli_real_escape_string($conn, $_POST['company_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               = 'document_update';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);
  	 	$insert 			= "UPDATE document_mst SET module='$module',branch='$branch',start_sr_no='$start_sr_no',end_sr_no='$end_sr_no',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               = 'document_add';
	$user_id            = $_SESSION['user_detail']['id'];
	$data               = serialize($_POST);
	$insert 			= "INSERT INTO `document_mst`(`module`, `branch`, `start_sr_no`, `end_sr_no`, `company_id`,`created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$module."','".$branch."','".$start_sr_no."','".$end_sr_no."','".$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=document_mst/list.php') );die();
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=tax_mst/add.php') );die();
} 