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

ini_set('display_errors', 1);
error_reporting(E_ERROR);

$zdb = $_SESSION["zdbyear_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];

//browse('work in progress...');
//
ini_set('display_errors', 1);
error_reporting(E_ERROR);
//$zroot = 'cepheisy_msp_root';
$comp = $_SESSION["comp_" . $dbname];

$trandb = "actran";
$myear = $_SESSION["tit_" . $dbname];
mysqli_select_db($conn, $zdb);
// browse($zdb);

if (isset($_POST["view"])) {
    $acond = "";
    $accond = "";
    $scond = "";
    if ($comp > 100) {
        $mpart = "party";
    } else {
        $mpart = "broker";
    }
    // browse($mpart);
    if (!empty($_POST["acname"])) {
        $accode = trim($_POST["acname"]);
        $accond = " AND `party` = '$accode' ";
        $acond  = " AND a.`$mpart`='$accode'";
        $acondp = " AND a.`party`='$accode'";
        $acond1 = " AND a.`accode`='$accode'";
    }
    // browse($accode);
    //    if (!empty($_POST["cmbstation"])) {
    //        $station = trim($_POST["cmbstation"]);
    //        $scond = "AND a.`station`='$station'";
    //    }
    // $invdtcond = "";
    // $invdtcond2 = "";

    $mtodate = $_POST["frmdt"];
    $prntype = $_POST["prntype"];

    if ($_POST["hiddenval"] == "R") {
        $mfile = "rawmain";
        $mfile2 = "gstmain";
        $mtype = "S";
        $mdate = 'invdt';
        $blsrc1 = 'SLM';
        $blsrc2 = 'SLFP';
        $fintot = "netamt";
        $grid = "osbill.php?para=R";
        $formname1 = "Outstanding Receivable";
        $agency = $_POST["agency"];
        $agenqry = mysqli_query($conn, "SELECT name from accmast WHERE `accode` = '$agency' ") or die(mysqli_error($conn));
        $agenfet = mysqli_fetch_array($agenqry);
        $agenname = $agenfet['name'];

        $frminvdt = $_POST["frminvdt"];
        $toinvdt = $_POST["toinvdt"];

        $checks = (isset($_POST['check'])) ? $_POST['check'] : array();

        $custcond = '';
        $custcond1 = '';

        if (count($checks) > 0) {
            foreach ($checks as $check) {
                //        echo $check . ' ';
                $mycheck2[$count2++] .= "OR a.`$mpart` = '" . $check . "' ";
                $mycheck3[$count2++] .= "OR a.`accode` = '" . $check . "' ";
            }
            foreach ($mycheck2 as $g => $values2) {
                $mcheck2 .= $values2;
            }
            foreach ($mycheck3 as $g => $values3) {
                $mcheck3 .= $values3;
            }

            $custcond = " AND (a.`$mpart` = '' " . $mcheck2 . " )";
            $custcond1 = " AND (a.`accode` = '' " . $mcheck3 . " )";
        }

        if ($frminvdt != "" && $toinvdt != "") {
            if ($frminvdt == "0000-00-00" && $toinvdt == "0000-00-00") {
                $invdtcond = "";
                $invdtcondb = "";
                $invdtcondv = "";
                $invdtcondd = "";
            } else {
                $invdtcond = "AND $mdate >= '$frminvdt' AND $mdate <= '$toinvdt' ";
                $invdtcondb = "AND billdate >= '$frminvdt' AND billdate <= '$toinvdt'";
                $invdtcondv = "AND vouchdt >= '$frminvdt' AND vouchdt <= '$toinvdt'";
                $invdtcondd = "AND `date` >= '$frminvdt' AND `date` <= '$toinvdt'";
            }
        }
    } else {
        $mfile = 'purrawmain';
        $mfile2 = "pgstmain";
        $blsrc1 = 'PUM';
        $blsrc2 = 'PUFP';
        $mtype = "P";
        $mdate = 'vouchdt';
        $fintot = "netamt";
        $grid = "osbill.php?para=P";
        $formname1 = "Outstanding Payable";
        $agency = '';
        $agenname = '';

        $frminvdt = $_POST["frminvdt"];
        $toinvdt = $_POST["toinvdt"];

        if ($frminvdt != "" && $toinvdt != "") {
            if ($frminvdt == "0000-00-00" && $toinvdt == "0000-00-00") {
                $invdtcond = "";
                $invdtcondb = "";
                $invdtcondv = "";
                $invdtcondd = "";
            } else {
                $invdtcond = "AND $mdate >= '$frminvdt' AND $mdate <= '$toinvdt' ";
                $invdtcondb = "AND billdate >= '$frminvdt' AND billdate <= '$toinvdt'";
                $invdtcondv = "AND vouchdt >= '$frminvdt' AND vouchdt <= '$toinvdt'";
                $invdtcondd = "AND `date` >= '$frminvdt' AND `date` <= '$toinvdt'";
            }
        }
    }

    if ($_POST["dtype"] == 1) {
        $formname = "$formname1 - Summary";
        $dtype = 1;
    } else {
        $formname = "$formname1 - Detail";
        $dtype = 2;
    }

    include("includes/linkshead.php");

    $mtdt = date("d-m-Y", strtotime($mtodate));
?>
    <!--<link href="MyStyle/rep_format.css" rel="stylesheet">-->

    <style>
        .report {
            margin-top: 0px;
        }

        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

        /* .report tr td {
                padding-right: 10px;
                font-size: 18px;
            } */
        #headrepeat {
            display: none;
        }

        @media print {
            #headrepeat {
                display: block;
            }

            /* body {
                margin: 0;
                margin-top: 0;
            } */


        }
    </style>
    </head>

    <body>
        <div class="row" style="margin-top: 15px;">
            <div class="col-lg-6" style="width:100%;padding-left: 7%;padding-right: 4%">
                <div class="box">
                    <?php // include("includes/head_rep.php");   
                    ?>
                    <div id="sortableTable" class="body collapse in">
                        <table style="width:100%;" id="" class="table table-condensed table-bordered ">
                            <thead class="">
                                <?php
                                include("includes/head_rep_repeat.php");
                                if ($agency != '') {
                                ?>
                                    <tr class="mySearchTr">
                                        <th style="" colspan="6">Agency : <?php echo $agenname ?></th>
                                    </tr>
                                <?php
                                }
                                if ($dtype == 1) {
                                ?>
                                    <tr class="mySearchTr">
                                        <th style="text-align:center" colspan="5">Name</th>
                                        <?php if ($invdtcond == "") { ?>
                                            <th style="text-align: right;">Amount</th>
                                        <?php } ?>
                                    </tr>
                                <?php } ?>

                            </thead>
                            <tbody>
                                <?php
                                $agencond = '';
                                if ($mtype == 'S') {
                                    if ($agency != '') {
                                        $agencyflag = 'Y';
                                        $agencond = " and b.broker = '$agency' ";
                                    } else {
                                        $agencyflag = 'N';
                                    }
                                } else {
                                    $agencyflag = 'N';
                                }

                                if ($agencyflag == 'N') {
                                    if ($_POST["hiddenval"] == "R") {
                                        // print "select * from 
                                        //                         (SELECT a.$mpart as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `$mfile` a, accmast b WHERE a.vouchno != '' AND a.$mpart  = b.accode $invdtcondv  AND a.canc_flag!='C' AND a.cashcred !='C' $acond GROUP BY a.party
                                        //                         union all
                                        //                         SELECT a.$mpart as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `$mfile2` a, accmast b WHERE a.vouchno != '' AND a.$mpart = b.accode $invdtcondv  AND a.canc_flag!='C' AND a.cashcred !='C' $acond GROUP BY a.party 
                                        //                         union all
                                        //                         SELECT a.accode as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `prevbill` a, accmast b WHERE a.vouchno != '' AND a.accode = b.accode $invdtcondb And a.type='$mtype' $acond1 GROUP BY a.accode order by `name` 
                                        //                          ) where party != '' GROUP BY party order by name";
                                        //                           die();
                                        $acc = mysqli_query($conn, "select * from 
                                                                (SELECT a.$mpart as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `$mfile` a, accmast b WHERE a.vouchno != '' AND a.$mpart  = b.accode $invdtcondv  AND a.canc_flag!='C' AND a.cashcred !='C' $acond 
                                                                union all
                                                                SELECT a.$mpart as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `$mfile2` a, accmast b WHERE a.vouchno != '' AND a.$mpart = b.accode $invdtcondv  AND a.canc_flag!='C' AND a.cashcred !='C' $acond  
                                                                union all
                                                                SELECT a.accode as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `prevbill` a, accmast b WHERE a.vouchno != '' AND a.accode = b.accode $invdtcondb And a.type='$mtype' $acond1 GROUP BY a.accode order by `name` 
                                                                 ) a where party != '' GROUP BY party order by name") or die(mysqli_error($conn));
                                        // print table($acc);
                                        // die();
                                    } else {
                                        //                                      print   "select * from 
                                        //                                                                (SELECT a.$mpart as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin 
                                        //                                                                        FROM `$mfile` a, accmast b WHERE a.vouchno != '' AND a.$mpart = b.accode 
                                        //                                                                        $invdtcondv  AND a.canc_flag!='C' AND a.cashcred !='C' $acond GROUP BY a.party
                                        //                                                                union all
                                        //                                                                SELECT a.$mpart as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin 
                                        //                                                                       FROM `$mfile2` a, accmast b WHERE a.vouchno != '' AND a.$mpart = b.accode $invdtcondv  AND a.canc_flag!='C' AND a.cashcred !='C' $acond GROUP BY a.party 
                                        //                                                                union all
                                        //                                                                SELECT a.party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `purexpmain` a, accmast b WHERE a.vouchno != '' AND a.party = b.accode $invdtcondv AND a.paidby!='C' $acondp GROUP BY a.party 
                                        //                                                                union all
                                        //                                                                SELECT a.accode as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `prevbill` a, accmast b WHERE a.vouchno != '' AND a.accode = b.accode $invdtcondb And a.type='$mtype' $acond1 GROUP BY a.accode 
                                        //                                                                 ) a where party != '' GROUP BY party order by name";
                                        $acc = mysqli_query($conn, "select * from 
                                                                (SELECT a.$mpart as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin 
                                                                        FROM `$mfile` a, accmast b WHERE a.vouchno != '' AND a.$mpart = b.accode 
                                                                        $invdtcondv  AND a.canc_flag!='C' AND a.cashcred !='C' $acond GROUP BY a.party
                                                                union all
                                                                SELECT a.$mpart as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin 
                                                                       FROM `$mfile2` a, accmast b WHERE a.vouchno != '' AND a.$mpart = b.accode $invdtcondv  AND a.canc_flag!='C' AND a.cashcred !='C' $acond GROUP BY a.party 
                                                                union all
                                                                SELECT a.party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `purexpmain` a, accmast b WHERE a.vouchno != '' AND a.party = b.accode $invdtcondv AND a.paidby!='C' $acondp GROUP BY a.party 
                                                                union all
                                                                SELECT a.accode as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `prevbill` a, accmast b WHERE a.vouchno != '' AND a.accode = b.accode $invdtcondb And a.type='$mtype' $acond1 GROUP BY a.accode 
                                                                 ) a where party != '' GROUP BY party order by name") or die(mysqli_error($conn));
                                        //                                        print table($acc);
                                        //                                        die();
                                    }
                                } else {
                                    $acc = mysqli_query($conn, "select * from 
                                                                    (SELECT a.$mpart as party, b.broker, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin 
                                                                            FROM `$mfile` a, accmast b WHERE a.vouchno != '' AND a.party = b.accode and b.grpcode != '040' AND a.canc_flag!='C'
                                                                            $invdtcondv $custcond  
                                                                    union all
                                                                    SELECT a.$mpart as party,  b.broker, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin 
                                                                           FROM `$mfile2` a, accmast b WHERE a.vouchno != '' AND a.party = b.accode and b.grpcode != '040' AND a.canc_flag!='C'
                                                                           $invdtcondv $custcond  
                                                                    union all
                                                                    SELECT a.accode as party, agency as broker, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin 
                                                                           FROM `prevbill` a, accmast b WHERE a.vouchno != '' AND a.accode = b.accode and b.grpcode != '040'
                                                                           $invdtcondb and a.type='$mtype' $custcond1 )
                                                                    a where party != '' GROUP BY party order by name") or die(mysqli_error($conn));
                                    //                                    print table($acc);
                                    //                                    die();
                                }

                                mysqli_query($conn, "drop temporary table if exists bsetdbf ") or die(mysqli_error($conn));
                                mysqli_query($conn, "create temporary table bsetdbf 
                                                            SELECT * from billsett where billno != '' and bnkdate <= '$mtodate' $accond ") or die(mysqli_error($conn));

                                $bbb = mysqli_query($conn, "SELECT * FROM `bsetdbf` ") or die(mysqli_error($conn));

                                $prev_name = '';
                                $grdbillamt = 0;
                                $grdpendamt = 0;
                                $grdrecdamt = 0;
                                $grdbalamt = 0;
                                $grdsumamt = 0;

                                $msamt1 = $msamt2 = $msamt1_1 = $msamt1_2 = 0;
                                // print table($acc);
                                // die();


                                while ($getac = mysqli_fetch_array($acc)) {
                                    $maccode = $getac["party"];
                                    $macname = $getac["name"];
                                    $madd1 = $getac["add1"];
                                    $madd2 = $getac["add2"];
                                    $madd3 = $getac["add3"];
                                    $mcity = $getac["city"];
                                    $mpin = $getac["pin"];
                                    $mmobile = $getac["mobile"];

                                    //                                    if ($dtype == 1) {
                                    $sub1 = mysqli_query($conn, "SELECT  SUM($fintot) AS amt1 FROM $mfile WHERE vouchno != '' and `$mpart` = '$maccode' AND canc_flag != 'C' and `cashcred` != 'C' $invdtcondv ") or die(mysqli_error($conn));
                                    $msub1 = mysqli_fetch_array($sub1);
                                    $msamt1 = $msub1["amt1"];

                                    $sub1_1 = mysqli_query($conn, "SELECT  SUM($fintot) AS amt1 FROM $mfile2 WHERE `$mpart` = '$maccode' AND `cashcred` != 'C'  AND canc_flag!='C'  $invdtcondv ") or die(mysqli_error($conn));
                                    $msub1_1 = mysqli_fetch_array($sub1_1);
                                    $msamt1_1 = $msub1_1["amt1"];

                                    if ($_POST["hiddenval"] != "R") {
                                        $sub1_2 = mysqli_query($conn, "SELECT  SUM(billamt) AS amt1 FROM purexpmain WHERE `party` = '$maccode' AND `paidby` != 'C'  $invdtcondv ") or die(mysqli_error($conn));
                                        $msub1_2 = mysqli_fetch_array($sub1_2);
                                        $msamt1_2 = $msub1_2["amt1"];
                                    }

                                    $sub2 = mysqli_query($conn, "SELECT  SUM(`billamt`) AS amt1 FROM `prevbill` WHERE `accode` = '$maccode' AND `cashcred` != 'C'  $invdtcondb") or die(mysqli_error($conn));
                                    $msub2 = mysqli_fetch_array($sub2);
                                    $msamt2 = $msub2["amt1"];

                                    $qamt1 = $msamt1_1 + $msamt1_2 + $msamt1 + $msamt2;

                                    $trn1 = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `bsetdbf` WHERE `party`= '$maccode' AND `type`='$mtype' ") or die(mysqli_error($conn));
                                    $gtrn1 = mysqli_fetch_object($trn1);
                                    $msetoff1 = $gtrn1->setoff;
                                    $mbalamt1 = $qamt1 - $msetoff1;

                                    //                                    browse($maccode);
                                    //                                    browse($mbalamt1);

                                    if ($acond != '') {
                                        $godet = 1;
                                    } else {
                                        if ($mbalamt1 > 0) {
                                            $godet = 1;
                                        } else {
                                            $godet = 0;
                                        }
                                    }
                                    // browse($godet);
                                    //                                    if ($maccode == 62) {
                                    //                                        browse($mtype);
                                    //
                                    //                                        browse($qamt1);
                                    //                                        browse($msetoff1);
                                    //                                        
                                    //                                        browse('bal');
                                    //                                        browse($mbalamt1);
                                    //                                    }

                                    if ($godet > 0) {

                                        if ($dtype == 1) {
                                            if ($prntype == 1) {
                                                // $macname;
                                                if ($macname != $prev_name && $prev_name != "") {
                                                    echo "<tr style='page-break-after: always;'></tr>";  // Page break before the new name
                                                }
                                            }
                                            echo "<thead>";
                                            echo "<tr>";
                                            echo "<th colspan='5' style=\"text-align:center;color:black\"><b>" . strtoupper($macname) . "</b></th>";
                                            if ($invdtcond == "") {
                                                echo "<th style=\"text-align:right;color:black\">";
                                                echo "$mbalamt1";
                                                echo "</th>";
                                                $grdsumamt = $grdsumamt + $mbalamt1;
                                            }
                                            echo "</tr>";

                                            echo "<tr>";
                                            echo "<th colspan='6' style=\"text-align:left;font-size:13px;color:black\">Address: $madd1 $madd2 $madd3 $mcity $mpin &nbsp;&nbsp;&nbsp;&nbsp;   Mobile: $mmobile </th>";
                                            echo "</tr>";

                                            echo "</thead>";
                                        }
                                    }
                                    //                                    }


                                    if ($dtype == 2) {
                                        if ($godet > 0) {
                                            if ($prntype == 1) {
                                                if ($macname != $prev_name && $prev_name != "") {
                                                    echo "<tr style='page-break-after: always;'></tr>";  // Page break before the new name
                                                }
                                            }
                                            $prev_name = $macname;

                                            echo "<thead>";
                                            echo "<tr>";
                                            echo "<th colspan='5' style=\"text-align:center;color:black\"><b>" . strtoupper($macname) . '  -  (' . $maccode . ')' . "</b></th>";
                                            echo "</tr>";

                                            echo "<tr>";
                                            echo "<th colspan='6' style=\"text-align:left;font-size:13px;color:black\">Address: $madd1 $madd2 $madd3 $mcity $mpin &nbsp;&nbsp;&nbsp;&nbsp;   Mobile: $mmobile </th>";
                                            echo "</tr>";
                                            echo "</thead>";
                                ?>

                                            <tr style="height:10px;" class="mySearchTr">
                                                <th align="left" width="15%">Date<i class="icon-sort"></i><i class="icon-sort-down"></i>
                                                    <i class="icon-sort-up"></i>
                                                </th>
                                                <th align="left" width="15%">Bill No.<i class="icon-sort"></i><i
                                                        class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                                <th align="right" width="20%" style="text-align:right;">Bill Amount<i
                                                        class="icon-sort"></i><i class="icon-sort-down"></i> <i
                                                        class="icon-sort-up"></i></th>
                                                <th align="right" width="20%" style="text-align:right;">Pending<i
                                                        class="icon-sort"></i><i class="icon-sort-down"></i> <i
                                                        class="icon-sort-up"></i></th>
                                                <th align="right" width="20%" style="text-align:right;">Recd. Amount<i
                                                        class="icon-sort"></i><i class="icon-sort-down"></i> <i
                                                        class="icon-sort-up"></i></th>
                                                <th align="right" width="20%" style="text-align:right;">Balance<i
                                                        class="icon-sort"></i><i class="icon-sort-down"></i> <i
                                                        class="icon-sort-up"></i></th>
                                                <th align="right" width="9%" style="text-align:right;">Days<i class="icon-sort"></i><i
                                                        class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                            </tr>
                                <?php

                                            if ($_POST["hiddenval"] != "R") {
                                                $subtmp1 = mysqli_query($conn, "Select * from 
                                                                (SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno, $mdate as tdt,
                                                                        DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`, `$fintot` as billamt, 
                                                                        `$fintot` as pendamt, `$mpart` as accode , '$blsrc1' as billsrc
                                                                        FROM $mfile WHERE `$mpart` = '$maccode' AND `cashcred` != 'C' AND canc_flag!='C'  $invdtcondv
                                                                UNION
                                                                SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno,$mdate as tdt,
                                                                        DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`, `$fintot` as billamt, 
                                                                        `$fintot` as pendamt, `$mpart` as accode , '$blsrc2' as billsrc
                                                                        FROM $mfile2 WHERE `$mpart` = '$maccode' AND `cashcred` != 'C' AND canc_flag!='C'  $invdtcondv
                                                                UNION
                                                                SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno, vouchdt as tdt,
                                                                        DATE_FORMAT(vouchdt, '%d-%m-%Y') AS `billdate`,  billamt, billamt as pendamt,
                                                                        `party` as accode , 'PUEX' as billsrc
                                                                        FROM purexpmain WHERE `party` = '$maccode' AND `paidby` != 'C'  $invdtcondv
                                                                UNION
                                                                SELECT `yearno`,`type`,`vouchno`, `billno`, billdate as tdt, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                                                                       `billdate`, `actbillamt` as billamt, `billamt` as pendamt, `accode`, 'PB' as billsrc 
                                                                        FROM `prevbill` WHERE `accode` = '$maccode' $invdtcondb
                                                                        AND `type` = '$mtype' ) a order by tdt,billno ") or die(mysqli_error($conn));
                                                //                                            print table($subtmp1);
                                                //                                            die();
                                            } else {
                                                $subtmp1 = mysqli_query($conn, "Select * from 
                                                                (SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno,$mdate as tdt,
                                                                        DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`, `$fintot` as billamt,  `$fintot` as pendamt, 
                                                                        `$mpart` as accode , '$blsrc1' as billsrc
                                                                        FROM $mfile WHERE `$mpart` = '$maccode' AND `cashcred` != 'C' AND canc_flag!='C'  $invdtcondv
                                                                UNION
                                                                SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno,$mdate as tdt,
                                                                        DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`, `$fintot` as billamt, `$fintot` as pendamt,
                                                                        `$mpart` as accode , '$blsrc2' as billsrc
                                                                        FROM $mfile2 WHERE `$mpart` = '$maccode' AND `cashcred` != 'C' AND canc_flag!='C'  $invdtcondv
                                                                UNION
                                                                SELECT `yearno`,`type`,`vouchno`, `billno`, billdate as tdt, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                                                                       `billdate`, `actbillamt` as billamt, `billamt` as pendamt, `accode`, 'PB' as billsrc 
                                                                        FROM `prevbill` WHERE `accode` = '$maccode' $invdtcondb
                                                                        AND `type` = '$mtype' ) a order by tdt,billno ") or die(mysqli_error($conn));
                                            }
                                            //    print table($subtmp1);
                                            //    die();

                                            $totbillamt = 0;
                                            $totpendamt = 0;
                                            $totrecdamt = 0;
                                            $totbalamt = 0;

                                            while ($murl = mysqli_fetch_object($subtmp1)) {

                                                $qyear = $murl->yearno;
                                                $qbilldate = $murl->billdate;
                                                $qbill = $murl->billno;
                                                $qvouch = $murl->vouchno;
                                                $qbillamt = $murl->billamt;
                                                $qamt = $murl->pendamt;
                                                $qparty = $murl->accode;
                                                $qbillsrc = $murl->billsrc;

                                                $date1 = date_create("$qbilldate");
                                                $date2 = date_create("$mtodate");
                                                $diff = date_diff($date1, $date2);
                                                $subtdate = $diff->format("%a");
                                                $mldays = $subtdate;

                                                // print "SELECT SUM(setoff) AS setoff FROM `bsetdbf` WHERE  `party`= '$qparty' AND `yearno` ='$qyear' AND 
                                                //                        `vouchno`='$qvouch' AND `type`='$mtype' and `billsrc` = '$qbillsrc' ";
                                                $trn = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `bsetdbf` WHERE  `party`= '$qparty' AND `yearno` ='$qyear' AND 
                                                                       `vouchno`='$qvouch' AND `type`='$mtype' and `billsrc` = '$qbillsrc' ") or die(mysqli_error($conn));
                                                //    print table($trn);

                                                $gtrn = mysqli_fetch_object($trn);

                                                $msetoff = $gtrn->setoff;
                                                $mbalamt = $qamt - $msetoff;

                                                if ($mbalamt > 0) {
                                                    $totbillamt = $totbillamt + $qbillamt;
                                                    $totpendamt = $totpendamt + $qamt;
                                                    $totrecdamt = $totrecdamt + $msetoff;
                                                    $totbalamt = $totbalamt + $mbalamt;

                                                    $grdbillamt = $grdbillamt + $qbillamt;
                                                    $grdpendamt = $grdpendamt + $qamt;
                                                    $grdrecdamt = $grdrecdamt + $msetoff;
                                                    $grdbalamt = $grdbalamt + $mbalamt;

                                                    echo "<tr style='height:20px;'>";
                                                    echo "<td style=\"text-align:left;\" scope=\"col\" width=\"300\">$qbilldate</td>";
                                                    echo "<td style=\"text-align:left;\" scope=\"col\" width=\"100\">$qbill</td>";
                                                    echo "<td style=\"text-align:right;\" scope=\"col\" width=\"100\">$qbillamt</td>";
                                                    echo "<td style=\"text-align:right;\" scope=\"col\" width=\"300\">$qamt</td>";
                                                    echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">$msetoff</td>";
                                                    echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">$mbalamt</td>";
                                                    echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">$mldays</td>";
                                                    echo "</tr>";
                                                }
                                            }
                                            echo "<tr style='height:20px;background-color: #ced4da'>";
                                            echo "<td style=\"text-align:left;\" scope=\"col\" width=\"300\"></td>";
                                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"100\"><b>Total</b></td>";
                                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"300\"><b>$totbillamt</b></td>";
                                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"300\"><b>$totpendamt</b></td>";
                                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><b>$totrecdamt</b></td>";
                                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><b>$totbalamt</b></td>";
                                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"></td>";
                                            echo "</tr>";
                                            echo "<tr style='height:20px;'>";
                                            echo "<td style=\"text-align:left;\" scope=\"col\" width=\"300\"></td>";
                                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"100\"></td>";
                                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"100\"></td>";
                                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"100\"></td>";
                                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"100\"></td>";
                                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"></td>";
                                            echo "</tr>";
                                        }
                                    }

                                    //                                    browse($prev_name);
                                }
                                if ($dtype == 1) {
                                    echo "<tr style='height:20px;background-color: #ced4da'>";
                                    echo "<th colspan='5' style=\"text-align:right;color:black\"><b>Grand Total</b></th>";
                                    echo "<th style=\"text-align:right;color:black\">";
                                    echo "$grdsumamt";
                                    echo "</th>";
                                    echo "</tr>";
                                } else {
                                    echo "<tr style='height:20px;background-color: #ced4da'>";
                                    echo "<td style=\"text-align:left;\"  ></td>";
                                    echo "<td style=\"text-align:right; font-size:15px\" ><b>Grand Total</b></td>";
                                    echo "<td style=\"text-align:right; font-size:15px\" ><b>$grdbillamt</b></td>";
                                    echo "<td style=\"text-align:right; font-size:15px\" ><b>$grdpendamt</b></td>";
                                    echo "<td style=\"text-align:right; font-size:15px\" ><b>$grdrecdamt</b></td>";
                                    echo "<td style=\"text-align:right; font-size:15px\" ><b>$grdbalamt</b></td>";
                                    echo "<td style=\"text-align:right;\" ></td>";
                                    echo "</tr>";
                                }
                                ?>

                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    <?php
}

if (isset($_POST["excel"])) {
    $acond = "";
    $scond = "";
    if ($comp > 100) {
        $mpart = "party";
    } else {
        $mpart = "broker";
    }

    if (!empty($_POST["acname"])) {
        $accode = trim($_POST["acname"]);
        $acond = "AND a.`$mpart`='$accode'";
        $acond1 = "AND a.`accode`='$accode'";
    }

    $mtodate = $_POST["frmdt"];
    $prntype = $_POST["prntype"];

    if ($_POST["hiddenval"] == "R") {
        $mfile = "rawmain";
        $mfile2 = "gstmain";
        $mtype = "S";
        $mdate = 'invdt';
        $blsrc1 = 'SLM';
        $blsrc2 = 'SLFP';
        $fintot = "netamt";
        $grid = "osbill.php?para=R";
        $formname1 = "Outstanding Receivable";
        $agency = $_POST["agency"];
        $agenqry = mysqli_query($conn, "SELECT name from accmast WHERE `accode` = '$agency' ") or die(mysqli_error($conn));
        $agenfet = mysqli_fetch_array($agenqry);
        $agenname = $agenfet['name'];

        $frminvdt = $_POST["frminvdt"];
        $toinvdt = $_POST["toinvdt"];

        $checks = (isset($_POST['check'])) ? $_POST['check'] : array();

        $custcond = '';
        $custcond1 = '';

        if (count($checks) > 0) {
            foreach ($checks as $check) {
                $mycheck2[$count2++] .= "OR a.`$mpart` = '" . $check . "' ";
                $mycheck3[$count2++] .= "OR a.`accode` = '" . $check . "' ";
            }
            foreach ($mycheck2 as $g => $values2) {
                $mcheck2 .= $values2;
            }
            foreach ($mycheck3 as $g => $values3) {
                $mcheck3 .= $values3;
            }

            $custcond = " AND (a.`$mpart` = '' " . $mcheck2 . " )";
            $custcond1 = " AND (a.`accode` = '' " . $mcheck3 . " )";
        }

        if ($frminvdt != "" && $toinvdt != "") {
            if ($frminvdt == "0000-00-00" && $toinvdt == "0000-00-00") {
                $invdtcond = "";
                $invdtcond2 = "";
                $invdtconda = "";
            } else {
                $invdtcond = "AND $mdate >= '$frminvdt' AND $mdate <= '$toinvdt' ";
                $invdtcond2 = "AND billdate >= '$frminvdt' AND billdate <= '$toinvdt'";
                $invdtconda = "AND vouchdt >= '$frminvdt' AND vouchdt <= '$toinvdt'";
            }
        }
    } else {
        $mfile = 'purrawmain';
        $mfile2 = "pgstmain";
        $blsrc1 = 'PUM';
        $blsrc2 = 'PUFP';
        $mtype = "P";
        $mdate = 'vouchdt';
        $fintot = "netamt";
        $grid = "osbill.php?para=P";
        $formname1 = "Outstanding Payable";
        $agency = '';
        $agenname = '';

        $frminvdt = $_POST["frminvdt"];
        $toinvdt = $_POST["toinvdt"];

        if ($frminvdt != "" && $toinvdt != "") {
            if ($frminvdt == "0000-00-00" && $toinvdt == "0000-00-00") {
                $invdtcond = "";
                $invdtcond2 = "";
                $invdtconda = "";
            } else {
                $invdtcond = "AND $mdate >= '$frminvdt' AND $mdate <= '$toinvdt' ";
                $invdtcond2 = "AND billdate >= '$frminvdt' AND billdate <= '$toinvdt'";
                $invdtconda = "AND vouchdt >= '$frminvdt' AND vouchdt <= '$toinvdt'";
            }
        }
    }

    if ($_POST["dtype"] == 1) {
        $formname = "$formname1 - Summary";
        $dtype = 1;
    } else {
        $formname = "$formname1 - Detail";
        $dtype = 2;
    }

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

    if ($agency != '') {

        $contents .= "Agency :  $agenname \n";
    }

    $contents .= "Name,";
    if ($invdtcond == "") {
        $contents .= "Amount,";
    }
    $contents .= "\n";
    $agencond = '';
    if ($mtype == 'S') {
        if ($agency != '') {
            $agencyflag = 'Y';
            $agencond = " and b.broker = '$agency' ";
        } else {
            $agencyflag = 'N';
        }
    } else {
        $agencyflag = 'N';
    }

    if ($agencyflag == 'N') {
        if ($_POST["hiddenval"] == "R") {

            $acc = mysqli_query($conn, "SELECT a.$mpart as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `$mfile` a, accmast b WHERE a.vouchno != '' AND a.$mpart  = b.accode and a.vouchdt <= '$mtodate'  AND a.canc_flag!='C' AND a.cashcred !='C'   $invdtcond $acond GROUP BY a.party
                                                                    union
                                                                    SELECT a.$mpart as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `$mfile2` a, accmast b WHERE a.vouchno != '' AND a.$mpart = b.accode and a.vouchdt <= '$mtodate'  AND a.canc_flag!='C' AND a.cashcred !='C' $invdtcond $acond GROUP BY a.party 
                                                                    union
                                                                    SELECT a.accode as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `prevbill` a, accmast b WHERE a.vouchno != '' AND a.accode = b.accode and a.billdate <= '$mtodate' And a.type='$mtype' $invdtcond2 $acond1 GROUP BY a.accode order by `name` ") or die(mysqli_error($conn));
        } else {

            $acc = mysqli_query($conn, "SELECT a.$mpart as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `$mfile` a, accmast b WHERE a.vouchno != '' AND a.$mpart  = b.accode and a.vouchdt <= '$mtodate'  AND a.canc_flag!='C' AND a.cashcred !='C'   $invdtcond $acond GROUP BY a.party
                                                                    union
                                                                    SELECT a.$mpart as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `$mfile2` a, accmast b WHERE a.vouchno != '' AND a.$mpart = b.accode and a.vouchdt <= '$mtodate'  AND a.canc_flag!='C' AND a.cashcred !='C' $invdtcond $acond GROUP BY a.party 
                                                                    union
                                                                    SELECT a.party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `purexpmain` a, accmast b WHERE a.vouchno != '' AND a.party = b.accode and a.vouchdt <= '$mtodate'  AND a.paidby!='C' $invdtconda $acond GROUP BY a.party 
                                                                    union
                                                                    SELECT a.accode as party, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `prevbill` a, accmast b WHERE a.vouchno != '' AND a.accode = b.accode and a.billdate <= '$mtodate' And a.type='$mtype' $invdtcond2 $acond1 GROUP BY a.accode order by `name` ") or die(mysqli_error($conn));
        }
    } else {

        $acc = mysqli_query($conn, "select * from 
                                                                        (SELECT a.$mpart as party, b.broker, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `$mfile` a, accmast b WHERE a.vouchno != '' AND a.party = b.accode   AND a.canc_flag!='C'
                                                                            and a.vouchdt <= '$mtodate' $custcond  $invdtcond GROUP BY a.$mpart 
                                                                        union
                                                                        SELECT a.$mpart as party,  b.broker, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `$mfile2` a, accmast b WHERE a.vouchno != '' AND a.party = b.accode   AND a.canc_flag!='C'
                                                                            and a.vouchdt <= '$mtodate' $custcond  $invdtcond GROUP BY a.$mpart 
                                                                        union 
                                                                        SELECT a.accode as party,  0 as broker, b.name,b.add1,b.add2,b.add3,b.mobile,b.city,b.pin FROM `prevbill` a, accmast b WHERE a.vouchno != '' AND a.accode = b.accode  
                                                                        and a.billdate <= '$mtodate' and a.type='$mtype' $custcond1  $invdtcond2 GROUP BY a.accode )
                                                                        a order by name") or die(mysqli_error($conn));
    }

    $prev_name = '';
    $grdbillamt = 0;
    $grdrecdamt = 0;
    $grdbalamt = 0;
    $grdsumamt = 0;

    while ($getac = mysqli_fetch_array($acc)) {

        $maccode = $getac["party"];
        $macname = $getac["name"];
        $madd1 = $getac["add1"];
        $madd2 = $getac["add2"];
        $madd3 = $getac["add3"];
        $mcity = $getac["city"];
        $mpin = $getac["pin"];
        $mmobile = $getac["mobile"];

        $sub1 = mysqli_query($conn, "SELECT  SUM($fintot) AS amt1 FROM $mfile WHERE `$mpart` = '$maccode' AND `cashcred` != 'C'  AND canc_flag!='C'  and  vouchdt <= '$mtodate' ") or die(mysqli_error($conn));
        $msub1 = mysqli_fetch_array($sub1);
        $msamt1_2 = 0;

        $sub1_1 = mysqli_query($conn, "SELECT  SUM($fintot) AS amt1 FROM $mfile2 WHERE `$mpart` = '$maccode' AND `cashcred` != 'C'  AND canc_flag!='C'  and vouchdt <= '$mtodate' ") or die(mysqli_error($conn));
        $msub1_1 = mysqli_fetch_array($sub1_1);
        if ($_POST["hiddenval"] != "R") {

            $sub1_2 = mysqli_query($conn, "SELECT  SUM(billamt) AS amt1 FROM purexpmain WHERE `party` = '$maccode' AND `paidby` != 'C'  and vouchdt <= '$mtodate' ") or die(mysqli_error($conn));
            $msub1_2 = mysqli_fetch_array($sub1_2);
            $msamt1_2 = $msub1_2["amt1"];
        }
        $sub2 = mysqli_query($conn, "SELECT  SUM(`billamt`) AS amt2 FROM `prevbill` WHERE `accode` = '$maccode' AND `cashcred` != 'C'  and billdate <= '$mtodate' $invdtcond2") or die(mysqli_error($conn));
        $msub2 = mysqli_fetch_array($sub2);
        $msamt1 = $msub1["amt1"];
        $msamt1_1 = $msub1_1["amt1"];

        $msamt2 = $msub2["amt2"];
        $qamt1 = $msamt1_1 + $msamt1 + $msamt2 + $msamt1_2;

        $trn1 = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `billsett` WHERE `party`= '$maccode' AND `type`='$mtype'") or die(mysqli_error($conn));
        $gtrn1 = mysqli_fetch_object($trn1);
        $msetoff1 = $gtrn1->setoff;
        $mbalamt1 = $qamt1 - $msetoff1;

        if ($mbalamt1 > 0) {
            // if ($prntype == 1) {
            //     if ($macname != $prev_name && $prev_name != "") {
            //         echo "<tr style='page-break-after: always;'></tr>";  // Page break before the new name
            //     }
            // }
            $macname = strtoupper($macname);
            $contents .= "$macname ,";
            if ($invdtcond == "") {
                $contents .= "$mbalamt1,";
                $grdsumamt = $grdsumamt + $mbalamt1;
            }
            $contents .= "\n";

            $contents .= "Address: $madd1 $madd2 $madd3 $mcity $mpin Mobile: $mmobile \n";

            if ($dtype == 2) {

                $contents .= "Date,Bill No., Bill Amount, Recd. Amount, Balance, Days \n";

                if ($_POST["hiddenval"] != "R") {
                    $subtmp1 = mysqli_query($conn, "Select * from 
                                                                    (SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno,$mdate as tdt,
                                                                            DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`,  `$fintot` as billamt, `$mpart` as accode , '$blsrc1' as billsrc
                                                                            FROM $mfile WHERE `$mpart` = '$maccode' AND `cashcred` != 'C' AND canc_flag!='C' and vouchdt <= '$mtodate' $invdtcond
                                                                    UNION
                                                                    SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno,$mdate as tdt,
                                                                            DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`,  `$fintot` as billamt, `$mpart` as accode , '$blsrc2' as billsrc
                                                                            FROM $mfile2 WHERE `$mpart` = '$maccode' AND `cashcred` != 'C' AND canc_flag!='C' and vouchdt <= '$mtodate' $invdtcond
                                                                    UNION
                                                                    SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno,vouchdt as tdt,
                                                                            DATE_FORMAT(vouchdt, '%d-%m-%Y') AS `billdate`,  billamt, `party` as accode , 'PUEX' as billsrc
                                                                            FROM purexpmain WHERE `party` = '$maccode' AND `paidby` != 'C' and vouchdt <= '$mtodate' $invdtconda
                                                                    UNION
                                                                    SELECT `yearno`,`type`,`vouchno`, `billno`, billdate as tdt, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                                                                           `billdate`, `billamt`, `accode`, 'PB' as billsrc FROM `prevbill` WHERE `accode` = '$maccode' $invdtcond2
                                                                            AND `type` = '$mtype' ) a order by tdt,billno ") or die(mysqli_error($conn));
                } else {
                    $subtmp1 = mysqli_query($conn, "Select * from 
                                                                    (SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno,$mdate as tdt,
                                                                            DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`,  `$fintot` as billamt, `$mpart` as accode , '$blsrc1' as billsrc
                                                                            FROM $mfile WHERE `$mpart` = '$maccode' AND `cashcred` != 'C' AND canc_flag!='C' and vouchdt <= '$mtodate' $invdtcond
                                                                    UNION
                                                                    SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `invno` as billno,$mdate as tdt,
                                                                            DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`,  `$fintot` as billamt, `$mpart` as accode , '$blsrc2' as billsrc
                                                                            FROM $mfile2 WHERE `$mpart` = '$maccode' AND `cashcred` != 'C' AND canc_flag!='C' and vouchdt <= '$mtodate' $invdtcond
                                                                    UNION
                                                                    SELECT `yearno`,`type`,`vouchno`, `billno`, billdate as tdt, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                                                                           `billdate`, `billamt`, `accode`, 'PB' as billsrc FROM `prevbill` WHERE `accode` = '$maccode' $invdtcond2
                                                                            AND `type` = '$mtype' ) a order by tdt,billno ") or die(mysqli_error($conn));
                }
                $totbillamt = 0;
                $totpendamt = 0;
                $totrecdamt = 0;
                $totbalamt = 0;

                while ($murl = mysqli_fetch_object($subtmp1)) {

                    $qyear = $murl->yearno;
                    $qbilldate = $murl->billdate;
                    $qbill = $murl->billno;
                    $qvouch = $murl->vouchno;
                    $qamt = $murl->billamt;
                    $qparty = $murl->accode;
                    $qbillsrc = $murl->billsrc;

                    $date1 = date_create("$qbilldate");
                    $date2 = date_create("$mtodate");
                    $diff = date_diff($date1, $date2);
                    $subtdate = $diff->format("%a");
                    $mldays = $subtdate;

                    $trn = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `billsett` WHERE  `party`= '$qparty' AND `yearno` ='$qyear' AND 
                                                                           `vouchno`='$qvouch' AND `type`='$mtype' and `billsrc` = '$qbillsrc' ") or die(mysqli_error($conn));

                    $gtrn = mysqli_fetch_object($trn);

                    $msetoff = $gtrn->setoff;
                    $mbalamt = $qamt - $msetoff;

                    if ($mbalamt > 0) {
                        $totbillamt = $totbillamt + $qamt;
                        $totrecdamt = $totrecdamt + $msetoff;
                        $totbalamt = $totbalamt + $mbalamt;

                        $grdbillamt = $grdbillamt + $qamt;
                        $grdrecdamt = $grdrecdamt + $msetoff;
                        $grdbalamt = $grdbalamt + $mbalamt;

                        $contents .= "$qbilldate" . ",";
                        $contents .= "$qbill" . ",";
                        $contents .= "$qamt" . ",";
                        $contents .= "$msetoff" . ",";
                        $contents .= "$mbalamt" . ",";
                        $contents .= "$mldays" . "\n";
                    }
                }

                $contents .= "" . ",";
                $contents .= "Total" . ",";
                $contents .= "$totbillamt" . ",";
                $contents .= "$totrecdamt" . ",";
                $contents .= "$totbalamt" . ",";
                $contents .= "" . "\n";

                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . "\n";
            }
        }
        $prev_name = $macname;
    }
    if ($dtype == 1) {
        $contents .= ",,,,Grand Total, $grdsumamt \n";
    } else {

        $contents .= "" . ",";
        $contents .= "Grand Total" . ",";
        $contents .= "$grdbillamt" . ",";
        $contents .= "$grdrecdamt" . ",";
        $contents .= "$grdbalamt" . ",";
        $contents .= "" . "\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;
    }
}
    ?>