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

$maindb = "purmain";
$trandb = "purtran";
$formname = "Purchase GST Register";
$file = "purchgstreg.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

if (isset($_POST["view"])) {
    $party = $_POST['party'];
    $item = $_POST['item'];
    $godown = $_POST['godown'];
    $gsttype = $_POST['gsttype'];
    $mtype = $_POST['type'];

    $gsttcond = " AND `oms`='$gsttype' ";

    $partycond = "";
    $brokercond = "";
    $transportcond = "";
    $itemcond = "";
    $godowncond = "";
    $mtypecond = "";

    if (!empty($party)) {
        $partycond = " AND `party`='$party' ";
    }

    if (!empty($item)) {
        $itemcond = " AND `name`='$item' ";
    }

    if (!empty($godown)) {
        $godowncond = " AND `godown`='$godown' ";
    }
    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; purchreg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; purchreg.php");
    }
    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    if (!empty($mtype)) {
        $mtypecond = " AND `type`='$mtype' ";
    }
    if ($gsttype == 2) {
        $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb`
             where `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'
             $partycond  $itemcond $godowncond $transcond $mtypecond $brokcond ")or die(mysqli_error($conn));
    } else {

        $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM"
                . " `$maindb` WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $partycond "
                . "$itemcond $godowncond $mtypecond $gsttcond")or die(mysqli_error($conn));
    }
    include("includes/linkshead.php");
    ?>
    <script src="MyScript/item.js" type="text/javascript"></script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <?php include("includes/head_rep.php"); ?>
                <div class="col-lg-12 pad">
                    <table style="width:100%;background: white" class="table table-condensed table-hover">
                        <thead>
                            <?php if ($gsttype == 2) { ?>
                                <tr style="height:10px;background-color: #1ABC9C">
                                    <th style="width:5%;">Sr</th>
                                    <th style="width:5%;">Type</th>
                                    <th style="width:5%;">Invoice No</th>
                                    <th style="width:5%;">Date</th>
                                    <th style="width:10%;">Supplier</th>
                                    <th style="width:7%;">Amount</th>
                                    <th style="width:5%;">Discount</th>
                                    <th style="width:5%;">Freight</th>
                                    <th style="width:5%;">Qty</th>
                                    <th style="width:5%;">Taxable Value</th>
                                    <th style="width:5%;">SGST Rate</th>
                                    <th style="width:7%;">SGST Amount</th>
                                    <th style="width:5%;">CGST Rate</th>
                                    <th style="width:7%;">CGST Amount</th>
                                    <th style="width:5%;">IGST Rate</th>
                                    <th style="width:7%;">IGST Amount</th>
                                    <th style="width:7%;" >Net Amount</th>                            
                                </tr>
                            <?php } elseif ($gsttype == '') { ?>
                                <tr style="height:10px;background-color: #1ABC9C">
                                    <th style="width:6%;">Sr</th>
                                    <th style="width:5%;">Type</th>
                                    <th style="width:5%;">Invoice No</th>
                                    <th style="width:5%;">Date</th>
                                    <th style="width:15%;">Supplier</th>
                                    <th style="width:9%;">Amount</th>
                                    <th style="width:7%;">Discount</th>
                                    <th style="width:5%;">Freight</th>
                                    <th style="width:5%;">Qty</th>
                                    <th style="width:5%;">Taxable Value</th>
                                    <th style="width:5%;">SGST Rate</th>
                                    <th style="width:7%;">SGST Amount</th>
                                    <th style="width:5%;">CGST Rate</th>
                                    <th style="width:7%;">CGST Amount</th>
                                    <th style="width:9%;">Net Amount</th>                            
                                </tr>
                            <?php } elseif ($gsttype == 1) { ?>
                                <tr style="height:10px;background-color: #1ABC9C">
                                    <th style="width:5%;">Sr</th>
                                    <th style="width:7%;">Type</th>
                                    <th style="width:7%;">Invoice No</th>
                                    <th style="width:5%;">Date</th>
                                    <th style="width:20%;">Supplier</th>
                                    <th style="width:7%;">Amount</th>
                                    <th style="width:7%;">Discount</th>
                                    <th style="width:7%;">Freight</th>
                                    <th style="width:7%;">Qty</th>
                                    <th style="width:6%;">Taxable Value</th>
                                    <th style="width:5%;">IGST Rate</th>
                                    <th style="width:7%;">IGST Amount</th>
                                    <th style="width:10%;" >Net Amount</th>                            
                                </tr>
                            <?php } ?>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $subtot = 0;
                            $totdisc = 0;
                            $totfr = 0;
                            $totqty1 = 0;
                            $totnet = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $getmtype = mysqli_query($conn, "SELECT * FROM `type` WHERE `code`='$pmain[type]'")or die(mysqli_error($conn));
                                $mtype = mysqli_fetch_array($getmtype);

                                $getcustomer = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
                                $mcust = mysqli_fetch_array($getcustomer);

                                $getmqty = mysqli_query($conn, "SELECT SUM(invqty)as totqty FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
                                $mqty = mysqli_fetch_array($getmqty);
                                $ctr++;
                                if ($gsttype == 2) {
                                    Echo "<tr>";
                                    Echo "<td>$ctr</td>";
                                    Echo "<td>$mtype[name]</td>";
                                    Echo "<td>$pmain[invno]</td>";
                                    Echo "<td>$pmain[mydt]</td>";
                                    Echo "<td>$mcust[name]</td>";
                                    Echo "<td>$pmain[subtotal]</td>";
                                    Echo "<td>$pmain[discount]</td>";
                                    Echo "<td>$pmain[freight]</td>";
                                    Echo "<td>$mqty[totqty]</td>";
                                    Echo "<td>$pmain[tottaxamt]</td>";
                                    Echo "<td>$pmain[sgstperc]</td>";
                                    Echo "<td>$pmain[totsgst]</td>";
                                    Echo "<td>$pmain[cgstperc]</td>";
                                    Echo "<td>$pmain[totcgst]</td>";
                                    Echo "<td>$pmain[igstperc]</td>";
                                    Echo "<td>$pmain[totigst]</td>";
                                    Echo "<td>$pmain[netamt]</td>";
                                    Echo "</tr>";
                                } elseif ($gsttype == '') {

                                    Echo "<tr>";
                                    Echo "<td>$ctr</td>";
                                    Echo "<td>$mtype[name]</td>";
                                    Echo "<td>$pmain[invno]</td>";
                                    Echo "<td>$pmain[mydt]</td>";
                                    Echo "<td>$mcust[name]</td>";
                                    Echo "<td>$pmain[subtotal]</td>";
                                    Echo "<td>$pmain[discount]</td>";
                                    Echo "<td>$pmain[freight]</td>";
                                    Echo "<td>$mqty[totqty]</td>";
                                    Echo "<td>$pmain[tottaxamt]</td>";
                                    Echo "<td>$pmain[sgstperc]</td>";
                                    Echo "<td>$pmain[totsgst]</td>";
                                    Echo "<td>$pmain[cgstperc]</td>";
                                    Echo "<td>$pmain[totcgst]</td>";
                                    Echo "<td>$pmain[netamt]</td>";
                                    Echo "</tr>";
                                } elseif ($gsttype == 1) {

                                    Echo "<tr>";
                                    Echo "<td>$ctr</td>";
                                    Echo "<td>$mtype[name]</td>";
                                    Echo "<td>$pmain[invno]</td>";
                                    Echo "<td>$pmain[mydt]</td>";
                                    Echo "<td>$mcust[name]</td>";
                                    Echo "<td>$pmain[subtotal]</td>";
                                    Echo "<td>$pmain[discount]</td>";
                                    Echo "<td>$pmain[freight]</td>";
                                    Echo "<td>$mqty[totqty]</td>";
                                    Echo "<td>$pmain[tottaxamt]</td>";
                                    Echo "<td>$pmain[igstperc]</td>";
                                    Echo "<td>$pmain[totigst]</td>";
                                    Echo "<td>$pmain[netamt]</td>";
                                    Echo "</tr>";
                                }

                                $totqty1 = $totqty1 + $mqty['totqty'];
                                $subtot = $subtot + $pmain['subtotal'];
                                $totdisc = $totdisc + $pmain['discount'];
                                $totfr = $totfr + $pmain['freight'];
                                $tottax = $tottax + $pmain['tottaxamt'];
                                $totsgst = $totsgst + $pmain['totsgst'];
                                $totcgst = $totcgst + $pmain['totcgst'];
                                $totigst = $totigst + $pmain['totigst'];
                                $totnet = $totnet + $pmain['netamt'];

                                if ($_POST["dtype"] == 2) {

                                    Echo "<tr style='height:10px;'>";
                                    Echo "<td colspan='25'>";
                                    Echo "<table style='width:100%;' class='table table-condensed table-hover'>";
                                    Echo "<th style='background-color: #cccccc'>Sr</th>";
                                    Echo "<th style='background-color: #cccccc'>Brand</th>";
                                    Echo "<th style='background-color: #cccccc'>Item Name</th>";
                                    Echo "<th style='background-color: #cccccc'>HSN Code</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Batch</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Act Batch</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Taka</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Act Taka</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Qty.</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Tot.Qty.</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Flag</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Flag Qty.</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Inv. Qty.</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Rate</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Amount</th>";
                                    Echo "</tr>";


                                    $ctr2 = 0;
                                    $totdtqty = 0;
                                    $totqty = 0;
                                    $totdtinv = 0;
                                    $totdtamt = 0;
                                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
                                    while ($tran = mysqli_fetch_array($gettran)) {

                                        $getbrand = mysqli_query($conn, "SELECT * FROM `brand` WHERE `code`='$tran[brand]'")or die(mysqli_error($conn));
                                        $mbrand = mysqli_fetch_array($getbrand);

                                        $getitem = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`='$tran[itemcode]'")or die(mysqli_error($conn));
                                        $mitem = mysqli_fetch_array($getitem);

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td>$mbrand[name]</td>";
                                        Echo "<td>$mitem[name]</td>";
                                        Echo "<td>$tran[hsncode]</td>";
                                        Echo "<td class='numeric'>$tran[batch]</td>";
                                        Echo "<td class='numeric'>$tran[actbatch]</td>";
                                        Echo "<td class='numeric'>$tran[taka]</td>";
                                        Echo "<td class='numeric'>$tran[acttaka]</td>";
                                        Echo "<td class='numeric'>$tran[qty]</td>";
                                        Echo "<td class='numeric'>$tran[totqty]</td>";
                                        Echo "<td class='numeric'>$tran[flag]</td>";
                                        Echo "<td class='numeric'>$tran[flagqty]</td>";
                                        Echo "<td class='numeric'>$tran[invqty]</td>";
                                        Echo "<td class='numeric'>$tran[rate]</td>";
                                        Echo "<td class='numeric'>$tran[amount]</td>";
                                        Echo "</tr>";
                                        $totqty = $totqty + $tran['qty'];
                                        $totdtinv = $totdtinv + $tran['invqty'];
                                        $totdtamt = $totdtamt + $tran['amount'];
                                        $totdtqty = $totdtqty + $tran['totqty'];
                                    }
                                    echo "<tr> ";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td><h4><b>Total</b></h4></td>";
                                    echo "<td></td>";
                                    echo "<td align='right' id='font'><h4><b>$totqty</b></h4></td>";
                                    echo "<td align='right' id='font'><h4><b>$totdtqty</b></h4></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td align='right' id='font'><h4><b>$totdtinv</b></h4></td>";
                                    echo "<td></td>";
                                    echo "<td align='right' id='font'><h4><b>$totdtamt</b></h4></td>";
                                    echo "</tr>";
                                    Echo "</table>";
                                    Echo "</td>";
                                }
                            }

                            if ($gsttype == 2) {
                                echo "<tr> ";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td><h4><b>Grand Total</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$subtot</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totdisc</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totfr</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totqty1</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$tottax</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b></b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totsgst</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b></b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totcgst</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b></b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totigst</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totnet</b></h4></td>";
                                echo "</tr>";
                            } elseif ($gsttype == '') {
                                echo "<tr> ";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td><h4><b>Grand Total</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$subtot</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totdisc</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totfr</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totqty1</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$tottax</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b></b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totsgst</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b></b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totcgst</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totnet</b></h4></td>";
                                echo "</tr>";
                            } elseif ($gsttype == 1) {
                                echo "<tr> ";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td><h4><b>Grand Total</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$subtot</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totdisc</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totfr</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totqty1</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$tottax</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b></b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totigst</b></h4></td>";
                                echo "<td align='right' id='font'><h4><b>$totnet</b></h4></td>";
                                echo "</tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

if (isset($_POST["excel"])) {
    $party = $_POST['party'];
    $item = $_POST['item'];
    $godown = $_POST['godown'];
    $gsttype = $_POST['gsttype'];
    $mtype = $_POST['type'];

    $gsttcond = " AND `oms`='$gsttype' ";

    $partycond = "";
    $brokercond = "";
    $transportcond = "";
    $itemcond = "";
    $godowncond = "";
    $mtypecond = "";

    if (!empty($party)) {
        $partycond = " AND `party`='$party' ";
    }

    if (!empty($item)) {
        $itemcond = " AND `name`='$item' ";
    }

    if (!empty($godown)) {
        $godowncond = " AND `godown`='$godown' ";
    }
    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; purchreg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; purchreg.php");
    }
    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    if (!empty($mtype)) {
        $mtypecond = " AND `type`='$mtype' ";
    }
    if ($gsttype == 2) {
        $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb`
             where `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'
             $partycond  $itemcond $godowncond $transcond $mtypecond $brokcond ")or die(mysqli_error($conn));
    } else {

        $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM"
                . " `$maindb` WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $partycond "
                . "$itemcond $godowncond $mtypecond $gsttcond")or die(mysqli_error($conn));
    }
    if ($gsttype == 2) {
        $contents.="SR., Type, Invocie No, Date, Supplier, Amount, Discount, Freight, Qty, "
                . "Taxable Value, SGST Rate, SGST Amount, CGST Rate, SGST Amount, IGST Rate,IGST Amount,"
                . "Net Amount,\n";
    } elseif ($gsttype == '') {
        $contents.="SR., Type, Invocie No, Date, Supplier, Amount, Discount, Freight, Qty, "
                . "Taxable Value, SGST Rate, SGST Amount, CGST Rate, SGST Amount,"
                . "Net Amount,\n";
    } elseif ($gsttype == 1) {
        $contents.="SR., Type, Invocie No, Date, Supplier, Amount, Discount, Freight, Qty, "
                . "Taxable Value, IGST Rate,SGST Amount"
                . "Net Amount,\n";
    }
    $ctr = 0;
    $subtot = 0;
    $totdisc = 0;
    $totfr = 0;
    $totqty1 = 0;
    $totnet = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $getmtype = mysqli_query($conn, "SELECT * FROM `type` WHERE `code`='$pmain[type]'")or die(mysqli_error($conn));
        $mtype = mysqli_fetch_array($getmtype);

        $getcustomer = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
        $mcust = mysqli_fetch_array($getcustomer);

        $getmqty = mysqli_query($conn, "SELECT SUM(invqty)as totqty FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
        $mqty = mysqli_fetch_array($getmqty);
        $ctr++;
        if ($gsttype == 2) {
            $contents.= "$ctr" . ",";
            $contents.= "$mtype[name]" . ",";
            $contents.= "$pmain[invno]" . ",";
            $contents.= "$pmain[mydt]" . ",";
            $contents.= "$mcust[name]" . ",";
            $contents.= "$pmain[subtotal]" . ",";
            $contents.= "$pmain[discount]" . ",";
            $contents.= "$pmain[freight]" . ",";
            $contents.= "$mqty[totqty]" . ",";
            $contents.= "$pmain[tottaxamt]" . ",";
            $contents.= "$pmain[sgstperc]" . ",";
            $contents.= "$pmain[totsgst]" . ",";
            $contents.= "$pmain[cgstperc]" . ",";
            $contents.= "$pmain[totcgst]" . ",";
            $contents.= "$pmain[igstperc]" . ",";
            $contents.= "$pmain[totigst]" . ",";
            $contents.= "$pmain[netamt]" . "\n";
        } elseif ($gsttype == '') {
            $contents.= "$ctr" . ",";
            $contents.= "$mtype[name]" . ",";
            $contents.= "$pmain[invno]" . ",";
            $contents.= "$pmain[mydt]" . ",";
            $contents.= "$mcust[name]" . ",";
            $contents.= "$pmain[subtotal]" . ",";
            $contents.= "$pmain[discount]" . ",";
            $contents.= "$pmain[freight]" . ",";
            $contents.= "$mqty[totqty]" . ",";
            $contents.= "$pmain[tottaxamt]" . ",";
            $contents.= "$pmain[sgstperc]" . ",";
            $contents.= "$pmain[totsgst]" . ",";
            $contents.= "$pmain[cgstperc]" . ",";
            $contents.= "$pmain[totcgst]" . ",";
            $contents.= "$pmain[netamt]" . "\n";
        } elseif ($gsttype == 1) {
            $contents.= "$ctr" . ",";
            $contents.= "$mtype[name]" . ",";
            $contents.= "$pmain[invno]" . ",";
            $contents.= "$pmain[mydt]" . ",";
            $contents.= "$mcust[name]" . ",";
            $contents.= "$pmain[subtotal]" . ",";
            $contents.= "$pmain[discount]" . ",";
            $contents.= "$pmain[freight]" . ",";
            $contents.= "$mqty[totqty]" . ",";
            $contents.= "$pmain[tottaxamt]" . ",";
            $contents.= "$pmain[igstperc]" . ",";
            $contents.= "$pmain[totigst]" . ",";
            $contents.= "$pmain[netamt]" . "\n";
        }

        $totqty1 = $totqty1 + $mqty['totqty'];
        $subtot = $subtot + $pmain['subtotal'];
        $totdisc = $totdisc + $pmain['discount'];
        $totfr = $totfr + $pmain['freight'];
        $tottax = $tottax + $pmain['tottaxamt'];
        $totsgst = $totsgst + $pmain['totsgst'];
        $totcgst = $totcgst + $pmain['totcgst'];
        $totigst = $totigst + $pmain['totigst'];
        $totnet = $totnet + $pmain['netamt'];

        if ($_POST["dtype"] == 2) {
            $contents.="Sr, Brand, Item Name, HSN Code, Batch, Act Batch, Taka, Act Taka, Qty, Tot.Qty, Flag, Flag Qty., Inv. Qty., Rate, Amount,\n";




            $ctr2 = 0;
            $totdtqty = 0;
            $totqty = 0;
            $totdtinv = 0;
            $totdtamt = 0;
            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {

                $getbrand = mysqli_query($conn, "SELECT * FROM `brand` WHERE `code`='$tran[brand]'")or die(mysqli_error($conn));
                $mbrand = mysqli_fetch_array($getbrand);

                $getitem = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`='$tran[itemcode]'")or die(mysqli_error($conn));
                $mitem = mysqli_fetch_array($getitem);

                $ctr2++;
                $ctr2++;
                $contents.="$ctr2" . ",";
                $contents.="$mbrand[name]" . ",";
                $contents.="$mitem[name]" . ",";
                $contents.="$tran[hsncode]" . ",";
                $contents.="$tran[batch]" . ",";
                $contents.="$tran[actbatch]" . ",";
                $contents.="$tran[taka]" . ",";
                $contents.="$tran[acttaka]" . ",";
                $contents.="$tran[qty]" . ",";
                $contents.="$tran[totqty]" . ",";
                $contents.="$tran[flag]" . ",";
                $contents.="$tran[flagqty]" . ",";
                $contents.="$tran[invqty]" . ",";
                $contents.="$tran[rate]" . ",";
                $contents.="$tran[amount]" . "\n";

                $totqty = $totqty + $tran[qty];
                $totdtinv = $totdtinv + $tran[invqty];
                $totdtamt = $totdtamt + $tran[amount];
                $totdtqty = $totdtqty + $tran[totqty];
            }
            $contents.= "" . ",";
            $contents.= "" . ",";
            $contents.= "" . ",";
            $contents.= "" . ",";
            $contents.= "" . ",";
            $contents.= "" . ",";
            $contents.= "Total" . ",";
            $contents.= "" . ",";
            $contents.= "$totqty" . ",";
            $contents.= "$totdtqty" . ",";
            $contents.= "" . ",";
            $contents.= "" . ",";
            $contents.= "$totdtinv" . ",";
            $contents.= "" . ",";
            $contents.= "$totdtamt" . "\n";
        }
    }

    if ($gsttype == 2) {
        $contents.= "" . ",";
        $contents.= "" . ",";
        $contents.= "" . ",";
        $contents.= "" . ",";
        $contents.= "Total Grand" . ",";
        $contents.= "$subtot" . ",";
        $contents.= "$totdisc" . ",";
        $contents.= "$totfr" . ",";
        $contents.= "$totqty1" . ",";
        $contents.= "$tottax" . ",";
        $contents.= "" . ",";
        $contents.= "$totsgst" . ",";
        $contents.= "" . ",";
        $contents.= "$totcgst" . ",";
        $contents.= "" . ",";
        $contents.= "$totigst" . ",";
        $contents.= "$totnet" . "\n";
    } elseif ($gsttype == '') {
        $contents.= "" . ",";
        $contents.= "" . ",";
        $contents.= "" . ",";
        $contents.= "" . ",";
        $contents.= "Total Grand" . ",";
        $contents.= "$subtot" . ",";
        $contents.= "$totdisc" . ",";
        $contents.= "$totfr" . ",";
        $contents.= "$totqty1" . ",";
        $contents.= "$tottax" . ",";
        $contents.= "" . ",";
        $contents.= "$totsgst" . ",";
        $contents.= "" . ",";
        $contents.= "$totcgst" . ",";
        $contents.= "" . ",";
        $contents.= "$totnet" . "\n";
    } elseif ($gsttype == 1) {
        $contents.= "" . ",";
        $contents.= "" . ",";
        $contents.= "" . ",";
        $contents.= "" . ",";
        $contents.= "Total Grand" . ",";
        $contents.= "$subtot" . ",";
        $contents.= "$totdisc" . ",";
        $contents.= "$totfr" . ",";
        $contents.= "$totqty1" . ",";
        $contents.= "$tottax" . ",";
        $contents.= "" . ",";
        $contents.= "$totigst" . ",";
        $contents.= "$totnet" . "\n";
    }

    $contents = strip_tags($contents);

    header("Content-Disposition: attachment; filename=$formname" . date('d-m-Y') . ".csv");
    print $contents;
}
?>
