<?php 
include("../config.php");
if(!empty($_POST)){
	
	if(!empty($_POST['product'])){
    	$product            = $_POST['product'];
  	}else{
    	$product         = '';
  	}

	$voucher_no 		= mysqli_real_escape_string($conn, $_POST['voucher_no']);
	$b_date 			= mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_POST['b_date'])));
	$bank 				= mysqli_real_escape_string($conn, $_POST['bank']);
	$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']);
	$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'];

    $banks      = "SELECT * FROM `act_master` WHERE `id` = '".$bank."'";
    $banks_res  = mysqli_query($conn,$banks);
    $banks_row  = mysqli_fetch_array($banks_res);
    $b_code     = $banks_row['act_code'];

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

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

		$gld 		= "DELETE FROM `general_ledger` 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 `payment`(`voucher_no`, `b_date`, `bank`, `account_name`, `amount`, `narration`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`, `flag`) VALUES ('".$voucher_no."','".$b_date."','".$bank."','".$code."','".$amount."','".$narration."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."','CR')";
		$results = mysqli_query($conn,$insert);

		//GENERAL LEDGER ENTRY
		$gl = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$voucher_no."','".$b_date."','CASH RECEIPT','".$code."','".$b_code."','".$amount."','','','','".$amount."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
        $gl_res = mysqli_query($conn,$gl);

		$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               = 'cash_receipt_add';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);

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

		//GENERAL LEDGER ENTRY
		$gl = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$voucher_no."','".$b_date."','CASH RECEIPT','".$code."','".$b_code."','".$amount."','','','','".$amount."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
        $gl_res = mysqli_query($conn,$gl);

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