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

$formname = "Daily Report";
$file = "dailyrep_fr.php";

$trandb = "actran";
$hallbookdb = "hallbook";
$subscripdb = "subscription";
$donationdb = "donationrecvd";
$rentitemdb = "rentmain";
$lodgingdb = "hotmain";
$sagaidb = "sagai";
$lagandb = "lagan";
$crfile = "cashrecp";
$cpfile = "cashpay";
$brfile = "bankrecp";
$bpfile = "bankpay";
$othrecpdb = "othrecp";
$othpaydb = "othpay";
$zroot = $_SESSION["zdir"];

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

$fseries = $_SESSION["series_" . $dbname];
$seriescond = " AND series = '$fseries'";
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

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

    $date = $_POST["date"];


    $getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` where code='$zcomp'")or die(mysqli_error($conn));
    $comp = mysqli_fetch_array($getcomp);
    
    
    $qryac = mysqli_query($conn, "SELECT * FROM `accmast` where accode = 3 ")or die(mysqli_error($conn));
    $getac = mysqli_fetch_array($qryac);
                                
    $mopbal = $getac["openbal"];
    $mopdc = $getac["dc"];

    if ($mopdc == "D") {
        $mopbal = $mopbal * - 1;
    }
    
/////////////////////// DAILY DATA START ///////////////////////////////


    $chall1 = mysqli_query($conn, "SELECT SUM(amount) AS amt FROM $hallbookdb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $cshall = mysqli_fetch_array($chall1);
    $hallamt = $cshall["amt"];

    $sdonation1 = mysqli_query($conn, "SELECT SUM(amount) AS amt FROM $donationdb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $donate = mysqli_fetch_array($sdonation1);
    $donateamt = $donate["amt"];

    $csubscrip1 = mysqli_query($conn, "SELECT SUM(subscription) AS amt FROM $subscripdb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $subscrip = mysqli_fetch_array($csubscrip1);
    $subscripamt = $subscrip["amt"];

    $rentitem1 = mysqli_query($conn, "SELECT SUM(totamt) AS amt FROM $rentitemdb WHERE date = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $rentit = mysqli_fetch_array($rentitem1);
    $rentitemamt = $rentit["amt"];

    $lodg1 = mysqli_query($conn, "SELECT SUM(totamt) AS amt FROM $lodgingdb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $lodging = mysqli_fetch_array($lodg1);
    $lodgingamt = $lodging["amt"];

    $sag1 = mysqli_query($conn, "SELECT SUM(totamt) AS amt FROM $sagaidb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $sagai = mysqli_fetch_array($sag1);
    $sagaiamt = $sagai["amt"];

    $lag1 = mysqli_query($conn, "SELECT SUM(totamt) AS amt FROM $lagandb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $lagan = mysqli_fetch_array($lag1);
    $laganamt = $lagan["amt"];

    $othrp1 = mysqli_query($conn, "SELECT SUM(amount) AS amt FROM $othrecpdb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $othrecp = mysqli_fetch_array($othrp1);
    $othrecpamt = $othrecp["amt"];

    $othpy1 = mysqli_query($conn, "SELECT SUM(amount) AS amt FROM $othpaydb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $othpay = mysqli_fetch_array($othpy1);
    $othpayamt = $othpay["amt"];



/////////////////////// DAILY DATA END ////////////////// 


//    browse($date);
//////////////////////////////////////////
//    $csale1 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $salefile WHERE vouchdt = '$date' AND `cashcred` = 'C' group by vouchdt") or die(mysqli_error($conn));
//    $cssale = mysqli_fetch_array($csale1);


      //
//    $cashsale = $cssale["amt"];
//    $csale2 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $salefile WHERE vouchdt = '$date' AND  `cashcred` != 'C' group by vouchdt") or die(mysqli_error($conn));
//    $crsale = mysqli_fetch_array($csale2);
//
//    $creditsale = $crsale["amt"];
//    
//    $totsale = $cashsale + $creditsale;

    // $cpur1 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $purchfile WHERE vouchdt = '$date' AND  `cashcred` = 'C' group by vouchdt") or die(mysqli_error($conn));
    // $cspur = mysqli_fetch_array($cpur1);

    // $cashpur = $cspur["amt"];

    // $cpur2 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $purchfile WHERE vouchdt = '$date' AND  `cashcred` != 'C' group by vouchdt") or die(mysqli_error($conn));
    // $cspur = mysqli_fetch_array($cpur2);

    // $creditpur = $cspur["amt"];
    // $totpur = $cashpur + $creditpur;

///////////////////////////////////////////// New Fildes cash sale, Credit Sale /////////////////////


    // $cashsale = $credale = $ccsale = $totsale = 0;

    // $csale1 = mysqli_query($conn, "SELECT SUM(cashamt) AS amt FROM $salefile WHERE vouchdt = '$date' ") or die(mysqli_error($conn));
    // $cssale = mysqli_fetch_array($csale1);
    // $cashsale = $cssale[amt];

    // $csale2 = mysqli_query($conn, "SELECT SUM(credamt) AS amt FROM $salefile WHERE vouchdt = '$date' ") or die(mysqli_error($conn));
    // $crsale = mysqli_fetch_array($csale2);
    // $creditsale = $crsale["amt"];

    // $csale3 = mysqli_query($conn, "SELECT SUM(ccamt) AS amt FROM $salefile WHERE vouchdt = '$date' ") or die(mysqli_error($conn));
    // $ccsale = mysqli_fetch_array($csale3);
    // $ccsale = $ccsale["amt"];

    // $totsale = $cashsale + $creditsale + $ccsale;


/////////////////////////////////////////////
    $crecp2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $crfile WHERE
                      vouchdt = '$date'  order by vouchno ") or die(mysqli_error($conn));

    $cpaid2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $cpfile WHERE
                      vouchdt = '$date' ") or die(mysqli_error($conn));

    $brecp2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $brfile WHERE
                       vouchdt = '$date' ") or die(mysqli_error($conn));

    $bpaid2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $bpfile WHERE
                       vouchdt = '$date' ") or die(mysqli_error($conn));

//////////////

    $crecp = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $crfile WHERE
                      vouchdt = '$date' group by vouchdt") or die(mysqli_error($conn));
    $crget = mysqli_fetch_array($crecp);

    $cramt = $crget["amt"];

    $cpaid = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `cashpay` WHERE
                      vouchdt = '$date' group by vouchdt") or die(mysqli_error($conn));
    $cpget = mysqli_fetch_array($cpaid);

    $cpamt = $cpget["amt"];

    $brecp = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankrecp` WHERE
                      vouchdt = '$date' group by vouchdt") or die(mysqli_error($conn));
    $brget = mysqli_fetch_array($brecp);

    $bramt = $brget["amt"];

    $bpaid = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankpay` WHERE
                      vouchdt = '$date' group by vouchdt") or die(mysqli_error($conn));
    $bpget = mysqli_fetch_array($bpaid);

    $bpamt = $bpget["amt"];

    $totrecp = $cramt + $bramt + $othrecpamt;
    $totpay = $cpamt + $bpamt + $othpayamt;

    include("includes/linkshead.php");
    ?>
    <style>
        .td{
            font-size: 15px;
        }
        @media print {
            .noPrint {
                display:none;
            }
        }

    </style>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 ">

                    <?php
                    $getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` where code='$zcomp'")or die(mysqli_error($conn));
                    $comp = mysqli_fetch_array($getcomp);
                    echo "<label style='font-size:20px;'>$comp[name]</label>"
                    ?>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-7 col-md-11 col-sm-11 col-xs-11 ">

                    <label class="label-control marg"><?php Echo $formname; ?> - </label>
                    <?= date("d-m-Y", strtotime($date)); ?>
                </div>
                <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                    <a onclick="window.close()" class="btn btn-primary noPrint">GO Back</a>
                </div>
            </div>
            <?php
