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

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

$maindb = 'slepmain';
$trandb = 'sleptran';
$zroot = $_SESSION["zdbroot"];

$formname = 'Sale Register Export';
$file = 'exsalereg_fr.php';


if (isset($_POST["view"])) {
    $partyname = $_POST["partyname"];
    $godown = $_POST["godown"];
    $staff = $_POST["staff"];
    $ptype = $_POST["ptype"];
    $dtype = $_POST["dtype"];
    $gsttype = $_POST["gsttype"];

    if (!empty($partyname)) {
        $partycond = " AND `party` = '$partyname' ";
    }
    if (!empty($godown)) {
        $godowncond = " AND `godown` = '$godown' ";
    }
    if (!empty($staff)) {
        $staffcond = " AND `staff` = '$staff '";
    }
    if (!empty($ptype)) {
        $ptypecond = " AND `paidby`= '$ptype'";
    }
    if (!empty($dtype)) {
        $dtypecond = " AND `dtype`= '$dtype' ";
    }

//    if ($gsttype == 1) {
//        $gstcond = "AND `oms` = 1";
//    } else {
//        $gstcond = "AND `oms` = ''";
//    }
    if (!empty($gsttype)) {
        if ($gsttype == 1) {
            $gstcond = "AND `oms` = '1'";
        } else {
            $gstcond = "AND `oms` = ''";
        }
    }



    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));








    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' $partycond $godowncond $staffcond $ptypecond $gstcond ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);


