<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$comp = $_SESSION["comp_" . $dbname];
mysqli_select_db($conn, $zdb);

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"];
    $parnew = $_GET["parnew"];
    $myear = $_SESSION["tit_" . $dbname];

    $cqry = mysqli_query($conn, "SELECT `grpcode` FROM `accmast` 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" || $grpcode["grpcode"] == "006" || $grpcode["grpcode"] == "039" || $grpcode["grpcode"] == "013") {

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

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

        if ($pvar == "R") {
            $mtit = 'Settlememt Of Bills ( Receipts )';
            $mfile = "rawmain";
            $mfile2 = "gstmain";
            if ($comp > 100) {
                $mpart = "party";
            } else {

                $mpart = "broker";
            }

            $mtype = "S";
        } else {

            $mtit = 'Settlememt Of Bills ( Payments )';
            $mfile = 'purrawmain';
            $mfile2 = "pgstmain";

            $mtype = "P";
        }

        if ($pvar == "R") {
            $subtmp1 = mysqli_query($conn, "SELECT * FROM  (SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as `billno`, vouchdt,

                       DATE_FORMAT(`invdt`, '%d-%m-%Y') AS `billdate`, `netamt` as  `billamt`, `$mpart` as  `accode` , 'SLM' as billsrc FROM `$mfile`

                       WHERE `$mpart` = '$maccode' AND `cashcred` != 'C' 
                           
                       UNION 
                       
                       SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as `billno`, vouchdt,

                       DATE_FORMAT(`invdt`, '%d-%m-%Y') AS `billdate`, `netamt` as  `billamt`, `$mpart` as  `accode`, 'SLFP' as billsrc FROM `$mfile2`

                       WHERE `$mpart` = '$maccode' AND `cashcred` != 'C' 
                           
                       UNION 

                       SELECT `yearno`,`type`,`vouchno`, `billno`, billdate as  vouchdt, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 

                      `billdate`, `billamt`, `accode`, 'PB' as billsrc FROM `prevbill` WHERE `accode` = '$maccode' AND

                      `type` = '$mtype') as output ORDER BY `vouchdt` ASC ") or die(mysqli_error($conn));
        } else {

            $subtmp1 = mysqli_query($conn, "SELECT * FROM  (SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as  `billno`, vouchdt,

                       DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdate`, `netamt` as  `billamt`, `broker` as  `accode`, 'PUM' as billsrc FROM $mfile

                       WHERE `party` = '$maccode' AND `cashcred` != 'C'
                       
                        UNION 

                       SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as  `billno`, vouchdt,

                       DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdate`, `netamt` as  `billamt`, `broker` as  `accode`, 'PUFP' as billsrc FROM $mfile2

                       WHERE `party` = '$maccode' AND `cashcred` != 'C'
                           
                       union
                       SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as  `billno`, vouchdt,

                       DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdate`, `billamt`, `party` as  `accode`, 'PUEX' as billsrc FROM purexpmain

                       WHERE `party` = '$maccode' AND `paidby` != 'C'
                           
                       union
                       
                       SELECT `yearno`,`type`,`vouchno`, `billno`,  billdate as  vouchdt,DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 

                      `billdate`, `billamt`, `accode`, 'PB' as billsrc FROM `prevbill` WHERE `accode` = '$maccode' AND

                      `type` = '$mtype' ) as output 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>Year</th>";

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

            echo "<th>Bill No.</th>";

            echo "<th style='width:125px;'>Date</th>";

            echo "<th id='numeric'>Bill Amount</th>";

            echo "<th id='numeric'>Adjusted</th>";

            echo "<th id='numeric'>Set Off</th>";

            echo "<th id='numeric'>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"];
                $qbillsrc = $subtmp["billsrc"];
                $qbilldate = $subtmp["vouchdt"];

                $totadj = 0;
                $totset = 0;
//                print "SELECT * FROM `billsett` WHERE `source` = '$msource' AND
//                                   `bnkvouch` = '$mvouch' $binitcond AND `party` = '$qparty' and `billsrc` = '$qbillsrc'  AND 
//                                   `yearno` = '$qyear' AND `vouchno` = '$qvouch' and `type` = '$mtype' ";
                $trn = mysqli_query($conn, "SELECT * FROM `billsett` WHERE `source` = '$msource' AND
                                   `bnkvouch` = '$mvouch' AND `party` = '$qparty' and `billsrc` = '$qbillsrc'  AND 
                                   `yearno` = '$qyear' AND `vouchno` = '$qvouch' and `type` = '$mtype' ") or die(mysqli_error($conn));

                $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 `billsett` WHERE 
                                     `yearno` = '$qyear' AND `type` = '$mtype' AND `vouchno` = '$qvouch' AND 
                                     `party` = '$qparty' and `billsrc` = '$qbillsrc' LIMIT 0,1 ") or die(mysqli_error($conn));

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

                $date1 = date_create("$mbilldate");
                $date2 = date_create("$mvouchdt");
                $diff = date_diff($date1, $date2);
                $subtdate = $diff->format("%a");
                $nodays = $subtdate;
//                print $nodays;

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

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

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

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

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

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

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

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

                        echo "<td id='numeric'><input type='text'  id='ddays_$counter' name='ddays_$counter'
                         value='$nodays' readonly size='6' style='text-align:right;' class='ddays form-control' /></td></tr>";
                    } else {
                        echo "<tr><td><input type='text' id='cyearno_$counter' name='cyearno_$counter'
                         value='$qyear' size='2' readonly class='form-control'/></td>";

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


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

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

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

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

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

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

                        echo "<td id='numeric'><input type='text'  id='cdays_$counter' name='cdays_$counter'
                         value='$nodays' readonly size='6' style='text-align:right;' class='cdays form-control' /></td></tr>";
                    }
                }
            }
            if ($parnew == "D") {
                echo "</table><input type='text' value='$counter' id='counter' name='counter' hidden='hidden' /> </div>";
            } else {
                echo "</table><input type='text' value='$counter' id='counter2' name='counter2' hidden='hidden' /> </div>";
            }
        }
    }
}
?>