<?php

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

$zdb = $_SESSION["zdbyear_$dbname"];
$comp = $_SESSION["comp_" . $dbname];

mysqli_select_db($conn, $zdb);

if ($comp > 100) {
    $mpart = "party";
} else {
    $mpart = "broker";
}
$mfile = "rawmain";
$mfile2 = "gstmain";

if ($_REQUEST["agency"]) {
    $mtodate = $_REQUEST["frmdt"];
    $agency = $_REQUEST["agency"];
    
    $agencond = " and a.agency = '$agency' ";
    $agencond1 = " and (b.broker = '$agency' or a.agency = '$agency' ) ";

//    print "select * from 
//                                (SELECT a.$mpart as party, b.name,sum(netamt) as tamt FROM `$mfile` a,
//                                 accmast b WHERE a.vouchno != '' AND a.party = b.accode  AND b.grpcode != '040' 
//                                $agencond group by party
//                                union
//                                SELECT a.$mpart as party, b.name,sum(netamt) as tamt FROM `$mfile2` a,
//                                accmast b WHERE a.vouchno != '' AND a.party = b.accode AND b.grpcode != '040'
//                                $agencond group by party
//                                union
//                                SELECT a.accode as party, b.name,sum(billamt) as tamt FROM `prevbill` a,
//                                accmast b WHERE a.vouchno != '' AND a.accode = b.accode AND b.grpcode != '040'
//                                $agencond1 GROUP BY a.accode )
//                                a WHERE party != '' group by party  order by name ";

 
    $acc = mysqli_query($conn, "SELECT * FROM 
                                (SELECT a.$mpart as party, b.name,sum(netamt) as tamt FROM `$mfile` a,
                                 accmast b WHERE a.vouchno != '' AND a.party = b.accode  AND b.grpcode != '040' 
                                $agencond group by party
                                union
                                SELECT a.$mpart as party, b.name,sum(netamt) as tamt FROM `$mfile2` a,
                                accmast b WHERE a.vouchno != '' AND a.party = b.accode AND b.grpcode != '040'
                                $agencond group by party
                                union
                                SELECT a.accode as party, b.name,sum(billamt) as tamt FROM `prevbill` a,
                                accmast b WHERE a.vouchno != '' AND a.accode = b.accode AND b.grpcode != '040'
                                $agencond1 GROUP BY a.accode ) a
                                WHERE party != '' group by party  order by name ") or die(mysqli_error($conn));

//     print table($acc);
//     die();
    $tctr = 0;
    while ($row = mysqli_fetch_array($acc)) {
        $maccode = $row['party'];
        $qamt1 = $row['tamt'];
//        $trn1 = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `billsett` WHERE  `party`= '$maccode' AND `type`='S'") or die(mysqli_error($conn));
//        $gtrn1 = mysqli_fetch_object($trn1);
//        $msetoff1 = $gtrn1->setoff;
//
//        $mbalamt1 = $qamt1 - $msetoff1;
//
//browse($qamt1);
//browse($msetoff1);
//browse($mbalamt1);
//
//        if ($mbalamt1 > 0) {
            
            echo "<tr>";
            echo "<td class='text-center' style=''><input type='checkbox' class='chk' name='check[]' style='height:25px;width:25px'  value='$row[party]' checked></td>";
            echo "<td tabindex='-1'>$row[name]</td>";
            //            echo "<td tabindex='-1'>$mbalamt1</td>";
            echo "</tr>";
            $tctr++;
//        }
    }
}
if ($tctr <= 0) {
    echo "<h3 style='color:red'>No Record Found</h3>";
}
?>