<?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];
$fseries = $_SESSION["series_" . $dbname];

if (isset($_REQUEST['party'])) {
    $party = $_REQUEST['party'];
    // $disblsrchparty = trim($_REQUEST['disblsrchparty']);
    $vouchdt = $_REQUEST['vouchdt'];


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

    $accode = $fetm['accode'];

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

    if ($mopdc == "D") {
        $mopbal = $mopbal * - 1;
    }
//print "SELECT SUM(amount) as amt FROM `actran` WHERE accode = '$accode' AND `date` <= '$vouchdt'  and `retdate` != '0000-00-00'";
    $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode = '$accode' AND `date` <= '$vouchdt'  and `retdate` = '0000-00-00'") or die(mysqli_error($conn));
    $getamt = mysqli_fetch_array($getbal);
    $amt = $getamt['amt'];

    $mobal = number_format($mopbal + $amt,2);

//    print "AC" . $mopbal;
//    print "TR" . $amt;
//    print "FI" . $mobal;
    $rows = array('mobal' => $mobal);

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