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

$maindb = "credmain";
$trandb = "credtran";
$formname = "Credit Note GST Register";
$file = "crgstreg.php";

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

if (isset($_POST["view"])) {


$party = $_POST['party'];
$broker = $_POST['broker'];
$item = $_POST['item'];
$transport = $_POST['transport'];
$godown = $_POST['godown'];
$mtype = $_POST['mtype'];
$gsttype = $_POST['gsttype'];
$frmdt = $_POST['frmdt'];
$todt = $_POST['todt'];

$partycond = "";
$brokcond = "";
$transportcond = "";
$itemcond = "";
$godowncond = "";
$transcond = "";
$mtypecond = "";


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


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

if (!empty($broker)) {
$brokcond = " AND `broker`='$broker' ";
}

if (!empty($transport)) {
$transportcond = " AND `transport`='$transport' ";
}

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; salereg.php");
}
if (empty($_POST["todt"])) {
browse("To Date Cannot Be Empty");
header("refresh:1; salereg.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 $transcond $mtypecond $brokcond $gsttcond ")or die(mysqli_error($conn));
}
include("includes/linkshead.php");
?>

</head>
<body>
    <div class="container">
        <div class="row">
            <?php include("includes/head_rep.php"); ?>
            <div>
                <table style="width:100%;background: white" class="table table-bordered">
                    <thead>


                        <?php if ($gsttype == '2') { ?>
                        <tr style="height:10px;background-color: #1ABC9C">
                            <th style="width:2%;">Sr</th>
                            <th style="width:6%;">Type</th>
                            <th style="width:5%;">Inv.</th>
                            <th style="width:9%;">Date</th>
                            <th style="width:20%;">Customer</th>
                            <th style="width:2%;">GST NO</th>
                            <th style="width:5%;" class="numeric">Amount</th>
                            <th style="width:4%;" class="numeric">Discount</th>
                            <th style="width:3%;" class="numeric">Taxable Amt</th>
                            <th style="width:2%;" class="numeric">Qty</th>
                            <th style="width:6%;" class="numeric" colspan = '2'>CGST</th>
                            <th style="width:6%;" class="numeric" colspan='2'>SGST</th>
                            <th style="width:6%;" class="numeric" colspan='2'>IGST</th>
                            <th style="width:7%;" class="numeric">Freight</th>
                            <th style="width:7%;" class="numeric">Net Amount</th>                            
                        </tr>
                        <tr style="height:10px;background-color: #1ABC9C">
                            <th style="width:2%;"></th>
                            <th style="width:6%;"></th>
                            <th style="width:5%;"></th>
                            <th style="width:6%;"></th>
                            <th style="width:20%;"></th>
                            <th style="width:5%;"></th>
                            <th style="width:7%;" class="numeric"></th>
                            <th style="width:6%;" class="numeric"></th>
                            <th style="width:6%;" class="numeric"></th>
                            <th style="width:5%;" class="numeric"></th>
                            <th style="width:2%;" class="numeric">CGST %</th>
                            <th style="width:4%;" class="numeric">CGST Amt</th>
                            <th style="width:2%;" class="numeric">SGST %</th>
                            <th style="width:4%;" class="numeric">SGST Amt</th>
                            <th style="width:2%;" class="numeric">IGST %</th>
                            <th style="width:4%;" class="numeric">IGST Amt</th>
                            <th style="width:6%;" class="numeric"></th>
                            <th style="width:7%;" class="numeric"></th>                            
                        </tr>
                        <?php
                        }

                        if ($gsttype != '2') {
                        ?>

                        <tr style="height:10px;background-color: #1ABC9C">
                            <th style="width:2%;">Sr</th>
                            <th style="width:5%;">Type</th>
                            <th style="width:5%;">Invoice</th>
                            <th style="width:5%;">Date</th>
                            <th style="width:20%;">Customer</th>
                            <th style="width:5%;">GST NO</th>
                            <th style="width:6%;" class="numeric">Amount</th>
                            <th style="width:5%;" class="numeric">Discount</th>
                            <th style="width:6%;" class="numeric">Taxable Amt</th>
                            <th style="width:5%;" class="numeric">Qty</th>
                            <?php if ($gsttype == '') { ?>
                            <th style="width:4%;" class="numeric">CGST %</th>
                            <th style="width:7%;" class="numeric">CGST Amt</th>
                            <th style="width:4%;" class="numeric">SGST %</th>
                            <th style="width:7%;" class="numeric">SGST Amt</th>
                            <?php } elseif ($gsttype == 1) { ?>
                            <th style="width:4%;" class="numeric">SGST %</th>
                            <th style="width:7%;" class="numeric">SGST Amt</th>
                            <?php } ?>
                            <th style="width:7%;" class="numeric">Freight</th>
                            <th style="width:7%;" class="numeric">Net Amount</th>                            
                        </tr>
                        <?php } ?>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = $subtot = $totdisc = $totfr = $totqty1 = $totnet = 0;
                        $totcgstamt = $totsgstamt = $tottaxamt = 0;

                        while ($pmain = mysqli_fetch_array($getpmain)) {

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

                        $getcustomer = mysqli_query($conn, "SELECT name 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);

                        $gstno = mysqli_query($conn, "SELECT `accode`,`gstno` from `accmast` where `grpcode` = '010' and  `accode` = '$pmain[party]'") or die(mysqli_error($conn));
                        $fgstno = mysqli_fetch_array($gstno);

                        $mname1 = $mcust[name];
                        $mname = htmlspecialchars($mname1, ENT_QUOTES);
                        $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>$mname</td>";
                        Echo "<td>$fgstno[gstno]</td>";
                        Echo "<td class='numeric'>$pmain[subtotal]</td>";
                        Echo "<td class='numeric'>$pmain[discount]</td>";
                        Echo "<td class='numeric'>$pmain[tottaxamt]</td>";
                        Echo "<td class='numeric'>$mqty[totqty]</td>";
                        Echo "<td class='numeric'>$pmain[sgstperc]</td>";
                        Echo "<td class='numeric'>$pmain[totsgst]</td>";
                        Echo "<td class='numeric'>$pmain[cgstperc]</td>";
                        Echo "<td class='numeric'>$pmain[totcgst]</td>";
                        Echo "<td class='numeric'>$pmain[igstperc]</td>";
                        Echo "<td class='numeric'>$pmain[totigst]</td>";
                        Echo "<td class='numeric'>$pmain[freight]</td>";
                        Echo "<td class='numeric'>$pmain[netamt]</td>";
                        Echo "</tr>";
                        }
                        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>$mname</td>";
                        Echo "<td>$fgstno[gstno]</td>";
                        Echo "<td class='numeric'>$pmain[subtotal]</td>";
                        Echo "<td class='numeric'>$pmain[discount]</td>";
                        Echo "<td class='numeric'>$pmain[tottaxamt]</td>";
                        Echo "<td class='numeric'>$mqty[totqty]</td>";
                        if ($gsttype == '') {
                        Echo "<td class='numeric'>$pmain[sgstperc]</td>";
                        Echo "<td class='numeric'>$pmain[totsgst]</td>";
                        Echo "<td class='numeric'>$pmain[cgstperc]</td>";
                        Echo "<td class='numeric'>$pmain[totcgst]</td>";
                        } elseif ($gsttype == 1) {
                        Echo "<td class='numeric'>$pmain[igstperc]</td>";
                        Echo "<td class='numeric'>$pmain[totigst]</td>";
                        }
                        Echo "<td class='numeric'>$pmain[freight]</td>";
                        Echo "<td class='numeric'>$pmain[netamt]</td>";
                        Echo "</tr>";
                        }

                        $subtot = $subtot + $pmain['subtotal'];
                        $tottaxamt = $tottaxamt + $pmain['tottaxamt'];
                        $totdisc = $totdisc + $pmain['discount'];
                        $totfr = $totfr + $pmain['freight'];
                        $totcgstamt = $totcgstamt + $pmain['totcgst'];
                        $totsgstamt = $totsgstamt + $pmain['totsgst'];
                        $totigstamt = $totigstamt + $pmain['totigst'];
                        $totnet = $totnet + $pmain['netamt'];

                        $totqty1 = $totqty1 + $mqty['totqty'];

                        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'><i>Sr</i></th>";
                        Echo "<th style='background-color: #cccccc'><i>Brand</i></th>";
                        Echo "<th style='background-color: #cccccc'><i>Item Name</i></th>";
                        Echo "<th style='background-color: #cccccc'><i>HSN Code</i></th>";
                        Echo "<th style='background-color: #cccccc'><i>Batch</i></th>";
                        Echo "<th style='background-color: #cccccc'><i>Act Batch</i></th>";
                        Echo "<th style='background-color: #cccccc' class='numeric'><i>Taka</i></th>";
                        Echo "<th style='background-color: #cccccc' class='numeric'><i>Act Taka</i></th>";
                        Echo "<th style='background-color: #cccccc' class='numeric'><i>Qty.</i></th>";
                        Echo "<th style='background-color: #cccccc' class='numeric'><i>Tot.Qty.</i></th>";
                        Echo "<th style='background-color: #cccccc' class='numeric'><i>Flag</i></th>";
                        Echo "<th style='background-color: #cccccc' class='numeric'><i>Flag Qty.</i></th>";
                        Echo "<th style='background-color: #cccccc' class='numeric'><i>Inv Qty.</i></th>";
                        Echo "<th style='background-color: #cccccc' class='numeric'><i>Rate</i></th>";
                        Echo "<th style='background-color: #cccccc' class='numeric'><i>Amount</i></th>";
                        Echo "<tr>";


                        $ctr2 = 0;
                        $totqty = 0;
                        $totamt = 0;
                        $totinv = 0;
                        $totdtqty = 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><i>$ctr2</i></td>";
                        Echo "<td><i>$mbrand[name]</td>";
                        Echo "<td><i>$mitem[name]</td>";
                        Echo "<td class='numeric'><i>$tran[hsncode]</i></td>";
                        Echo "<td class='numeric'><i>$tran[batch]</i></td>";
                        Echo "<td class='numeric'><i>$tran[actbatch]</i></td>";
                        Echo "<td class='numeric'><i>$tran[taka]</i></td>";
                        Echo "<td class='numeric'><i>$tran[acttaka]</i></td>";
                        Echo "<td class='numeric'><i>$tran[qty]</i></td>";
                        Echo "<td class='numeric'><i>$tran[totqty]</i></td>";
                        Echo "<td class='numeric'><i>$tran[flag]</i></td>";
                        Echo "<td class='numeric'><i>$tran[flagqty]</i></td>";
                        Echo "<td class='numeric'><i>$tran[invqty]</i></td>";
                        Echo "<td class='numeric'><i>$tran[rate]</i></td>";
                        Echo "<td class='numeric'><i>$tran[amount]</i></td>";
                        Echo "</tr>";
                        $totqty = $totqty + $tran['qty'];
                        $totdtqty = $totdtqty + $tran['totqty'];
                        $totdtinv = $totdtinv + $tran['invqty'];
                        $totdtamt = $totdtamt + $tran['amount'];
                        $totqty1 = $totqty1 + $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></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>$tottaxamt</b></h4></td>";
                        echo "<td align='right' id='font'><h4><b>$totqty1</b></h4></td>";
                        echo "<td align='right' id='font'><h4><b></b></h4></td>";
                        echo "<td align='right' id='font'><h4><b>$totsgstamt</b></h4></td>";
                        echo "<td align='right' id='font'><h4><b></b></h4></td>";
                        echo "<td align='right' id='font'><h4><b>$totcgstamt</b></h4></td>";
                        echo "<td align='right' id='font'><h4><b></b></h4></td>";
                        echo "<td align='right' id='font'><h4><b>$totigstamt</b></h4></td>";
                        echo "<td align='right' id='font'><h4><b>$totfr</b></h4></td>";
                        echo "<td align='right' id='font'><h4><b>$totnet</b></h4></td>";
                        echo "</tr>";
                        }
                        if($gsttype != 2){
                        echo "<tr> ";
                        echo "<td></td>";
                        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>$tottaxamt</b></h4></td>";
                        echo "<td align='right' id='font'><h4><b>$totqty1</b></h4></td>";
                        if ($gsttype == '') {
                        echo "<td align='right' id='font'><h4><b></b></h4></td>";
                        echo "<td align='right' id='font'><h4><b>$totsgstamt</b></h4></td>";
                        echo "<td align='right' id='font'><h4><b></b></h4></td>";
                        echo "<td align='right' id='font'><h4><b>$totcgstamt</b></h4></td>";
                        } elseif ($gsttype == 1) {

                        echo "<td align='right' id='font'><h4><b></b></h4></td>";
                        echo "<td align='right' id='font'><h4><b>$totigstamt</b></h4></td>";
                        }
                        echo "<td align='right' id='font'><h4><b>$totfr</b></h4></td>";
                        echo "<td align='right' id='font'><h4><b>$totnet</b></h4></td>";
                        echo "<td></td>";
                        echo "</tr>";
                        }
                        
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

