<?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];
$zroot = $_SESSION["zdbroot"];

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

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

    $mobal = $amt * -1;
    
    $getcom = mysqli_query($conn,"Select tcsperc,tcsmin from $zroot.company ");
    $fetcom = mysqli_fetch_array($getcom);
    $tcsmin = $fetcom['tcsmin'];
    $tcsperc = 0;
    if($mobal > $tcsmin){
        $tcsperc = $fetcom['tcsperc'];
    }
    
    
    echo $tcsperc;
}