//    $gexam = mysqli_query($conn, "SELECT `code`,`name` FROM `exam` WHERE `code`= '$exam'")or die(mysqli_error($conn));
//    $dexam = mysqli_fetch_array($gexam);

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            background: #6082BF;
        }

    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>


        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">

                    <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%;">Sr</th>
                                <th style="width:6%;">Voucher</th>
                                <th style="width:10%;">Godown</th>
                                <th style="width:20%;">Party Name</th>
                                <th style="width:6%;">Staff</th>
                                <th style="width:6%;">Pay type</th>
                                <th style="width:7%" class="text-right">Amount</th>
                                <th style="width:7%" class="text-right">Disc Amt.</th>
                                <th style="width:7%" class="text-right">Taxable</th>
                                <th style="text-align:right; width:6%;">GST 5%</th>
                                <th style="text-align:right; width:6%;">GST 12%</th>
                                <th style="text-align:right; width:6%;">GST 18%</th>
                                <th style="text-align:right; width:6%;">GST 28%</th>
                                <th style="width:9%" class="text-right">Bill Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;
                            $totdiscamt = 0;
                            $tottaxable = 0;
                            $totgst5 = 0;
                            $totgst12 = 0;
                            $totgst18 = 0;
                            $totgst28 = 0;
                            $totpackamt = $totstamt = $totothamt = $totbillamt = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                if ($pmain[paidby] == "C") {
                                    $ptype = "CASH";
                                } else {
                                    $ptye = "CREDIT";
                                }

                                $gstaf = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[staff]' AND grpcode='040'  ") or die(mysqli_error($conn));
                                $sstaf = mysqli_fetch_array($gstaf);

                                $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]' AND grpcode='010'  ") or die(mysqli_error($conn));
                                $sparty = mysqli_fetch_array($gparty);

                                $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$pmain[godown]'") or die(mysqli_error($conn));
                                $sgodw = mysqli_fetch_array($ggodw);

                                if ($gsttype == '2') {
                                    $gst5 = $pmain[cgst5] + $pmain[sgst5];
                                    $gst12 = $pmain[cgst12] + $pmain[sgst12];
                                    $gst18 = $pmain[cgst18] + $pmain[sgst18];
                                    $gst28 = $pmain[cgst28] + $pmain[sgst28];
                                    $billamt = $pmain[billamt];
                                    $subtot = $pmain[grossamt];
                                    $totdisc = $pmain[discamt];
                                    $taxable = $pmain[taxableamt];
                                } else {
                                    $gst5 = $pmain[igst5];
                                    $gst12 = $pmain[igst12];
                                    $gst18 = $pmain[igst18];
                                    $gst28 = $pmain[igst28];
                                    $billamt = $pmain[billamt];
                                    $subtot = $pmain[grossamt];
                                    $totdisc = $pmain[discamt];
                                    $taxable = $pmain[taxableamt];
                                }





                                $ctr++;
                                Echo"<tr>";
                                Echo"<td>$ctr</td>";
                                Echo"<td>$pmain[vouchno]</td>";
                                Echo"<td>$sgodw[gcode]</td>";
                                Echo"<td>$sparty[name]</td>";
                                Echo"<td>$sstaf[name]</td>";
                                Echo"<td>$ptype</td>";
                                Echo"<td class='text-right'>$subtot</td>";
                                Echo"<td class='text-right'>$totdisc</td>";
                                Echo"<td class='text-right'>$taxable</td>";
                                Echo"<td class='text-right'>$gst5</td>";
                                Echo"<td class='text-right'>$gst12</td>";
                                Echo"<td class='text-right'>$gst18</td>";
                                Echo"<td class='text-right'>$gst28</td>";
                                Echo"<td class='text-right'>$billamt</td>";
                                Echo"</tr>";



                                $grsubtot = $grsubtot + $subtot;
                                $grtotdis = $grtotdis + $totdisc;
                                $grtottaxable = $grtottaxable + $taxable;
                                $grtotgst5 = $grtotgst5 + $gst5;
                                $grtotgst12 = $grtotgst12 + $gst12;
                                $grtotgst18 = $grtotgst18 + $gst18;
                                $grtotgst28 = $grtotgst28 + $gst28;
                                $grtotbillamt = $grtotbillamt + $billamt;

                                $totpackamt = $totpackamt + $pmain[packamt];
                                $totstamt = $totstamt + $pmain[stamt];
                                $totothamt = $totothamt + $pmain[otheramt];
                                $totbillamt = $totbillamt + $pmain[billamt];


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

                                    Echo "<tr style='height:10px;'>";
                                    Echo "<td colspan='25'>";
                                    Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                    Echo "<thead>";
                                    Echo "<tr class='mySearchTr2' style='background-color:#c45254'>";
                                    Echo "<th style='width:5%;'>Sr</th>";
                                    Echo "<th style='width:7%;'>Barcode</th>";
                                    Echo "<th style='width:5%;' class=''>Item Code</th>";
                                    Echo "<th style='width:9%;' class=''>Item Name</th>";
                                    Echo "<th style='width:8%;' class=''>HSN Code</th>";
                                    Echo "<th style='width:5%;' class=''>Gram</th>";
                                    Echo "<th style='width:5%;' class=''>Unit</th>";
                                    Echo "<th style='width:6%;' class=''>Mrp</th>";
                                    Echo "<th style='width:5%;' class=''>Batch</th>";
                                    Echo "<th style='width:5%;' class=''>Qty.</th>";
                                    Echo "<th style='width:5%;' class=''>Rate</th>";
                                    Echo "<th style='width:5%;' class=''>Amount</th>";
                                    Echo "<th style='width:5%;' class=''>Disc%</th>";
                                    Echo "<th style='width:5%;' class=''>Disc. Amt.</th>";
                                    Echo "<th style='width:5%;' class=''>Taxable Amount</th>";
                                    if ($gsttype == 1) {
                                        Echo "<th style='width:5%;' class=''>IGST Rate</th>";
                                        Echo "<th style='width:5%;' class=''>IGST Amount</th>";
                                    } elseif ($gsttype == 2) {
                                        Echo "<th style='width:5%;' class=''>CGST Rate</th>";
                                        Echo "<th style='width:5%;' class=''>CGST Amount</th>";
                                        Echo "<th style='width:5%;' class=''>SGST Rate</th>";
                                        Echo "<th style='width:5%;' class=''>SGST Amount</th>";
                                    }
                                    Echo "<th style='width:10%;' class=''>Total</th>";
                                    Echo "</tr>";
                                    Echo "<thead>";
                                    Echo "<tr>";
