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

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 `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") {

        if (!empty($bnkcode) && $bnkcode != null) {
            $bqry = mysqli_query($conn, "SELECT `bankinit` FROM `accmast` WHERE `accode` = '$bnkcode' LIMIT 0,1 ");
            $init = mysqli_fetch_assoc($bqry);
            $pbinit = $init["bankinit"];
            if (!empty($pbinit)) {
                $binitcond = " and bankinit = '$pbinit' ";
            }
        } else {
            $pbinit = "";
            $binitcond = "";
        }


        if ($pvar == "R") {
            $mtit = 'Settlememt Of Bills ( Receipts )';
            $mfile = 'invmain';
            $mfile2 = "";

            $mtype = "S";
        } else {

            $mtit = 'Settlememt Of Bills ( Payments )';
            $mfile = 'purmain';

            $mfile2 = "";

            $mtype = "P";
        }

        if ($pvar == "R") {
//            $subtmp1 = mysqli_query($conn, "SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`,`vouchno` as billno,
//                       DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdate`,  `billamt`, `accode` FROM $mfile
//                       WHERE `accode` = '$maccode'  UNION 
//                       SELECT `yearno`,`type`,`vouchno`, `billno`, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
//                      `billdate`, `billamt`, `accode` FROM `prevbill` WHERE `accode` = '$maccode' AND
//                      `type` = '$mtype'  ") or die(mysqli_error($conn));

            mysqli_query($conn, "create temporary table getdbf as  (SELECT `yearno`,`type`,`vouchno`, `billdate`,`billno`, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                      `billdt`, `billamt`, `accode` FROM `prevbill` WHERE `accode` = '$maccode' AND
                      `type` = '$mtype' order by billdate)
                       UNION ALL
                       (SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `vouchdt` as billdate,`vouchno` as billno,
                       DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdt`,  `billamt`, `accode` FROM $mfile
                       WHERE `accode` = '$maccode' order by vouchdt)") or die(mysqli_error($conn));
        } else {
            $subtmp1 = mysqli_query($conn, " (SELECT `yearno`,`type`,`vouchno`, `billno`, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                      `billdate`, `billamt`, `accode` FROM `prevbill` WHERE `accode` = '$maccode' AND
                      `type` = '$mtype'  order by billdate )
                       union all 
                       (SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as  `billno`,
                       DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdate`, `netamt` as  `billamt`, `party` as  `accode` FROM $mfile
                       WHERE `party` = '$maccode' AND `cashcred` != 'C' order by vouchdt) ") or die(mysqli_error($conn));
        }


        $subtmp1 = mysqli_query($conn, "SELECT * FROM `getdbf` order by billdate") or die(mysqli_error($conn));

//        print table($subtmp1);
        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["billdt"];
                $qvouch = $subtmp["vouchno"];
                $qamt = $subtmp["billamt"];


                $totadj = 0;
                $totset = 0;

                $trn = mysqli_query($conn, "SELECT * FROM `billsett` WHERE `source` = '$msource' AND
                                   `bnkvouch` = '$mvouch' $binitcond AND `party` = '$qparty' AND 
                                   `yearno` = '$qyear' AND `vouchno` = '$qvouch' 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 `billsett` WHERE 
                                     `yearno` = '$qyear' AND `type` = '$mtype' AND `vouchno` = '$qvouch' AND 
                                     `party` = '$qparty' LIMIT 0,1 ") or die(mysqli_error($conn));

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

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


                if ($qamt - $amtadj > 0) {
                    $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='billno_$counter' name='billno_$counter'
                         value='$qbill' readonly size='7' class='form-control billno'/>";
                    if ($pvar == 'R') {
                        $getexm = mysqli_query($conn, "SELECT `examcode` FROM `invmain` WHERE `year` = '$qyear' and vouchno='$qvouch'") or die(mysqli_error($conn));
                        $num = mysqli_num_rows($getexm);
                        if ($num > 0) {
                            $get = mysqli_fetch_array($getexm);
                            $exmcode = $get['examcode'];
                        } else {
                            $getpexm = mysqli_query($conn, "SELECT * FROM `prevbill` WHERE `yearno` = '$qyear' and vouchno='$qvouch'") or die(mysqli_error($conn));
                            $getp = mysqli_fetch_array($getpexm);
                            $exmcode = $getp['examcode'];
                        }

                        echo "<input type='hidden' id='examcode_$counter' name='examcode_$counter'
                         value='$exmcode' readonly class='form-control examcode' />";
                    }
                    echo "</td>";

                    echo "<td><input type='text' id='billdate_$counter' name='billdate_$counter'
                         value='$subtmp[billdt]' 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' /></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>";
                }
            }
        } else {
            echo "<center><h1><font color='red'>No bills found for this party</font></h1></center>";
        }
        echo "</table><input type='hidden' value='$counter' id='counter' name='counter'  /> </div>";
    }
}
?>


