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

$zdb = $_SESSION["zdbyear_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];

$formname = "LEDGER";
$grid = "ledger.php";
$file = "ledger.php";
$maindb = "accmast";
$para = $_GET["para"];

mysqli_select_db($conn, $zdb);

$trandb = "actran";

if (isset($_POST["view"])) {
    $gcond = "";
    $accond = "";
    $stcond = "";
    $ctcond = "";

    if (!empty($_POST["group"])) {
        $grp = trim($_POST["group"]);
        $gcond = "AND `grpcode`='$grp'";
    }

    if (!empty($_POST["acname"])) {
        $accode = trim($_POST["acname"]);
        $accond = "AND `accode`='$accode'";
    }

    if (!empty($_POST["broker"])) {
        $broker = trim($_POST["broker"]);
        $brokcond = "AND `accode`='$broker'";
    }

    if (!empty($_POST["cmbstation"])) {
        $station = trim($_POST["cmbstation"]);
        $stcond = " AND `station`= '$station' ";
    }

    if (!empty($_POST["city"])) {
        $city = trim($_POST["city"]);
        $ctcond = " AND `city`='$city'";
    }

    $mfrmdate = $_POST["frmdt"];
    $mtodate = $_POST["todt"];

    $recpfrom = $_POST["recpfrom"];
    $recpto = $_POST["recpto"];

    if ($mfrmdate > $mtodate) {
        browse("To Date Cannot Be Less Than From Date");
        header("refresh: 1; ledger.php");
        exit();
    }

    if ($recpfrom > $recpto) {
        browse("Receipt To Date Cannot Be Less Than Receipt From Date");
        header("refresh: 1; ledger.php");
        exit();
    }

    $prnzero = $_POST["zerobal"];
    $prnnarr = $_POST["prinnarr"];

    if ($_POST["dtype"] == 1) {
        $repname = "Ledger - Summary";
        $dtype = 1;
    } else {
        $repname = "Ledger - Detail";
        $dtype = 2;
    }

    //    print "SELECT * FROM `$maindb` WHERE accode!='' $gcond $accond $ctcond $stcond  order by name";

    include("includes/linkshead.php");
    $acc = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE grpcode!='001' and grpcode !='002' $gcond $accond $brokcond $ctcond $stcond  order by name") or die(mysqli_error($conn));
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
?>

    <style>
        table th {
            background-color: #58b19f;
        }
    </style>


    </head>

    <body id="ledger">
        <div class="row">
            <!--<div class="col-lg-6">-->
            <?php
            //                    include ("includes/head_rep.php");
            //                require("includes/dbconnect.php");
            //                $comp = mysqli_query($conn, "SELECT * FROM `company`") or die(mysql_errno());
            //                $zcomp = mysqli_fetch_assoc($comp);
            //                echo "<font size='4px' color=''><b>$zcomp[name]</font></b></font><br />";
            //                echo "<b><font color=''>$repname &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            //                &nbsp;&nbsp;&nbsp;&nbsp;<font size='3px'>Date : " . date("M-d-Y") . "</b><br />";
            //                echo "From Date - " . date("d-m-Y", strtotime($mfrmdate)) . "<br/>To Date &nbsp;&nbsp;&nbsp;&nbsp;-  " . date("d-m-Y", strtotime($mtodate)) . "<br/>";
            ?>
            <!--</div>-->
            <!--            <div class="col-lg-6">
                            <label class="label-control marg">Print - </label>
                            <label class="label-control marg"><?php echo $formname; ?></label>
                        </div>-->
            <!--            <div class="col-lg-1" style="float: right;">
                            <a href="<?= $file ?>" class="btn btn-primary">GO Back</a>
                            <a onclick="window.close()" class="btn btn-primary">GO Back</a>
                        </div>-->
        </div>
        <?php
        //        require("includes/dbconnect.php");
        //
        //        $comp = mysqli_query($conn, "SELECT * FROM `company`") or die(mysql_errno());
        //
        //        $zcomp = mysqli_fetch_assoc($comp);
        //
        //        echo "<font size='4px' color=''><b>$zcomp[compname]</font></b></font><br />";
        //
        //        echo "<font size='2px' color=''><b> $zcomp[add1]. $zcomp[add2].  $zcomp[add3] </b></font><br /><br />";
        //
        //        echo "<b><font color=''>$repname &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        //
        //                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        //
        //                &nbsp;&nbsp;&nbsp;&nbsp;<font size='3px'>Date : " . date("M-d-Y") . "</b><br />";
        //
        //        echo "From Date - " . date("d-m-Y", strtotime($mfrmdate)) . "<br/>To Date &nbsp;&nbsp;&nbsp;&nbsp;-  " . date("d-m-Y", strtotime($mtodate)) . "<br/>";
        //
        //        echo "<br />";
        ?>

        <div class="container pad" style="width:100%">
            <?php include("includes/head_rep.php");
            ?>
        </div>
        <div class="row" style="text-align:center">
            <div class="col-lg-5 text-center" style="width:100%;">

                <div class="box">

                    <!--                    <header>
                    
                                            <h5><?php // echo $formname;                   
                                                ?></h5>
                                        </header>         -->

                    <div id="sortableTable" class="body collapse in">
                        <table class="table table-bordered sortableTable responsive-table">
                            <thead class="mySearchTr">
                                <tr>
                                    <?php if ($dtype == 1) { ?>
                                        <th align="left">Account Name<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="right" style="text-align:right;">Debit<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="right" style="text-align:right;">Credit<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="right" style="text-align:right;">Balance<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                    <?php
                                    }
                                    ?>
                                </tr>
                            </thead>
                            <tbody>

                                <?php
                                mysqli_select_db($conn, $zdb);

                                $totbal = 0;
                                $url = '';
                                while ($getac = mysqli_fetch_array($acc)) {

                                    if ($dtype == 1) {
                                        echo '<tr>';
                                        $maccode = $getac["accode"];
                                        $mgrpcode = $getac["grpcode"];
                                        $mopbal = $getac["openbal"];
                                        $mopdc = $getac["dc"];
                                        if ($mopdc == "D") {
                                            $mopbal = $mopbal * -1;
                                        }

                                        ############PARTY NAME####################

                                        $getpartyname = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$maccode'") or die(mysqli_error($conn));
                                        $zname = mysqli_fetch_object($getpartyname);
                                        $mname = $zname->name;

                                        ############PARTY NAME####################

                                        $getdeb = mysqli_query($conn, "SELECT SUM(amount) as debamt FROM `$trandb` WHERE accode='$maccode' and amount < 0
                                                                              and date >= '$mfrmdate' and date <= '$mtodate'") or die(mysqli_error($conn));
                                        $fetdeb = mysqli_fetch_array($getdeb);
                                        $debamt = $fetdeb["debamt"];

                                        if ($mgrpcode != '010') {
                                            $getcrd = mysqli_query($conn, "SELECT SUM(amount) as crdamt FROM `$trandb` WHERE accode='$maccode' and amount > 0
                                                                              and date >= '$mfrmdate' and date <= '$mtodate'") or die(mysqli_error($conn));
                                        } else {
                                            $getcrd = mysqli_query($conn, "SELECT SUM(amount) as crdamt FROM `$trandb` WHERE accode='$maccode' and amount > 0
                                                                              and date >= '$recpfrom' and date <= '$recpto'") or die(mysqli_error($conn));
                                        }
                                        $fetcrd = mysqli_fetch_array($getcrd);
                                        $crdamt = $fetcrd["crdamt"];

                                        //                                        $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE accode='$maccode' AND  date <= '$mtodate'") or die(mysqli_error($conn));
                                        //                                        $getamt = mysqli_fetch_array($getbal);
                                        //                                        $amt = $getamt["amt"];

                                        $amt = abs($crdamt) - abs($debamt);

                                        $mbal = $mopbal + $amt;
                                        if ($mbal < 0) {
                                            $mdc = "DR";
                                        } else {
                                            $mdc = "CR";
                                        }

                                        $show = 1;

                                        if ($prnzero == 1) {
                                            $show = 1;
                                        } else {
                                            if ($mbal == 0) {
                                                $show = 0;
                                            }
                                        }

                                        if ($show == 1) {
                                            echo "<td style='text-align:left'>$mname</td>";
                                            echo "<td style='text-align:right'>" . abs($debamt) . "</td>";
                                            echo "<td style='text-align:right'>" . abs($crdamt) . "</td>";
                                            echo "<td style='text-align:right'>" . abs(round($mbal, 2)) . " " . $mdc . "</td>";

                                            $totdeb = $totdeb + $debamt;
                                            $totcrd = $totcrd + $crdamt;
                                            $totbal = $totbal + $mbal;
                                        }

                                        if ($totbal < 0) {
                                            $mdctot = "DR";
                                        } else {
                                            $mdctot = "CR";
                                        }
                                    } else {

                                        /** Detail Part* */
                                        $totbal = 0;
                                        $dtot = 0;
                                        $ctot = 0;

                                        $maccode = $getac["accode"];
                                        $mgrpcode = $getac["grpcode"];
                                        $macname = $getac["name"];
                                        $mopbal = $getac["openbal"];
                                        $mopdc = $getac["dc"];

                                        if ($mopdc == "D") {
                                            $mopbal = $mopbal * -1;
                                        }

                                        if ($mgrpcode != '010') {
                                            $ctran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
                                                                  WHERE accode = '$maccode' AND `date`>='$mfrmdate' AND `date`<='$mtodate'
                                                                  ORDER BY `date`, vouchno") or die(mysqli_error($conn));
                                        } else {
                                            $ctran1 = mysqli_query($conn, "SELECT * from 
                                                                  (select * , DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
                                                                  WHERE accode = '$maccode' AND `date`>='$mfrmdate' AND `date`<='$mtodate'
                                                                  and amount < 0    
                                                                  union all    
                                                                  select * , DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
                                                                  WHERE accode = '$maccode' AND `date`>='$recpfrom' AND `date`<='$recpto' 
                                                                  and amount > 0 ) as a
                                                                  ORDER BY `date`, vouchno") or die(mysqli_error($conn));
                                        }

                                        //                                        print table($ctran1);
                                        $curl = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
                                                                 WHERE accode = '$maccode' AND `date`>='$mfrmdate' AND `date`<='$mtodate'
                                                                 ORDER BY `date`, vouchno") or die(mysqli_error($conn));

                                        while ($murl = mysqli_fetch_object($curl)) {
                                            switch ($murl->source) {
                                                case "PU";
                                                    $head = "Purchase";
                                                    $url = "purch.php?vouchno=$murl->vouchno&method=ledger";
                                                    break;
                                                case "PR";
                                                    $head = "Purchase Return";
                                                    $url = "debnote.php?vouchno=$murl->vouchno&method=ledger";
                                                    break;
                                                case "SL";
                                                    $head = "Sale";
                                                    $url = "sale.php?vouchno=$murl->vouchno&method=ledger";
                                                    break;
                                                case "SLM";
                                                    $head = "Sale Raw Material";
                                                    $url = "sale.php?vouchno=$murl->vouchno&method=ledger";
                                                    break;
                                                case "SRM";
                                                    $head = "Sale Return - Raw Material";
                                                    $url = "salematreturn.php?vouchno=$murl->vouchno&method=ledger";
                                                    break;
                                                case "SLTX";
                                                    $head = "Sale - GST";
                                                    $url = "salegstmulti.php?vouchno=$murl->vouchno&method=ledger";
                                                    break;
                                                case "PUTX";
                                                    $head = "Purchase - GST";
                                                    $url = "purchgstmulti.php?vouchno=$murl->vouchno&method=ledger";
                                                    break;
                                                case "PUEX";
                                                    $head = "Purchase Expense";
                                                    $url = "purchexp.php?vouchno=$murl->vouchno&method=ledger";
                                                    break;
                                                case "BRBP";
                                                    $head = "Transfer - Customer to Karigar";
                                                    $url = "custtokari.php?vouchno=$murl->vouchno&method=ledger";
                                                    break;
                                                case "SR";
                                                    $head = "Sale Return";
                                                    $url = "crednote.php?vouchno=$murl->vouchno&method=ledger";
                                                    break;
                                                case "BR";
                                                    $url = "bankrecp.php?vouchno=$murl->vouchno&method=ledger";
                                                    break;
                                                case "BP";
                                                    $head = "Bank Payment";
                                                    $url = "bankpay.php?$murl->vouchno&method=ledger";
                                                    break;
                                                case "CR";
                                                    $head = "Cash Receipt";
                                                    $url = "cashrecp.php?$murl->vouchno&method=ledger";
                                                    break;
                                                case "CP";
                                                    $head = "Cash Payment";
                                                    $url = "cashpay.php?$murl->vouchno&method=ledger";
                                                    break;
                                                case "TR";
                                                    $head = "Contra / Transfer";
                                                    $url = "contra.php?vouchno=$murl->vouchno&method=ledger";
                                                    break;
                                                case "JV";
                                                    $head = "Journal Voucher";
                                                    $url = "journal.php?$murl->vouchno&method=ledger";
                                                    break;
                                                case "JR";
                                                    $head = "Job Received";
                                                    $url = "jobreceived.php?$murl->vouchno&method=ledger";
                                                    break;
                                                case "DG";
                                                    $head = "Discount Given";
                                                    break;
                                                case "DR";
                                                    $head = "Discount Received";
                                                    break;
                                                default:
                                                    $head = "Other";
                                            }
                                        }

                                        $rtran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`retdate`, '%d-%m-%Y') as mydt FROM `$trandb` WHERE
                                                           retdate >= '$mfrmdate' AND retdate <= '$mtodate' AND
                                                           accode = '$maccode' ORDER BY date, vouchno") or die(mysqli_error($conn));

                                        $getbal = mysqli_query($conn, "SELECT SUM(amount) as oamt FROM `$trandb` WHERE accode='$maccode' AND  date < '$mfrmdate'") or die(mysqli_error($conn));
                                        $getamt = mysqli_fetch_array($getbal);
                                        $oamt = $getamt["oamt"];

                                        $mobal = $mopbal + $oamt;

                                        $totunclaimgstamt = 0;
                                        $mdebit = 0;
                                        $mcredit = 0;

                                        if ($mobal < 0) {
                                            $mdc = "DR";
                                            $mdebit = abs($mobal);
                                            $dtot = $dtot + $mdebit;
                                        } else {
                                            $mdc = "CR";
                                            $mcredit = abs($mobal);
                                            $ctot = $ctot + $mcredit;
                                        }

                                        $vdate1 = strtotime($mfrmdate);
                                        $vdate = date('d-m-Y', $vdate1);
                                        $mvouch = "";

                                        $mdescrip = "Opening Balance";
                                        $totbal = $totbal + $mobal;

                                        if ($totbal < 0) {
                                            $mdctot = "DR";
                                        } else {
                                            $mdctot = "CR";
                                        }

                                        echo "<tr>";
                                        echo "<th style=\"text-align:left;\" scope=\"col\" width=\"500\" colspan='9'><b>" . strtoupper($macname) . "</b></th>";
                                        echo "</tr>";
                                ?>

                                        <tr>
                                            <th align="left">Date<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                            <th align="left">Source<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                            <th align="left">Voucher<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                            <th align="left">Description<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                            <th align="right" style="text-align:right;">Unclaim Amt<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                            <th align="right" style="text-align:right;">Debit<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                            <th align="right" style="text-align:right;">Credit<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                            <th align="right" style="text-align:right;">Balance<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        </tr>

                                <?php
                                        echo "<tr  class='clickable-row' href='$url'>";
                                        echo "<td style=\"text-align:left;\" scope=\"col\"  width=\"14%\">$vdate</td>";
                                        echo "<td style=\"text-align:left;\" scope=\"col\"  width=\"4%\"></td>";
                                        echo "<td style=\"text-align:left;\" scope=\"col\"  width=\"8%\">$mvouch</td>";
                                        echo "<td style=\"text-align:left;\" scope=\"col\"  width=\"35%\">$mdescrip</td>";
                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"8%\"></td>";
                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"10%\">$mdebit</td>";
                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"10%\">$mcredit</td>";
                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"11%\">" . abs($totbal) . " " . $mdctot . "</td>";
                                        echo "</tr>";

                                        while ($row = mysqli_fetch_array($ctran1)) {
                                            $vdate = $row["mydt"];
                                            $msource = $row["source"];
                                            $mvouch = $row["vouchno"];
                                            $mamount = $row["amount"];

                                            $mgrpcode = $getac["grpcode"];
                                            $mdiscprov = $getac["discount"];
                                            $totdiscprov = 0;

                                            $unclaimgstamt = 0;
                                            $mdebit = 0;
                                            $mcredit = 0;

                                            if ($mamount < 0) {
                                                $mdc = "DR";
                                                $mdebit = abs($mamount);
                                                $dtot = $dtot + $mdebit;
                                            } else {
                                                $mdc = "CR";
                                                $mcredit = abs($mamount);
                                                $ctot = $ctot + $mcredit;
                                            }

                                            $totbal = $totbal + $mamount;

                                            if ($totbal < 0) {
                                                $mdctot = "DR";
                                            } else {
                                                $mdctot = "CR";
                                            }

                                            $mdescrip = "";
                                            $mchqdtl = "";
                                            $mnarr = "";
                                            $colr = "";

                                            switch ($msource) {
                                                case "PUM";
                                                    $head = "Purchase Raw Material";
                                                    $munclaimgst = '';
                                                    if ($para == "K") {
                                                        $url = "purchmat.php?vouchno=$mvouch&mode=edit";
                                                    } else {
                                                        $url = "purchmatgst.php?vouchno=$mvouch&mode=edit";
                                                    }
                                                    $totdiscprov = $totdiscprov + $mamount;

                                                    //                                                    if ($prnnarr == 1) {
                                                    $nqry1 = mysqli_query($conn, "SELECT `invno` as billno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2`, `unclaimgst`, totgst5, totgst12, totgst18, totgst28 
                                                                         FROM `purrawmain` WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                                    $nqry = mysqli_fetch_array($nqry1);

                                                    $mbill = $nqry["billno"];
                                                    $mbilldt = $nqry["mbilldate"];
                                                    $munclaimgst = $nqry["unclaimgst"];

                                                    $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);

                                                    $mchqdtl = "Bill No. : " . trim($mbill);
                                                    //                                                    }
                                                    if ($munclaimgst == '1') {
                                                        $head = "Purchase Raw Material - (Unclaim GST) ";
                                                        $colr = "color:#ff006e";
                                                        $unclaimgstamt = $nqry["totgst5"] + $nqry["totgst12"] + $nqry["totgst18"] + $nqry["totgst28"];
                                                        $totunclaimgstamt = $totunclaimgstamt + $unclaimgstamt;
                                                    }

                                                    break;
                                                case "PRRW";
                                                    $head = "Purchase Return Raw Material";
                                                    if ($para == "K") {
                                                        $url = "debnoteraw.php?vouchno=$mvouch&mode=edit";
                                                    } else {
                                                        $url = "debnoteraw_gst.php?vouchno=$mvouch&mode=edit";
                                                    }
                                                    $totdiscprov = $totdiscprov + $mamount;

                                                    if ($prnnarr == 1) {
                                                        $nqry1 = mysqli_query($conn, "SELECT `invno` as billno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `debrawmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                                        $nqry = mysqli_fetch_array($nqry1);

                                                        $mbill = $nqry["billno"];
                                                        $mbilldt = $nqry["mbilldate"];

                                                        $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);

                                                        $mchqdtl = "Bill No. : " . trim($mbill);
                                                    }
                                                    break;
                                                case "PU";
                                                    $head = "Purchase";
                                                    $url = "purch.php?vouchno=$mvouch&mode=edit";
                                                    $totdiscprov = $totdiscprov + $mamount;

                                                    if ($prnnarr == 1) {
                                                        $nqry1 = mysqli_query($conn, "SELECT invno as billno, DATE_FORMAT(`billdate`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `purmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                                        $nqry = mysqli_fetch_array($nqry1);
                                                        $mbill = $nqry["billno"];
                                                        $mbilldt = $nqry["mbilldate"];
                                                        $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);

                                                        $mchqdtl = "Bill No. : " . trim($mbill);
                                                    }
                                                    break;

                                                case "PUFP";
                                                    $head = "Purchase Finished Products";
                                                    $munclaimgst = '';

                                                    if ($para == "K") {
                                                        $url = "purchase.php?vouchno=$mvouch&mode=edit";
                                                        $tablenm = "pgstmain";
                                                    } else {
                                                        $url = "purchgstmulti.php?vouchno=$mvouch&mode=edit&method=ledger";
                                                        $tablenm = "pgstmain";
                                                    }
                                                    $totdiscprov = $totdiscprov + $mamount;

                                                    // if ($prnnarr == 1) {
                                                    $nqry1 = mysqli_query($conn, "SELECT invno as billno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                                                                 `remark1`, `remark2`, `unclaimgst`, totgst5, totgst12, totgst18, totgst28 
                                                                                  FROM `$tablenm` WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                                    $nqry = mysqli_fetch_array($nqry1);
                                                    $mbill = $nqry["billno"];
                                                    $mbilldt = $nqry["mbilldate"];
                                                    $munclaimgst = $nqry["unclaimgst"];

                                                    $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);

                                                    $mchqdtl = "Bill No. : " . trim($mbill);
                                                    // }
                                                    if ($munclaimgst == '1') {
                                                        $head = "Purchase Finished Products - (Unclaim GST) ";
                                                        $colr = "color:#ff006e";
                                                        $unclaimgstamt = $nqry["totgst5"] + $nqry["totgst12"] + $nqry["totgst18"] + $nqry["totgst28"];
                                                        $totunclaimgstamt = $totunclaimgstamt + $unclaimgstamt;
                                                    }
                                                    break;

                                                case "PUEX";
                                                    $head = "Purchase Expense";
                                                    $munclaimgst = '';

                                                    $url = "purchase_exp.php?vouchno=$mvouch&mode=edit&method=ledger";
                                                    $tablenm = "purexpmain";

                                                    $totdiscprov = $totdiscprov + $mamount;

                                                    // if ($prnnarr == 1) {
                                                    $nqry1 = mysqli_query($conn, "SELECT invno as billno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                                                                 `rem1`, `rem2`,`unclaimgst`, totgst5, totgst12, totgst18, totgst28 
                                                                                  FROM `$tablenm` WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                                    $nqry = mysqli_fetch_array($nqry1);
                                                    $mbill = $nqry["billno"];
                                                    $mbilldt = $nqry["mbilldate"];
                                                    $munclaimgst = $nqry["unclaimgst"];
                                                    
                                                    $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);

                                                    $mchqdtl = "Bill No. : " . trim($mbill);
                                                    if ($munclaimgst == '1') {
                                                        $head = "Purchase Expense - (Unclaim GST) ";
                                                        $colr = "color:#ff006e";
                                                        $unclaimgstamt = $nqry["totgst5"] + $nqry["totgst12"] + $nqry["totgst18"] + $nqry["totgst28"];
                                                        $totunclaimgstamt = $totunclaimgstamt + $unclaimgstamt;
                                                    }
                                                    break;

                                                case "PR";
                                                    $head = "Purchase Return";
                                                    $url = "debnote.php?vouchno=$mvouch&method=ledger";
                                                    $totdiscprov = $totdiscprov + $mamount;
                                                    if ($prnnarr == 1) {
                                                        $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `debmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                                                        $nqry = mysqli_fetch_array($nqry1);

                                                        $mbill = $nqry["invno"];
                                                        $mbilldt = $nqry["mbilldate"];
                                                        $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                                                        $mchqdtl = "Bill No. : " . trim($mbill);
                                                    }
                                                    break;
                                                case "PRFP";
                                                    $head = "(GR) Purchase Return Finish Products";

                                                    if ($para == "K") {
                                                        $url = "debnote.php?vouchno=$mvouch&mode=edit&method=ledger";
                                                        $tablenm = "debmain";
                                                    } else {
                                                        $tablenm = "debmain";
                                                        $url = "debnotegstmulti.php?vouchno=$mvouch&mode=edit&method=ledger";
                                                    }

                                                    $totdiscprov = $totdiscprov + $mamount;
                                                    if ($prnnarr == 1) {
                                                        $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `$tablenm`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                                                        $nqry = mysqli_fetch_array($nqry1);

                                                        $mbill = $nqry["invno"];
                                                        $mbilldt = $nqry["mbilldate"];
                                                        $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                                                        $mchqdtl = "Bill No. : " . trim($mbill);
                                                    }
                                                    break;

                                                case "SL";
                                                    $head = "Sale";
                                                    if ($prnnarr == 1) {
                                                        $mnarr = "";
                                                        $mchqdtl = "";
                                                    }
                                                    break;

                                                case "SLM";
                                                    $head = "Sale Raw Material";

                                                    if ($para == "K") {
                                                        $url = "salemat.php?vouchno=$mvouch&mode=edit";
                                                    } else {
                                                        $url = "salematgstmulti.php?vouchno=$mvouch&mode=edit";
                                                    }

                                                    if ($prnnarr == 1) {
                                                        $mnarr = "";
                                                        $mchqdtl = "";
                                                        $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `rawmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                                                        $nqry = mysqli_fetch_array($nqry1);

                                                        $mbill = $nqry["invno"];
                                                        $mbilldt = $nqry["mbilldate"];
                                                        $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);

                                                        $mchqdtl = "Bill No. : " . trim($mbill);
                                                    }
                                                    break;

                                                case "SLFP";

                                                    $head = "Sale Finished Product";
                                                    if ($para == "K") {
                                                        $tablenm = "gstmain";
                                                        $url = "sale.php?vouchno=$mvouch&mode=edit&method=ledger";
                                                    } else {
                                                        $tablenm = "gstmain";
                                                        $url = "salegstmulti.php?vouchno=$mvouch&mode=edit&method=ledger";
                                                    }

                                                    if ($prnnarr == 1) {
                                                        $mnarr = "";
                                                        $mchqdtl = "";
                                                        $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `$tablenm`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                                                        $nqry = mysqli_fetch_array($nqry1);

                                                        $mbill = $nqry["invno"];
                                                        $mbilldt = $nqry["mbilldate"];
                                                        $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);

                                                        $mchqdtl = "Bill No. : " . $mbill;
                                                    }
                                                    break;

                                                case "SRFP";
                                                    $head = "Sale Return Finished Product";

                                                    if ($para == "K") {
                                                        $url = "crednote.php?vouchno=$mvouch&mode=edit";
                                                    } else {
                                                        $url = "creditnotegstmulti.php?vouchno=$mvouch&mode=edit";
                                                    }

                                                    if ($prnnarr == 1) {
                                                        $mnarr = "";
                                                        $mchqdtl = "";
                                                        $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `credmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                                                        $nqry = mysqli_fetch_array($nqry1);

                                                        $mbill = $nqry["invno"];
                                                        $mbilldt = $nqry["mbilldate"];
                                                        $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                                                        //                                                        $mchqdtl = "Bill No. : " .  trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                                        $mchqdtl = "Bill No. : " . $mbill;

                                                        //                                                        browse($mchqdtl);
                                                    }
                                                    break;

                                                case "SLFB";
                                                    $head = "Sale Finished Product - Brokerage";
                                                    $url = "salematgstmulti.php?vouchno=$mvouch&mode=edit";

                                                    if ($prnnarr == 1) {
                                                        $mnarr = "";
                                                        $mchqdtl = "";
                                                    }
                                                    break;

                                                case "SLAM";
                                                    $head = "Sale Only Amount";
                                                    $url = "saleamt.php?vouchno=$mvouch&mode=edit";

                                                    if ($prnnarr == 1) {
                                                        $mnarr = "";
                                                        $mchqdtl = "";
                                                        $nqry1 = mysqli_query($conn, "SELECT recpno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `saleamt`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                                                        $nqry = mysqli_fetch_array($nqry1);

                                                        $mbill = $nqry["recpno"];
                                                        $mbilldt = $nqry["mbilldate"];
                                                        $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                                                        //                                                        $mchqdtl = "Bill No. : " .  trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                                        $mchqdtl = "Bill No. : " . $mbill;

                                                        //                                                        browse($mchqdtl);
                                                    }
                                                    break;

                                                case "SRM";
                                                    $head = "Sale Return - Raw Material";
                                                    $url = "crednoteraw_gst.php?vouchno=$mvouch&mode=edit";
                                                    if ($prnnarr == 1) {
                                                        $mnarr = "";
                                                        $mchqdtl = "";
                                                        $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `credrawmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                                                        $nqry = mysqli_fetch_array($nqry1);

                                                        $mbill = $nqry["invno"];
                                                        $mbilldt = $nqry["mbilldate"];
                                                        $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);

                                                        //                                                        $mchqdtl = "Bill No. : " .  trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                                        $mchqdtl = "Bill No. : " . $mbill;
                                                        //                                                        browse($mchqdtl);
                                                    }
                                                    break;

                                                case "SLTX";
                                                    $head = "Sale - GST";
                                                    if ($prnnarr == 1) {
                                                        $mnarr = "";
                                                        $mchqdtl = "";
                                                    }
                                                    break;

                                                case "PUTX";
                                                    $head = "Purchase - GST";
                                                    if ($prnnarr == 1) {
                                                        $mnarr = "";
                                                        $mchqdtl = "";
                                                    }
                                                    break;

                                                case "BRBP";
                                                    $head = "Transfer - Customer to Karigar";
                                                    $nqry11 = mysqli_query($conn, "SELECT party, karigar,acnum FROM `custtokari`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                                                    $n1qry = mysqli_fetch_array($nqry11);
                                                    $party = $n1qry["party"];
                                                    $karigar = $n1qry["karigar"];
                                                    $acnum = $n1qry["acnum"];
                                                    if ($mgrpcode == '010') {
                                                        $getname = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                            WHERE accode='$karigar'") or die(mysqli_error($conn));
                                                    } else {
                                                        $getname = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                            WHERE accode='$party'") or die(mysqli_error($conn));
                                                    }

                                                    $fetname = mysqli_fetch_array($getname);
                                                    $showname = $fetname['name'];
                                                    $head = $head . '( ' . $showname . '-' . $acnum . ' )';
                                                    if ($prnnarr == 1) {
                                                        $mnarr = "";
                                                        $mchqdtl = "";
                                                    }
                                                    break;

                                                case "SR";
                                                    $head = "Sale Return";
                                                    break;

                                                case "BR";
                                                    $head = "Bank Receipt";
                                                    $url = "bankrecp.php?vouchno=$mvouch&mode=edit&method=ledger";
                                                    if ($prnnarr == 1) {
                                                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                       `remark1`,remark2 FROM `bankrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                                                        $nqry = mysqli_fetch_array($nqry1);
                                                        $mbill = $nqry["chqno"];
                                                        $mbilldt = $nqry["mbilldate"];
                                                        $mnarr = $nqry["remark1"] . " " . $nqry["remark2"];
                                                        $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                                    }
                                                    break;

                                                case "BP";
                                                    $head = "Bank Payment";
                                                    $url = "bankpay.php?vouchno=$mvouch&mode=edit&method=ledger";
                                                    if ($prnnarr == 1) {
                                                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                        `remark1`,remark2 FROM `bankpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                                                        $nqry = mysqli_fetch_array($nqry1);
                                                        $mbill = $nqry["chqno"];
                                                        $mbilldt = $nqry["mbilldate"];
                                                        $mnarr = $nqry["remark1"] . " " . $nqry["remark2"];

                                                        $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                                    }
                                                    break;

                                                case "CR";
                                                    $head = "Cash Receipt";
                                                    $url = "cashrecp.php?vouchno=$mvouch&mode=edit";
                                                    if ($prnnarr == 1) {
                                                        $nqry1 = mysqli_query($conn, "SELECT `remark1`,remark2 FROM `cashrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                                                        $nqry = mysqli_fetch_array($nqry1);
                                                        $mnarr = $nqry["remark1"] . " " . $nqry["remark2"];
//                                                        browse($mnarr);
                                                    }
                                                    break;

                                                case "CP";
                                                    $head = "Cash Payment";
                                                    $url = "cashpay.php?vouchno=$mvouch&mode=edit&method=ledger";
                                                    if ($prnnarr == 1) {
                                                        $nqry1 = mysqli_query($conn, "SELECT `remark1`,remark2 FROM `cashpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                                                        $nqry = mysqli_fetch_array($nqry1);
                                                        $mnarr = $nqry["remark1"] . " " . $nqry["remark2"];
                                                    }
                                                    break;

                                                case "TR";
                                                    $head = "Contra / Transfer";
                                                    $url = "contra.php?vouchno=$mvouch&method=ledger";
                                                    if ($prnnarr == 1) {
                                                        $nqry1 = mysqli_query($conn, "SELECT `narr1`,narr2 FROM `contra`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                                                        $nqry = mysqli_fetch_array($nqry1);
                                                        $mnarr = $nqry["narr1"] . " " . $nqry["narr2"];
                                                    }
                                                    break;

                                                case "JV";
                                                    $head = "Journal Voucher";
                                                    $url = "jv.php?vouchno=$mvouch&mode=edit";

                                                    break;

                                                case "JVS";
                                                    $head = "Journal Voucher Single";
                                                    $url = "jventry.php?vouchno=$mvouch&mode=edit";

                                                    break;

                                                case "JOBR";
                                                    $head = "Job Received";
                                                    $url = "jobreceived.php?vouchno=$mvouch&mode=edit";

                                                    break;

                                                case "DG";
                                                    $head = "Discount Given";
                                                    $url = "discgvn.php?vouchno=$mvouch&mode=edit";

                                                    if ($accode == '13') {
                                                        $nqry11 = mysqli_query($conn, "SELECT a.accode, b.name FROM `discgive` a, accmast b 
                                                                                          WHERE a.vouchno='$mvouch' and a.accode = b.accode ") or die(mysqli_error($conn));
                                                        $n1qry = mysqli_fetch_array($nqry11);
                                                        $showname = $n1qry["name"];
                                                        $head = $showname;
                                                    }

                                                    if ($prnnarr == 1) {
                                                        $mnarr = "";
                                                        $mchqdtl = "";
                                                    }
                                                    break;

                                                case "DR";
                                                    $head = "Discount Received";
                                                    $url = "discrecd.php?vouchno=$mvouch&mode=edit";

                                                    if ($accode == '1') {
                                                        $nqry11 = mysqli_query($conn, "SELECT a.accode, b.name FROM `discrecd` a, accmast b 
                                                                                          WHERE a.vouchno='$mvouch' and a.accode = b.accode ") or die(mysqli_error($conn));
                                                        $n1qry = mysqli_fetch_array($nqry11);
                                                        $showname = $n1qry["name"];
                                                        $head = $showname;
                                                    }
                                                    if ($prnnarr == 1) {
                                                        $mnarr = "";
                                                        $mchqdtl = "";
                                                    }
                                                    break;

                                                default:
                                                    $head = "Other";
                                            }

                                            echo "<tr  class='clickable-row2' data-href='$url' data-target='_blank'>";
                                            // echo "<tr  class='' >";
                                            echo "<td style=\"text-align:left;\" scope=\"col\"  width=\"14%\">$vdate</td>";
                                            echo "<td style=\"text-align:left;\" scope=\"col\"  width=\"4%\">$msource</td>";
                                            echo "<td style=\"text-align:left;\" scope=\"col\"  width=\"8%\">$mvouch</td>";
                                            if ($mchqdtl == '') {
                                                echo "<td style=\"text-align:left; $colr;\" scope=\"col\"  width=\"35%\">$head</td>";
                                            } else {
                                                echo "<td style=\"text-align:left; $colr;\" scope=\"col\"  width=\"35%\">$head - $mchqdtl</td>";
                                            }
                                            echo "<td style=\"text-align:right;$colr;\" scope=\"col\" width=\"8%\">$unclaimgstamt</td>";
                                            echo "<td style=\"text-align:right;$colr;\" scope=\"col\" width=\"10%\">$mdebit</td>";
                                            echo "<td style=\"text-align:right;$colr;\" scope=\"col\" width=\"10%\">$mcredit</td>";
                                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"11%\">" . abs(round($totbal, 2)) . " " . $mdctot . "</td>";
                                            echo "</tr>";

                                            if ($prnnarr == 1) {
                                                if (!empty($mnarr)) {
                                                    echo "<tr>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td style=\"text-align:left;\" scope=\"col\" width=\"200\">$mnarr</td>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "<td></td>";
                                                    echo "</tr>";
                                                }
                                            }
                                        }

                                        echo "<tr style='background-color:white'>";
                                        echo "<td style=\"text-align:left;\" scope=\"col\" width=\"14%\"></td>";
                                        echo "<td style=\"text-align:left;\" scope=\"col\" width=\"4%\"></td>";
                                        echo "<td style=\"text-align:left;\" scope=\"col\" width=\"4%\"></td>";
                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"32%\" ><strong>Opening : " . round(abs($mobal)) . "</strong>";
                                        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Total</td>";
                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"8%\">" . (round($totunclaimgstamt)) . "</td>";
                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"10%\">" . (round($dtot)) . "</td>";
                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"10%\">" . (round($ctot)) . "</td>";
                                        if ($totbal < 0) {
                                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"11%\"><font color='red'><strong>" . (round(abs($totbal))) . " " . $mdctot . "</strong></font></td>";
                                        } else {
                                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"11%\"><font color='green'><strong>" . (round(abs($totbal))) . " " . $mdctot . "</strong></font></td>";
                                        }
                                        echo "</tr>";
                                    }
                                }

                                if ($dtype == 1) {
                                    echo '</tr>';

                                    echo "<tr>";
                                    echo "<td align='right'><strong>Total</strong></td>";
                                    echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><strong>" . abs($totdeb) . "</strong></td>";
                                    echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><strong>" . abs($totcrd) . "</strong></td>";
                                    echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><strong>" . abs($totbal) . " " . $mdctot . "</strong></td>";
                                    echo "</tr>";
                                }

                                //                                if ($grp == "020") {
                                //                                    
                                //                                    //// gst input claimed
                                //                                    
                                //                                    mysqli_query($conn, "drop temporary table if exists dbftax") or die(mysqli_error($conn));
                                //                                    mysqli_query($conn, "drop temporary table if exists rettax") or die(mysqli_error($conn));
                                //                                    mysqli_query($conn, "create temporary table dbftax 
                                //                                                SELECT `cgst5`,`sgst5`,`igst5`,`cgst12`, `sgst12`,`igst12`, 
                                //                                                       `cgst18`,`sgst18`, `igst18`,`cgst28`, `sgst28`,`igst28` 
                                //                                                        FROM `pgstmain` WHERE  vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate' and unclaimgst != 1
                                //                                                union all 
                                //                                                SELECT `cgst5`,`sgst5`,`igst5`,`cgst12`, `sgst12`,`igst12`, 
                                //                                                       `cgst18`,`sgst18`, `igst18`, `cgst28`, `sgst28`,`igst28` 
                                //                                                        FROM `purrawmain` WHERE  vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate'  and unclaimgst != 1") or die(mysqli_error($conn));
                                //
                                //                                    mysqli_query($conn, "create temporary table rettax 
                                //                                                SELECT `cgst5`,`sgst5`,`igst5`,`cgst12`, `sgst12`,`igst12`, 
                                //                                                       `cgst18`,`sgst18`, `igst18`,`cgst28`, `sgst28`,`igst28`
                                //                                                        FROM `debmain` WHERE  vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate'  and unclaimgst != 1
                                //                                                union all 
                                //                                                SELECT `cgst5`,`sgst5`,`igst5`,`cgst12`, `sgst12`,`igst12`, 
                                //                                                       `cgst18`,`sgst18`, `igst18`, `cgst28`, `sgst28`,`igst28` 
                                //                                                        FROM `debrawmain` WHERE  vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate' and unclaimgst != 1") or die(mysqli_error($conn));
                                //
                                //                                    $getin = mysqli_query($conn, "SELECT sum(`cgst5`) as cgst5,   sum(`igst5`) as igst5,
                                //                                                                         sum(`cgst12`) as cgst12, sum(`igst12`) as igst12, 
                                //                                                                         sum(`cgst18`) as cgst18, sum(`igst18`) as igst18,
                                //                                                                         sum(`cgst28`) as cgst28, sum(`igst28`) as igst28
                                //                                                                         from dbftax  ") or die(mysqli_error($conn));
                                //
                                //                                    $ctrin = 0;
                                //                                    echo "<tr>";
                                //                                    echo "<th>GST INPUT CLAIMED - BREAK UP</th>";
                                //                                    echo '<th align="right" style="text-align:right;">Debit</th>';
                                //                                    echo '<th align="right" style="text-align:right;">Credit</th>';
                                //                                    echo "</tr>";
                                //                                    $totin = $totinret = 0 ;
                                //
                                //                                    while ($fetin = mysqli_fetch_array($getin)) {
                                //
                                //                                        $getret = mysqli_query($conn, "SELECT sum(`cgst5`) as cgst5,   sum(`igst5`) as igst5,
                                //                                                                         sum(`cgst12`) as cgst12, sum(`igst12`) as igst12, 
                                //                                                                         sum(`cgst18`) as cgst18, sum(`igst18`) as igst18,
                                //                                                                         sum(`cgst28`) as cgst28, sum(`igst28`) as igst28
                                //                                                                         from rettax  ") or die(mysqli_error($conn));
                                //                                        $fetret = mysqli_fetch_array($getret);
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>CGST 2.5</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst5] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst5] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //                                        
                                //                                        $totin = $totin + $fetin['cgst5'] ;
                                //                                        $totinret = $totinret + $fetret['cgst5'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>SGST 2.5</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst5] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst5] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['cgst5'] ;
                                //                                        $totinret = $totinret + $fetret['cgst5'] ;
                                //
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>IGST 5</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[igst5] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[igst5] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['igst5'] ;
                                //                                        $totinret = $totinret + $fetret['igst5'] ;
                                //                                        
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>CGST 6</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst12] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst12] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['cgst12'] ;
                                //                                        $totinret = $totinret + $fetret['cgst12'] ;
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>SGST 6</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst12] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst12] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['cgst12'] ;
                                //                                        $totinret = $totinret + $fetret['cgst12'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>IGST 12</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[igst12] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[igst12] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //                                        
                                //                                        $totin = $totin + $fetin['igst12'] ;
                                //                                        $totinret = $totinret + $fetret['igst12'] ;
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>CGST 9</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst18] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst18] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['cgst18'] ;
                                //                                        $totinret = $totinret + $fetret['cgst18'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>SGST 9</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst18] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst18] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['cgst18'] ;
                                //                                        $totinret = $totinret + $fetret['cgst18'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>IGST 18</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[igst18] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[igst18] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['igst18'] ;
                                //                                        $totinret = $totinret + $fetret['igst18'] ;
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>CGST 14</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst28] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst28] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['cgst28'] ;
                                //                                        $totinret = $totinret + $fetret['cgst28'] ;
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>SGST 14</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst28] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst28] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['cgst28'] ;
                                //                                        $totinret = $totinret + $fetret['cgst28'] ;
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>IGST 28</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[igst28] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[igst28] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['igst28'] ;
                                //                                        $totinret = $totinret + $fetret['igst28'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td align='right'><strong>Total</strong></td>";
                                //                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><strong>" . abs($totin) . "</strong></td>";
                                //                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><strong>" . abs($totinret) . "</strong></td>";
                                //                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><strong></strong></td>";
                                //                                        echo "</tr>";
                                //                                    }
                                //                                    
                                //                                    // GST INPUT - UNCLAIMED
                                //
                                //                                    mysqli_query($conn, "drop temporary table if exists dbftax") or die(mysqli_error($conn));
                                //                                    mysqli_query($conn, "drop temporary table if exists rettax") or die(mysqli_error($conn));
                                //                                    mysqli_query($conn, "create temporary table dbftax 
                                //                                                SELECT `cgst5`,`sgst5`,`igst5`,`cgst12`, `sgst12`,`igst12`, 
                                //                                                       `cgst18`,`sgst18`, `igst18`,`cgst28`, `sgst28`,`igst28`
                                //                                                        FROM `pgstmain` WHERE  vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate'  and unclaimgst = 1
                                //                                                union all 
                                //                                                SELECT `cgst5`,`sgst5`,`igst5`,`cgst12`, `sgst12`,`igst12`, 
                                //                                                       `cgst18`,`sgst18`, `igst18`, `cgst28`, `sgst28`,`igst28` 
                                //                                                        FROM `purrawmain` WHERE  vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate'  and unclaimgst = 1") or die(mysqli_error($conn));
                                //
                                //                                    mysqli_query($conn, "create temporary table rettax 
                                //                                                SELECT `cgst5`,`sgst5`,`igst5`,`cgst12`, `sgst12`,`igst12`, 
                                //                                                       `cgst18`,`sgst18`, `igst18`,`cgst28`, `sgst28`,`igst28`
                                //                                                        FROM `debmain` WHERE  vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate'  and unclaimgst = 1
                                //                                                union all 
                                //                                                SELECT `cgst5`,`sgst5`,`igst5`,`cgst12`, `sgst12`,`igst12`, 
                                //                                                       `cgst18`,`sgst18`, `igst18`, `cgst28`, `sgst28`,`igst28` 
                                //                                                        FROM `debrawmain` WHERE  vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate'  and unclaimgst = 1") or die(mysqli_error($conn));
                                //
                                //                                    $getin = mysqli_query($conn, "SELECT sum(`cgst5`) as cgst5,   sum(`igst5`) as igst5,
                                //                                                                         sum(`cgst12`) as cgst12, sum(`igst12`) as igst12, 
                                //                                                                         sum(`cgst18`) as cgst18, sum(`igst18`) as igst18,
                                //                                                                         sum(`cgst28`) as cgst28, sum(`igst28`) as igst28
                                //                                                                         from dbftax  ") or die(mysqli_error($conn));
                                //
                                //                                    $ctrin = 0;
                                //                                    echo "<tr>";
                                //                                    echo "<th>GST INPUT UNCLAIMED - BREAK UP </th>";
                                //                                    echo '<th align="right" style="text-align:right;">Debit</th>';
                                //                                    echo '<th align="right" style="text-align:right;">Credit</th>';
                                //                                    echo "</tr>";
                                //                                    $totin = $totinret = 0 ;
                                //
                                //                                    while ($fetin = mysqli_fetch_array($getin)) {
                                //
                                //                                        $getret = mysqli_query($conn, "SELECT sum(`cgst5`) as cgst5,   sum(`igst5`) as igst5,
                                //                                                                         sum(`cgst12`) as cgst12, sum(`igst12`) as igst12, 
                                //                                                                         sum(`cgst18`) as cgst18, sum(`igst18`) as igst18,
                                //                                                                         sum(`cgst28`) as cgst28, sum(`igst28`) as igst28
                                //                                                                         from rettax  ") or die(mysqli_error($conn));
                                //                                        $fetret = mysqli_fetch_array($getret);
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>CGST 2.5</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst5] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst5] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //                                        
                                //                                        $totin = $totin + $fetin['cgst5'] ;
                                //                                        $totinret = $totinret + $fetret['cgst5'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>SGST 2.5</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst5] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst5] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['cgst5'] ;
                                //                                        $totinret = $totinret + $fetret['cgst5'] ;
                                //
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>IGST 5</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[igst5] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[igst5] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['igst5'] ;
                                //                                        $totinret = $totinret + $fetret['igst5'] ;
                                //                                        
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>CGST 6</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst12] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst12] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['cgst12'] ;
                                //                                        $totinret = $totinret + $fetret['cgst12'] ;
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>SGST 6</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst12] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst12] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['cgst12'] ;
                                //                                        $totinret = $totinret + $fetret['cgst12'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>IGST 12</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[igst12] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[igst12] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //                                        
                                //                                        $totin = $totin + $fetin['igst12'] ;
                                //                                        $totinret = $totinret + $fetret['igst12'] ;
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>CGST 9</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst18] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst18] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['cgst18'] ;
                                //                                        $totinret = $totinret + $fetret['cgst18'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>SGST 9</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst18] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst18] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['cgst18'] ;
                                //                                        $totinret = $totinret + $fetret['cgst18'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>IGST 18</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[igst18] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[igst18] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['igst18'] ;
                                //                                        $totinret = $totinret + $fetret['igst18'] ;
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>CGST 14</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst28] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst28] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['cgst28'] ;
                                //                                        $totinret = $totinret + $fetret['cgst28'] ;
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>SGST 14</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[cgst28] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst28] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['cgst28'] ;
                                //                                        $totinret = $totinret + $fetret['cgst28'] ;
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>IGST 28</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetin[igst28] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[igst28] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetin['igst28'] ;
                                //                                        $totinret = $totinret + $fetret['igst28'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td align='right'><strong>Total</strong></td>";
                                //                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><strong>" . abs($totin) . "</strong></td>";
                                //                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><strong>" . abs($totinret) . "</strong></td>";
                                //                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><strong></strong></td>";
                                //                                        echo "</tr>";
                                //                                    }
                                //
                                //                                    /// GST OUPUT
                                //
                                //                                    mysqli_query($conn, "drop temporary table if exists dbftax") or die(mysqli_error($conn));
                                //                                    mysqli_query($conn, "create temporary table dbftax 
                                //                                                SELECT `cgst5`,`sgst5`,`igst5`,`cgst12`, `sgst12`,`igst12`, packgtperc, packgtamt,
                                //                                                       `cgst18`,`sgst18`, `igst18`,`cgst28`, `sgst28`,`igst28`, oms
                                //                                                        FROM `gstmain` WHERE  vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate'
                                //                                                union all 
                                //                                                SELECT `cgst5`,`sgst5`,`igst5`,`cgst12`, `sgst12`,`igst12`, packgtperc, packgtamt,
                                //                                                       `cgst18`,`sgst18`, `igst18`, `cgst28`, `sgst28`,`igst28`, oms
                                //                                                        FROM `rawmain` WHERE  vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate'") or die(mysqli_error($conn));
                                //
                                //                                    mysqli_query($conn, "update dbftax set cgst5  = cgst5  + packgtamt where packgtperc = 5  and oms != 1 and packgtamt > 0 ") or die(mysqli_error($conn));
                                //                                    mysqli_query($conn, "update dbftax set cgst12 = cgst12 + packgtamt where packgtperc = 12 and oms != 1 and packgtamt > 0 ") or die(mysqli_error($conn));
                                //                                    mysqli_query($conn, "update dbftax set cgst18 = cgst18 + packgtamt where packgtperc = 18 and oms != 1 and packgtamt > 0 ") or die(mysqli_error($conn));
                                //                                    mysqli_query($conn, "update dbftax set cgst28 = cgst28 + packgtamt where packgtperc = 28 and oms != 1 and packgtamt > 0 ") or die(mysqli_error($conn));
                                //
                                //                                    mysqli_query($conn, "update dbftax set igst5  = igst5  + packgtamt where packgtperc = 5  and oms = 1 and packgtamt > 0 ") or die(mysqli_error($conn));
                                //                                    mysqli_query($conn, "update dbftax set igst12 = igst12 + packgtamt where packgtperc = 12 and oms = 1 and packgtamt > 0 ") or die(mysqli_error($conn));
                                //                                    mysqli_query($conn, "update dbftax set igst18 = igst18 + packgtamt where packgtperc = 18 and oms = 1 and packgtamt > 0 ") or die(mysqli_error($conn));
                                //                                    mysqli_query($conn, "update dbftax set igst28 = igst28 + packgtamt where packgtperc = 28 and oms = 1 and packgtamt > 0 ") or die(mysqli_error($conn));
                                //
                                //                                    mysqli_query($conn, "drop temporary table if exists rettax") or die(mysqli_error($conn));
                                //
                                //                                    mysqli_query($conn, "create temporary table rettax 
                                //                                                SELECT `cgst5`,`sgst5`,`igst5`,`cgst12`, `sgst12`,`igst12`, 
                                //                                                       `cgst18`,`sgst18`, `igst18`,`cgst28`, `sgst28`,`igst28`
                                //                                                        FROM `credmain` WHERE  vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate'
                                //                                                union all 
                                //                                                SELECT `cgst5`,`sgst5`,`igst5`,`cgst12`, `sgst12`,`igst12`, 
                                //                                                       `cgst18`,`sgst18`, `igst18`, `cgst28`, `sgst28`,`igst28` 
                                //                                                        FROM `credrawmain` WHERE  vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate'") or die(mysqli_error($conn));
                                //
                                //                                    $getou = mysqli_query($conn, "SELECT sum(`cgst5`) as cgst5,   sum(`igst5`) as igst5,
                                //                                                                         sum(`cgst12`) as cgst12, sum(`igst12`) as igst12, 
                                //                                                                         sum(`cgst18`) as cgst18, sum(`igst18`) as igst18,
                                //                                                                         sum(`cgst28`) as cgst28, sum(`igst28`) as igst28
                                //                                                                         from dbftax  ") or die(mysqli_error($conn));
                                //
                                //                                    $ctrin = 0;
                                //                                    echo "<tr>";
                                //                                    echo "<th>GST OUTPUT - BREAK UP</th>";
                                //                                    echo '<th align="right" style="text-align:right;">Debit</th>';
                                //                                    echo '<th align="right" style="text-align:right;">Credit</th>';
                                //                                    echo "</tr>";
                                //
                                //                                    $totin = $totinret = 0 ;
                                //                                    
                                //                                    while ($fetou = mysqli_fetch_array($getou)) {
                                //
                                //                                        $getret = mysqli_query($conn, "SELECT sum(`cgst5`) as cgst5,   sum(`igst5`) as igst5,
                                //                                                                         sum(`cgst12`) as cgst12, sum(`igst12`) as igst12, 
                                //                                                                         sum(`cgst18`) as cgst18, sum(`igst18`) as igst18,
                                //                                                                         sum(`cgst28`) as cgst28, sum(`igst28`) as igst28
                                //                                                                         from rettax  ") or die(mysqli_error($conn));
                                //                                        $fetret = mysqli_fetch_array($getret);
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>CGST 2.5</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst5] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetou[cgst5] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetou['cgst5'] ;
                                //                                        $totinret = $totinret + $fetret['cgst5'] ;
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>SGST 2.5</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst5] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetou[cgst5] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetou['cgst5'] ;
                                //                                        $totinret = $totinret + $fetret['cgst5'] ;
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>IGST 5</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[igst5] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetou[igst5] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetou['igst5'] ;
                                //                                        $totinret = $totinret + $fetret['igst5'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>CGST 6</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst12] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetou[cgst12] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetou['cgst12'] ;
                                //                                        $totinret = $totinret + $fetret['cgst12'] ;
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>SGST 6</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst12] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetou[cgst12] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetou['cgst12'] ;
                                //                                        $totinret = $totinret + $fetret['cgst12'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>IGST 12</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[igst12] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetou[igst12] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetou['igst12'] ;
                                //                                        $totinret = $totinret + $fetret['igst12'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>CGST 9</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst18] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetou[cgst18] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetou['cgst18'] ;
                                //                                        $totinret = $totinret + $fetret['cgst18'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>SGST 9</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst18] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetou[cgst18] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetou['cgst18'] ;
                                //                                        $totinret = $totinret + $fetret['cgst18'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>IGST 18</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[igst18] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetou[igst18] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetou['igst18'] ;
                                //                                        $totinret = $totinret + $fetret['igst18'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>CGST 14</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "<td style='text-align:right'>" . $fetou[cgst28] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetou['cgst28'] ;
                                //                                        $totinret = $totinret + $fetret['cgst28'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>SGST 14</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[cgst28] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetou[cgst28] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //
                                //                                        $totin = $totin + $fetou['cgst28'] ;
                                //                                        $totinret = $totinret + $fetret['cgst28'] ;
                                //
                                //                                        echo "<tr>";
                                //                                        echo "<td style='text-align:left'>IGST 28</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetret[igst28] . "</td>";
                                //                                        echo "<td style='text-align:right'>" . $fetou[igst28] . "</td>";
                                //                                        echo "<td style='text-align:right'></td>";
                                //                                        echo "</tr>";
                                //                                        
                                //                                        $totin = $totin + $fetou['igst28'] ;
                                //                                        $totinret = $totinret + $fetret['igst28'] ;
                                //                                        
                                //                                        echo "<tr>";
                                //                                        echo "<td align='right'><strong>Total</strong></td>";
                                //                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><strong>" . abs($totinret) . "</strong></td>";
                                //                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><strong>" . abs($totin) . "</strong></td>";
                                //                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><strong></strong></td>";
                                //                                        echo "</tr>";
                                //                                    }
                                //                                }
                                ?>

                            </tbody>

                            <tfoot>
                                <tr>
                                    <td colspan="9">
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php"); ?>
    </body>

    </html>