//            include("includes/head_rep.php");
//            echo " ( " . date("d-m-Y", strtotime($date)) . ")";
            ?>
                                <!--<table class="table table-bordered" summary="Ledger - Detail" width='100%' border="0">-->
            <table style="width:70%;" class="table-bordered table table-stripped table-hover" summary="">
                <tbody>
                    <!-- <tr style ="height:10px;"> -->
                        <!-- <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Opening Balance</b></font></td> -->
                        <?php
                        
                                // $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE accode = 3 AND 
                                //                            `date` < '$date' and `retdate` = '0000-00-00'  ") or die(mysqli_error($conn));
                                // $getamt = mysqli_fetch_array($getbal);
                                // $amt = $getamt['amt'];

                                // $mobal = $mopbal + $amt;
                                
                                // $mobal = $mobal * -1 ;
                        
                        // echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$mobal</td>";
                        ?>
                        <!-- <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td> -->
                    <!-- </tr> -->
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Hall Booking</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$hallamt</td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Lodging</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$lodgingamt</td>";
                        ?>
                    </tr>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Item on rent</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$rentitemamt</td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Donations</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$donateamt</td>";
                        ?>
                    </tr>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Subscription</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$subscripamt</td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Sagai</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$sagaiamt</td>";
                        ?>
                    </tr>

                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Permission of date</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\"></td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Lagan</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$laganamt</td>";
                        ?>
                    </tr>
                </table>  

            <table style="width:70%;" class="table-bordered table table-stripped table-hover" summary="">
                <tbody>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Opening Balance</b></font></td>
                        <?php
                        
                                $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE accode = 3 AND 
                                                           `date` < '$date' and `retdate` = '0000-00-00'  ") or die(mysqli_error($conn));
                                $getamt = mysqli_fetch_array($getbal);
                                $amt = $getamt['amt'];

                                $mobal = $mopbal + $amt;
                                
                                $mobal = $mobal * -1 ;

                                //////////////////////////////
                        
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$mobal</td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                    </tr>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Cash Receipt</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$cramt</td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Cash Payment</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$cpamt</td>";
                        ?>
                    </tr>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Bank Receipt</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$bramt</td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Bank Payment</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$bpamt</td>";
                        ?>
                    </tr>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Other Receipt</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$othrecpamt</td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Other Payment</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$othpayamt</td>";
                        ?>
                    </tr>
                    
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Total Receipt</b></font></td>
                        <td class = 'td'   style="text-align:right;" scope="col" width="90"><?php echo number_format($totrecp,2); ?> </td>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Total Payment</b></font></td>
                        <td class = 'td'   style="text-align:right;" scope="col" width="90"><?php echo number_format($totpay,2); ?> </td>
                    </tr>

                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\"></td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Closing Balance</b></font></td>
                        <?php

                        $closingbal = $mobal + $totrecp - $totpay;
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">" . number_format($closingbal, 2) . "</td>";
                        ?>
                    </tr>
            </table>  
            
            <?php include("includes/linksfoot.php"); ?>
        </div>

    </body>
    </html>
    <?php
}

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


    $date = $_POST["date"];


    $getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` where code='$zcomp'")or die(mysqli_error($conn));
    $comp = mysqli_fetch_array($getcomp);
    
    
    $qryac = mysqli_query($conn, "SELECT * FROM `accmast` where accode = 3 ")or die(mysqli_error($conn));
    $getac = mysqli_fetch_array($qryac);
                                
    $mopbal = $getac["openbal"];
    $mopdc = $getac["dc"];

    if ($mopdc == "D") {
        $mopbal = $mopbal * - 1;
    }


    $chall1 = mysqli_query($conn, "SELECT SUM(amount) AS amt FROM $hallbookdb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $cshall = mysqli_fetch_array($chall1);
    $hallamt = $cshall["amt"];

    $sdonation1 = mysqli_query($conn, "SELECT SUM(amount) AS amt FROM $donationdb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $donate = mysqli_fetch_array($sdonation1);
    $donateamt = $donate["amt"];

    $csubscrip1 = mysqli_query($conn, "SELECT SUM(subscription) AS amt FROM $subscripdb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $subscrip = mysqli_fetch_array($csubscrip1);
    $subscripamt = $subscrip["amt"];

    $rentitem1 = mysqli_query($conn, "SELECT SUM(totamt) AS amt FROM $rentitemdb WHERE date = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $rentit = mysqli_fetch_array($rentitem1);
    $rentitemamt = $rentit["amt"];

    $lodg1 = mysqli_query($conn, "SELECT SUM(totamt) AS amt FROM $lodgingdb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $lodging = mysqli_fetch_array($lodg1);
    $lodgingamt = $lodging["amt"];

    $sag1 = mysqli_query($conn, "SELECT SUM(totamt) AS amt FROM $sagaidb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $sagai = mysqli_fetch_array($sag1);
    $sagaiamt = $sagai["amt"];

    $lag1 = mysqli_query($conn, "SELECT SUM(totamt) AS amt FROM $lagandb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $lagan = mysqli_fetch_array($lag1);
    $laganamt = $lagan["amt"];

    $othrp1 = mysqli_query($conn, "SELECT SUM(amount) AS amt FROM $othrecpdb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $othrecp = mysqli_fetch_array($othrp1);
    $othrecpamt = $othrecp["amt"];

    $othpy1 = mysqli_query($conn, "SELECT SUM(amount) AS amt FROM $othpaydb WHERE vouchdt = '$date' $seriescond group by vouchdt") or die(mysqli_error($conn));
    $othpay = mysqli_fetch_array($othpy1);
    $othpayamt = $othpay["amt"];


    $crecp2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $crfile WHERE
                      vouchdt = '$date'  order by vouchno ") or die(mysqli_error($conn));

    $cpaid2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $cpfile WHERE
                      vouchdt = '$date' ") or die(mysqli_error($conn));

    $brecp2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $brfile WHERE
                       vouchdt = '$date' ") or die(mysqli_error($conn));

    $bpaid2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $bpfile WHERE
                       vouchdt = '$date' ") or die(mysqli_error($conn));


    $crecp = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $crfile WHERE
                      vouchdt = '$date' group by vouchdt") or die(mysqli_error($conn));
    $crget = mysqli_fetch_array($crecp);

    $cramt = $crget["amt"];

    $cpaid = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `cashpay` WHERE
                      vouchdt = '$date' group by vouchdt") or die(mysqli_error($conn));
    $cpget = mysqli_fetch_array($cpaid);

    $cpamt = $cpget["amt"];

    $brecp = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankrecp` WHERE
                      vouchdt = '$date' group by vouchdt") or die(mysqli_error($conn));
    $brget = mysqli_fetch_array($brecp);

    $bramt = $brget["amt"];

    $bpaid = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankpay` WHERE
                      vouchdt = '$date' group by vouchdt") or die(mysqli_error($conn));
    $bpget = mysqli_fetch_array($bpaid);

    $bpamt = $bpget["amt"];

    $totrecp = $cramt + $bramt + $othrecpamt;
    $totpay = $cpamt + $bpamt + $othpayamt;

                       $getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` where code='$zcomp'")or die(mysqli_error($conn));
                    $comp = mysqli_fetch_array($getcomp);

            
                    $contents .="Hall Booking" . ",";
                    $contents .="$hallamt" . ",";
                    $contents .="" . ",";
                    $contents .="Lodging" . ",";
                    $contents .="$lodgingamt" . "\n";


                   
                   $contents .="Item on rent" . ",";
                   $contents .="$rentitemamt" . ",";
                   $contents .="" . ",";
                   $contents .="Donations" . ",";
                   $contents .="$donateamt" . "\n";


                   $contents .="Subscription" . ",";
                   $contents .="$subscripamt" . ",";
                   $contents .="" . ",";
                   $contents .="Sagai" . ",";
                   $contents .="$sagaiamt" . "\n";

                
                    
                    $contents .="Permission of date" . ",";
                    $contents .="" . ",";
                    $contents .="" . ",";
                    $contents .="Lagan" . ",";
                    $contents .="$laganamt" . "\n";

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

                        // openning balance //
                               $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE accode = 3 AND 
                                                           `date` < '$date' and `retdate` = '0000-00-00'  ") or die(mysqli_error($conn));
                                $getamt = mysqli_fetch_array($getbal);
                                $amt = $getamt['amt'];

                                $mobal = $mopbal + $amt;
                                
                                $mobal = $mobal * -1 ;

                                $contents .="Opening Balance" . ",";
                                
                                $contents .="$mobal" . "\n";
                    
                                                           

                                $contents .="Cash Receipt" . ",";
                                $contents .="$cramt" . ",";
                                $contents .="" . ",";
                                $contents .="Cash Payment" . ",";
                                $contents .="$cpamt" . "\n";
                 

                                $contents .="Bank Receipt" . ",";
                                $contents .="$bramt" .",";
                                $contents .="" . ",";
                                $contents .="Bank Payment" . ",";
                                $contents .="$bpamt" . "\n";
                   
                                
                                $contents .="Other Receipt" . ",";
                                $contents .="$othrecpamt" . ",";
                                $contents .="" . ",";
                                $contents .="Other Payment" . ",";
                                $contents .="$othpayamt" . "\n";

                               
                                $contents .="Total Receipt" . ",";
                                $contents .="$totrecp" . ",";
                                $contents .="" . ",";
                                $contents .="Total Payment" . ",";
                                $contents .="$totpay" . "\n";

                    
                                $contents .="" . ",";
                                $contents .="" . ",";
                                $contents .="" . ",";
                             
                                 
                                 
                        $closingbal = $mobal + $totrecp - $totpay;
                        $contents .="Closing Balance" . ",";
                        $contents .="$closingbal" . "\n";  
                        

    $date = $_POST["date"];
   
    $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;
    }
}
?>