<?php 
include("../config.php");
if(!empty($_POST)){
	if($_POST["id"] !== ''){
		$voucher_no 		= mysqli_real_escape_string($conn, $_POST['voucher_no']);
	}else{
		$voucher_no 		= mysqli_real_escape_string($conn, 'O'.$_POST['voucher_no']);
	}
	$v_date 			= mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_POST['v_date'])));
	$account_name 		= mysqli_real_escape_string($conn, $_POST['account_name']);
	$amount 			= mysqli_real_escape_string($conn, $_POST['amount']);
	$narration          = mysqli_real_escape_string($conn, $_POST['narration']);
	$flag          		= mysqli_real_escape_string($conn, $_POST['flag']);
	$company_id         = $_SESSION['company_id'];
  	$branch_id          = $_SESSION['branch_id'];
	$created_id         = mysqli_real_escape_string($conn, $_SESSION['user_detail']['id']);
	$created_datetime   = mysqli_real_escape_string($conn, date('Y-m-d H:i:s'));
	$updated_id         = mysqli_real_escape_string($conn, $_SESSION['user_detail']['id']);
	$updated_datetime   = mysqli_real_escape_string($conn, date('Y-m-d H:i:s'));
	
	// COMMON
	$party      = "SELECT * FROM `act_master` WHERE `id` = '".$account_name."'";
    $party_res  = mysqli_query($conn,$party);
    $party_row  = mysqli_fetch_array($party_res);
    $code 		= $party_row['act_code'];

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

		$pd 				= "DELETE FROM `opening_balance` WHERE invoice_no = '".$voucher_no."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
		$pd_res 			= mysqli_query($conn,$pd);

		$gld 				= "DELETE FROM `trans` WHERE voucher_no = '".$voucher_no."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
		$gld_res 			= mysqli_query($conn,$gld);
		
		$insert = "INSERT INTO `opening_balance`(`invoice_no`, `v_date`, `account_name`, `amount`, `narration`, `flag`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$voucher_no."','".$v_date."','".$code."','".$amount."','".$narration."','".$flag."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
		$results = mysqli_query($conn,$insert);

		// TRANS TABLE

		$trans = "INSERT INTO `trans`(`voucher_no`, `voucher_date`, `act_code`, `amount`, `adjust`, `balance`, `discount`, `tds`, `other_add`, `other_less`, `flag`, `mark`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$voucher_no."','".$v_date."','".$code."','".$amount."','0.00','".$amount."','0.00','0.00','','0.00','".$flag."','','".$_SESSION['branch_id']."','".$_SESSION['company_id']."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
        $trans_res = mysqli_query($conn,$trans);
        
		$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               = 'opening_balance_add';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);

		$insert = "INSERT INTO `opening_balance`(`invoice_no`, `v_date`, `account_name`, `amount`, `narration`, `flag`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$voucher_no."','".$v_date."','".$code."','".$amount."','".$narration."','".$flag."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
		$results = mysqli_query($conn,$insert);

		// TRANS TABLE

		$trans = "INSERT INTO `trans`(`voucher_no`, `voucher_date`, `act_code`, `amount`, `adjust`, `balance`, `discount`, `tds`, `other_add`, `other_less`, `flag`, `mark`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$voucher_no."','".$v_date."','".$code."','".$amount."','0.00','".$amount."','0.00','0.00','','0.00','".$flag."','','".$_SESSION['branch_id']."','".$_SESSION['company_id']."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
        $trans_res = mysqli_query($conn,$trans);

		$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=opening_balance/list.php') );die();
}else{
	echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=opening_balance/add.php') );die();
}  
?>