<?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'];
    $contno = $_REQUEST['contno'];
    
    $accdb = "actran";
//    if ($para == "D") {
//        $maindbf = "cashrecp";
//        $source = 'PYIN';
//        $accamt = $depamt;
//    } else {
        $maindbf = "cashpay";
        $source = 'PYOU';
        $minus_amt = $depamt * -1;
//    }
//    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) {
        $getbankinit = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$bankname'");
        $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, `contno`)
                    VALUES ('$depdate','$qvouch', '$qvouch', '$source', '001', '$binit', '$bankname', '002', '$depamt', '$contno')") or die(mysqli_error($conn));

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

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

?>
