<?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);
$zroot = $_SESSION["zdbroot"];
$zcomp = $_SESSION["comp_" . $dbname];
$zyear = $myyear;

$file = 'salereg_fr.php';

$supplier = $_POST["supplier"];
$p = $_GET["p"];
$ptype = $_POST["ptype"];
$pchtype = $_POST["pchtype"];
$dtype = $_POST["dtype"];
$gsttype = $_POST["gsttype"];

if (!empty($supplier)) {
    $suppcond = " AND `party` = '$supplier' ";
    $suppcond2 = " AND `accode` = '$supplier' ";
}
if (!empty($p)) {
    $pcond = " AND `party` = '$p' ";
}

if (!empty($ptype)) {
    $ptypecond = " AND `cashcred`= '$ptype'";
}

if ($ptype == "C") {
    $paytype = "CASH";
} else {
    $paytype = "CREDIT";
}

$formname = 'Sale Register';

if (!empty($dtype)) {
    $dtypecond = " AND `dtype`= '$dtype' ";
}


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

if (($_GET["fdt"]) != '') {
    $mfdt = $_GET["fdt"];
    $mtdt = $_GET["tdt"];
    $mfrmdate = date("y-m-d", strtotime($mfdt));
    $mtodate = date("y-m-d", strtotime($mtdt));
    $dtype = $_GET["rpt"];
} else {

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

    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");
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}

