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

$maindb = "billmain";
$trandb = "billtran";
$formname = "Bill Summary";
$file = "billreg.php";

$zroot = $_SESSION["zdir"];
$zcomp = $_SESSION["comp_" . $dbname];
$fseries = $_SESSION["society_" . $dbname];

$getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` where `code` = '$fseries'") or die(mysqli_error($conn));
$comp = mysqli_fetch_array($getcomp);
$society = $_SESSION["society_" . $dbname];

if (empty($society)) {
    browse("Invalid Society Credentials Forced Logout.");
    print '<script type="text/javascript">';
    print " top.window.location = 'index.php'";
    print '</script>';
}

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
$getsoc = mysqli_query($conn, "SELECT * FROM `company` WHERE `code` = '$society'") or die(mysqli_error($conn));
$fetsoc = mysqli_fetch_array($getsoc);
$billcycle = $fetsoc['billcycle'];

switch ($billcycle) {
    case '1':
        $byname = 'MONTHLY';
        break;
    case '2':
        $byname = 'BI - MONTHLY';
        break;
    case '3':
        $byname = 'QUARTERLY';
        break;
    case '4':
        $byname = 'HALF YEARLY';
        break;
    default:
        $byname = '';
}

$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$para = $_GET['para'];
if ($para != "D") {
    $formname = "Bill Summary";
} else {
    $formname = "Bill Register";
}

$billcode = $_POST["billcode"];

$member = $_POST["member"];
$date = $_POST["date"];

$memcond = '';
$billcond = '';

if (!empty($member)) {
    $memcond = "and `accode` = '$member'";
}

if (!empty($billcode)) {
    $billcond = "and `billcode` = '$billcode'";
}

$getdt = mysqli_query($conn, "SELECT billcycle, billcode, DATE_FORMAT(`billdate`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `society`='$society'
                           $billcond $memcond") or die(mysqli_error($conn));
$dmain = mysqli_fetch_array($getdt);

$bcode = mysqli_query($conn, "SELECT `code`,`descrip`,`billcycle` FROM $zroot.`billingcycle` where `code` = '$dmain[billcode]' and `billcycle`='$dmain[billcycle]' ") or die(mysqli_error($conn));
$scode = mysqli_fetch_array($bcode);


$getdata = mysqli_query($conn, "SELECT accode, flatno, sum(billamt) as totbill FROM `$maindb` WHERE `society`='$society' 
                                       $billcond $memcond AND billamt != '' group by flatno ORDER BY flatno") or die(mysqli_error($conn));

$getsname = mysqli_query($conn, "SELECT maintype,srno FROM $zroot.`soctran` WHERE  `cono`='$society' order by srno") or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    $zroot = $_SESSION["zdir"];
    $zcomp = $_SESSION["comp_" . $dbname];
    $fseries = $_SESSION["society_" . $dbname];
    $getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` where `code` = '$fseries'") or die(mysqli_error($conn));
    $comp = mysqli_fetch_array($getcomp);
