<?php
ini_set('display_errors', 1);
error_reporting(E_ERROR);  

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

$zdb = $_SESSION["zdbyear_$dbname"];
mysqli_select_db($conn, $zdb);
$username = $_SESSION["username_" . $dbname];

date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("H:i:sa");
$adduser = $_SESSION["username_" . $dbname];

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

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

// AGENT INTERNAL TRANSFER

if ($data['toagent']) {

    $maindb = 'inout';
    $accdb = "actran";
    $file = 'agentinttran.php';
    $grid = '';
    $source = 'AG';

    $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"];

    $fragent = $data["fragent"];
    $toagent = $data["toagent"];
    $amt = $data["amt"];
    $roe = $data["roe"];
    $inr = $data["inr"];
    $remark1 = $data["remark1"];
    $remark2 = $data["remark2"];

    $gentfr = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM `accmast` where accode='$fragent'"));
    $gentto = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM `accmast` where accode='$toagent'"));

    $frgpcode = $gentfr['grpcode'];
    $trgpcode = $gentto['grpcode'];

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

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

    // FROM AGENT part
    mysqli_query($conn, "INSERT INTO `$accdb`(`date`, `vouchno`, `source`, `trnno`, `bankinit`,
                                              `accode`, `grpcode`, `amount`)
                                      VALUES ('$vouchdt', '$mvouch', '$source', '001', '$binit',
                                              '$fragent', '$fgrpcode', '$amt')") or die(mysqli_error($conn));

    // TO AGENT part
    mysqli_query($conn, "INSERT INTO `$accdb`(`date`, `vouchno`, `source`, `trnno`, `bankinit`, 
                                              `accode`, `grpcode`, `amount`)
                                      VALUES ('$vouchdt', '$mvouch', '$source', '002', '$binit',
                                              '$toagent', '$tgrpcode', '$minus_amt')") or die(mysqli_error($conn));
    // }
    ######################## ACCOUNTS PART ENDED ######################################

    mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `frombc`, `tobc`,
                                               `amount`, `narr1`, `narr2`, `username`)
                                       VALUES ('$qvouch', '$vouchdt', '$fragent', '$toagent',
                                               '$amt', '$remark1', '$remark2', '$username')") or die(mysqli_error($conn));

//    mysqli_query($conn, "INSERT INTO `logsacn`( `enttype`, `entname`, `vouchno`, `user`, `action`, `entdate`, `enttime`)
//                                        VALUES( 'T', 'Agent Internal Transfer', '$qvouch', '$adduser', 'Add', '$adddate', '$addtime')") or die(mysqli_error($conn));
}

$rows = array();
$ctr = 0;
$rows = array(
    'success' => true,
    'vouchno' => "$mvouch"
);

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