<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$cust = 'accmast';
$bild = 'billsett';
$prevd = 'prevbill';
$zroot = $_SESSION["zdir"];
$ztit = $_SESSION["tit_" . $dbname];

$billdb = $bild;
$prevdb = $prevd;
$custdb = $cust;


if (isset($_GET['accode'])) {
    $maccode = $_GET["accode"];
    $mvouch = $_GET["vouchno"];
    $mvouchdt = $_GET["vouchdt"];
    $mamount = $_GET["amount"];
    $bnkcode = $_GET["bank"];
    $mndbf = $_GET["dbf"];
    $pvar = $_GET["pvar"];
    $msource = $_GET["source"];
    $myear = $_SESSION["tit_" . $dbname];

    $cqry = mysqli_query($conn, "SELECT `grpcode` FROM `$custdb` WHERE `accode` = '$maccode' LIMIT 0,1 ");
    if (mysqli_num_rows($cqry) <= 0) {
        exit();
    }
    $grpcode = mysqli_fetch_assoc($cqry);
    if ($grpcode["grpcode"] == "009" || $grpcode["grpcode"] == "010") {

        $bqry = mysqli_query($conn, "SELECT `bankinit` FROM `$custdb` WHERE `accode` = '$bnkcode' LIMIT 0,1 ");
        $init = mysqli_fetch_assoc($bqry);
        $pbinit = $init["bankinit"];
        $binitcond = "";

        if (!empty($pbinit)) {
            $binitcond = " and bankinit = '$pbinit' ";
        }

        if ($pvar == "R") {
            $mtit = 'Settlement Of Bills ( Receipts )';
            $mfile = "salemainwh";
            $mfile2 = "salemainrt";
            $mfile3 = "jvtran";


            $mtype = "S";
        } else {

            $mtit = 'Settlement Of Bills ( Payments )';
            $mfile = 'purchmain';
            $mfile3 = "jvtran";

            $mfile2 = "";

            $mtype = "P";
        }



        if ($pvar == "R") {

            $subtmp1 = mysqli_query($conn, "SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `billno`, 'WH' as src, vouchdt,
                                    DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdate`, `netamt` as  `billamt`, `party` as  `accode` FROM $mfile
                                    WHERE `party` = '$maccode' and cancdate = '0000-00-00'
                                    UNION 
                                    SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `billno`, 'RT' as src,vouchdt,
                                    DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdate`, `netamt` as  `billamt`, `party` as  `accode` FROM $mfile2
                                    WHERE `party` = '$maccode'  and cancdate = '0000-00-00'
                                    UNION 
                                    SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, vouchno as  `billno`, 'JV' as src,vouchdt,
                                    DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdate`, `debit` as  `billamt`, `accode` FROM $mfile3
                                    WHERE `accode` = '$maccode'  and (debit + credit > 0)  and bs = 'B'
                                    UNION 
                                    SELECT `yearno`,`type`,`vouchno`, `billno`, 'PB' as src, billdate as vouchdt, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                                   `billdate`, `billamt`, `accode` FROM `$prevdb` WHERE `accode` = '$maccode' AND
                                   `type` = '$mtype'  order by vouchdt ASC") or die(mysqli_error($conn));
        } else {

            $subtmp1 = mysqli_query($conn, "SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`,  `billno`, 'PU' as src, vouchdt,
                       DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdate`, `netamt` as  `billamt`, `party` as  `accode` FROM $mfile
                       WHERE `party` = '$maccode' AND `cashcred` != 'C'  and cancdate = '0000-00-00'
                       UNION 
                       SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, vouchno as  `billno`, 'JV' as src,vouchdt,
                       DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdate`, `credit` as  `billamt`, `accode` FROM $mfile3
                       WHERE `accode` = '$maccode'  and credit > 0 and debit = 0  and bs != 'S'                    
                       UNION 
                       SELECT `yearno`,`type`,`vouchno`, `billno`, 'PB' as src, billdate as vouchdt, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                      `billdate`, `billamt`, `accode` FROM `$prevdb` WHERE `accode` = '$maccode' AND
                      `type` = 'P' order by vouchdt ASC ") or die(mysqli_error($conn));
        }


        if (mysqli_num_rows($subtmp1) > 0) {

            $counter = 0;

            echo "<div id='settle'> <table id='bill_table' border='1' class='table table-bordered' width='100%'> ";

            echo "<tr style='background-color:#78909C'>";

            echo "<th style='width:10%'>Year</th>";

//            echo "<th>Voucher</th>";

            echo "<th  style='width:15%'>Bill No.</th>";

            echo "<th style='width:10%;'>Date</th>";

            echo "<th id='numeric'  style='width:10%'>Bill Amount</th>";

            echo "<th id='numeric'  style='width:10%'>Adjusted</th>";

            echo "<th id='numeric'  style='width:10%'>Set Off</th>";

            echo "<th id='numeric'  style='width:10%'>Balance Amt.</th>";

//            echo "<th style='width:7%' id='numeric'>Days</th>";

            echo "</tr>";

            while ($subtmp = mysqli_fetch_assoc($subtmp1)) {
                $qparty = $subtmp["accode"];
                $qyear = $subtmp["yearno"];
                $qbill = $subtmp["billno"];
                $mbilldate = $subtmp["billdate"];
                $qvouch = $subtmp["vouchno"];
                $qamt = $subtmp["billamt"];
                $src = $subtmp["src"];


                $totadj = 0;
                $totset = 0;

                $trn = mysqli_query($conn, "SELECT * FROM `$billdb` WHERE `source` = '$msource' AND
                                   `bnkvouch` = '$mvouch' $binitcond AND `party` = '$qparty' AND 
                                   `yearno` = '$qyear' AND `billno` = '$qbill' and `type` = '$mtype' ") or die(mysqli_error($conn));

//                print table($trn);
                $tranqry = mysqli_fetch_assoc($trn);
                if (mysqli_num_rows($trn) > 0) {
                    $totset = $tranqry["setoff"];
                }


                $trn2 = mysqli_query($conn, " SELECT SUM(`setoff`) AS `totadj`, nodays FROM `$billdb` WHERE 
                                     `yearno` = '$qyear' AND `type` = '$mtype' AND `billno` = '$qbill' AND 
                                     `party` = '$qparty' LIMIT 0,1 ") or die(mysqli_error($conn));



                $tranqry2 = mysqli_fetch_assoc($trn2);
                $totadj = $tranqry2["totadj"];
                $amtadj = ($totadj - $totset);
                $setoff = $totset;
                $balamt = round($qamt - $totadj,2);

                $nodays = $mbilldate - $mvouchdt;
//                print $nodays;

                if ($qamt - $amtadj > 0) {
                    $mbill = $src . " - " . $qbill;
                    $counter++;
                    echo "<tr><td><input type='text' id='yearno_$counter' name='yearno_$counter'
                         value='$qyear' size='2' readonly class='form-control'/></td>";

                    echo "<input type='hidden' id='vouchno_$counter' name='vouchno_$counter'
                         value='$qvouch' readonly size='4' class='form-control' />";

                    echo "<td><input type='text' id='showbillno_$counter' name='showbillno_$counter'
                         value='$mbill' readonly size='7' class='form-control'/></td>
                         <input type='hidden' id='billno_$counter' name='billno_$counter'
                         value='$qbill' readonly size='7' class='form-control'/></td>";

                    echo "<td><input type='text' id='billdate_$counter' name='billdate_$counter'
                         value='$subtmp[billdate]' readonly size='8' class='billdate form-control'/></td>";

                    echo "<td id='numeric'><input type='text' id='billamt_$counter'
                         name='billamt_$counter' value='$qamt' readonly size='11' style='text-align:right;' class='billamt form-control' /></td>";

                    echo "<td id='numeric'><input type='text' id='amtadj_$counter' name='amtadj_$counter'
                         value='$amtadj' readonly size='11' style='text-align:right;' class='amtadj form-control' /></td>";

                    echo "<td id='numeric'><input type='text'  id='setoff_$counter' name='setoff_$counter'
                         value='$setoff' size='11' style='text-align:right;' class='setoff form-control' tabindex='1' autofocus/></td>";

                    echo "<td id='numeric'><input type='text'  id='balamt_$counter' name='balamt_$counter'
                         value='$balamt' readonly size='11' style='text-align:right;' class='balamt form-control' />"
                    . "<input type='hidden'  value='$balamt' readonly size='11' style='text-align:right;' class='balamt2 form-control' /></td>";

//                    echo "<td id='numeric'><input type='text'  id='days_$counter' name='days_$counter'
//                         value='$nodays' readonly size='6' style='text-align:right;' class='days form-control' /></td></tr>";
                }
            }

            echo "</table><input type='text' value='$counter' id='counter' name='counter' hidden='hidden' /> </div>";
        }
    }
}
?>


