<?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];
if (isset($_REQUEST['bankname'])) {
    $para = $_REQUEST['para'];
    $depamt = $_REQUEST['depamt'];
    $accode = $_REQUEST['accode'];
    $bankname = $_REQUEST['bankname'];
    $depdate = $_REQUEST['depdate'];
    $type = $_REQUEST['type'];
    $roe = $_REQUEST['roe'];

    $accdb = "actran";
    if ($para == "D") {
        $maindbf = "cashrecp";
        $source = 'PYIN';
        $accamt = $depamt  * -1;
    } else {
        $maindbf = "cashpay";
        $source = 'PYOU';
        $accamt = $depamt;
    }
//    browse($para);
    $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindbf` 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';
    }
    mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$qvouch' AND `source`='$source'  ") or die(mysqli_error($conn));
    if ($depamt > 0) {
        $minus_amt = ($accamt * -1);
        $getbankinit = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$bankname'")or die(mysqli_error($conn));
        $bankinit = mysqli_fetch_array($getbankinit);
        $binit = $bankinit['bankinit'];

        $qrygrp = mysqli_query($conn, "SELECT `grpcode` FROM accmast WHERE `accode` = '$accode'") or die(mysqli_error($conn));
        $fetgrp = mysqli_fetch_array($qrygrp);
        $pgrp = $fetgrp['grpcode'];

        // cash part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount, `type`, `roe`)
                    VALUES ('$depdate','$qvouch', '$qvouch', '$source', '001', '$binit', '$bankname', '002', '$minus_amt', '$type', '$roe')") or die(mysqli_error($conn));

        // party part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount, `type`, `roe`)
                    VALUES ('$depdate','$qvouch', '$qvouch', '$source', '002', '$binit', '$accode', '$pgrp', '$accamt', '$type', '$roe')") or die(mysqli_error($conn));
    }

    mysqli_query($conn, "INSERT INTO `$maindbf`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `recpno`, `party`, `amount`, `remark1`, `remark2`)
                                       Values ('$qvouch', '$depdate', '$bankname', '$binit', '$qvouch', '$accode', '$depamt', '$remark1','$remark2')") or die(mysqli_error($conn));
    echo "$qvouch";
}
if (isset($_REQUEST['agentname'])) {
    
   
    

}
?>