<?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"];

$file = 'purexpreg_fr.php';

$supplier = $_POST["supplier"];
$p = $_GET["p"];
$godown = $_POST["godown"];
$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 `paidby`= '$ptype'";
}

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

$formname = 'Purchase Expense 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; saleitemgrp.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.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 = "";
}

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");





            $getpmain2 = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `purexpmain` 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 ($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>Purchase Expense 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="4%;">Voucher</th>
                                    <th width="6%;">Invoice No.</th>
                                    <th width="6%;">Date</th>
                                    <!--<th width="5%;">Godown</th>-->
                                    <th width="19%;">Party Name</th>                                
                                    <th width="8%;">GST NO.</th>                                
                                    <?php if ($ptype != "") { ?>
                                        <th width="7%;">Pay type</th>
                                    <?php } ?>
                                    <th width="6%;" class="text-right">Sub Total</th>
                                    <th width="5%;" class="text-right">Disc Amt.</th>
                                    <th width="5%;" class="text-right">Taxable</th>
                                    <th width="5%;" class="text-right">GST 3%</th>
                                    <th width="5%;" class="text-right">GST 5%</th>
                                    <th width="5%;" class="text-right">GST 12%</th>
                                    <th width="5%;" class="text-right">GST 18%</th>
                                    <th width="5%;" class="text-right">GST 28%</th>
                                    <th width="7%;" class="text-right">Net Amount</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)) {
                                    if ($pmain['paidby'] == "C") {
                                        $ptyped = "CASH";
                                    } else {
                                        $ptyped = "CREDIT";
                                    }


                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name`, gstno FROM `accmast` where `accode` = '$pmain[party]'") 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);

                                    $mown = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` WHERE accode = '$pmain[debitac]' ORDER BY name") or die(mysqli_error($conn));
                                    $trow = mysqli_fetch_array($mown);

                                    $voucher = $pmain['vouchno'];
                                    $invno = $pmain['invno'];
                                    $mydt = $pmain['mydt'];
                                    $gcode = $sgodw['gcode'];
                                    $party = $sparty['name'];
                                    $gstno = $sparty['gstno'];
                                    $gst3 = $pmain['totgst3'];
                                    $gst5 = $pmain['totgst5'];
                                    $gst12 = $pmain['totgst12'];
                                    $gst18 = $pmain['totgst18'];
                                    $gst28 = $pmain['totgst28'];
                                    $billamt = $pmain['billamt'];
                                    $subtot = $pmain['grossamt'];
                                    $totdisc = $pmain['discamt'];
                                    $taxable = $pmain['taxableamt'];

                                    $pctr++;
                                    Echo"<tr data-href='purchase_exp.php?mode=edit&vouchno=$voucher' class='clickable-row2' [target]='_blank'>";
                                    Echo"<td>$pctr</td>";
                                    Echo"<td>$voucher</td>";
                                    Echo"<td>$invno</td>";
                                    Echo"<td>$mydt</td>";
//                                    Echo"<td>$gcode</td>";
                                    Echo"<td>$party</td>";
                                    Echo"<td>$gstno</td>";
                                    if ($ptype != "") {
                                        Echo"<td>$ptyped</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'>$gst3</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>";



                                    $pgrsubtot = $pgrsubtot + $subtot;
                                    $pgrtotdis = $pgrtotdis + $totdisc;
                                    $pgrtottaxable = $pgrtottaxable + $taxable;
                                    $pgrtotgst3 = $pgrtotgst3 + $gst3;
                                    $pgrtotgst5 = $pgrtotgst5 + $gst5;
                                    $pgrtotgst12 = $pgrtotgst12 + $gst12;
                                    $pgrtotgst18 = $pgrtotgst18 + $gst18;
                                    $pgrtotgst28 = $pgrtotgst28 + $gst28;
                                    $pgrtotbillamt = $pgrtotbillamt + $billamt;

                                    $ptotpackamt = $ptotpackamt + $pmain['packamt'];
                                    $ptotstamt = $ptotstamt + $pmain['stamt'];
                                    $ptotothamt = $ptotothamt + $pmain['otheramt'];
                                    $ptotbillamt = $ptotbillamt + $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='' style='background-color:#BABFC2'>";
                                        Echo "<th style='width:5%;'>Sr</th>";
                                        Echo "<th style='width:15%;' class=''>Expense</th>";
                                        Echo "<th style='width:7%;' class=''>HSN Code</th>";
//                                        Echo "<th style='width:5%;' class=''>Unit</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:4%;' class='text-right'>Disc%</th>";
                                        Echo "<th style='width:8%;' class='text-right'>Disc. Amt.</th>";
                                        Echo "<th style='width:8%;' class='text-right'>Taxable Amount</th>";
                                        if ($gsttype == 1) {
                                            Echo "<th style='width:5%;' class='text-right'>IGST Rate</th>";
                                            Echo "<th style='width:5%;' class='text-right'>IGST Amount</th>";
                                        } elseif ($gsttype == 2) {
                                            Echo "<th style='width:5%;' class='text-right'>CGST Rate</th>";
                                            Echo "<th style='width:5%;' class='text-right'>CGST Amount</th>";
                                            Echo "<th style='width:5%;' class='text-right'>SGST Rate</th>";
                                            Echo "<th style='width:5%;' class='text-right'>SGST Amount</th>";
                                        } elseif ($gsttype == '') {

                                            Echo "<th style='width:5%;' class='text-right'>CGST Rate</th>";
                                            Echo "<th style='width:5%;' class='text-right'>CGST Amount</th>";
                                            Echo "<th style='width:5%;' class='text-right'>SGST Rate</th>";
                                            Echo "<th style='width:5%;' class='text-right'>SGST Amount</th>";
                                            Echo "<th style='width:5%;' class='text-right'>IGST Rate</th>";
                                            Echo "<th style='width:5%;' class='text-right'>IGST Amount</th>";
                                        }
                                        Echo "<th style='width:5%;' class='text-right'>Total</th>";
                                        Echo "</tr>";
                                        Echo "<thead>";
                                        Echo "<tr>";
//
//
                                        $pctr2 = 0;
                                        $ptotqty = 0;
                                        $ptotrate = 0;
                                        $ptotamount = $ptotdiscount = $ptotsubtot = $ptotcgstamt = $ptotsgstamt = $ptotigstamt = 0;

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

                                            $getitem = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$tran[expense]'") 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>$tran[unit]</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[discamt]</td>";
                                            Echo "<td class='text-right'>$tran[subtot]</td>";
                                            if ($gsttype == 1) {
                                                Echo "<td class='text-right'>$tran[igstrate]</td>";
                                                Echo "<td class='text-right'>$tran[igstamt]</td>";
                                            } elseif ($gsttype == 2) {
                                                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>";
                                            } elseif ($gsttype == '') {

                                                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[amount]</td>";
                                            Echo "</tr>";

                                            $ptotqty = $ptotqty + $tran['qty'];
                                            $ptotamount = $ptotamount + $tran['amount'];
                                            $ptotdiscount = $ptotdiscount + $tran['discamt'];
                                            $ptotsubtot = $ptotsubtot + $tran['subtot'];
                                            $ptotcgstamt = $ptotcgstamt + $tran['cgstamt'];
                                            $ptotsgstamt = $ptotsgstamt + $tran['sgstamt'];
                                            $ptotigstamt = $ptotigstamt + $tran['igstamt'];
                                        }

                                        Echo "<tr style='background-color:#d8e0e6'>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
//                                        Echo "<td></td>";
                                        Echo "<td>Total</td>";
//                                        Echo "<td class='text-right'>$ptotqty</td>";
//                                        Echo "<td></td>";
                                        Echo "<td class='text-right'>$ptotamount</td>";
                                        Echo "<td></td>";
                                        Echo "<td class='text-right'>$ptotdiscount</td>";
                                        Echo "<td class='text-right'>$ptotsubtot</td>";
                                        if ($gsttype == 1) {
                                            Echo "<td></td>";
                                            Echo "<td class='text-right'>$ptotigstamt</td>";
                                        } elseif ($gsttype == 2) {
                                            Echo "<td></td>";
                                            Echo "<td class='text-right'>$ptotcgstamt</td>";
                                            Echo "<td></td>";
                                            Echo "<td class='text-right'>$ptotsgstamt</td>";
                                        } elseif ($gsttype == '') {

                                            Echo "<td></td>";
                                            Echo "<td class='text-right'>$ptotcgstamt</td>";
                                            Echo "<td></td>";
                                            Echo "<td class='text-right'>$ptotsgstamt</td>";
                                            Echo "<td></td>";
                                            Echo "<td class='text-right'>$ptotigstamt</td>";
                                        }
                                        Echo "<td class='text-right'>$ptotamount</td>";
                                        Echo "</tr>";
                                        Echo "</table>";
                                    }
                                }
                                Echo "<tr style='background-color: #98F9F0'>";
                                Echo "<td></td>";
                                Echo "<td></td>";
                                Echo "<td></td>";
                                Echo "<td></td>";
