<?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];

$tourcode = $_SESSION['tourcode'];


if (isset($_REQUEST['party'])) {
    $party = $_REQUEST['party'];
    $disblsrchparty = trim($_REQUEST['disblsrchparty']);
    $vouchdt = $_REQUEST['vouchdt'];
    $vouchno = $_REQUEST['vouchno'];
    $amount = $_REQUEST['amount'];
    $source = $_REQUEST['source'];
    $recpagnst = $_REQUEST['recpagnst'];


    $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;
    }

    // this part approve by Sufiyan bhai and written by sahil
    $tourcond = "";
    if ($recpagnst != "F"){
        $tourcond = " AND `tour` = '$tourcode' ";
    }

//    echo "$mopbal";
    
//    print "SELECT SUM(amount) as amt FROM `actran` WHERE accode = '$party' AND `date` <= '$vouchdt' 
//                                          and concat(vouchno,source) != concat('$vouchno','$source')  and `retdate` = '0000-00-00'  $tourcond ";

    $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode = '$party' AND `date` <= '$vouchdt' 
                                          and concat(vouchno,source) != concat('$vouchno','$source')  and `retdate` = '0000-00-00'  $tourcond ") or die(mysqli_error($conn));

    $getamt = mysqli_fetch_array($getbal);
    $amt = $getamt['amt'];
//    echo $amt;
    

    $mobal = $mopbal + $amt + $amount;

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

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