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

$yearno = substr($myyear, 0, 2);
$yearno = substr($myyear, -2);

$maindb = 'salemain';
$trandb = 'saletran';
$zroot = $_SESSION["zdbroot"];

$formname = 'Monthly Sale Summary Report';
$file = 'salesumm_fr.php';



//browse('work in progress... ');

$party = $_POST["party"];
$month = $_POST["mono"];
$tomono = $_POST["tomono"];
$scope = $_POST["scope"];
$yearno = $_POST["yearno"];

$partycond = '';
$monofrom = '';
$monoto = '';
$scopecond = '';

if ($scope == 'A') {
    
} else {
    $scopecond = " and b.type = '$scope' ";
}
$typecond = $scopecond;
//browse($typecond);


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

switch ($month) {
    case '1':
        $month = "January";
        $fdt = "$yearno-01-01";
        break;

    case "2":
        $month = "February";
        $fdt = "$yearno-02-01";
        break;

    case "3":
        $month = "March";
        $fdt = "$yearno-03-01";
        break;

    case "4":
        $month = "April";
        $fdt = "$yearno-04-01";
        break;

    case "5":
        $month = "May";
        $fdt = "$yearno-05-01";
        break;

    case "6":
        $month = "June";
        $fdt = "$yearno-06-01";
        break;

    case "7":
        $month = "July";
        $fdt = "$yearno-07-01";
        break;

    case "8":
        $month = "August";
        $fdt = "$yearno-08-01";
        break;

    case "9":
        $month = "September";
        $fdt = "$yearno-09-01";
        break;

    case "10":
        $month = "October";
        $fdt = "$yearno-10-01";
        break;

    case "11":
        $month = "November";
        $fdt = "$yearno-11-01";
        break;

    case "12":
        $month = "December";
        $fdt = "$yearno-12-01";
        break;
}

if (!empty($month)) {
    $monofrom = " AND vouchdt >= '$fdt'  ";
    $monofrom1 = " AND date >= '$fdt'  ";
    $moexpfrom = " AND invdate >= '$fdt'  ";
}

switch ($tomono) {

    case '1':
        $month = "January";
        $tdt = "$yearno-01-31";
        break;

    case "2":
        $month = "February";
        $tdt = "$yearno-02-29";
        break;

    case "3":
        $month = "March";
        $tdt = "$yearno-03-31";
        break;

    case "4":
        $month = "April";
        $tdt = "$yearno-04-30";
        break;

    case "5":
        $month = "May";
        $tdt = "$yearno-05-31";
        break;

    case "6":
        $month = "June";
        $tdt = "$yearno-06-30";
        break;

    case "7":
        $month = "July";
        $tdt = "$yearno-07-31";
        break;

    case "8":
        $month = "August";
        $tdt = "$yearno-08-31";
        break;

    case "9":
        $month = "September";
        $tdt = "$yearno-09-30";
        break;

    case "10":
        $month = "October";
        $tdt = "$yearno-10-31";
        break;

    case "11":
        $month = "November";
        $tdt = "$yearno-11-30";
        break;

    case "12":
        $month = "December";
        $tdt = "$yearno-12-31";
        break;
}

if (!empty($tomono)) {
    $monoto = " AND vouchdt <= '$tdt' ";
    $monoto1 = " AND date <= '$tdt' ";
    $moexpto = " AND invdate <= '$tdt' ";
}

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

if (!empty($party)) {
    $rtype2 = 'Party : ' . $partyname;
} else {
    $rtype2 = 'Party : All ';
}

//browse($monofrom) ;
//browse($monoto) ;