//
//
                                    $ctr2 = 0;
                                    $totqty = 0;
                                    $totrate = 0;
                                    $totamount = $totdiscount = $totsubtot = $totcgstamt = $totsgstamt = $totigstamt = 0;

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

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

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td>$tran[barcode]</td>";
                                        Echo "<td>$tran[itemcode]</td>";
                                        Echo "<td>$tran[itemname]</td>";
                                        Echo "<td>$tran[hsncode]</td>";
                                        Echo "<td>$tran[gram]</td>";
                                        Echo "<td>$tran[unit]</td>";
                                        Echo "<td>$tran[mrp]</td>";
                                        Echo "<td>$tran[batch]</td>";
                                        Echo "<td class='numeric'>$tran[qty]</td>";
                                        Echo "<td class='numeric'>$tran[rate]</td>";
                                        Echo "<td class='numeric'>$tran[amount]</td>";
                                        Echo "<td class='numeric'>$tran[discperc]</td>";
                                        Echo "<td class='numeric'>$tran[discamt]</td>";
                                        Echo "<td class='numeric'>$tran[subtot]</td>";
                                        if ($gsttype == 1) {
                                            Echo "<td class='numeric'>$tran[igstrate]</td>";
                                            Echo "<td class='numeric'>$tran[igstamt]</td>";
                                        } elseif ($gsttype == 2) {
                                            Echo "<td class='numeric'>$tran[cgstrate]</td>";
                                            Echo "<td class='numeric'>$tran[cgstamt]</td>";
                                            Echo "<td class='numeric'>$tran[sgstrate]</td>";
                                            Echo "<td class='numeric'>$tran[sgstamt]</td>";
                                        }
                                        Echo "<td class='numeric'>$tran[amount]</td>";
                                        Echo "</tr>";

                                        $totqty = $totqty + $tran[qty];
                                        $totamount = $totamount + $tran[amount];
                                        $totdiscount = $totdiscount + $tran[discamt];
                                        $totsubtot = $totsubtot + $tran[subtot];
                                        $totcgstamt = $totcgstamt + $tran[cgstamt];
                                        $totsgstamt = $totsgstamt + $tran[sgstamt];
                                        $totigstamt = $totigstamt + $tran[igstamt];
