<?php

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

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

if (isset($_REQUEST['bankcode'])) {
    $bankcode = $_REQUEST['bankcode'];
    $vouchdt = $_REQUEST['vouchdt'];

    $getm = mysqli_query($conn, "SELECT openbal, dc FROM `accmast` WHERE accode='$bankcode'") or die(mysqli_error($conn));
    $fetm = mysqli_fetch_array($getm);


    $mopbal = $fetm["openbal"];
    $mopdc = $fetm["dc"];

    if ($mopdc == "D") {
        $mopbal = $mopbal * - 1;
    }


    $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode = '$bankcode' AND 
                                                           `date` < '$vouchdt' and (`retdate` != '' or `retdate` != '0000-00-00') ") or die(mysqli_error($conn));
    $getamt = mysqli_fetch_array($getbal);
    $amt = $getamt['amt'];

    $mobal = $mopbal + $amt;
    
    
    $rows = array( 'mobal' => $mobal);

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