<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$zdb = $_SESSION["zdbyear_$dbname"];
mysqli_select_db($conn, $zdb);
date_default_timezone_set("Asia/Dubai");
$addtime = date("H:i:sa");
$username = $_SESSION["username_" . $dbname];

$jsonData = file_get_contents('php://input');

// Decode JSON data to an associative array
$data = json_decode($jsonData, true);


if ($data["bankname"]) {

    $maindb = "agentin";
    $accdb = "actran";
    $source = "TOAG";

    $bankname = $data["bankname"];
    $vouchdt = $data["withdate"];
    $agent = $data["agent"];
    $inr = $data["inr"];
    $roe = $data["roe"];
    $dollar = $data["dollar"];
    $rmb = $data["rmb"];
    $rmbamt = $data["rmbamt"];
    $amount = $data["amount"];
    $dollarrecd = $data["dollarrecd"];
    $conchgs = $data["conchgs"];
    $inrtormb = $data["inrtormb"];



    $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb` LIMIT 0,1 ") or die(mysqli_error($conn));

    if (mysqli_num_rows($gen1) > 0) {
        $vvouch = mysqli_fetch_assoc($gen1);
        $qvouch = $vvouch["maxcode"] + 1;
    } else {
        $qvouch = '1';
    }

    $mvouch = $qvouch;

    mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$mvouch' AND `source`='$source'  ") or die(mysqli_error($conn));

    // if ($inr > 0) {
        $minus_amt = ($inr * -1);
        $minus_rmbamt = ($rmbamt * -1);

        $getbankinit = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$bankname'")or die(mysqli_error($conn));
        $bankinit = mysqli_fetch_array($getbankinit);
        $binit = $bankinit['bankinit'];

        // cash part inr
        mysqli_query($conn, "INSERT INTO `$accdb`(`date`, `vouchno`, `source`, `trnno`, `bankinit`,
                                                                    `accode`, `grpcode`, `amount`, `feamt`, `roe`, `type`)
                                                            VALUES ('$vouchdt', '$mvouch', '$source', '001', '$binit',
                                                                    '$bankname', '002', '$inr', '$rmbamt', '$inrtormb', 'R')") or die(mysqli_error($conn));

        // party part inr 
        mysqli_query($conn, "INSERT INTO `$accdb`(`date`, `vouchno`, `source`, `trnno`, `bankinit`, 
                                                                    `accode`, `grpcode`, `amount`, `feamt`, `roe`, `type`)
                                                            VALUES ('$vouchdt', '$mvouch', '$source', '002', '$binit',
                                                                    '$agent', '044', '$minus_amt', '$minus_rmbamt', '$inrtormb', 'R')") or die(mysqli_error($conn));

        // cash part rmb
        // mysqli_query($conn, "INSERT INTO `$accdb`(`date`, `vouchno`, `source`, `trnno`, `bankinit`,
        //                                                         `accode`, `grpcode`, `amount`, `feamt`, `roe`, `type`)
        //                                              VALUES ('$vouchdt', '$mvouch', '$source', '001', '$binit',
        //                                                      '$bankname', '002', '$inr', '$rmbamt', '$inrtormb', 'R')") or die(mysqli_error($conn));

        // // party part rmb
        // mysqli_query($conn, "INSERT INTO `$accdb`(`date`, `vouchno`, `source`, `trnno`, `bankinit`, 
        //                                                             `accode`, `grpcode`, `amount`, `feamt`, `roe`, `type`)
        //                                                     VALUES ('$vouchdt', '$mvouch', '$source', '002', '$binit',
        //                                                             '$agent', '044', '$minus_amt', '$minus_rmbamt', '$inrtormb', 'R')") or die(mysqli_error($conn));
        // }

######################## ACCOUNTS PART ENDED ######################################

    mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `agent`, `party`, `inr`, `roe`, `dollar`,
                                                                     `rmb`, `rmbamt`, `remark1`, `remark2`, `dollarrecd`, 
                                                                     `conchgs`, `inrtormb`, `bankname`)
                                                             VALUES ('$mvouch', '$vouchdt', '$agent', '$agent', '$inr', '$roe', '$dollar',
                                                                     '$rmb', '$rmbamt', '$remark1', '$remark2', '$dollarrecd',
                                                                     '$conchgs', '$inrtormb', '$bankname')") or die(mysqli_error($conn));
}

if ($data['party']) {



    $maindb = 'agentout';
    $accdb = "actran";
    $file = 'agentout.php';
    $grid = '';
    $source = 'FRAG';

    $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb` LIMIT 0,1 ") or die(mysqli_error($conn));

    if (mysqli_num_rows($gen1) > 0) {
        $vvouch = mysqli_fetch_assoc($gen1);
        $qvouch = $vvouch["maxcode"] + 1;
    } else {
        $qvouch = '1';
    }

    $mvouch = $qvouch;
    $vouchdt = $data["withdate"];


    $agent = $data["agent"];
    $party = $data["party"];
    $rmb = $data["rmb"];
    $roe = $data["roe"];
    $inr = $data["inr"];


    mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$mvouch' AND `source`='$source'  ") or die(mysqli_error($conn));

    // if ($inr > 0) {
        $minus_amt = ($inr * -1);
        $minus_rmbamt = ($rmb * -1);

        // cash part
        mysqli_query($conn, "INSERT INTO `$accdb`(`date`, `vouchno`, `source`, `trnno`, `bankinit`,
                                                                    `accode`, `grpcode`, `amount`, `feamt`, `roe`, `type`)
                                                            VALUES ('$vouchdt', '$mvouch', '$source', '001', '$binit',
                                                                    '$agent', '044', '$inr', '$rmb', '$roe', 'R')") or die(mysqli_error($conn));

        // party part
        mysqli_query($conn, "INSERT INTO `$accdb`(`date`, `vouchno`, `source`, `trnno`, `bankinit`, 
                                                                    `accode`, `grpcode`, `amount`, `feamt`, `roe`, `type`)
                                                            VALUES ('$vouchdt', '$mvouch', '$source', '002', '$binit',
                                                                    '$party', '010', '$minus_amt', '$minus_rmbamt', '$roe', 'I')") or die(mysqli_error($conn));
    // }

    ######################## ACCOUNTS PART ENDED ######################################



    mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `agent`, `party`,
                                               `inr`, `roe`, `rmb`, `remark1`, `remark2`)
                                       VALUES ('$qvouch', '$vouchdt', '$agent', '$party',
                                               '$inr', '$roe', '$rmb', '$remark1', '$remark2')") or die(mysqli_error($conn));
}
$rows = array();
$ctr = 0;
$rows = array(
    'success' => true,
    'vouchno' => "$mvouch"
);

header("Content-type: application\json");
echo json_encode($rows);
?>