<?php
}



if (isset($_POST["excel"])) {
    $gcond = "";
    $accond = "";
    $stcond = "";
    $ctcond = "";

    if (!empty($_POST["group"])) {
        $grp = trim($_POST["group"]);
        $gcond = "AND `grpcode`='$grp'";
    }

    if (!empty($_POST["acname"])) {
        $accode = trim($_POST["acname"]);
        $accond = "AND `accode`='$accode'";
    }

    if (!empty($_POST["broker"])) {
        $broker = trim($_POST["broker"]);
        $brokcond = "AND `accode`='$broker'";
    }

    if (!empty($_POST["cmbstation"])) {
        $station = trim($_POST["cmbstation"]);
        $stcond = " AND `station`= '$station' ";
    }

    if (!empty($_POST["city"])) {
        $city = trim($_POST["city"]);
        $ctcond = " AND `city`='$city'";
    }

    $mfrmdate = $_POST["frmdt"];
    $mtodate = $_POST["todt"];

    $recpfrom = $_POST["recpfrom"];
    $recpto = $_POST["recpto"];

    if ($mfrmdate > $mtodate) {
        browse("To Date Cannot Be Less Than From Date");
        header("refresh: 1; ledger.php");
        exit();
    }

    if ($recpfrom > $recpto) {
        browse("Receipt To Date Cannot Be Less Than Receipt From Date");
        header("refresh: 1; ledger.php");
        exit();
    }

    $prnzero = $_POST["zerobal"];
    $prnnarr = $_POST["prinnarr"];

    if ($_POST["dtype"] == 1) {
        $repname = "Ledger - Summary";
        $dtype = 1;
    } else {
        $repname = "Ledger - Detail";
        $dtype = 2;
    }

    $acc = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE grpcode!='001' and grpcode !='002' $gcond $accond $brokcond $ctcond $stcond  order by name") or die(mysqli_error($conn));
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    include('includes/head_excel_rep.php');

    if ($dtype == 1) {
        $contents = " Account Name, Debit, Credit, Balance \n";
    }
    mysqli_select_db($conn, $zdb);

    $totbal = 0;
    $url = '';
    while ($getac = mysqli_fetch_array($acc)) {

        if ($dtype == 1) {
            $maccode = $getac["accode"];
            $mgrpcode = $getac["grpcode"];
            $mopbal = $getac["openbal"];
            $mopdc = $getac["dc"];
            if ($mopdc == "D") {
                $mopbal = $mopbal * -1;
            }
            ############PARTY NAME####################

            $getpartyname = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$maccode'") or die(mysqli_error($conn));
            $zname = mysqli_fetch_object($getpartyname);
            $mname = $zname->name;

            ############PARTY NAME####################

            $getdeb = mysqli_query($conn, "SELECT SUM(amount) as debamt FROM `$trandb` WHERE accode='$maccode' and amount < 0
                                                                              and date >= '$mfrmdate' and date <= '$mtodate'") or die(mysqli_error($conn));
            $fetdeb = mysqli_fetch_array($getdeb);
            $debamt = $fetdeb["debamt"];

            if ($mgrpcode != '010') {
                $getcrd = mysqli_query($conn, "SELECT SUM(amount) as crdamt FROM `$trandb` WHERE accode='$maccode' and amount > 0
                                                                              and date >= '$mfrmdate' and date <= '$mtodate'") or die(mysqli_error($conn));
            } else {
                $getcrd = mysqli_query($conn, "SELECT SUM(amount) as crdamt FROM `$trandb` WHERE accode='$maccode' and amount > 0
                                                                              and date >= '$recpfrom' and date <= '$recpto'") or die(mysqli_error($conn));
            }
            $fetcrd = mysqli_fetch_array($getcrd);
            $crdamt = $fetcrd["crdamt"];

            $amt = abs($crdamt) - abs($debamt);

            $mbal = $mopbal + $amt;
            if ($mbal < 0) {
                $mdc = "DR";
            } else {
                $mdc = "CR";
            }

            $show = 1;

            if ($prnzero == 1) {
                $show = 1;
            } else {
                if ($mbal == 0) {
                    $show = 0;
                }
            }

            if ($show == 1) {
                $contents .= "$mname" . ",";
                $contents .= "$debamt" . ",";
                $contents .= "$crdamt" . ",";
                $contents .= "$mbal" . " $mdc" . "\n";

                $totdeb = $totdeb + $debamt;
                $totcrd = $totcrd + $crdamt;
                $totbal = $totbal + $mbal;
            }

            if ($totbal < 0) {
                $mdctot = "DR";
            } else {
                $mdctot = "CR";
            }
        } else {

            $totbal = 0;
            $dtot = 0;
            $ctot = 0;

            $maccode = $getac["accode"];
            $mgrpcode = $getac["grpcode"];
            $macname = $getac["name"];
            $mopbal = $getac["openbal"];
            $mopdc = $getac["dc"];

            if ($mopdc == "D") {
                $mopbal = $mopbal * -1;
            }

            if ($mgrpcode != '010') {
                $ctran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
                                                                  WHERE accode = '$maccode' AND `date`>='$mfrmdate' AND `date`<='$mtodate'
                                                                  ORDER BY `date`, vouchno") or die(mysqli_error($conn));
            } else {
                $ctran1 = mysqli_query($conn, "SELECT * from 
                                                                  (select * , DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
                                                                  WHERE accode = '$maccode' AND `date`>='$mfrmdate' AND `date`<='$mtodate'
                                                                  and amount < 0    
                                                                  union all    
                                                                  select * , DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
                                                                  WHERE accode = '$maccode' AND `date`>='$recpfrom' AND `date`<='$recpto' 
                                                                  and amount > 0 ) as a
                                                                  ORDER BY `date`, vouchno") or die(mysqli_error($conn));
            }

            //                                        print table($ctran1);
            $curl = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
                                                                 WHERE accode = '$maccode' AND `date`>='$mfrmdate' AND `date`<='$mtodate'
                                                                 ORDER BY `date`, vouchno") or die(mysqli_error($conn));

            while ($murl = mysqli_fetch_object($curl)) {
                switch ($murl->source) {
                    case "PU";
                        $head = "Purchase";
                        $url = "purch.php?vouchno=$murl->vouchno&method=ledger";
                        break;
                    case "PR";
                        $head = "Purchase Return";
                        $url = "debnote.php?vouchno=$murl->vouchno&method=ledger";
                        break;
                    case "SL";
                        $head = "Sale";
                        $url = "sale.php?vouchno=$murl->vouchno&method=ledger";
                        break;
                    case "SLM";
                        $head = "Sale Raw Material";
                        $url = "sale.php?vouchno=$murl->vouchno&method=ledger";
                        break;
                    case "SRM";
                        $head = "Sale Return - Raw Material";
                        $url = "salematreturn.php?vouchno=$murl->vouchno&method=ledger";
                        break;
                    case "SLTX";
                        $head = "Sale - GST";
                        $url = "salegstmulti.php?vouchno=$murl->vouchno&method=ledger";
                        break;
                    case "PUTX";
                        $head = "Purchase - GST";
                        $url = "purchgstmulti.php?vouchno=$murl->vouchno&method=ledger";
                        break;
                    case "BRBP";
                        $head = "Transfer - Customer to Karigar";
                        $url = "custtokari.php?vouchno=$murl->vouchno&method=ledger";
                        break;
                    case "SR";
                        $head = "Sale Return";
                        $url = "crednote.php?vouchno=$murl->vouchno&method=ledger";
                        break;
                    case "BR";
                        $url = "bankrecp.php?vouchno=$murl->vouchno&method=ledger";
                        break;
                    case "BP";
                        $head = "Bank Payment";
                        $url = "bankpay.php?$murl->vouchno&method=ledger";
                        break;
                    case "CR";
                        $head = "Cash Receipt";
                        $url = "cashrecp.php?$murl->vouchno&method=ledger";
                        break;
                    case "CP";
                        $head = "Cash Payment";
                        $url = "cashpay.php?$murl->vouchno&method=ledger";
                        break;
                    case "TR";
                        $head = "Contra / Transfer";
                        $url = "contra.php?vouchno=$murl->vouchno&method=ledger";
                        break;
                    case "JV";
                        $head = "Journal Voucher";
                        $url = "journal.php?$murl->vouchno&method=ledger";
                        break;
                    case "DG";
                        $head = "Discount Given";
                        break;
                    case "DR";
                        $head = "Discount Received";
                        break;
                    default:
                        $head = "Other";
                }
            }

            $rtran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`retdate`, '%d-%m-%Y') as mydt FROM `$trandb` WHERE
                                                           retdate >= '$mfrmdate' AND retdate <= '$mtodate' AND
                                                           accode = '$maccode' ORDER BY date, vouchno") or die(mysqli_error($conn));

            $getbal = mysqli_query($conn, "SELECT SUM(amount) as oamt FROM `$trandb` WHERE accode='$maccode' AND  date < '$mfrmdate'") or die(mysqli_error($conn));
            $getamt = mysqli_fetch_array($getbal);
            $oamt = $getamt["oamt"];

            $mobal = $mopbal + $oamt;

            $totunclaimgstamt = 0;
            $mdebit = 0;
            $mcredit = 0;

            if ($mobal < 0) {
                $mdc = "DR";
                $mdebit = abs($mobal);
                $dtot = $dtot + $mdebit;
            } else {
                $mdc = "CR";
                $mcredit = abs($mobal);
                $ctot = $ctot + $mcredit;
            }

            $vdate1 = strtotime($mfrmdate);
            $vdate = date('d-m-Y', $vdate1);
            $mvouch = "";

            $mdescrip = "Opening Balance";
            $totbal = $totbal + $mobal;

            if ($totbal < 0) {
                $mdctot = "DR";
            } else {
                $mdctot = "CR";
            }
            $struppname =  strtoupper($macname);
            $contents .= "$struppname \n";

            $contents .= "Date, Source, Voucher, Description, Unclaim Amt, Debit, Credit, Balance \n";

            $contents .= "$vdate" . ",";
            $contents .= "" . ",";
            $contents .= "$mvouch" . ",";
            $contents .= "$mdescrip" . ",";
            $contents .= "" . ",";
            $contents .= "$mdebit" . ",";
            $contents .= "$mcredit" . ",";
            $contents .= "$totbal" . " $mdctot" . "\n";

            while ($row = mysqli_fetch_array($ctran1)) {
                $vdate = $row["mydt"];
                $msource = $row["source"];
                $mvouch = $row["vouchno"];
                $mamount = $row["amount"];

                $mgrpcode = $getac["grpcode"];
                $mdiscprov = $getac["discount"];
                $totdiscprov = 0;

                $unclaimgstamt = 0;
                $mdebit = 0;
                $mcredit = 0;

                if ($mamount < 0) {
                    $mdc = "DR";
                    $mdebit = abs($mamount);
                    $dtot = $dtot + $mdebit;
                } else {
                    $mdc = "CR";
                    $mcredit = abs($mamount);
                    $ctot = $ctot + $mcredit;
                }

                $totbal = $totbal + $mamount;

                if ($totbal < 0) {
                    $mdctot = "DR";
                } else {
                    $mdctot = "CR";
                }

                $mdescrip = "";
                $mchqdtl = "";
                $mnarr = "";
                $colr = "";

                switch ($msource) {
                    case "PUM";
                        $head = "Purchase Raw Material";
                        $munclaimgst = '';
                        if ($para == "K") {
                            $url = "purchmat.php?vouchno=$mvouch&mode=edit";
                        } else {
                            $url = "purchmatgst.php?vouchno=$mvouch&mode=edit";
                        }
                        $totdiscprov = $totdiscprov + $mamount;

                        $nqry1 = mysqli_query($conn, "SELECT `invno` as billno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2`, `unclaimgst`, totgst5, totgst12, totgst18, totgst28 
                                                                         FROM `purrawmain` WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);

                        $mbill = $nqry["billno"];
                        $mbilldt = $nqry["mbilldate"];
                        $munclaimgst = $nqry["unclaimgst"];

                        $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);

                        $mchqdtl = "Bill No. : " . trim($mbill);

                        if ($munclaimgst == '1') {
                            $head = "Purchase Raw Material - (Unclaim GST) ";
                            $colr = "color:#ff006e";
                            $unclaimgstamt = $nqry["totgst5"] + $nqry["totgst12"] + $nqry["totgst18"] + $nqry["totgst28"];
                            $totunclaimgstamt = $totunclaimgstamt + $unclaimgstamt;
                        }

                        break;
                    case "PRRW";
                        $head = "Purchase Return Raw Material";
                        if ($para == "K") {
                            $url = "debnoteraw.php?vouchno=$mvouch&mode=edit";
                        } else {
                            $url = "debnoteraw_gst.php?vouchno=$mvouch&mode=edit";
                        }
                        $totdiscprov = $totdiscprov + $mamount;

                        if ($prnnarr == 1) {
                            $nqry1 = mysqli_query($conn, "SELECT `invno` as billno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `debrawmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                            $nqry = mysqli_fetch_array($nqry1);

                            $mbill = $nqry["billno"];
                            $mbilldt = $nqry["mbilldate"];

                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);

                            $mchqdtl = "Bill No. : " . trim($mbill);
                        }
                        break;
                    case "PU";
                        $head = "Purchase";
                        $url = "purch.php?vouchno=$mvouch&mode=edit";
                        $totdiscprov = $totdiscprov + $mamount;

                        if ($prnnarr == 1) {
                            $nqry1 = mysqli_query($conn, "SELECT invno as billno, DATE_FORMAT(`billdate`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `purmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                            $nqry = mysqli_fetch_array($nqry1);
                            $mbill = $nqry["billno"];
                            $mbilldt = $nqry["mbilldate"];
                            $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);

                            $mchqdtl = "Bill No. : " . trim($mbill);
                        }
                        break;

                    case "PUFP";
                        $head = "Purchase Finished Products";
                        $munclaimgst = '';

                        if ($para == "K") {
                            $url = "purchase.php?vouchno=$mvouch&mode=edit";
                            $tablenm = "pgstmain";
                        } else {
                            $url = "purchgstmulti.php?vouchno=$mvouch&mode=edit&method=ledger";
                            $tablenm = "pgstmain";
                        }
                        $totdiscprov = $totdiscprov + $mamount;

                        // if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT invno as billno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                                                                 `remark1`, `remark2`, `unclaimgst`, totgst5, totgst12, totgst18, totgst28 
                                                                                  FROM `$tablenm` WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);
                        $mbill = $nqry["billno"];
                        $mbilldt = $nqry["mbilldate"];
                        $munclaimgst = $nqry["unclaimgst"];

                        $mnarr = trim($nqry["remark1"]) . " " . trim($nqry["remark2"]);

                        $mchqdtl = "Bill No. : " . trim($mbill);
                        // }
                        if ($munclaimgst == '1') {
                            $head = "Purchase Finished Products - (Unclaim GST) ";
                            $colr = "color:#ff006e";
                            $unclaimgstamt = $nqry["totgst5"] + $nqry["totgst12"] + $nqry["totgst18"] + $nqry["totgst28"];
                            $totunclaimgstamt = $totunclaimgstamt + $unclaimgstamt;
                        }
                        break;

                    case "PR";
                        $head = "Purchase Return";
                        $url = "debnote.php?vouchno=$mvouch&method=ledger";
                        $totdiscprov = $totdiscprov + $mamount;
                        if ($prnnarr == 1) {
                            $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `debmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);

                            $mbill = $nqry["invno"];
                            $mbilldt = $nqry["mbilldate"];
                            $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                            $mchqdtl = "Bill No. : " . trim($mbill);
                        }
                        break;
                    case "PRFP";
                        $head = "(GR) Purchase Return Finish Products";

                        if ($para == "K") {
                            $url = "debnote.php?vouchno=$mvouch&mode=edit&method=ledger";
                            $tablenm = "debmain";
                        } else {
                            $tablenm = "debmain";
                            $url = "debnotegstmulti.php?vouchno=$mvouch&mode=edit&method=ledger";
                        }

                        $totdiscprov = $totdiscprov + $mamount;
                        if ($prnnarr == 1) {
                            $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `$tablenm`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);

                            $mbill = $nqry["invno"];
                            $mbilldt = $nqry["mbilldate"];
                            $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                            $mchqdtl = "Bill No. : " . trim($mbill);
                        }
                        break;

                    case "SL";
                        $head = "Sale";
                        if ($prnnarr == 1) {
                            $mnarr = "";
                            $mchqdtl = "";
                        }
                        break;

                    case "SLM";
                        $head = "Sale Raw Material";

                        if ($para == "K") {
                            $url = "salemat.php?vouchno=$mvouch&mode=edit";
                        } else {
                            $url = "salematgstmulti.php?vouchno=$mvouch&mode=edit";
                        }

                        if ($prnnarr == 1) {
                            $mnarr = "";
                            $mchqdtl = "";
                            $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `rawmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);

                            $mbill = $nqry["invno"];
                            $mbilldt = $nqry["mbilldate"];
                            $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);

                            $mchqdtl = "Bill No. : " . trim($mbill);
                        }
                        break;

                    case "SLFP";

                        $head = "Sale Finished Product";
                        if ($para == "K") {
                            $tablenm = "gstmain";
                            $url = "sale.php?vouchno=$mvouch&mode=edit&method=ledger";
                        } else {
                            $tablenm = "gstmain";
                            $url = "salegstmulti.php?vouchno=$mvouch&mode=edit&method=ledger";
                        }

                        if ($prnnarr == 1) {
                            $mnarr = "";
                            $mchqdtl = "";
                            $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `$tablenm`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);

                            $mbill = $nqry["invno"];
                            $mbilldt = $nqry["mbilldate"];
                            $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);

                            $mchqdtl = "Bill No. : " . $mbill;
                        }
                        break;

                    case "SRFP";
                        $head = "Sale Return Finished Product";

                        if ($para == "K") {
                            $url = "crednote.php?vouchno=$mvouch&mode=edit";
                        } else {
                            $url = "creditnotegstmulti.php?vouchno=$mvouch&mode=edit";
                        }

                        if ($prnnarr == 1) {
                            $mnarr = "";
                            $mchqdtl = "";
                            $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `credmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);

                            $mbill = $nqry["invno"];
                            $mbilldt = $nqry["mbilldate"];
                            $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                            //                                                        $mchqdtl = "Bill No. : " .  trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                            $mchqdtl = "Bill No. : " . $mbill;

                            //                                                        browse($mchqdtl);
                        }
                        break;

                    case "SLFB";
                        $head = "Sale Finished Product - Brokerage";
                        $url = "salematgstmulti.php?vouchno=$mvouch&mode=edit";

                        if ($prnnarr == 1) {
                            $mnarr = "";
                            $mchqdtl = "";
                        }
                        break;

                    case "SLAM";
                        $head = "Sale Only Amount";
                        $url = "saleamt.php?vouchno=$mvouch&mode=edit";

                        if ($prnnarr == 1) {
                            $mnarr = "";
                            $mchqdtl = "";
                            $nqry1 = mysqli_query($conn, "SELECT recpno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `saleamt`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);

                            $mbill = $nqry["recpno"];
                            $mbilldt = $nqry["mbilldate"];
                            $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);
                            //                                                        $mchqdtl = "Bill No. : " .  trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                            $mchqdtl = "Bill No. : " . $mbill;

                            //                                                        browse($mchqdtl);
                        }
                        break;

                    case "SRM";
                        $head = "Sale Return - Raw Material";
                        $url = "crednoteraw_gst.php?vouchno=$mvouch&mode=edit";
                        if ($prnnarr == 1) {
                            $mnarr = "";
                            $mchqdtl = "";
                            $nqry1 = mysqli_query($conn, "SELECT invno, DATE_FORMAT(`invdt`, '%d-%m-%Y') as mbilldate,
                                                                        `remark1`, `remark2` FROM `credrawmain`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);

                            $mbill = $nqry["invno"];
                            $mbilldt = $nqry["mbilldate"];
                            $mnarr = trim($nqry["rem1"]) . " " . trim($nqry["rem2"]);

                            //                                                        $mchqdtl = "Bill No. : " .  trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                            $mchqdtl = "Bill No. : " . $mbill;
                            //                                                        browse($mchqdtl);
                        }
                        break;

                    case "SLTX";
                        $head = "Sale - GST";
                        if ($prnnarr == 1) {
                            $mnarr = "";
                            $mchqdtl = "";
                        }
                        break;

                    case "PUTX";
                        $head = "Purchase - GST";
                        if ($prnnarr == 1) {
                            $mnarr = "";
                            $mchqdtl = "";
                        }
                        break;

                    case "BRBP";
                        $head = "Transfer - Customer to Karigar";
                        $nqry11 = mysqli_query($conn, "SELECT party, karigar,acnum FROM `custtokari`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                        $n1qry = mysqli_fetch_array($nqry11);
                        $party = $n1qry["party"];
                        $karigar = $n1qry["karigar"];
                        $acnum = $n1qry["acnum"];
                        if ($mgrpcode == '010') {
                            $getname = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                            WHERE accode='$karigar'") or die(mysqli_error($conn));
                        } else {
                            $getname = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                            WHERE accode='$party'") or die(mysqli_error($conn));
                        }

                        $fetname = mysqli_fetch_array($getname);
                        $showname = $fetname['name'];
                        $head = $head . '( ' . $showname . '-' . $acnum . ' )';
                        if ($prnnarr == 1) {
                            $mnarr = "";
                            $mchqdtl = "";
                        }
                        break;

                    case "SR";
                        $head = "Sale Return";
                        break;

                    case "BR";
                        $head = "Bank Receipt";
                        $url = "bankrecp.php?vouchno=$mvouch&mode=edit&method=ledger";
                        if ($prnnarr == 1) {
                            $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                       `remark1`,remark2 FROM `bankrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $mbill = $nqry["chqno"];
                            $mbilldt = $nqry["mbilldate"];
                            $mnarr = $nqry["remark1"] . " " . $nqry["remark2"];
                            $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                        }
                        break;

                    case "BP";
                        $head = "Bank Payment";
                        $url = "bankpay.php?vouchno=$mvouch&mode=edit&method=ledger";
                        if ($prnnarr == 1) {
                            $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                        `remark1`,remark2 FROM `bankpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $mbill = $nqry["chqno"];
                            $mbilldt = $nqry["mbilldate"];
                            $mnarr = $nqry["remark1"] . " " . $nqry["remark2"];

                            $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                        }
                        break;

                    case "CR";
                        $head = "Cash Receipt";
                        $url = "cashrecp.php?vouchno=$mvouch&mode=edit";
                        if ($prnnarr == 1) {
                            $nqry1 = mysqli_query($conn, "SELECT `remark1`,remark2 FROM `cashrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $mnarr = $nqry["remark1"] . " " . $nqry["remark2"];
                        }
                        break;

                    case "CP";
                        $head = "Cash Payment";
                        $url = "cashpay.php?vouchno=$mvouch&mode=edit&method=ledger";
                        if ($prnnarr == 1) {
                            $nqry1 = mysqli_query($conn, "SELECT `remark1`,remark2 FROM `cashpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $mnarr = $nqry["remark1"] . " " . $nqry["remark2"];
                        }
                        break;

                    case "TR";
                        $head = "Contra / Transfer";
                        $url = "contra.php?vouchno=$mvouch&method=ledger";
                        if ($prnnarr == 1) {
                            $nqry1 = mysqli_query($conn, "SELECT `narr1`,narr2 FROM `contra`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $mnarr = $nqry["narr1"] . " " . $nqry["narr2"];
                        }
                        break;

                    case "JV";
                        $head = "Journal Voucher";
                        $url = "jv.php?vouchno=$mvouch&mode=edit";

                        break;

                    case "DG";
                        $head = "Discount Given";
                        $url = "discgvn.php?vouchno=$mvouch&mode=edit";

                        if ($accode == '13') {
                            $nqry11 = mysqli_query($conn, "SELECT a.accode, b.name FROM `discgive` a, accmast b 
                                                                                          WHERE a.vouchno='$mvouch' and a.accode = b.accode ") or die(mysqli_error($conn));
                            $n1qry = mysqli_fetch_array($nqry11);
                            $showname = $n1qry["name"];
                            $head = $showname;
                        }

                        if ($prnnarr == 1) {
                            $mnarr = "";
                            $mchqdtl = "";
                        }
                        break;

                    case "DR";
                        $head = "Discount Received";
                        $url = "discrecd.php?vouchno=$mvouch&mode=edit";

                        if ($accode == '1') {
                            $nqry11 = mysqli_query($conn, "SELECT a.accode, b.name FROM `discrecd` a, accmast b 
                                                                                          WHERE a.vouchno='$mvouch' and a.accode = b.accode ") or die(mysqli_error($conn));
                            $n1qry = mysqli_fetch_array($nqry11);
                            $showname = $n1qry["name"];
                            $head = $showname;
                        }
                        if ($prnnarr == 1) {
                            $mnarr = "";
                            $mchqdtl = "";
                        }
                        break;

                    default:
                        $head = "Other";
                }

                $contents .= "$vdate" . ",";
                $contents .= "$msource" . ",";
                $contents .= "$mvouch" . ",";
                if ($mchqdtl == '') {
                    $contents .= "$head" . ",";
                } else {
                    $contents .= "$head - $mchqdtl" . ",";
                }
                $contents .= "$unclaimgstamt" . ",";
                $contents .= "$mdebit" . ",";
                $contents .= "$mcredit" . ",";
                $contents .= "$totbal" . " $mdctot" . "\n";

                if ($prnnarr == 1) {
                    if (!empty($mnarr)) {
                        $contents .= "" . ",";
                        $contents .= "" . ",";
                        $contents .= "" . ",";
                        $contents .= "$mnarr" . ",";
                        $contents .= "" . ",";
                        $contents .= "" . ",";
                        $contents .= "" . "\n";
                    }
                }
            }

            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "Opening : $mobal  Total" . ",";
            $contents .= "$totunclaimgstamt" . ",";
            $contents .= "$dtot" . ",";
            $contents .= "$ctot" . ",";
            if ($totbal < 0) {
                $contents .= "$totbal" . " $mdctot" . ",";
            } else {
                $contents .= "$totbal" . " $mdctot" . ",";
            }
            $contents .= "" . "\n";
        }
    }

    if ($dtype == 1) {

        $contents .= "Total" . ",";
        $contents .= "$totdeb" . ",";
        $contents .= "$totcrd" . ",";
        $contents .= "$totbal" . " " . " $mdctot" . "\n";
    }

    $contents .= "" . ",,,,,,,,,\n";

    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
?>