mysqli_query($conn, "drop temporary table if exists localdbf ")or die(mysqli_error($conn));
mysqli_query($conn, "Create temporary table localdbf 
                            SELECT month(vouchdt) as monno, SUM(consultfees) as consultfees, sum(ttamount) as ttamount,
                            sum(tmedamount) as tmedamount FROM `consultmain` WHERE accode > 0
                            $monofrom $monoto 
                            group by month(vouchdt) order by vouchdt ") or die(mysqli_error($conn));

mysqli_query($conn, "drop temporary table if exists receiptdbf ")or die(mysqli_error($conn));
mysqli_query($conn, "Create temporary table receiptdbf 
                            SELECT month(date) as monno, SUM(amount) as tamount,paidfor FROM `actran` WHERE amount > 0
                            $monofrom1 $monoto1 
                            group by month(date), paidfor order by date ") or die(mysqli_error($conn));

//mysqli_query($conn, "drop temporary table if exists retdbf ")or die(mysqli_error($conn));
//mysqli_query($conn, "create temporary table retdbf 
//                            SELECT month(vouchdt) as monno, SUM(billamt) as amount FROM `credmain` WHERE  vouchno != ''
//                            $partycond $monofrom $monoto and vouchno in 
//                            (select a.vouchno from credtran a, product b where a.barcode = b.barcode $typecond ) 
//                            group by month(vouchdt) order by vouchdt ") or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    <!--    <script type="text/javascript">
            $(document).ready(function () {
                $("#data tr").click(function () {
                    $(this).toggleClass("highlight");
                    calCulat();
                });

                function calCulat() {
                    var totamt = 0;
                    $(".myTable").find('tr').each(function () {
                        var row = $(this);
                        //                    if (row.is(":visible")) {
                        if ((row).hasClass('highlight')) {
                            //                        $(rowGet).hasClass('myclass');
                            //                        alert('1');


                            var amount = row.find('.totamt').val();
                            if (!isNaN(amount) && amount != 0 && amount != undefined) {

                                //                        alert(amount);
                                totamt += parseFloat(amount);

                            }
                        } else {
                            amount = 0;
                            totamt += parseFloat(amount);
                        }
                        $(".ttotamt").text(totamt.toFixed(2));

                    });
                    //                alert(totamt);

                }
            });
        </script>-->
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }

        .highlight { background-color: yellowgreen; }

        .highlight{
            /*background-color: #EFE4B0;*/
            -webkit-print-color-adjust : exact !important;
        }

    </style>
    </head>
    <body>
        <div class="container" style="width:100%">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12">

                    <table style="background: white;width:100%" class="table table-condensed table-bordered">
                        <thead class="mySearchTr">
    <!--                            <tr>
                                <th style="width:90%;">Party : <?php $Party ?></th>
                            </tr>-->

                            <tr>
                                <th style="width:10%;">Month</th>
                                <th style="width:10%;">Consulting Sale</th>
                                <th style="width:10%;">Treatment Sale</th>
                                <th style="width:10%;">Medical Sale</th>
                                <th style="width:10%;" class="text-right">Total</th>
                                <th style="width:10%;">Consulting Receipt</th>
                                <th style="width:10%;">Treatment Receipt</th>
                                <th style="width:10%;">Medical Receipt</th>
                                <th style="width:10%;">Other Receipt</th>
                                <th style="width:10%;" class="text-right">Total Collection</th>
                                <!--<th style="width:10%;" class="text-right"></th>-->
                            </tr>
                        </thead>
                        <tbody id="data" class="myTable">
                            <?php
                            $ctr = 0;
                            $totconsult = $ttotamt = $tmedamt = 0;

                            for ($f = 1; $f <= 12; $f++) {
                                switch ($f) {

                                    case '1':
                                        $month = "January";
                                        $fdt = "01-01-$yearno";
                                        $tdt = "31-01-$yearno";
                                        break;

                                    case "2":
                                        $month = "February";
                                        $fdt = "01-02-$yearno";
                                        $tdt = "29-02-$yearno";
                                        break;

                                    case "3":
                                        $month = "March";
                                        $fdt = "01-03-$yearno";
                                        $tdt = "31-03-$yearno";
                                        break;

                                    case "4":
                                        $month = "April";
                                        $fdt = "01-04-$yearno";
                                        $tdt = "30-04-$yearno";
                                        break;

                                    case "5":
                                        $month = "May";
                                        $fdt = "01-05-$yearno";
                                        $tdt = "31-05-$yearno";
                                        break;

                                    case "6":
                                        $month = "June";
                                        $fdt = "01-06-$yearno";
                                        $tdt = "30-06-$yearno";
                                        break;

                                    case "7":
                                        $month = "July";
                                        $fdt = "01-07-$yearno";
                                        $tdt = "31-07-$yearno";
                                        break;

                                    case "8":
                                        $month = "August";
                                        $fdt = "01-08-$yearno";
                                        $tdt = "31-08-$yearno";
                                        break;

                                    case "9":
                                        $month = "September";
                                        $fdt = "01-09-$yearno";
                                        $tdt = "30-09-$yearno";
                                        break;

                                    case "10":
                                        $month = "October";
                                        $fdt = "01-10-$yearno";
                                        $tdt = "31-10-$yearno";
                                        break;

                                    case "11":
                                        $month = "November";
                                        $fdt = "01-11-$yearno";
                                        $tdt = "30-11-$yearno";
                                        break;

                                    case "12":
                                        $month = "December";
                                        $fdt = "01-12-$yearno";
                                        $tdt = "31-12-$yearno";
                                        break;
                                }

                                $ctr++;
                                Echo"<tr class='clickable-row2' data-href='$action?view=view&p=$party&fdt=$fdt&tdt=$tdt&rpt=1' [target]='_blank'>";
                                Echo"<td>$month</td>";

                                $localqry = mysqli_query($conn, "SELECT consultfees, ttamount, tmedamount FROM `localdbf` WHERE monno = '$f'  ")or die(mysqli_error($conn));
                                $localfet = mysqli_fetch_array($localqry);
                                $consultfees = $localfet['consultfees'];
                                $ttamount = $localfet['ttamount'];
                                $tmedamt = $localfet['tmedamount'];

                                $conscoll = 0;
                                $treatcoll = 0;
                                $medcoll = 0;

                                $colcons = mysqli_query($conn, "SELECT tamount FROM `receiptdbf` WHERE monno = '$f' and paidfor = 'C' ")or die(mysqli_error($conn));
                                $fcons = mysqli_fetch_array($colcons);
                                $coltreat = mysqli_query($conn, "SELECT tamount FROM `receiptdbf` WHERE monno = '$f' and paidfor = 'T' ")or die(mysqli_error($conn));
                                $ftreat = mysqli_fetch_array($coltreat);
                                $colmed = mysqli_query($conn, "SELECT tamount FROM `receiptdbf` WHERE monno = '$f' and paidfor = 'M' ")or die(mysqli_error($conn));
                                $fmed = mysqli_fetch_array($colmed);
                                $coloth = mysqli_query($conn, "SELECT tamount FROM `receiptdbf` WHERE monno = '$f' and paidfor = 'O' ")or die(mysqli_error($conn));
                                $foth = mysqli_fetch_array($coloth);

                                $conscoll = $fcons['tamount'];
                                $treatcoll = $ftreat['tamount'];
                                $medcoll = $fmed['tamount'];
                                $othcoll = $foth['tamount'];


                                Echo"<td class='text-right'>" . number_format($consultfees) . ""
                                . "<input type='hidden' class='totamt' value='$consultfees'></td>";

                                Echo"<td class='text-right'>" . number_format($ttamount) . ""
                                . "<input type='hidden' class='deemamt' value='$ttamount'></td>";

                                Echo"<td class='text-right'>" . number_format($tmedamt) . ""
                                . "<input type='hidden' class='expoamt' value='$tmedamt'></td>";



                                $totalamt = $consultfees + $ttamount + $tmedamt;

                                Echo"<td class='text-right' style='background-color:#FADFAD;font-size:18px;color:blue'>" . number_format($totalamt) . ""
                                . "<input type='hidden' class='totalamt' value='$totalamt'></td>";

                                Echo"<td class='text-right'>" . number_format($conscoll) . ""
                                . "<input type='hidden' class='retamt' value='$conscoll'></td>";


                                Echo"<td class='text-right'>" . number_format($treatcoll) . ""
                                . "<input type='hidden' class='actamt' value='$treatcoll'></td>";

                                Echo"<td class='text-right'>" . number_format($medcoll) . ""
                                . "<input type='hidden' class='actamt' value='$medcoll'></td>";

                                Echo"<td class='text-right'>" . number_format($othcoll) . ""
                                . "<input type='hidden' class='actamt' value='$othcoll'></td>";


                                $totcoll = $conscoll + $treatcoll + $medcoll + $othcoll;

                                Echo"<td class='text-right' style='background-color:#FADFAD;color:green;font-size:18px'>" . number_format($totcoll) . ""
                                . "<input type='hidden' class='totalamt' value='$totcoll'></td>";
                                Echo"</tr>";

                                $totconsult = $totconsult + $consultfees;
                                $tottreat = $tottreat + $ttamount;
                                $totmed = $totmed + $tmedamt;
                                $totccoll = $totccoll + $conscoll;
                                $tottcoll = $tottcoll + $treatcoll;
                                $totmcoll = $totmcoll + $medcoll;
                                $totmoth = $totmoth + $othcoll;

                                $totsale = $totsale + $totalamt;
                                $gtotcoll = $gtotcoll + $totcoll;
                            }

//                            Echo "<tr class='mySearchTr'>";
//                            Echo "<td  class='text-right' style='color:white'>Selected</td>";
//                            Echo "<td  class='text-right'><b class='ttotamt' style='color:white;font-size:16px'></b></td>";
//                            Echo "<td  class='text-right'><b class='ttotamt' style='color:white;font-size:16px'></b></td>";
//                            Echo "<td  class='text-right'><b class='ttotamt' style='color:white;font-size:16px'></b></td>";
//                            Echo "<td  class='text-right'><b class='ttotamt' style='color:white;font-size:16px'></b></td>";
//                            Echo "<td  class='text-right'><b class='ttotamt' style='color:white;font-size:16px'></b></td>";
//                            Echo "<td  class='text-right'><b class='ttotamt' style='color:white;font-size:16px'></b></td>";
//                            Echo "<td  class='text-right'><b class='ttotamt' style='color:white;font-size:16px'></b></td>";
//                            Echo "<td  class='text-right'><b class='ttotamt' style='color:white;font-size:16px'></b></td>";
//                            Echo "</tr>";

                            Echo "<tr>";
                            Echo "<td class='text-right' style='font-size:15px;'><b><h5>Total</h5></b></td>";
                            Echo "<td class='text-right' style='font-size:15px;'><b><h5>" . number_format($totconsult, 2) . "</h5></b></td>";
                            Echo "<td class='text-right' style='font-size:15px;'><b><h5>" . number_format($tottreat, 2) . "</h5></b></td>";
                            Echo "<td class='text-right' style='font-size:15px;'><b><h5>" . number_format($totmed, 2) . "</h5></b></td>";
                            Echo "<td class='text-right'  style='background-color:#FADFAD;font-size:18px;color:blue'><b>" . number_format($totsale, 2) . "</b></td>";

                            Echo "<td class='text-right' style='font-size:15px;'><b><h5>" . number_format($totccoll, 2) . "</h5></b></td>";
                            Echo "<td class='text-right' style='font-size:15px;'><b><h5>" . number_format($tottcoll, 2) . "</h5></b></td>";
                            Echo "<td class='text-right' style='font-size:15px;'><b><h5>" . number_format($totmcoll, 2) . "</h5></b></td>";
                            Echo "<td class='text-right' style='font-size:15px;'><b><h5>" . number_format($totmoth, 2) . "</h5></b></td>";
                            Echo "<td class='text-right'  style='background-color:#FADFAD;font-size:18px;color:green'><b>" . number_format($gtotcoll, 2) . "</b></td>";
                            Echo "</tr>";

//                            Echo "<tr class='mySearchTr'>";
//                            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></td>";
//                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div> 
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}

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


    $contents .= "Sr, Month, Local Sale, Deemed Sale, Export Sale, Other Sale, Online Sale, Total, Return, Actual Sale\n";
    $ctr = 0;
    $totlocal = $totdeem = 0;
    for ($f = 4; $f <= 12; $f++) {
        switch ($f) {

            case '1':
                $month = "January";
                $fdt = "01-01-$yearno";
                $tdt = "31-01-$yearno";
                break;

            case "2":
                $month = "February";
                $fdt = "01-02-$yearno";
                $tdt = "29-02-$yearno";
                break;

            case "3":
                $month = "March";
                $fdt = "01-03-$yearno";
                $tdt = "31-03-$yearno";
                break;

            case "4":
                $month = "April";
                $fdt = "01-04-$yearno";
                $tdt = "30-04-$yearno";
                break;

            case "5":
                $month = "May";
                $fdt = "01-05-$yearno";
                $tdt = "31-05-$yearno";
                break;

            case "6":
                $month = "June";
                $fdt = "01-06-$yearno";
                $tdt = "30-06-$yearno";
                break;

            case "7":
                $month = "July";
                $fdt = "01-07-$yearno";
                $tdt = "31-07-$yearno";
                break;

            case "8":
                $month = "August";
                $fdt = "01-08-$yearno";
                $tdt = "31-08-$yearno";
                break;

            case "9":
                $month = "September";
                $fdt = "01-09-$yearno";
                $tdt = "30-09-$yearno";
                break;

            case "10":
                $month = "October";
                $fdt = "01-10-$yearno";
                $tdt = "31-10-$yearno";
                break;

            case "11":
                $month = "November";
                $fdt = "01-11-$yearno";
                $tdt = "30-11-$yearno";
                break;

            case "12":
                $month = "December";
                $fdt = "01-12-$yearno";
                $tdt = "31-12-$yearno";
                break;
        }

        $ctr++;

        $localqry = mysqli_query($conn, "SELECT amount FROM `localdbf` WHERE monno = '$f'  ")or die(mysqli_error($conn));
        $localfet = mysqli_fetch_array($localqry);
        $localamt = $localfet['amount'];

        $deemqry = mysqli_query($conn, "SELECT amount FROM `deemdbf` WHERE monno = '$f'  ")or die(mysqli_error($conn));
        $deemfet = mysqli_fetch_array($deemqry);
        $deemamt = $deemfet['amount'];

        $expoqry = mysqli_query($conn, "SELECT amount FROM `expodbf` WHERE monno = '$f'  ")or die(mysqli_error($conn));
        $expofet = mysqli_fetch_array($expoqry);
        $expoamt = $expofet['amount'];

        $othqry = mysqli_query($conn, "SELECT amount FROM `othdbf` WHERE monno = '$f'  ")or die(mysqli_error($conn));
        $othfet = mysqli_fetch_array($othqry);
        $othamt = $othfet['amount'];

        $onlqry = mysqli_query($conn, "SELECT amount FROM `onldbf` WHERE monno = '$f'  ")or die(mysqli_error($conn));
        $onlfet = mysqli_fetch_array($onlqry);
        $onlamt = $onlfet['amount'];

        $retqry = mysqli_query($conn, "SELECT amount FROM `retdbf` WHERE monno = '$f'  ")or die(mysqli_error($conn));
        $retfet = mysqli_fetch_array($retqry);
        $retamt = $retfet['amount'];

        $contents .= "$ctr" . ",";
        $contents .= "$month" . ",";
        $contents .= "$localamt" . ",";
        $contents .= "$deemamt" . ",";
        $contents .= "$expoamt" . ",";
        $contents .= "$othamt" . ",";
        $contents .= "$onlamt" . ",";
        $contents .= "$totalamt" . ",";
        $contents .= "$retamt" . ",";
        $contents .= "$actamt" . "\n";

        $totalamt = $localamt + $deemamt + $expoamt + $othamt + $onlamt;

        $actamt = $totalamt - $retamt;

        $totlocal = $totlocal + $localamt;
        $totdeem = $totdeem + $deemamt;
        $totexpo = $totexpo + $expoamt;
        $tototh = $tototh + $othamt;
        $totonl = $totonl + $onlamt;
        $mtotalamt = $mtotalamt + $totalamt;
        $dretamt = $dretamt + $retamt;
        $totact = $totact + $actamt;
    }
    for ($f = 1; $f <= 3; $f++) {
        switch ($f) {

            case '1':
                $month = "January";
                $fdt = "01-01-$yearno";
                $tdt = "31-01-$yearno";
                break;

            case "2":
                $month = "February";
                $fdt = "01-02-$yearno";
                $tdt = "29-02-$yearno";
                break;

            case "3":
                $month = "March";
                $fdt = "01-03-$yearno";
                $tdt = "31-03-$yearno";
                break;

            case "4":
                $month = "April";
                $fdt = "01-04-$yearno";
                $tdt = "30-04-$yearno";
                break;

            case "5":
                $month = "May";
                $fdt = "01-05-$yearno";
                $tdt = "31-05-$yearno";
                break;

            case "6":
                $month = "June";
                $fdt = "01-06-$yearno";
                $tdt = "30-06-$yearno";
                break;

            case "7":
                $month = "July";
                $fdt = "01-07-$yearno";
                $tdt = "31-07-$yearno";
                break;

            case "8":
                $month = "August";
                $fdt = "01-08-$yearno";
                $tdt = "31-08-$yearno";
                break;

            case "9":
                $month = "September";
                $fdt = "01-09-$yearno";
                $tdt = "30-09-$yearno";
                break;

            case "10":
                $month = "October";
                $fdt = "01-10-$yearno";
                $tdt = "31-10-$yearno";
                break;

            case "11":
                $month = "November";
                $fdt = "01-11-$yearno";
                $tdt = "30-11-$yearno";
                break;

            case "12":
                $month = "December";
                $fdt = "01-12-$yearno";
                $tdt = "31-12-$yearno";
                break;
        }

        $ctr++;

        $localqry = mysqli_query($conn, "SELECT amount FROM `localdbf` WHERE monno = '$f'  ")or die(mysqli_error($conn));
        $localfet = mysqli_fetch_array($localqry);
        $localamt = $localfet['amount'];

        $deemqry = mysqli_query($conn, "SELECT amount FROM `deemdbf` WHERE monno = '$f'  ")or die(mysqli_error($conn));
        $deemfet = mysqli_fetch_array($deemqry);
        $deemamt = $deemfet['amount'];

        $expoqry = mysqli_query($conn, "SELECT amount FROM `expodbf` WHERE monno = '$f'  ")or die(mysqli_error($conn));
        $expofet = mysqli_fetch_array($expoqry);
        $expoamt = $expofet['amount'];

        $othqry = mysqli_query($conn, "SELECT amount FROM `othdbf` WHERE monno = '$f'  ")or die(mysqli_error($conn));
        $othfet = mysqli_fetch_array($othqry);
        $othamt = $othfet['amount'];

        $onlqry = mysqli_query($conn, "SELECT amount FROM `onldbf` WHERE monno = '$f'  ")or die(mysqli_error($conn));
        $onlfet = mysqli_fetch_array($onlqry);
        $onlamt = $onlfet['amount'];

        $retqry = mysqli_query($conn, "SELECT amount FROM `retdbf` WHERE monno = '$f'  ")or die(mysqli_error($conn));
        $retfet = mysqli_fetch_array($retqry);
        $retamt = $retfet['amount'];

        $contents .= "$ctr" . ",";
        $contents .= "$month" . ",";
        $contents .= "$localamt" . ",";
        $contents .= "$deemamt" . ",";
        $contents .= "$expoamt" . ",";
        $contents .= "$othamt" . ",";
        $contents .= "$onlamt" . ",";
        $contents .= "$totalamt" . ",";
        $contents .= "$retamt" . ",";
        $contents .= "$actamt" . "\n";

        $totalamt = $localamt + $deemamt + $expoamt + $othamt + $onlamt;

        $actamt = $totalamt - $retamt;

        $totlocal = $totlocal + $localamt;
        $totdeem = $totdeem + $deemamt;
        $totexpo = $totexpo + $expoamt;
        $tototh = $tototh + $othamt;
        $totonl = $totonl + $onlamt;
        $mtotalamt = $mtotalamt + $totalamt;
        $dretamt = $dretamt + $retamt;
        $totact = $totact + $actamt;
    }

    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$totlocal" . ",";
    $contents .= "$totdeem" . ",";
    $contents .= "$totexpo" . ",";
    $contents .= "$tototh" . ",";
    $contents .= "$totonl" . ",";
    $contents .= "$mtotalamt" . ",";
    $contents .= "$dretamt" . ",";
    $contents .= "$totact" . "\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;
    }
}
?>