//                                Echo "<td></td>";
                                Echo "<td></td>";
                                if ($ptype != "") {
                                    Echo "<td></td>";
                                }
                                Echo "<td class='text-right'><b>Total</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>$pgrtottaxable</b></td>";
                                Echo "<td class='numeric text-right'><b>$pgrtotgst3</b></td>";
                                Echo "<td class='numeric text-right'><b>$pgrtotgst5</b></td>";
                                Echo "<td class='numeric text-right'><b>$pgrtotgst12</b></td>";
                                Echo "<td class='numeric text-right'><b>$pgrtotgst18</b></td>";
                                Echo "<td class='numeric text-right'><b>$pgrtotgst28</b></td>";
                                Echo "<td class='numeric text-right'><b>$pgrtotbillamt</b></td>";
                                Echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div> 
                </div>

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

    $getpmain2 = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `purexpmain` 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 ($getno2 > 0) {
        $contents .= ",,,,Purchase Expense Register\n";
        $contents .= ",,,,Total No. of Records : $getno2\n";
        if ($ptype != "") {
            $contents .= "Pay Type : $paytype\n";
            $pt = "Pay Type,";
        } else {
            $pt = "";
        }

        $contents .= "Sr, Voucher, Invoice No., Date, Party Name, GST NO., Sub Total, Disc Amt., Total Taxable, Taxable 0%, " ;
        $contents .= "Taxable 3%, CGST 1.5%, SGST 1.5%, Taxable 5%, CGST 2.5%, SGST 2.5%, Taxable 12%, CGST 6%, SGST 6%, ";
        $contents .= "Taxable 18%, CGST 9%, SGST 9%, Taxable 28%, CGST 14%, SGST 14%, ";
        $contents .= "Taxable IGST 0%, Taxable IGST 3%, IGST 3%, Taxable IGST 5%, IGST 5%, Taxable IGST 12%, IGST 12%, " ;
        $contents .= "Taxable IGST 18%, IGST 18%, Taxable IGST 28%, IGST 28%, Bill Amt.\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 = "";
        
        
        $pgritaxable0 = $pgritaxable3 = $pgritaxable5 = $pgritaxable12 = $pgritaxable18 = $pgritaxable28 = 0;
        $pgrigst3  = $pgrigst5  = $pgrigst12 = $pgrigst18 = $pgrigst28 = 0;
        
        $ptyped = "";
        while ($pmain = mysqli_fetch_array($getpmain2)) {
            if ($ptype != "") {
                if ($pmain['paidby'] == "C") {
                    $ptyped = "CASH,";
                } else {
                    $ptyped = "CREDIT,";
                }
            }

            $gparty = mysqli_query($conn, "SELECT `accode`,`name`, gstno FROM `accmast` where `accode` = '$pmain[party]'") 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);

            $mown = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` WHERE accode = '$pmain[debitac]' ORDER BY name") or die(mysqli_error($conn));
            $trow = mysqli_fetch_array($mown);

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

            $gcode = $sgodw['gcode'];
            $party = $sparty['name'];
            $gstno = $sparty['gstno'];

            $billamt = $pmain['billamt'];
            $subtot = $pmain['grossamt'];
            $totdisc = $pmain['discamt'];
            
            $taxable = $pmain['taxableamt'];

            
            $gst3  = $gst5  = $gst12 = $gst18 = $gst28 = 0 ;
            $igst3  = $igst5  = $igst12 = $igst18 = $igst28 = 0 ;

            $taxable0 = $taxable3 = $taxable5 = $taxable12 = $taxable18 = $taxable28 = 0;
            $itaxable0 = $itaxable3 = $itaxable5 = $itaxable12 = $itaxable18 = $itaxable28 = 0;

            if ($oms == 1) {
                $itaxable0 = $pmain['tottaxble0'];
                $itaxable3 = $pmain['tottaxble3'];
                $itaxable5 = $pmain['tottaxble5'];
                $itaxable12 = $pmain['tottaxble12'];
                $itaxable18 = $pmain['tottaxble18'];
                $itaxable28 = $pmain['tottaxble28'];

                $igst3 = $pmain['totgst3'];
                $igst5 = $pmain['totgst5'];
                $igst12 = $pmain['totgst12'];
                $igst18 = $pmain['totgst18'];
                $igst28 = $pmain['totgst28'];
            } else {
                $taxable0 = $pmain['tottaxble0'];
                $taxable3 = $pmain['tottaxble3'];
                $taxable5 = $pmain['tottaxble5'];
                $taxable12 = $pmain['tottaxble12'];
                $taxable18 = $pmain['tottaxble18'];
                $taxable28 = $pmain['tottaxble28'];

                $gst3  = $pmain['totgst3'] / 2 ;
                $gst5  = $pmain['totgst5'] / 2 ;
                $gst12 = $pmain['totgst12'] / 2 ;
                $gst18 = $pmain['totgst18'] / 2 ;
                $gst28 = $pmain['totgst28'] / 2 ;
            }

            
            $pctr++;
            $contents .= "$pctr" . ",";
            $contents .= "$voucher" . ",";
            $contents .= "$invno" . ",";
            $contents .= "$mydt" . ",";
            $contents .= "$party" . ",";
            $contents .= "$gstno" . ",";
            $contents .= "$subtot" . ",";
            $contents .= "$totdisc" . ",";
            $contents .= "$taxable" . ",";

            $contents .= "$taxable0" . ",";

            $contents .= "$taxable3" . ",";
            $contents .= "$gst3" . ",";
            $contents .= "$gst3" . ",";

            $contents .= "$taxable5" . ",";
            $contents .= "$gst5" . ",";
            $contents .= "$gst5" . ",";

            $contents .= "$taxable12" . ",";
            $contents .= "$gst12" . ",";
            $contents .= "$gst12" . ",";

            $contents .= "$taxable18" . ",";
            $contents .= "$gst18" . ",";
            $contents .= "$gst18" . ",";

            $contents .= "$taxable28" . ",";
            $contents .= "$gst28" . ",";
            $contents .= "$gst28" . ",";
            
//////////////  next igst part
            
            $contents .= "$itaxable0" . ",";

            $contents .= "$itaxable3" . ",";
            $contents .= "$igst3" . ",";

            $contents .= "$itaxable5" . ",";
            $contents .= "$igst5" . ",";
            
            $contents .= "$itaxable12" . ",";
            $contents .= "$igst12" . ",";

            $contents .= "$itaxable18" . ",";
            $contents .= "$igst18" . ",";

            $contents .= "$itaxable28" . ",";
            $contents .= "$igst28" . ",";
            
            $contents .= "$billamt" . "\n";

            $pgrsubtot = $pgrsubtot + $subtot;
            $pgrtotdis = $pgrtotdis + $totdisc;

            $pgrtottaxable = $pgrtottaxable + $taxable;
            $pgrtottaxable0 = $pgrtottaxable0 + $taxable0;
            $pgrtottaxable3 = $pgrtottaxable3 + $taxable3;
            $pgrtottaxable5 = $pgrtottaxable5 + $taxable5;
            $pgrtottaxable12 = $pgrtottaxable12 + $taxable12;
            $pgrtottaxable18 = $pgrtottaxable18 + $taxable18;
            $pgrtottaxable28 = $pgrtottaxable28 + $taxable28;

            $pgrtotgst3 = $pgrtotgst3 + $gst3;
            $pgrtotgst5 = $pgrtotgst5 + $gst5;
            $pgrtotgst12 = $pgrtotgst12 + $gst12;
            $pgrtotgst18 = $pgrtotgst18 + $gst18;
            $pgrtotgst28 = $pgrtotgst28 + $gst28;

            $pgritaxable0 = $pgritaxable0 + $itaxable0;
            $pgritaxable3 = $pgritaxable3 + $itaxable3;
            $pgritaxable5 = $pgritaxable5 + $itaxable5;
            $pgritaxable12 = $pgritaxable12 + $itaxable12;
            $pgritaxable18 = $pgritaxable18 + $itaxable18;
            $pgritaxable28 = $pgritaxable28 + $itaxable28;

            $pgrigst3  = $pgrigst3  + $igst3;
            $pgrigst5  = $pgrigst5  + $igst5;
            $pgrigst12 = $pgrigst12 + $igst12;
            $pgrigst18 = $pgrigst18 + $igst18;
            $pgrigst28 = $pgrigst28 + $igst28;

            $pgrtotbillamt = $pgrtotbillamt + $billamt;

            $ptotpackamt = $ptotpackamt + $pmain['packamt'];
            $ptotstamt = $ptotstamt + $pmain['stamt'];
            $ptotothamt = $ptotothamt + $pmain['otheramt'];
            $ptotbillamt = $ptotbillamt + $pmain['billamt'];

            
        }
            
        $contents .= ",,,,,Total, $pgrsubtot, $pgrtotdis, $pgrtottaxable, $pgrtottaxable0, $pgrtottaxable3, $pgrtotgst3, $pgrtotgst3, ";
        $contents .= "$pgrtottaxable5, $pgrtotgst5, $pgrtotgst5, $pgrtottaxable12, $pgrtotgst12, $pgrtotgst12, $pgrtottaxable18, $pgrtotgst18, $pgrtotgst18, " ;
        $contents .= "$pgrtottaxable28, $pgrtotgst28, $pgrtotgst28, " ;
        $contents .= "$pgritaxable0, $pgritaxable3, $pgrigst3, $pgritaxable5, $pgrigst5, $pgritaxable12, $pgrigst12, $pgritaxable18, $pgrigst18, " ;
        $contents .= "$pgritaxable28, $pgrigst28, $pgrtotbillamt\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;
    }
}
?>