//                                        $totamt = $totamt + $trantotal;
//                                        $ftot = $ftot + $tran[subtot];
                                    }

                                    Echo "<tr>";
                                    Echo "<td></td>";
                                    Echo "<td></td>";
                                    Echo "<td></td>";
                                    Echo "<td></td>";
                                    Echo "<td></td>";
                                    Echo "<td></td>";
                                    Echo "<td></td>";
                                    Echo "<td></td>";
                                    Echo "<td>Total</td>";
                                    Echo "<td>$totqty</td>";
                                    Echo "<td></td>";
                                    Echo "<td>$totamount</td>";
                                    Echo "<td></td>";
                                    Echo "<td>$totdiscount</td>";
                                    Echo "<td>$totsubtot</td>";
                                    if ($gsttype == 1) {
                                        Echo "<td></td>";
                                        Echo "<td>$totigstamt2</td>";
                                    } elseif ($gsttype == 2) {
                                        Echo "<td></td>";
                                        Echo "<td>$totcgstamt</td>";
                                        Echo "<td></td>";
                                        Echo "<td>$totsgstamt</td>";
                                    }
                                    Echo "<td>$totamount</td>";
                                    Echo "</tr>";
                                    Echo "</table>";
                                }
                            }
                            Echo "<tr>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td class='text-right'><b>Total</b></td>";
                            Echo "<td class='numeric text-right'><b>$grsubtot</b></td>";
                            Echo "<td class='numeric text-right'><b>$grtotdis</b></td>";
                            Echo "<td class='numeric text-right'><b>$grtottaxable</b></td>";
                            Echo "<td class='numeric text-right'><b>$grtotgst5</b></td>";
                            Echo "<td class='numeric text-right'><b>$grtotgst12</b></td>";
                            Echo "<td class='numeric text-right'><b>$grtotgst18</b></td>";
                            Echo "<td class='numeric text-right'><b>$grtotgst28</b></td>";
                            Echo "<td class='numeric text-right'><b>$grtotbillamt</b></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div> 
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
    $partyname = $_POST["partyname"];
    $godown = $_POST["godown"];
    $staff = $_POST["staff"];
    $ptype = $_POST["ptype"];
    $dtype = $_POST["dtype"];
    $gsttype = $_POST["gsttype"];

    if (!empty($partyname)) {
        $partycond = " AND `party` = '$partyname' ";
    }
    if (!empty($godown)) {
        $godowncond = " AND `godown` = '$godown' ";
    }
    if (!empty($staff)) {
        $staffcond = " AND `staff` = '$staff '";
    }
    if (!empty($ptype)) {
        $ptypecond = " AND `paidby`= '$ptype'";
    }
    if (!empty($dtype)) {
        $dtypecond = " AND `dtype`= '$dtype' ";
    }
    if (!empty($gsttype)) {
        if ($gsttype == 1) {
            $gstcond = "AND `oms` = '1'";
        } else {
            $gstcond = "AND `oms` = ''";
        }
    }



    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));

    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' $partycond $godowncond $staffcond $ptypecond $gstcond ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);



    $contents.="Sr,Voucher,Godown,Party Name,Staff,Pay type,Amount,Disc Amt.,Taxable,GST 5%,GST 12%,GST 18%,GST 28%,Bill Amount\n";

    $ctr = 0;
    $totamt = 0;
    $totdiscamt = 0;
    $tottaxable = 0;
    $totgst5 = 0;
    $totgst12 = 0;
    $totgst18 = 0;
    $totgst28 = 0;
    $totpackamt = $totstamt = $totothamt = $totbillamt = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {
        if ($pmain[paidby] == "C") {
            $ptype = "CASH";
        } else {
            $ptye = "CREDIT";
        }

        $gstaf = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[staff]' AND grpcode='040'  ") or die(mysqli_error($conn));
        $sstaf = mysqli_fetch_array($gstaf);

        $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]' AND grpcode='010'  ") or die(mysqli_error($conn));
        $sparty = mysqli_fetch_array($gparty);

        $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$pmain[godown]'") or die(mysqli_error($conn));
        $sgodw = mysqli_fetch_array($ggodw);

        if ($gsttype == '2') {
            $gst5 = $pmain[cgst5] + $pmain[sgst5];
            $gst12 = $pmain[cgst12] + $pmain[sgst12];
            $gst18 = $pmain[cgst18] + $pmain[sgst18];
            $gst28 = $pmain[cgst28] + $pmain[sgst28];
            $billamt = $pmain[billamt];
            $subtot = $pmain[grossamt];
            $totdisc = $pmain[discamt];
            $taxable = $pmain[taxableamt];
        } else {
            $gst5 = $pmain[igst5];
            $gst12 = $pmain[igst12];
            $gst18 = $pmain[igst18];
            $gst28 = $pmain[igst28];
            $billamt = $pmain[billamt];
            $subtot = $pmain[grossamt];
            $totdisc = $pmain[discamt];
            $taxable = $pmain[taxableamt];
        }


        $ctr++;
        $contents.= $ctr . ",";
        $contents.= $pmain[vouchno] . ",";
        $contents.= $sgodw[gcode] . ",";
        $contents.= $sparty[name] . ",";
        $contents.= $sstaf[name] . ",";
        $contents.= $ptype . ",";
        $contents.= $subtot . ",";
        $contents.= $totdisc . ",";
        $contents.= $taxable . ",";
        $contents.= $gst5 . ",";
        $contents.= $gst12 . ",";
        $contents.= $gst18 . ",";
        $contents.= $gst28 . ",";
        $contents.= $billamt . "\n";




        $grsubtot = $grsubtot + $subtot;
        $grtotdis = $grtotdis + $totdisc;
        $grtottaxable = $grtottaxable + $taxable;
        $grtotgst5 = $grtotgst5 + $gst5;
        $grtotgst12 = $grtotgst12 + $gst12;
        $grtotgst18 = $grtotgst18 + $gst18;
        $grtotgst28 = $grtotgst28 + $gst28;
        $grtotbillamt = $grtotbillamt + $billamt;

        $totpackamt = $totpackamt + $pmain[packamt];
        $totstamt = $totstamt + $pmain[stamt];
        $totothamt = $totothamt + $pmain[otheramt];
        $totbillamt = $totbillamt + $pmain[billamt];


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

            if ($gsttype == 1) {
                $contents.="Sr,Barcode,Item Code,Item Name,HSN Code,Gram,Unit,Mrp,Batch,Qty.,Rate,Amount,Disc%,Disc. Amt.,Taxable Amount,IGST Rate,IGST Amount,Total\n";
            } elseif ($gsttype == 2) {
                $contents.="Sr,Barcode,Item Code,Item Name,HSN Code,Gram,Unit,Mrp,Batch,Qty.,Rate,Amount,Disc%,Disc. Amt.,Taxable Amount,CGST Rate,CGST Amount,SGST Rate,SGST Amount,Total\n";
            }

            $ctr2 = 0;
            $totqty = 0;
            $totrate = 0;
            $totamount = $totdiscount = $totsubtot = $totcgstamt = $totsgstamt = $totigstamt = 0;

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

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

                $ctr2++;




                $contents.= $ctr2 . ",";
                $contents.= $tran[barcode] . ",";
                $contents.= $tran[itemcode] . ",";
                $contents.= $tran[itemname] . ",";
                $contents.= $tran[hsncode] . ",";
                $contents.= $tran[gram] . ",";
                $contents.= $tran[unit] . ",";
                $contents.= $tran[mrp] . ",";
                $contents.= $tran[batch] . ",";
                $contents.= $tran[qty] . ",";
                $contents.= $tran[rate] . ",";
                $contents.= $tran[amount] . ",";
                $contents.= $tran[discperc] . ",";
                $contents.= $tran[discamt] . ",";
                $contents.= $tran[subtot] . ",";
                if ($gsttype == 1) {
                    $contents.= $tran[igstrate] . ",";
                    $contents.= $tran[igstamt] . ",";
                } elseif ($gsttype == 2) {
                    $contents.= $tran[cgstrate] . ",";
                    $contents.= $tran[cgstamt] . ",";
                    $contents.= $tran[sgstrate] . ",";
                    $contents.= $tran[sgstamt] . ",";
                }
                $contents.= $tran[amount] . "\n";


                $totqty = $totqty + $tran[qty];
                $totamount = $totamount + $tran[amount];
                $totdiscount = $totdiscount + $tran[discamt];
                $totsubtot = $totsubtot + $tran[subtot];
                $totcgstamt = $totcgstamt + $tran[cgstamt];
                $totsgstamt = $totsgstamt + $tran[sgstamt];
                $totigstamt = $totigstamt + $tran[igstamt];

            }

            Echo "<tr>";
            $contents.= " " . ",";
            $contents.= " " . ",";
            $contents.= " " . ",";
            $contents.= " " . ",";
            $contents.= " " . ",";
            $contents.= " " . ",";
            $contents.= " " . ",";
            $contents.= " " . ",";
            $contents.= "Total" . ",";
            $contents.= "$totqty" . ",";
            $contents.= " " . ",";
            $contents.= $totamount . ",";
            $contents.= " " . ",";
            $contents.= $totdiscount . ",";
            $contents.= $totsubtot . ",";
            if ($gsttype == 1) {
                $contents.= " " . ",";
                $contents.= $totigstamt2 . ",";
            } elseif ($gsttype == 2) {
                $contents.= " " . ",";
                $contents.= $totcgstamt . ",";
                $contents.= " " . ",";
                $contents.= $totsgstamt. ",";
            }
            $contents.=$totamount . "\n";
        }
    }
    
  $contents .= " " . ",";
  $contents .= " " . ",";
  $contents .= " " . ",";
  $contents .= " " . ",";
  $contents .= " " . ",";
    $contents.= "Total" . ",";
    $contents .= $grsubtot . ",";
    $contents .= $grtotdis . ",";
    $contents .= $grtottaxable . ",";
    $contents .= $grtotgst5 . ",";
    $contents .= $grtotgst12 . ",";
    $contents .= $grtotgst18 . ",";
    $contents .= $grtotgst28 . ",";
    $contents .= $grtotbillamt . "\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;
    }
}
?>