<?php 
include("../config.php");
if(!empty($_POST)){
	$product            = $_POST['product'];
	$jv_no 				= mysqli_real_escape_string($conn, $_POST['jv_no']);
	$jv_date 			= mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_POST['jv_date'])));
	$main_acc 			= mysqli_real_escape_string($conn, $_POST['main_acc']);
	$t_mode 			= mysqli_real_escape_string($conn, $_POST['t_mode']);
	$total_amount 		= mysqli_real_escape_string($conn, $_POST['total_amount']);
	$total_adjust       = mysqli_real_escape_string($conn, $_POST['total_adjust']);
	$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'));
	$master_accounts    = combine(getAllRecords('act_master'), 'id', 'act_code');
	if($_POST["id"] != ''){
		// DELETE LAST DATA
		$jv_del 	= "DELETE FROM journal_voucher WHERE jv_no = '".$jv_no."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
		$jv_del_res = mysqli_query($conn,$jv_del);

		$gl_del 	= "DELETE FROM general_ledger WHERE voucher_no = '".$jv_no."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
		$gl_del_res = mysqli_query($conn,$gl_del);

		// UPDATE
		$type               = 'jv_update';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);
		foreach ($product as $product) {
			if( isset($product['sub_acc']) && !empty($product['sub_acc']) ) {
				if($t_mode == 'CR'){
					$insert = "INSERT INTO `journal_voucher`(`jv_no`, `jv_date`, `main_acc`, `t_mode`, `total_amount`, `sub_acc`, `ref_no`, `amount`, `remarks`, `total_adjust`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$jv_no."','".$jv_date."','".$main_acc."','".$t_mode."','".$total_amount."','".$product['act_code']."','".$product['ref_no']."','".$product['amount']."','".$product['remarks']."','".$total_adjust."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
					$result = mysqli_query($conn,$insert);
					
					$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 ('".$jv_no."','".$jv_date."','JV','".$main_acc."','".$product['act_code']."','".$product['amount']."','".$product['remarks']."','','','".$product['amount']."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
            		$tds_am_res = mysqli_query($conn,$gl);	
				}else{
					
					$insert = "INSERT INTO `journal_voucher`(`jv_no`, `jv_date`, `main_acc`, `t_mode`, `total_amount`, `sub_acc`, `ref_no`, `amount`, `remarks`, `total_adjust`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$jv_no."','".$jv_date."','".$main_acc."','".$t_mode."','".$total_amount."','".$product['act_code']."','".$product['ref_no']."','".$product['amount']."','".$product['remarks']."','".$total_adjust."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
					$result = mysqli_query($conn,$insert);
					
					$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 ('".$jv_no."','".$jv_date."','JV','".$product['act_code']."','".$main_acc."','".$product['amount']."','".$product['remarks']."','','','".$product['amount']."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
            		$tds_am_res = mysqli_query($conn,$gl);
				}
			}
		}
		
	}else{
		$type               = 'jv_add';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);
		foreach ($product as $product) {
			if( isset($product['sub_acc']) && !empty($product['sub_acc']) ) {
				if($t_mode == 'CR'){
					$insert = "INSERT INTO `journal_voucher`(`jv_no`, `jv_date`, `main_acc`, `t_mode`, `total_amount`, `sub_acc`, `ref_no`, `amount`, `remarks`, `total_adjust`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$jv_no."','".$jv_date."','".$main_acc."','".$t_mode."','".$total_amount."','".$product['act_code']."','".$product['ref_no']."','".$product['amount']."','".$product['remarks']."','".$total_adjust."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
					$result = mysqli_query($conn,$insert);
					
					$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 ('".$jv_no."','".$jv_date."','JV','".$main_acc."','".$product['act_code']."','".$product['amount']."','".$product['remarks']."','','','".$product['amount']."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
            		$tds_am_res = mysqli_query($conn,$gl);	
				}else{
					
					$insert = "INSERT INTO `journal_voucher`(`jv_no`, `jv_date`, `main_acc`, `t_mode`, `total_amount`, `sub_acc`, `ref_no`, `amount`, `remarks`, `total_adjust`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$jv_no."','".$jv_date."','".$main_acc."','".$t_mode."','".$total_amount."','".$product['act_code']."','".$product['ref_no']."','".$product['amount']."','".$product['remarks']."','".$product['amount']."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
					$result = mysqli_query($conn,$insert);
					
					$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 ('".$jv_no."','".$jv_date."','JV','".$product['act_code']."','".$main_acc."','".$product['amount']."','".$product['remarks']."','','','".$product['amount']."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
            		$tds_am_res = mysqli_query($conn,$gl);
				}
			}
		}
	}
	
	echo json_encode( array('response' => 1, 'msg' => 'DATA SAVED SUCCESSFULLY', 'alert' => 1, 'redirect' => 'index.php?page=journal_voucher/list.php') );die();
}else{
	echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=journal_voucher/add.php') );die();
}  
?>