?>

    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

        .mtotclr {
            color: #0098DA;
        }

        .ttotclr {
            color: #ed3237;
        }

        @media print {
            @page {
                size: landscape;
            }
        }
    </style>
    </head>

    <body>
        <div class="container-fluid">
            <table style="width:100%">
                <tbody>
                    <tr>
                        <td>
                            <table class="" width="100%" id="head" style='background-color:#CCCCCC;'>
                                <tr>
                                    <td style="font-size: 18px;">
                                        <label style='font-size:20px;'><? echo $comp['compname']; ?></label>
                                    </td>
                                    <td style="text-align:right">
                                        <a onclick="window.close()" style="height:30px" class="btn btn-primary noPrint">GO Back</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="font-size: 12px">
                                        <label style='font-size:15px;'><?php echo $comp['address'] ?></label>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="font-size: 16px"><b>
                                            <label class="label-control marg" style="font-size:15px; color:red"><?php
                                                                                                                if ($para != "D") {
                                                                                                                    $rtype = " - Summary";
                                                                                                                } else {
                                                                                                                    $rtype = " - Detail";
                                                                                                                }
                                                                                                                echo $formname . $rtype;
                                                                                                                ?></label></b>
                                    </td>
                                    <td style="font-size: 15px" align="right">
                                        <label class="label-control marg" style="font-size:15px">Report Date - <?php echo Date("d-m-Y"); ?></label>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="font-size: 15px">
                                        <?php if ($mfdt != '') { ?>
                                            From Date -
                                            <?php echo $mfdt; ?>&nbsp;&nbsp;&nbsp;
                                        <?php } ?>
                                        <?php if ($mtdt != '') { ?>
                                            To Date -
                                            <?php echo $mtdt; ?>
                                        <?php } ?>
                                        <?php if ($asdt != '') { ?>
                                            As On Date -
                                            <?php echo $asdt; ?>
                                        <?php } ?>


                                    </td>
                                </tr>
                            </table>
                            <table width="100%" id="head" style='background-color:#CCCCCC;'>
                                <tr>
                                    <td style="font-size: 15px"><label style="color:#6082BF;font-weight: bold">Date:&nbsp;<?= $dmain['mydt'] ?></label></td>
                                    <td style="font-size: 15px;text-align: right"><label style="color:#6082BF;font-weight: bold">Billing Period:&nbsp;<?= $scode['descrip']; ?></label></td>
                                </tr>
                            </table>
                            <table style="background: white;width: 100%" class="table table-bordered table-condensed "><?= $byname ?>
                                <tr class="mySearchTr">
                                    <?php if ($para != "D") { ?>
                                        <th style="width:10">Sr.</th>
                                        <th style="width:20%">Flat No.</th>
                                        <th style="width:50%">Member</th>
                                        <th style="width:20%" class='text-right'>Amount</th>
                                    <?php } else {
                                    ?>
                                        <th style="width:9">Sr.</th>
                                        <th style="width:9">Bill No.</th>
                                        <th style="width:9">Bill Date</th>
                                        <th style="width:10%">Flat No.</th>
                                        <th style="width:30%">Member</th>
                                        <th style="width:10%" class='text-right'>Amount</th>
                                    <?php } ?>
                                </tr>
                                <?php
                                $ctr = 0;
                                $totamt = $totbilamt = $totarrears = $totintamt = $totadvance = 0;

                                $nctr = 0;
                                while ($pmain = mysqli_fetch_array($getdata)) {
                                    $totbill = $pmain['totbill'];

                                    $gmem = mysqli_query($conn, "SELECT * FROM `accmast` where `accode` = '$pmain[accode]'") or die(mysqli_error($conn));
                                    $smem = mysqli_fetch_array($gmem);

                                    $openbal = $smem['openbal'];
                                    $dc = $smem['dc'];
                                    $member = $smem['accode'];
                                    $flatno = $smem['flatno'];

                                    if ($dc == 'D') {
                                        $mopen = abs($openbal) * -1;
                                    } else {
                                        $mopen = abs($openbal);
                                    }

                                    $ctr++;
                                    $nctr++;
                                    if ($nctr > 15) {
                                        $nctr = 0;
                                    }

                                    if ($para != "D") {
                                        echo "<tr>";
                                        echo "<td >$ctr</td>";
                                        echo "<td >$flatno</td>";
                                        echo "<td >$smem[name]</td>";
                                        echo "<td class='text-right'>$totbill</td>";
                                        echo "</tr>";

                                        $mtotbill = $mtotbill + $totbill;
                                    } else {

                                        $getmemdtl2 = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `accode`='$pmain[accode]' AND `billno` != ''") or die(mysqli_error($conn));
                                        $memdtl2 = mysqli_fetch_array($getmemdtl2);

                                        echo "<tr>";
                                        echo "<td >$ctr</td>";
                                        echo "<td >$memdtl2[billno]</td>";
                                        echo "<td >$memdtl2[trandate]</td>";
                                        echo "<td >$flatno</td>";
                                        echo "<td >$smem[name]</td>";
                                        echo "<td class='text-right'>$memdtl2[amount]</td>";
                                        echo "</tr>";

                                        $ttotbal = $ttotbal + $memdtl2[amount];
                                    }
                                }
                                if ($para != "D") {

                                    echo "<tr class='mtotclr '>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td class='text-right'>Total</td>";
                                    echo "<td class='text-right'><i class='fa fa-inr'></i>&nbsp;$mtotbill</td>";
                                    echo "</tr>";
                                } else {
                                    echo "<tr class='mtotclr '>";
                                    echo "<td colspan='4'></td>";
                                    echo "<td class='text-right'>Total</td>";
                                    echo "<td class='text-right'><i class='fa fa-inr'></i>&nbsp;$ttotbal</td>";
                                    echo "</tr>";
                                }

                                ?>
                            </table>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