if (!empty($gsttype)) {
    if ($gsttype == 1) {
        $gstcond = "AND `oms` = '1'";
    } else {
        $gstcond = "AND `oms` = ''";
    }
} else {
    $gstcond = "";
}
$getpmain2 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `salemain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $suppcond $ptypecond  $gstcond $pcond order by vouchdt,vouchno") or die(mysqli_error($conn));


$getno2 = mysqli_num_rows($getpmain2);

if (isset($_POST["view"]) || ($_GET["view"])) {
    include("includes/linkshead.php");
?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }
    </style>
    </head>

    <body>


        <div class="container-fluid">
            <?php
            include("includes/head_rep2.php");

            if ($getno2 > 0) {
            ?>
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table class="table table-condesed table-bordered">
                            <tr class="mySearchTr">
                                <!--9221292520-->
                                <th width="100%" class="h4" style="color:white"><b>Sale Register</b></th>
                            </tr>
                            <tr>
                                <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                    Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno2; ?></b>
                                </td>
                            </tr>
                        </table>
                        <?php if ($ptype != "") { ?>
                            <table width="100%" class="table" style="background-color: #98F9F0">
                                <tr>
                                    <td width="7%" style="font-size:14px">Pay Type : </td>
                                    <td width="93%" style="font-size:14px"><b><?php echo $paytype; ?></b></td>
                                </tr>
                            </table>
                        <?php } ?>
                        <table style="background: white ;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th width="3%;">Sr</th>
                                    <th width="3%;">Voucher</th>
                                    <th width="7%;">Date</th>
                                    <th width="4%;">Invoice No.</th>
                                    <th width="15%;">Party Name</th>
                                    <th width="5%;">Pay type</th>
                                    <th width="6%;" class="text-right">LR .No.</th>
                                    <th width="6%;">LR Date</th>
                                    <th width="5%;">Transporter</th>
                                    <!-- <th width="5%;" >Area</th> -->
                                    <th width="5%;" class="text-right">Total Qty.</th>
                                    <th width="6%;" class="text-right">Sub Total</th>
                                    <th width="6%;" class="text-right">Less Item Discount</th>
                                    <th width="6%;" class="text-right">Taxable Value</th>
                                    <th width="6%;" class="text-right">Total Gst </th>
                                    <th width="6%;" class="text-right">Total Amount</th>
                                    <th width="10%;">Remarks</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $pctr = 0;
                                $ptotamt = 0;
                                $ptotdiscamt = 0;
                                $ptottaxable = 0;
                                $ptotgst3 = 0;
                                $ptotgst5 = 0;
                                $ptotgst12 = 0;
                                $ptotgst18 = 0;
                                $ptotgst28 = 0;
                                $ptotpackamt = $ptotstamt = $ptotothamt = $ptotbillamt = 0;
                                $pchtypecond = "";

                                //                                $getpmain_p = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `rawmain` WHERE `vouchdt` >= '$mfrmdate' 
                                //                                        AND `vouchdt` <= '$mtodate' $suppcond $godowncond $gstcond $pcond order by vouchdt,vouchno")or die(mysqli_error($conn));


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


                                    $cmpname = mysqli_query($conn, "SELECT * From $zroot.`company` where `code`='$zcomp' ") or die(mysqli_error($conn));
                                    $getcmpname = mysqli_fetch_array($cmpname);
                                    $init  = $getcmpname['init'];

                                    $invoice =  $init . "/" . $zyear . "/" . $pmain["vouchno"];



                                    if ($pmain['cashcred'] == "C") {
                                        $ptyped = "CASH";
                                    } else {
                                        $ptyped = "CREDIT";
                                    }

                                    $voucher = $pmain['vouchno'];
                                    $invno = $pmain['invno'];
                                    $totalqty = $pmain['totalqty'];
                                    $mydt = $pmain['mydt'];

                                    if ($pmain['canc_flag'] == "C") {
                                        $partynm = "<b style='color:red'>CANCELLED</b>";
                                        $color = "red";
                                        $subtot = $totdisc = $totvat = $billamt = 0;
                                        $gstno = "";
                                        $billamt = $subtot = $totvat = $totdisc = $totgst = 0;
                                    } else if ($pmain['canc_flag'] != "C") {
                                        $gparty = mysqli_query($conn, "SELECT `accode`,`name`,`shipname`, `gstno` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
                                        $sparty = mysqli_fetch_array($gparty);

                                        $transport = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` where `code` = '$pmain[transporter]'") or die(mysqli_error($conn));
                                        $gettransport = mysqli_fetch_array($transport);

                                        $partynm = $sparty['name'];
                                        $shipname = $sparty['shipname'];
                                        // $gstno = $sparty['gstno'];

                                        if ($pmain["shipgstno"] == $sparty['gstno'] || $pmain['shipgstno'] == '') {
                                            $gstno = $sparty['gstno'];
                                        } else {
                                            $gstno = $pmain['shipgstno'];
                                        }

                                        $dname = $gettransport['name'];
                                        $totdisc = $pmain['totdisc'];
                                        $lrno = $pmain['lrno'];
                                        $lrdate = $pmain['lrdate'];
                                        $area = $pmain['area'];
                                        $totqty = $pmain['totqty'];
                                        $subtotal = $pmain['subtotal'];
                                        $litemdisc = $pmain['litemdisc'];
                                        $tottaxamt = $pmain['tottaxamt'];
                                        $totalgst = $pmain['totalgst'];
                                        $netamt = $pmain['netamt'];
                                        $remark1 = $pmain['remark1'];
                                        $remark2 = $pmain['remark2'];
                                    }

                                    $pctr++;
                                    echo "<tr data-href='salegstmulti.php?mode=edit&vouchno=$voucher' style='background-color: lightyellow' class='clickable-row2' [target]='_blank'>";
                                    echo "<td>$pctr</td>";
                                    echo "<td>$voucher</td>";
                                    echo "<td>$mydt</td>";
                                    echo "<td>$invoice</td>";
                                    echo "<td>$partynm <br> $gstno</td>";
                                    echo "<td>$ptyped</td>";
                                    echo "<td class='text-right'>$lrno</td>";
                                    echo "<td class='text-right'>$lrdate</td>";
                                    echo "<td>$dname</td>";
                                    // Echo"<td>$area</td>";
                                    echo "<td class='text-right'>$totqty</td>";
                                    echo "<td class='text-right'>$subtotal</td>";
                                    echo "<td class='text-right'>$litemdisc</td>";
                                    echo "<td class='text-right'>$tottaxamt</td>";
                                    echo "<td class='text-right'>$totalgst</td>";
                                    echo "<td class='text-right'>$netamt</td>";
                                    echo "<td>$remark1 $remark2</td>";
                                    echo "</tr>";


                                    //                                    Echo"<tr>";
                                    //                                    Echo"<td></td>";
                                    //                                    Echo"<td></td>";
                                    //                                    Echo"<td></td>";
                                    //                                    Echo"<td></td>";
                                    //                                    echo "<td>$gstno</td>";
                                    //                                    Echo"<td></td>";
                                    //                                    Echo"<td class='text-right'></td>";
                                    //                                    Echo"<td class='text-right'></td>";
                                    //                                    Echo"<td></td>";
                                    //                                    Echo"<td></td>";
                                    //                                    Echo"<td class='text-right'></td>";
                                    //                                    Echo"<td class='text-right'></td>";
                                    //                                    Echo"<td class='text-right'></td>";
                                    //                                    Echo"<td class='text-right'></td>";
                                    //                                    Echo"<td class='text-right'></td>";
                                    //                                    Echo"<td class='text-right'></td>";
                                    //                                    Echo"<td></td>";
                                    //                                    Echo"</tr>";

                                    $pgrtotqty = $pgrtotqty + $totqty;
                                    $pgrsubtot = $pgrsubtot + $subtotal;
                                    $pgrtotdis = $pgrtotdis + $litemdisc;
                                    $pgrtotvat = $pgrtotvat + $tottaxamt;
                                    $pgrtotgst = $pgrtotgst + $totalgst;
                                    $pgrtotbillamt = $pgrtotbillamt + $netamt;

                                    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='' style='background-color:#BABFC2'>";
                                        echo "<th style='width:5%;'>Sr</th>";
                                        echo "<th style='width:15%;' class=''>Itemname</th>";
                                        echo "<th style='width:5%;' class=''>HSN Code</th>";
                                        echo "<th style='width:4%;' class='text-right'> Qty.</th>";
                                        echo "<th style='width:5%;' class='text-right'>Rate</th>";
                                        echo "<th style='width:5%;' class='text-right'>Amount</th>";
                                        echo "<th style='width:5%;' class='text-right'>Disc %</th>";
                                        echo "<th style='width:5%;' class='text-right'>Disc Amt</th>";
                                        echo "<th style='width:5%;' class='text-right'>Taxable Value</th>";
                                        echo "<th style='width:5%;' class='text-right'>CGST Rate</th>";
                                        echo "<th style='width:5%;' class='text-right'>CGST Amt</th>";
                                        echo "<th style='width:5%;' class='text-right'>SGST Rate</th>";
                                        echo "<th style='width:5%;' class='text-right'>SGST Amt</th>";
                                        echo "<th style='width:5%;' class='text-right'>IGST Rate</th>";
                                        echo "<th style='width:5%;' class='text-right'>IGST Amt</th>";
                                        echo "<th style='width:5%;' class='text-right'>Total</th>";
                                        echo "</tr>";
                                        echo "<thead>";
                                        echo "<tr>";

                                        $ctr2 = 0;
                                        $pctr2 = 0;
                                        $pcartqty = $pqty = $ptotqty = $psubtot = $ptotamount = $pcgstamt = $psgstamt = $pigstamt = $ptotdiscount = $ptotvat = $ptotal = 0;

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

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

                                            $ctr2++;
                                            echo "<tr>";
                                            echo "<td>$ctr2</td>";
                                            echo "<td>$showitem[name]</td>";
                                            echo "<td>$tran[hsncode]</td>";
                                            echo "<td class='text-right'>$tran[qty]</td>";
                                            echo "<td class='text-right'>$tran[rate]</td>";
                                            echo "<td class='text-right'>$tran[amount]</td>";
                                            echo "<td class='text-right'>$tran[discperc]</td>";
                                            echo "<td class='text-right'>$tran[discount]</td>";
                                            echo "<td class='text-right'>$tran[subtot]</td>";
                                            echo "<td class='text-right'>$tran[cgstrate]</td>";
                                            echo "<td class='text-right'>$tran[cgstamt]</td>";
                                            echo "<td class='text-right'>$tran[sgstrate]</td>";
                                            echo "<td class='text-right'>$tran[sgstamt]</td>";
                                            echo "<td class='text-right'>$tran[igstrate]</td>";
                                            echo "<td class='text-right'>$tran[igstamt]</td>";
                                            echo "<td class='text-right'>$tran[total]</td>";
                                            echo "</tr>";

                                            $pqty = $pqty + $tran['qty'];
                                            //                                            $prate        = $prate + $tran['rate'];
                                            $ptotamount = $ptotamount + $tran['amount'];
                                            //                                            $pdiscperc    = $pdiscperc + $tran['discperc'];
                                            $pdiscount = $pdiscount + $tran['discount'];
                                            $psubtot = $psubtot + $tran['subtot'];
                                            //                                            $pcgstrate    = $pcgstrate + $tran['cgstrate'];
                                            $pcgstamt = $pcgstamt + $tran['cgstamt'];
                                            //                                            $psgstrate    = $psgstrate + $tran['sgstrate'];
                                            $psgstamt = $psgstamt + $tran['sgstamt'];
                                            //                                            $pigstrate    = $pigstrate + $tran['igstrate'];
                                            $pigstamt = $pigstamt + $tran['igstamt'];
                                            $ptotal = $ptotal + $tran['total'];
                                            $tran['qty'] = $tran['amount'] = $tran['discount'] = $tran['subtot'] = $tran['cgstamt'] = $tran['sgstamt'] = $tran['igstamt'] = $tran['total'] = 0;
                                        }

                                        echo "<tr style='background-color:#d8e0e6'>";
                                        echo "<td colspan='2'></td>";
                                        echo "<td>Total</td>";
                                        echo "<td class='text-right'>$pqty</td>";
                                        echo "<td class='text-right'>$prate</td>";
                                        echo "<td class='text-right'>$ptotamount</td>";
                                        echo "<td class='text-right'>$pdiscperc</td>";
                                        echo "<td class='text-right'>$pdiscount</td>";
                                        echo "<td class='text-right'>$psubtot</td>";
                                        echo "<td class='text-right'>$pcgstrate</td>";
                                        echo "<td class='text-right'>$pcgstamt</td>";
                                        echo "<td class='text-right'>$psgstrate</td>";
                                        echo "<td class='text-right'>$psgstamt</td>";
                                        echo "<td class='text-right'>$pigstrate</td>";
                                        echo "<td class='text-right'>$pigstamt</td>";
                                        echo "<td class='text-right'>$ptotal</td>";
                                        echo "</tr>";
                                        echo "</table>";
                                    }
                                }

                                echo "<tr style='background-color: #98F9F0'>";
                                echo "<td class='text-right' colspan='9'><b>Total</b></td>";
                                echo "<td class='numeric text-right'><b>$pgrtotqty</b></td>";
                                echo "<td class='numeric text-right'><b>$pgrsubtot</b></td>";
                                echo "<td class='numeric text-right'><b>$pgrtotdis</b></td>";
                                echo "<td class='numeric text-right'><b>$pgrtotvat</b></td>";
                                echo "<td class='numeric text-right'><b>$pgrtotgst</b></td>";
                                echo "<td class='numeric text-right'><b>$pgrtotbillamt</b></td>";
                                echo "<td class='numeric text-right'><b></b></td>";
                                echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>

            <?php
            } else {
                browse("No Records Found");
                echo '<script type="text/javascript">
        window.close();
      </script>';
            }
            ?>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
