<?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" || $grpcode["grpcode"] == "040") {

        $bqry = mysqli_query($conn, "SELECT `bankinit` FROM `accmast` 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 = 'Settlememt Of Bills ( Receipts )';
            $mfile = 'invmain';
            $mfile2 = "";

            $mtype = "R";
        } 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`, a.`vouchno`, a.`vouchno` as `billno`,

                       DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') AS `billdate`, b.`memcash` as  `billamt`, a.`agent` as  `accode`, b.passport,b.paxname FROM $mfile a, invtran b

                       WHERE a.`agent` = '$maccode' and a.vouchno = b.vouchno") or die(mysqli_error($conn));
        } else {

            $subtmp1 = mysqli_query($conn, "SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, tiktinvno as `billno`, 'TK' as src, 
                                            DATE_FORMAT(vouchdt, '%d-%m-%Y') AS `billdate`,  `totamt` as billamt, `supplier` as  accode 
                                            FROM tiktnewmain WHERE `supplier` = '$maccode' 
                                            UNION 
                                            SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, vouchno as `billno`, 'SETT' as src,
                                            DATE_FORMAT(vouchdt, '%d-%m-%Y') AS `billdate`,  `totamt` as billamt, `supplier` as  accode 
                                            FROM settmain WHERE `supplier` = '$maccode' 
                                            UNION
                                            SELECT `yearno`,`type`,`vouchno`, `billno`,'PB' as src, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                                           `billdate`, `billamt`, `accode` FROM `prevbill` WHERE `accode` = '$maccode' AND
                                           `type` = '$mtype'  ") 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>";
            if ($pvar == "R") {

                echo "<th id='numeric'>Passport No.</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"];
                $passport = $subtmp["passport"];
                $billsrc = $subtmp["src"];




                $totadj = 0;
                $totset = 0;
                $pcond = "";

                if ($pvar == "R") {
                    $pcond = " and passport ='$passport'";
                }

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



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

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

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

                    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='billsrc_$counter' name='billsrc_$counter' value='$billsrc'>
                         <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>";

                    if ($pvar == "R") {
                        echo "<td><input type='text' id='passport_$counter' name='passport_$counter'
                         value='$passport' readonly class='passport 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'  id='balamt2_$counter' name='balamt2_$counter'
                         value='$balamt' readonly size='11' style='text-align:right;' class='balamt2 form-control' /></td>";
                }
            }
        } else {
            echo "<center><h1><font color='red'>No bills found for this party</font></h1></center>";
        }
        echo "</table><input type='text' value='$counter' id='counter' name='counter' hidden='hidden' /> </div>";
    }
}
?>