<?php include("includes/linksfoot.php"); ?>
</body>
</html>
<?php
}
if (isset($_POST["excel"])) {


    $party = $_POST['party'];
    $broker = $_POST['broker'];
    $item = $_POST['item'];
    $transport = $_POST['transport'];
    $godown = $_POST['godown'];
    $mtype = $_POST['mtype'];
    $gsttype = $_POST['gsttype'];
    $frmdt = $_POST['frmdt'];
    $todt = $_POST['todt'];

    $partycond = "";
    $brokcond = "";
    $transportcond = "";
    $itemcond = "";
    $godowncond = "";
    $transcond = "";
    $mtypecond = "";


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


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

    if (!empty($broker)) {
        $brokcond = " AND `broker`='$broker' ";
    }

    if (!empty($transport)) {
        $transportcond = " AND `transport`='$transport' ";
    }

    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; salereg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; salereg.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 $transcond $mtypecond $brokcond $gsttcond ")or die(mysqli_error($conn));
    }
    if ($gsttype == '2') {
        $contents .= "Sr, Type, Inv, Date, Customer, GST NO, Amount, Discount, Taxable Amt, Qty, CGST %, CGST Amt, SGST %, SGST Amt, IGST %, IGST Amt, Freight, Net Amount,\n";
    } elseif ($gsttype == '') {
        $contents .= "Sr, Type, Inv, Date, Customer, GST NO, Amount, Discount, Taxable Amt, Qty, CGST %, CGST Amt, SGST %, SGST Amt, Freight, Net Amount,\n";
    } elseif ($gsttype == 1) {
        $contents .= "Sr, Type, Inv, Date, Customer, GST NO, Amount, Discount, Taxable Amt, Qty, IGST %, IGST Amt, Freight, Net Amount,\n";
    }


    $ctr = $subtot = $totdisc = $totfr = $totqty1 = $totnet = 0;
    $totcgstamt = $totsgstamt = $tottaxamt = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {

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

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

        $getmqty = mysqli_query($conn, "SELECT SUM(qty) as totqty FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
        $mqty = mysqli_fetch_array($getmqty);

        $gstno = mysqli_query($conn, "SELECT `accode`,`gstno` from `accmast` where `grpcode` = '010' and  `accode` = '$pmain[party]'") or die(mysqli_error($conn));
        $fgstno = mysqli_fetch_array($gstno);

        $mname1 = $mcust[name];
        $mname = htmlspecialchars($mname1, ENT_QUOTES);
        $mname = str_replace(",", "", "$mname");
        $ctr++;

        if ($gsttype == 2) {
            $contents .= "$ctr" . ",";
            $contents .= "$mtype[name]" . ",";
            $contents .= "$pmain[invno]" . ",";
            $contents .= "$pmain[mydt]" . ",";
            $contents .= "$mname" . ",";
            $contents .= "$fgstno[gstno]" . ",";
            $contents .= "$pmain[subtotal]" . ",";
            $contents .= "$pmain[discount]" . ",";
            $contents .= "$pmain[tottaxamt]" . ",";
            $contents .= "$mqty[totqty]" . ",";
            $contents .= "$pmain[sgstperc]" . ",";
            $contents .= "$pmain[totsgst]" . ",";
            $contents .= "$pmain[cgstperc]" . ",";
            $contents .= "$pmain[totcgst]" . ",";
            $contents .= "$pmain[igstperc]" . ",";
            $contents .= "$pmain[totigst]" . ",";
            $contents .= "$pmain[freight]" . ",";
            $contents .= "$pmain[netamt]" . "\n";
        } elseif ($gsttype == '') {
            $contents .= "$ctr" . ",";
            $contents .= "$mtype[name]" . ",";
            $contents .= "$pmain[invno]" . ",";
            $contents .= "$pmain[mydt]" . ",";
            $contents .= "$mname" . ",";
            $contents .= "$fgstno[gstno]" . ",";
            $contents .= "$pmain[subtotal]" . ",";
            $contents .= "$pmain[discount]" . ",";
            $contents .= "$pmain[tottaxamt]" . ",";
            $contents .= "$mqty[totqty]" . ",";
            $contents .= "$pmain[sgstperc]" . ",";
            $contents .= "$pmain[totsgst]" . ",";
            $contents .= "$pmain[cgstperc]" . ",";
            $contents .= "$pmain[totcgst]" . ",";
            $contents .= "$pmain[freight]" . ",";
            $contents .= "$pmain[netamt]" . "\n";
        } elseif ($gsttype == 1) {
            $contents .= "$ctr" . ",";
            $contents .= "$mtype[name]" . ",";
            $contents .= "$pmain[invno]" . ",";
            $contents .= "$pmain[mydt]" . ",";
            $contents .= "$mname" . ",";
            $contents .= "$fgstno[gstno]" . ",";
            $contents .= "$pmain[subtotal]" . ",";
            $contents .= "$pmain[discount]" . ",";
            $contents .= "$pmain[tottaxamt]" . ",";
            $contents .= "$mqty[totqty]" . ",";
            $contents .= "$pmain[igstperc]" . ",";
            $contents .= "$pmain[totigst]" . ",";
            $contents .= "$pmain[freight]" . ",";
            $contents .= "$pmain[netamt]" . "\n";
        }

//                                Echo "<tr>";
//                                Echo "<td></td>";
//                                Echo "<td></td>";
//                                Echo "<td></td>";
//                                Echo "<td></td>";
//                                Echo "<td></td>";
//                                Echo "<td class='numeric'></td>";
//                                Echo "<td class='numeric'></td>";
//                                Echo "<td class='numeric'></td>";
//                                Echo "<td class='numeric'></td>";
//                                Echo "<td class='numeric'>$pmain[totsgst]</td>";
//                                Echo "<td class='numeric'>$pmain[totcgst]</td>";
//                                Echo "<td class='numeric'>$pmain[totigst]</td>";
//                                Echo "<td class='numeric'></td>";
//                                Echo "<td class='numeric'></td>";
//                                Echo "</tr>";


        $subtot = $subtot + $pmain['subtotal'];
        $tottaxamt = $tottaxamt + $pmain['tottaxamt'];
        $totdisc = $totdisc + $pmain['discount'];
        $totfr = $totfr + $pmain['freight'];
        $totcgstamt = $totcgstamt + $pmain['totcgst'];
        $totsgstamt = $totsgstamt + $pmain['totsgst'];
        $totigstamt = $totigstamt + $pmain['totigst'];
        $totnet = $totnet + $pmain['netamt'];

        $totqty1 = $totqty1 + $mqty['totqty'];

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



            $ctr2 = 0;
            $totqty = 0;
            $totamt = 0;
            $totinv = 0;
            $totdtqty = 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++;
                $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'];
                $totdtqty = $totdtqty + $tran['totqty'];
                $totdtinv = $totdtinv + $tran['invqty'];
                $totdtamt = $totdtamt + $tran['amount'];
                $totqty1 = $totqty1 + $tran['totqty'];
            }
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "Total" . ",";
            $contents .= "" . ",";
            $contents .= "$totqty" . ",";
            $contents .= "$totdtqty" . ",";
            $contents .= "$totdtinv" . ",";
            $contents .= "$totdtamt" . "\n";
        }
    }
    if ($gsttype == 2) {
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Grand Total" . ",";
        $contents .= "$subtot" . ",";
        $contents .= "$totdisc" . ",";
        $contents .= "$tottaxamt" . ",";
        $contents .= "$totqty1" . ",";
        $contents .= "" . ",";
        $contents .= "$totsgstamt" . ",";
        $contents .= "" . ",";
        $contents .= "$totcgstamt" . ",";
        $contents .= "" . ",";
        $contents .= "$totigstamt" . ",";
        $contents .= "$totfr" . ",";
        $contents .= "$totnet" . "\n";
    } elseif ($gsttype == '') {

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Grand Total" . ",";
        $contents .= "$subtot" . ",";
        $contents .= "$totdisc" . ",";
        $contents .= "$tottaxamt" . ",";
        $contents .= "$totqty1" . ",";
        $contents .= "" . ",";
        $contents .= "$totsgstamt" . ",";
        $contents .= "" . ",";
        $contents .= "$totcgstamt" . ",";
        $contents .= "$totfr" . ",";
    } elseif ($gsttype == 1) {
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Grand Total" . ",";
        $contents .= "$subtot" . ",";
        $contents .= "$totdisc" . ",";
        $contents .= "$tottaxamt" . ",";
        $contents .= "$totqty1" . ",";
        $contents .= "" . ",";
        $contents .= "$totsgstamt" . ",";
        $contents .= "$totcgstamt" . ",";
        $contents .= "$totfr" . ",";
    }
    $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;
    }
}
?>