<?php
}

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

    if ($getno2 > 0) {

        $contents .= "Sale Register\n";

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

        $contents .= "Sr., Voucher, Date, Invoice No., Party Name, GST. No., LR No., LR Date, Transporter, Area, Total Qty., Sub Total, Less Item Discount, Taxable Value, Total GST, Total Amount, Type, Remarks\n";


        $pctr = 0;
        $ptotamt = 0;
        $ptotdiscamt = 0;
        $ptottaxable = 0;
        $ptotgst3 = 0;
        $ptotgst5 = 0;
        $ptotgst12 = 0;
        $ptotgst18 = 0;
        $ptotgst28 = 0;
        $ptotpackamt = $ptotstamt = $ptotothamt = $ptotbillamt = 0;
        $pchtypecond = "";

        //                                $getpmain_p = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `rawmain` WHERE `vouchdt` >= '$mfrmdate' 
        //                                        AND `vouchdt` <= '$mtodate' $suppcond $godowncond $gstcond $pcond order by vouchdt,vouchno")or die(mysqli_error($conn));


        while ($pmain = mysqli_fetch_array($getpmain2)) {
            if ($pmain['paidby'] == "C") {
                $ptyped = "CASH";
            } else {
                $ptyped = "CREDIT";
            }

            $voucher = $pmain['vouchno'];
            $invno = $pmain['invno'];
            $totalqty = $pmain['totalqty'];
            $mydt = $pmain['mydt'];

            if ($pmain['canc_flag'] == "C") {
                $partynm = "<b style='color:red'>CANCELLED</b>";
                $color = "red";
                $subtot = $totdisc = $totvat = $billamt = 0;
                $gstno = "";
                $billamt = $subtot = $totvat = $totdisc = $totgst = 0;
            } else if ($pmain['canc_flag'] != "C") {
                $gparty = mysqli_query($conn, "SELECT `accode`,`name`, `shipname`,`gstno` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
                $sparty = mysqli_fetch_array($gparty);

                $transport = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` where `code` = '$pmain[transporter]'") or die(mysqli_error($conn));
                $gettransport = mysqli_fetch_array($transport);

                $partynm = $sparty['name'];
                $shipname = $sparty['shipname'];
                $gstno = $sparty['gstno'];

                $dname = $gettransport['name'];
                $totdisc = $pmain['totdisc'];
                $lrno = $pmain['lrno'];
                $lrdate = $pmain['lrdate'];
                $area = $pmain['area'];
                $totqty = $pmain['totqty'];
                $subtotal = $pmain['subtotal'];
                $litemdisc = $pmain['litemdisc'];
                $tottaxamt = $pmain['tottaxamt'];
                $totalgst = $pmain['totalgst'];
                $netamt = $pmain['netamt'];
                $remark1 = $pmain['remark1'];
                $remark2 = $pmain['remark2'];
            }

            $pctr++;
            $contents .= "$pctr" . ",";
            $contents .= "$voucher" . ",";
            $contents .= "$mydt" . ",";
            $contents .= "$invno" . ",";
            $contents .= "$shipname" . ",";
            $contents .= "$gstno" . ",";
            $contents .= "$lrno" . ",";
            $contents .= "$lrdate" . ",";
            $contents .= "$dname" . ",";
            $contents .= "$area" . ",";
            $contents .= "$totqty" . ",";
            $contents .= "$subtotal" . ",";
            $contents .= "$litemdisc" . ",";
            $contents .= "$tottaxamt" . ",";
            $contents .= "$totalgst" . ",";
            $contents .= "$netamt" . ",";
            $contents .= "$ptyped" . ",";
            $contents .= "$remark1 $remark2" . "\n";

            $pgrtotqty = $pgrtotqty + $totqty;
            $pgrsubtot = $pgrsubtot + $subtotal;
            $pgrtotdis = $pgrtotdis + $litemdisc;
            $pgrtotvat = $pgrtotvat + $tottaxamt;
            $pgrtotgst = $pgrtotgst + $totalgst;
            $pgrtotbillamt = $pgrtotbillamt + $netamt;

            if ($_POST["dtype"] == 2) {
                $contents .= "Sr., Itemname, HSN Code, Qty.,Rate, Amount, Disc%,Dics Amt, Taxable Value, CGST Rate, CGST Amt, SGST Rate, SGST Amt, IGST Rate, IGST Amt, Total\n";

                $ctr2 = 0;
                $pctr2 = 0;
                $pcartqty = $pqty = $ptotqty = $psubtot = $ptotamount = $pcgstamt = $psgstamt = $pigstamt = $ptotdiscount = $ptotvat = $ptotal = 0;


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

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

                    $ctr2++;

                    $contents .= "$ctr2" . ",";
                    $contents .= "$showitem[name]" . ",";
                    $contents .= "$tran[hsncode]" . ",";
                    $contents .= "$tran[qty]" . ",";
                    $contents .= "$tran[amount]" . ",";
                    $contents .= "$tran[discperc]" . ",";
                    $contents .= "$tran[discount]" . ",";
                    $contents .= "$tran[subtot]" . ",";
                    $contents .= "$tran[cgstrate]" . ",";
                    $contents .= "$tran[cgstamt]" . ",";
                    $contents .= "$tran[sgstrate]" . ",";
                    $contents .= "$tran[sgstamt]" . ",";
                    $contents .= "$tran[igstrate]" . ",";
                    $contents .= "$tran[igstamt]" . ",";
                    $contents .= "$tran[total]" . "\n";

                    $pqty = $pqty + $tran['qty'];
                    //                                            $prate        = $prate + $tran['rate'];
                    $ptotamount = $ptotamount + $tran['amount'];
                    //                                            $pdiscperc    = $pdiscperc + $tran['discperc'];
                    $pdiscount = $pdiscount + $tran['discount'];
                    $psubtot = $psubtot + $tran['subtot'];
                    //                                            $pcgstrate    = $pcgstrate + $tran['cgstrate'];
                    $pcgstamt = $pcgstamt + $tran['cgstamt'];
                    //                                            $psgstrate    = $psgstrate + $tran['sgstrate'];
                    $psgstamt = $psgstamt + $tran['sgstamt'];
                    //                                            $pigstrate    = $pigstrate + $tran['igstrate'];
                    $pigstamt = $pigstamt + $tran['igstamt'];
                    $ptotal = $ptotal + $tran['total'];
                }

                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "Total" . ",";
                $contents .= "$pqty" . ",";
                $contents .= "$prate" . ",";
                $contents .= "$ptotamount" . ",";
                $contents .= "$pdiscperc" . ",";
                $contents .= "$pdiscount" . ",";
                $contents .= "$psubtot" . ",";
                $contents .= "$pcgstrate" . ",";
                $contents .= "$pcgstamt" . ",";
                $contents .= "$psgstrate" . ",";
                $contents .= "$psgstamt" . ",";
                $contents .= "$pigstrate" . ",";
                $contents .= "$pigstamt" . ",";
                $contents .= "$ptotal" . "\n";
            }
        }

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= "$pgrtotqty" . ",";
        $contents .= "$pgrsubtot" . ",";
        $contents .= "$pgrtotdis" . ",";
        $contents .= "$pgrtotvat" . ",";
        $contents .= "$pgrtotgst" . ",";
        $contents .= "$pgrtotbillamt" . ",";
        $contents .= ",";
        $contents .= "," . "\n";
    }

    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
        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;
    }
}
?>