<?php
}


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

    $billcode = $_POST["billcode"];

    $member = $_POST["member"];
    $date = $_POST["date"];

    $memcond = '';
    $billcond = '';

    if (!empty($member)) {

        $memcond = "and `accode` = '$member'";
    }

    if (!empty($billcode)) {
        $billcond = "and `billcode` = '$billcode'";
    }

    $para = $_POST["para"];

    $getdt = mysqli_query($conn, "SELECT billcycle, billcode, DATE_FORMAT(`billdate`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `society`='$society'
                           $billcond $memcond") or die(mysqli_error($conn));
    $dmain = mysqli_fetch_array($getdt);

    $bcode = mysqli_query($conn, "SELECT `code`,`descrip`,`billcycle` FROM $zroot.`billingcycle` where `code` = '$dmain[billcode]' and `billcycle`='$dmain[billcycle]' ") or die(mysqli_error($conn));
    $scode = mysqli_fetch_array($bcode);


    $getdata = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `society`='$society'
                           $billcond $memcond ORDER BY newbill") or die(mysqli_error($conn));

    $getsname = mysqli_query($conn, "SELECT maintype,srno FROM $zroot.`soctran` WHERE  `cono`='$society' order by srno") or die(mysqli_error($conn));

    $fetnumsname = mysqli_num_rows($getsname);
    $snamectr = 0;
    $contents .= "Date :" . $dmain[mydt] . ",";
    $contents .= "Billing Period :" . $scode[descrip] . "\n";
    if ($dtype == 1) {

        $contents .= "Bill No.,Flat No.,Member,Amount,Arrears,Int.Arrears,Advanced,Bill Amount\n";

        $ctr = 0;
        $totamt = $totbilamt = $totarrears = $totintamt = $totadvance = 0;
        while ($pmain = mysqli_fetch_array($getdata)) {
            $intamt = $pmain['intamt'];
            $totbill = $pmain['billamt'] + $intamt;
            $total = $pmain['total'] + $intamt;

            $gmem = mysqli_query($conn, "SELECT * FROM `accmast` where `active`='1' and `accode` = '$pmain[accode]' AND grpcode='010' and `series`='$society'") or die(mysqli_error($conn));
            $smem = mysqli_fetch_array($gmem);

            $openbal = $smem['openbal'];
            $dc = $smem['dc'];
            $member = $smem['accode'];
            $flatno = $smem['flatno'];

            if ($dc == 'D') {
                $mopen = abs($openbal) * -1;
            } else {
                $mopen = abs($openbal);
            }

            $ctr++;
            $contents .= $pmain['newbill'] . ",";
            $contents .= $pmain['flatno'] . ",";
            $contents .= $smem['name'] . ",";
            $contents .= $totbill . ",";
            $contents .= $pmain['arrears'] . ",";
            $contents .= $pmain['intamt'] . ",";
            $contents .= $pmain['advance'] . ",";
            $contents .= $total . "\n";

            $totbilamt = $totbilamt + $totbill;
            $totarrears = $totarrears + $pmain['arrears'];
            $totintamt = $totintamt + $pmain['intamt'];
            $totadvance = $totadvance + $pmain['advance'];
            $totamt = $totamt + $total;
        }

        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= "Total" . ",";
        $contents .= $totbilamt . ",";
        $contents .= $totarrears . ",";
        $contents .= $totintamt . ",";
        $contents .= $totadvance . ",";
        $contents .= $totamt . "\n";
    } else {

        $ctr = 1;
        $totamt = $totbilamt = $totarrears = $totintamt = $totadvance = 0;
        while ($pmain = mysqli_fetch_array($getdata)) {
            $billno = $pmain['billno'];
            $billcode = $pmain['billcode'];
            $billcycle = $pmain['billcycle'];
            $prevint = $pmain['intprev'];
            $arrears = $pmain['arrears'];
            $intamt = $pmain['intamt'];
            $total = $pmain['total'] + $intamt;
            $billamt = $pmain['billamt'];
            $totbill = $billamt + $intamt;
            $advance = $pmain['advance'];

            $getname = mysqli_query($conn, "SELECT name FROM `accmast` WHERE `active`='1' and  `series`='$society' and `accode`='$pmain[accode]'") or die(mysqli_error($conn));
            $fetname = mysqli_fetch_array($getname);
            $memname = $fetname['name'];


            $getcompdtl = mysqli_query($conn, "SELECT * FROM $zroot.`soctran` WHERE  `cono`='$society' order by srno") or die(mysqli_error($conn));
            if ($ctr == 1) {
                $contents .= "Bill No.,Flat No.,Member - Name,Bill Amt.,Arrears,Prev Int.,Total,Current Int." . ",";
                $namectr = 0;
                while ($fsname = mysqli_fetch_array($getsname)) {
                    $maintype = $fsname['maintype'];
                    $snamectr++;
                    $getmname = mysqli_query($conn, "SELECT `accode`,`shortname` FROM `accmast` WHERE `active`='1' and `accode`='$maintype' and
                                             `type`='I' and `series`='$society' ") or die(mysqli_error($conn));
                    $name = mysqli_num_rows($getmname);
                    $name1 = mysqli_fetch_array($name);
                    $name2 = $name1['shortname'];
                    if ($name1 == $namectr) {
                        $norcomma = ",";
                    } else {
                        $norcomma = "\n";
                    }
                    $contents .= $name2 . $norcomma;
                }
            }

            $contents .= $pmain['newbill'] . ",";
            $contents .= $pmain['flatno'] . ",";
            $contents .= $memname . ",";
            $contents .= $totbill . ",";
            $contents .= $arrears . ",";
            $contents .= $prevint . ",";
            $contents .= $total . ",";
            $contents .= $intamt . ",";
            $pctr = 1;
            while ($compdtl = mysqli_fetch_array($getcompdtl)) {
                $maintype = $compdtl['maintype'];
                $getmemdtl = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE  `society`='$society' and `accode`='$pmain[accode]' and
                `billno`='$billno' and `billcode`='$billcode' and `maintcode`='$maintype' and `amount` != ''") or die(mysqli_error($conn));
                $memdtl = mysqli_fetch_array($getmemdtl);

                $amount = $memdtl['amount'];

                $contents .= $amount . "\n";
                $pctr++;
            }
            $ctr++;

            $totbillamt = $totbillamt + $totbill;
            $totarreas = $totarreas + $arrears;
            $totprevint = $totprevint + $prevint;
            $totintamt = $totintamt + $intamt;
            $totamt = $totamt + $total;
        }

        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= "Total" . ",";
        $contents .= $totbillamt . ",";
        $contents .= $totarreas . ",";
        $contents .= $totprevint . ",";
        $contents .= $totamt . ",";
        $contents .= $totintamt . "\n";

        $gmname = mysqli_query($conn, "SELECT maintype FROM $zroot.`soctran` WHERE  `cono`='$society' order by srno") or die(mysqli_error($conn));

        while ($gmtname = mysqli_fetch_array($gmname)) {
            $getmt = $gmtname['maintype'];

            $gamt = mysqli_query($conn, "SELECT SUM(amount) as amt from billtran where maintcode = '$getmt' and `billcode`='$billcode'
                                                        and `society` = '$society'  ") or die(mysqli_error($conn));
            $totmamt = mysqli_fetch_array($gamt);

            $contents .= $totmamt[amt] . "\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;
    }
}

if (isset($_POST["subpdf"])) {
    $billcode = $_POST["billcode"];


    $member = $_POST["member"];
    $date = $_POST["date"];

    $memcond = '';
    $billcond = '';

    if (!empty($member)) {

        $memcond = "and `accode` = '$member'";
    }

    if (!empty($billcode)) {
        $billcond = "and `billcode` = '$billcode'";
    }

    $dtype = $_POST["dtype"];

    $getdata = mysqli_query($conn, "SELECT *, DATE_FORMAT(`billdate`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `society`='$society'
                           $billcond $memcond ORDER BY society,billno") or die(mysqli_error($conn));



    require("library/fpdf/fpdf.php");

    class pdf extends fpdf
    {

        function Header()
        {
        }

        function Footer()
        {

            //Position at 1.5 cm from bottom
            $this->SetY(-15);
            //Arial italic 8
            $this->SetFont('Arial', 'I', 8);
            //Page number
            $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }
    }

    $pdf = new pdf('L', 'mm', 'A4');

    $pdf->AliasNbPages();
    $pdf->AddPage();
    $pdf->SetFont('Arial', '', 12);
    $pdf->SetFillColor(210, 204, 204);
    $pdf->SetTextColor(000, 000, 000);
    if ($dtype == 1) {
        $rtype = " - Summary";
    } elseif ($dtype == 2) {
        $rtype = " - Detail";
    } elseif ($dtype == '') {
        $rtype = '';
    }
    if ($mfdt != '') {
        $mfdt = "From Date - $mfdt";
    }
    if ($mtdt != '') {
        $mtdt = "To Date - $mtdt";
    }
    $pdf->MultiCell(273, 7, "$comp[compname]\n$comp[address]\n$formname $rtype " . "                                                                                                   " . "Report Date-" . Date("d-m-Y") . "\n" . $mfdt . "          " . $mtdt, Alignment . LEFT, false, 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    //    $pdf->SetMargins(10, 10, 10);
    //    $pdf->SetXY(2, 2);

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetTextColor(000, 000, 000);
    $pdf->SetFillColor(239, 228, 176);
    $pdf->Cell(7, 8, 'Sr.', 1, 0, '', 1);
    $pdf->Cell(35, 8, 'Bill No', 1, 0, '', 1);
    $pdf->Cell(25, 8, 'Date', 1, 0, '', 1);
    $pdf->Cell(80, 8, 'Member', 1, 0, '', 1);
    $pdf->Cell(30, 8, 'Billing Period', 1, 0, '', 1);
    $pdf->Cell(24, 8, 'Amount', 1, 0, '', 1);
    $pdf->Cell(23, 8, 'Arrears', 1, 0, '', 1);
    $pdf->Cell(23, 8, 'Advanced', 1, 0, '', 1);
    $pdf->Cell(25, 8, 'Bill Amount', 1, 0, '', 1);

    $pdf->SetFont('Arial', '', 10);
    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();
    $totamt = 0;
    $ctr = 0;
    while ($pmain = mysqli_fetch_array($getdata)) {


        $gmem = mysqli_query($conn, "SELECT * FROM `accmast` where `active`='1' and `accode` = '$pmain[accode]' AND grpcode='010' and `series`='$society'  ") or die(mysqli_error($conn));
        $smem = mysqli_fetch_array($gmem);


        $openbal = $smem['openbal'];
        $dc = $smem['dc'];
        $member = $smem['accode'];
        $flatno = $smem['flatno'];

        if ($dc == 'D') {
            $mopen = abs($openbal) * -1;
        } else {
            $mopen = abs($openbal);
        }

        $bcode = mysqli_query($conn, "SELECT `code`,`descrip` FROM $zroot.`billingcycle` where `code` = '$pmain[billcode]' and `billcycle`='$billcycle' ") or die(mysqli_error($conn));
        $scode = mysqli_fetch_array($bcode);
        $ctr++;
        $pdf->SetFont('Arial', '', 9);

        $pdf->Cell(7, 8, "$ctr", 1, 0);
        $pdf->Cell(35, 8, "$pmain[billno]", 1, 0);
        $pdf->Cell(25, 8, "$pmain[mydt]", 1, 0);
        $pdf->Cell(80, 8, "$smem[name]", 1, 0);
        $pdf->Cell(30, 8, "$scode[descrip]", 1, 0);
        $pdf->Cell(24, 8, "$pmain[billamt]", 1, 0);
        $pdf->Cell(23, 8, "$pmain[arrears]", 1, 0);
        $pdf->Cell(23, 8, "$advamt", 1, 0);
        $pdf->Cell(25, 8, "$pmain[total]", 1, 0);


        $totbal = $totbal + $pax[amount];

        $pdf->Ln();
    }

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetTextColor(255, 0, 0);
    $pdf->Cell(247, 8, 'Grand Total', 'B,L');
    $pdf->Cell(25, 8, $totamt, 'B,R', 0, 'R');
    $pdf->Output("Bill Register.pdf", "D");
}

//if (isset($_POST["excel"])) {
//
//    $billcode = $_POST["billcode"];
//
//
//    $member = $_POST["member"];
//    $date = $_POST["date"];
//
//    $memcond = '';
//    $billcond = '';
//
//    if (!empty($member)) {
//
//        $memcond = "and `accode` = '$member'";
//    }
//
//    if (!empty($billcode)) {
//        $billcond = "and `billcode` = '$billcode'";
//    }
//
//    $dtype = $_POST["dtype"];
//
//    $getdt = mysqli_query($conn, "SELECT billcycle, billcode, DATE_FORMAT(`billdate`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `society`='$society'
//                           $billcond $memcond") or die(mysqli_error($conn));
//    $dmain = mysqli_fetch_array($getdt);
//
//    $bcode = mysqli_query($conn, "SELECT `code`,`descrip`,`billcycle` FROM $zroot.`billingcycle` where `code` = '$dmain[billcode]' and `billcycle`='$dmain[billcycle]' ") or die(mysqli_error($conn));
//    $scode = mysqli_fetch_array($bcode);
//
//
//    $getdata = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `society`='$society'
//                           $billcond $memcond ORDER BY newbill") or die(mysqli_error($conn));
//
//    $getsname = mysqli_query($conn, "SELECT maintype,srno FROM $zroot.`soctran` WHERE  `cono`='$society' order by srno") or die(mysqli_error($conn));
//
//    if ($dtype == 1) {
//
//        $contents .= "Bill No, Flat No, Member, Amount, Arrears, Int.Arrears, Advanced, Bill Amount \n";
//        $ctr = 0;
//        $totamt = $totbilamt = $totarrears = $totintamt = $totadvance = 0;
//        while ($pmain = mysqli_fetch_array($getdata)) {
//            $intamt = $pmain[intamt];
//            $totbill = $pmain[billamt] + $intamt;
//            $total = $pmain[total] + $intamt;
//
//            $gmem = mysqli_query($conn, "SELECT * FROM `accmast` where `accode` = '$pmain[accode]' AND grpcode='010' and `series`='$society'") or die(mysqli_error($conn));
//            $smem = mysqli_fetch_array($gmem);
//
//            $openbal = $smem['openbal'];
//            $dc = $smem['dc'];
//            $member = $smem['accode'];
//            $flatno = $smem['flatno'];
//
//            if ($dc == 'D') {
//                $mopen = abs($openbal) * -1;
//            } else {
//                $mopen = abs($openbal);
//            }
//
//            $ctr++;
//            $contents.= $pmain[newbill] . ",";
//            $contents.= $pmain[flatno] . ",";
//            $contents.= $smem[name] . ",";
//            $contents.= $totbill . ",";
//            $contents.= $pmain[arrears] . ",";
//            $contents.= $pmain[intamt] . ",";
//            $contents.= $pmain[advance] . ",";
//            $contents.= $total . "\n";
//
//            $totbilamt = $totbilamt + $totbill;
//            $totarrears = $totarrears + $pmain[arrears];
//            $totintamt = $totintamt + $pmain[intamt];
//            $totadvance = $totadvance + $pmain[advance];
//            $totamt = $totamt + $total;
//        }
//
//        $contents.= "" . ",";
//        $contents.= "" . ",";
//        $contents.= "Total" . ",";
//        $contents.= $totbilamt . ",";
//        $contents.= $totarrears . ",";
//        $contents.= $totintamt . ",";
//        $contents.= $totadvance . ",";
//        $contents.= $totamt . "\n";
//    } else {
//
//        $ctr = 1;
//        $totamt = $totbilamt = $totarrears = $totintamt = $totadvance = 0;
//        while ($pmain = mysqli_fetch_array($getdata)) {
//            $billno = $pmain[billno];
//            $billcode = $pmain['billcode'];
//            $billcycle = $pmain['billcycle'];
//            $prevint = $pmain['intprev'];
//            $arrears = $pmain['arrears'];
//            $intamt = $pmain['intamt'];
//            $total = $pmain['total'] + $intamt;
//            $billamt = $pmain['billamt'];
//            $totbill = $billamt + $intamt;
//            $advance = $pmain['advance'];
//
//            $getname = mysqli_query($conn, "SELECT name FROM `accmast` WHERE  `series`='$society' and `accode`='$pmain[accode]'") or die(mysqli_error($conn));
//            $fetname = mysqli_fetch_array($getname);
//            $memname = $fetname['name'];
//
//
//            $getcompdtl = mysqli_query($conn, "SELECT * FROM $zroot.`soctran` WHERE  `cono`='$society' order by srno") or die(mysqli_error($conn));
//            if ($ctr == 1) {
//                $contents .= "Bill No, Flat No, MEMBER - NAME, Bill Amt, Arrears, Prev Int, Total, Current INT \n";
//                while ($fsname = mysqli_fetch_array($getsname)) {
//                    $maintype = $fsname['maintype'];
//
//                    $getmname = mysqli_query($conn, "SELECT `accode`,`shortname` FROM `accmast` WHERE `accode`='$maintype' and
//                                                                    `grpcode`='011' and `series`='$society' ") or die(mysqli_error($conn));
//                    $mname = mysqli_fetch_array($getmname);
//                    $name = $mname['shortname'];
//                    $contents.= $name . "\n";
//                }
//            }
//
//            $contents.= $pmain[newbill] . ",";
//            $contents.= $pmain[flatno] . ",";
//            $contents.= $memname . ",";
//            $contents.= $totbill . ",";
//            $contents.= $arrears . ",";
//            $contents.= $prevint . ",";
//            $contents.= $total . ",";
//            $contents.= $intamt . "\n";
//            $pctr = 1;
//            while ($compdtl = mysqli_fetch_array($getcompdtl)) {
//                $maintype = $compdtl['maintype'];
//                $getmemdtl = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE  `society`='$society' and `accode`='$pmain[accode]' and
//                                                                 `billno`='$billno' and `billcode`='$billcode' and `maintcode`='$maintype'") or die(mysqli_error($conn));
//                $memdtl = mysqli_fetch_array($getmemdtl);
//
//                $amount = $memdtl['amount'];
//
//                $contents.= $amount . "\n";
//                $pctr++;
//            }
//            $ctr++;
//            $totbillamt = $totbillamt + $totbill;
//            $totarreas = $totarreas + $arrears;
//            $totprevint = $totprevint + $prevint;
//            $totintamt = $totintamt + $intamt;
//            $totamt = $totamt + $total;
//        }
//
//
//        $contents.= "Total" . ",";
//        $contents.= $totbillamt . ",";
//        $contents.= $totarreas . ",";
//        $contents.= $totprevint . ",";
//        $contents.= $totadvance . ",";
//        $contents.= $totamt . ",";
//        $contents.= $totintamt . "\n";
//
//        $gmname = mysqli_query($conn, "SELECT maintype FROM $zroot.`soctran` WHERE  `cono`='$society' order by srno") or die(mysqli_error($conn));
//
//        while ($gmtname = mysqli_fetch_array($gmname)) {
//            $getmt = $gmtname['maintype'];
//
//            $gamt = mysqli_query($conn, "SELECT SUM(amount) as amt from billtran where maintcode = '$getmt' and `billcode`='$billcode'
//                                                        and `society` = '$society'  ") or die(mysqli_error($conn));
//            $totmamt = mysqli_fetch_array($gamt);
//
//            $contents.=$totmamt[amt] . "\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;
//        }
//    }
//}
?>