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

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

mysqli_select_db($conn, $zdb);

$repname = "Daily Report";
$mfrmdate = $_POST["vouchdt"];
$prntype = $_POST["dtype"];

    $salefile = "salemain";
    $purchfile = "purmain";
    $crfile = "cashrecp";
    $cpfile = "cashpay";
    $brfile = "bankrecp";
    $bpfile = "bankpay";

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

    $csale1 = mysqli_query($conn, "SELECT SUM(billamt) AS amt FROM $salefile WHERE vouchdt = '$mfrmdate' 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(billamt) AS amt FROM $salefile WHERE vouchdt = '$mfrmdate' 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(billamt) AS amt FROM $purchfile WHERE vouchdt = '$mfrmdate' 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(billamt) AS amt FROM $purchfile WHERE vouchdt = '$mfrmdate' AND cashcred != 'C' group by vouchdt") or die(mysqli_error($conn));
    $cspur = mysqli_fetch_array($cpur2);

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


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

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

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

    $bpaid2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $bpfile WHERE
                       vouchdt = '$mfrmdate' ") 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 = '$mfrmdate' 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 = '$mfrmdate' 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 = '$mfrmdate' 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 = '$mfrmdate' group by vouchdt") or die(mysqli_error($conn));
    $bpget = mysqli_fetch_array($bpaid);

    $bpamt = $bpget["amt"];
    include("includes/files.php");
    ?>

<!--    <!DOCTYPE HTML>

    <html lang="en-US">
        <head>
            <meta charset="UTF-8">
            <title><?= $formname ?></title>
            <meta name="msapplication-TileColor" content="#5bc0de"/>
            <meta name="msapplication-TileImage" content="assets/img/metis-tile.png"/>
            <link rel="stylesheet" href="assets/lib/bootstrap/css/bootstrap.css">
            <link rel="stylesheet" href="assets/css/main.css"/>
            <link rel="stylesheet" href="assets/lib/Font-Awesome/css/font-awesome.css"/>
            <link rel="stylesheet" href="assets/css/theme.css">
            <link rel="stylesheet" href="assets/lib/uniform/themes/default/css/uniform.default.css">
            <link rel="stylesheet" href="assets/lib/inputlimiter/jquery.inputlimiter.1.0.css">
            <link rel="stylesheet" href="assets/lib/chosen/chosen.min.css">
            <link rel="stylesheet" href="assets/lib/colorpicker/css/colorpicker.css">
            <link rel="stylesheet" href="assets/lib/tagsinput/jquery.tagsinput.css">
            <link rel="stylesheet" href="assets/lib/daterangepicker/daterangepicker-bs3.css">
            <link rel="stylesheet" href="assets/lib/datepicker/css/datepicker.css">
            <link rel="stylesheet" href="assets/lib/timepicker/css/bootstrap-timepicker.min.css">
            <link rel="stylesheet" href="assets/lib/switch/static/stylesheets/bootstrap-switch.css">
            <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/flick/jquery-ui.css">
            <script>

                (function (i, s, o, g, r, a, m) {
                    i['GoogleAnalyticsObject'] = r;
                    i[r] = i[r] || function () {
                        (i[r].q = i[r].q || []).push(arguments)
                    }, i[r].l = 1 * new Date();
                    a = s.createElement(o),
                            m = s.getElementsByTagName(o)[0];
                    a.async = 1;
                    a.src = g;
                    m.parentNode.insertBefore(a, m)
                })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
                ga('create', 'UA-1669764-16', 'onokumus.com');
                ga('send', 'pageview');

            </script>
            <script type="text/javascript" src="js/jquery.js"></script>
            <script type="text/javascript">

                $(document).keydown(function (event) {
                    //19 for Mac Command+S
                    if (!(String.fromCharCode(event.which).toLowerCase() == 'v' && event.ctrlKey) && !(event.which == 19))
                        return true;
                    $("#view").click();
                    event.preventDefault();
                    return false;
                });

            </script>        
            <script src="assets/lib/modernizr-build.min.js"></script>
            <script type="text/javascript" src="../js/jquery.js"></script>-->
        <body>
            <?php
            require("includes/dbconnect.php");
            $comp = mysqli_query($conn, "SELECT * FROM `company`") or die(mysql_errno());
            $zcomp = mysqli_fetch_assoc($comp);

            echo "<font size='4px' color=''><b>$zcomp[compname]</font></b></font><br />";
            echo "<font size='2px' color=''><b> $zcomp[add1]. $zcomp[add2].  $zcomp[add3] </b></font><br />";
            echo "<b><font color=''>$repname &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </font></b>";
            echo " ( " . date("d-m-Y", strtotime($mfrmdate)) . ")";
//        echo "From Date - " . date("d-m-Y", strtotime($mfrmdate)) . "<br/>To Date &nbsp;&nbsp;&nbsp;&nbsp;-  " . date("d-m-Y", strtotime($mtodate)) . "<br/>";
            
            ?>
            <!--<table class="table table-bordered" summary="Ledger - Detail" width='100%' border="0">-->
                <table style="width:100%;" class="table-bordered" summary="Ledger - Detail" width='100%' border="0">
                <tbody>
                    <tr style ="height:10px;">
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Cash Sale</b></font></td>
                        <?php
                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"90\">$cashsale</td>";
                        ?>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Cash Purchase</b></font></td>
                        <?php
                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"90\">$cashpur</td>";
                        ?>
                    </tr>
                    <tr style ="height:10px;">
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Credit Sale</b></font></td>
                        <?php
                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"90\">$creditsale</td>";
                        ?>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Credit Purchase</b></font></td>
                        <?php
                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"90\">$creditpur</td>";
                        ?>
                    </tr>
                    <tr style ="height:10px;">
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Total Sale</b></font></td>
                        <?php
                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"90\">" . number_format($totsale, 2) . "</td>";
                        ?>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Total Purchase</b></font></td>
                        <?php
                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"90\">" . number_format($totpur, 2) . "</td>";
                        ?>
                    </tr>
            </table>  
            <!--<table class="table table-bordered" summary="Ledger - Detail" width='100%' border="0">-->
            <table style="width:100%;" class="table-bordered" summary="Ledger - Detail" width='100%' border="0">    
                <?php if ($prntype == 2) { ?>
                <tr style="height:10px;">
                        <td style="text-align:left;" scope="col" width="10%"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="40%"><font size="3px" color=''><b>Cash Receipt</b></font></td>
                        <td style="text-align:left;" scope="col" width="12%"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="12%"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="12%"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="14%"><font size="3px" color=''><b></b></font></td>
                    </tr>
                    <tr style="height:10px;">
                        <td align="left"  color=''>Voucher No.</td>
                        <td align="left"  color=''>A/c. Name.</td>
                        <td align="right" color=''>Recd. Amount</td>
                        <td align="right" color=''>Discount</td>
                        <td align="right" color=''>Claim</td>
                        <td align="right" color=''>Amount</td>
                    </tr>

                    <?php
                    $totrecd = 0;
                    $totdisc = 0;
                    $totclaim = 0;
                    $totamt = 0;

                    mysqli_select_db($conn, $zdb);
                    while ($row = mysqli_fetch_array($crecp2)) {
                        $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row[accode]'") or die(mysqli_error($conn));
                        $name = mysqli_fetch_array($getname);
                        echo "<tr style='height:10px;'>";
                        echo "<td align='left' color=''>$row[vouchno]</td>";
                        echo "<td align='left' color=''>$name[name]</td>";
                        echo "<td align='right' color=''>$row[recdamt]</td>";
                        echo "<td align='right' color=''>$row[discount]</td>";
                        echo "<td align='right' color=''>$row[claim]</td>";
                        echo "<td align='right' color=''>$row[amount]</td>";
                        echo "</tr>";

                        $totrecd = $totrecd + $row[recdamt];
                        $totdisc = $totdisc + $row[discount];
                        $totclaim = $totclaim + $row[claim];
                        $totamt = $totamt + $row[amount];
                    }
                    ?>
                    <tr>
                        <td colspan="1">
                            <?php
                            echo "<td align='right' color=''>Total</td>";
                            echo "<td align='right' color=''>" . number_format($totrecd, 2) . "</td>";
                            echo "<td align='right' color=''>" . number_format($totdisc, 2) . "</td>";
                            echo "<td align='right' color=''>" . number_format($totclaim, 2) . "</td>";
                            echo "<td align='right' color=''>" . number_format($totamt, 2) . "</td>";
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="9">
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align:left;" scope="col" width="10"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Cash Payment</b></font></td>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></b></font></td>
                    </tr>
                    <tr>
                        <td align="left" color=''>Voucher No.</td>
                        <td align="left" color=''>A/c. Name.</td>
                        <td align="right" color=''>Recd. Amount</td>
                        <td align="right" color=''>Discount</td>
                        <td align="right" color=''>Claim</td>
                        <td align="right" color=''>Amount</td>
                    </tr>
                    <?php
                    $totrecd = 0;
                    $totdisc = 0;
                    $totclaim = 0;
                    $totamt = 0;
                    while ($row2 = mysqli_fetch_array($cpaid2)) {

                        $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row2[accode]'") or die(mysqli_error($conn));
                        $name = mysqli_fetch_array($getname);

                        echo "<tr>";
                        echo "<td align='left' color=''>$row2[vouchno]</td>";
                        echo "<td align='left' color=''>$name[name]</td>";
                        echo "<td align='right' color=''>$row2[recdamt]</td>";
                        echo "<td align='right' color=''>$row2[discount]</td>";
                        echo "<td align='right' color=''>$row2[claim]</td>";
                        echo "<td align='right' color=''>$row2[amount]</td>";
                        echo "</tr>";

                        $totrecd = $totrecd + $row2[recdamt];
                        $totdisc = $totdisc + $row2[discount];
                        $totclaim = $totclaim + $row2[claim];
                        $totamt = $totamt + $row2[amount];
                    }
                    ?>
                    <tr>
                        <td colspan="1">
                            <?php
                            echo "<td align='right' color=''>Total</td>";
                            echo "<td align='right' color=''>" . number_format($totrecd, 2) . "</td>";
                            echo "<td align='right' color=''>" . number_format($totdisc, 2) . "</td>";
                            echo "<td align='right' color=''>" . number_format($totclaim, 2) . "</td>";
                            echo "<td align='right' color=''>" . number_format($totamt, 2) . "</td>";
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="9">
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align:left;" scope="col" width="10"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Bank Receipt</b></font></td>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></b></font></td>
                    </tr>
                    <tr>
                        <td align="left"  color=''>Voucher No.</td>
                        <td align="left"  color=''>A/c Name.</td>
                        <td align="right" color=''>Recd. Amount</td>
                        <td align="right" color=''>Discount</td>
                        <td align="right" color=''>Claim</td>
                        <td align="right" color=''>Amount</td>
                    </tr>
                    <?php
                    /*                     * for bankrecp* */
                    $totrecd = 0;
                    $totdisc = 0;
                    $totclaim = 0;
                    $totamt = 0;

                    while ($row3 = mysqli_fetch_array($brecp2)) {

                        $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row3[accode]'") or die(mysqli_error($conn));
                        $name = mysqli_fetch_array($getname);
                        echo "<tr>";
                        echo "<td align='left' color=''>$row3[vouchno]</td>";
                        echo "<td align='left' color=''>$name[name]</td>";
                        echo "<td align='right' color=''>$row3[recdamt]</td>";
                        echo "<td align='right' color=''>$row3[discount]</td>";
                        echo "<td align='right' color=''>$row3[claim]</td>";
                        echo "<td align='right' color=''>$row3[amount]</td>";
                        echo "</tr>";

                        $totrecd = $totrecd + $row3[recdamt];
                        $totdisc = $totdisc + $row3[discount];
                        $totclaim = $totclaim + $row3[claim];
                        $totamt = $totamt + $row3[amount];
                    }
                    ?>
                    <tr>
                        <td colspan="1">
                            <?php
                            echo "<td align='right' color=''>Total</td>";
                            echo "<td align='right' color=''>" . number_format($totrecd, 2) . "</td>";
                            echo "<td align='right' color=''>" . number_format($totdisc, 2) . "</td>";
                            echo "<td align='right' color=''>" . number_format($totclaim, 2) . "</td>";
                            echo "<td align='right' color=''>" . number_format($totamt, 2) . "</td>";
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="9">
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align:left;" scope="col" width="10"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Bank Payment</b></font></td>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></b></font></td>
                        <td style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></b></font></td>
                    </tr>
                    <tr>
                        <td align="left" color=''>Voucher No.</td>
                        <td align="left" color=''>A/c Name.</td>
                        <td align="right" color=''>Recd. Amount</td>
                        <td align="right" color=''>Discount</td>
                        <td align="right" color=''>Claim</td>
                        <td align="right" color=''>Amount</td>
                    </tr>
                    <?php
                    /** for bankpay* */
//                    $bpaid2 = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankpay` WHERE
//                                                 vouchdt = '$mfrmdate' group by vouchdt") or die(mysqli_error($conn));
                    $totrecd = 0;
                    $totdisc = 0;
                    $totclaim = 0;
                    $totamt = 0;
                    while ($row4 = mysqli_fetch_array($bpaid2)) {

                        $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row4[accode]'") or die(mysqli_error($conn));
                        $name = mysqli_fetch_array($getname);
                        echo "<tr>";
                        echo "<td align='left' color=''>$row4[vouchno]</td>";
                        echo "<td align='left' color=''>$name[name]</td>";
                        echo "<td align='right' color=''>$row4[recdamt]</td>";
                        echo "<td align='right' color=''>$row4[discount]</td>";
                        echo "<td align='right' color=''>$row4[claim]</td>";
                        echo "<td align='right' color=''>$row4[amount]</td>";
                        echo "</tr>";

                        $totrecd = $totrecd + $row4[recdamt];
                        $totdisc = $totdisc + $row4[discount];
                        $totclaim = $totclaim + $row4[claim];
                        $totamt = $totamt + $row4[amount];
                    }
                    ?>
                    <tr>
                        <td colspan="1">
                            <?php
                            echo "<td align='right' color=''>Total</td>";
                            echo "<td align='right' color=''>" . number_format($totrecd, 2) . "</td>";
                            echo "<td align='right' color=''>" . number_format($totdisc, 2) . "</td>";
                            echo "<td align='right' color=''>" . number_format($totclaim, 2) . "</td>";
                            echo "<td align='right' color=''>" . number_format($totamt, 2) . "</td>";
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="9">
                        </td>
                    </tr>
                <?php } ?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="9">
                        <hr style=""/>
                    </td>
                </tr>
            </tfoot>
        </table>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
        <script>window.jQuery || document.write('<script src="assets/lib/jquery.min.js"><\/script>')</script> 
        <script src="assets/lib/bootstrap/js/bootstrap.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
        <script src="assets/lib/datatables/jquery.dataTables.js"></script>
        <script src="assets/lib/datatables/DT_bootstrap.js"></script>
        <script src="assets/lib/tablesorter/js/jquery.tablesorter.min.js"></script>
        <script src="assets/lib/touch-punch/jquery.ui.touch-punch.min.js"></script>
        <script src="assets/js/main.js"></script>
        <script>

                $(function () {
                    metisTable();
                    metisSortable();
                });

        </script>
    </body>
    </html>
    <?php
}
if (isset($_POST["export"])) {

    $csale1 = mysqli_query($conn, "SELECT SUM(billamt) AS amt FROM $salefile WHERE vouchdt = '$mfrmdate' 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(billamt) AS amt FROM $salefile WHERE vouchdt = '$mfrmdate' 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(billamt) AS amt FROM $purchfile WHERE vouchdt = '$mfrmdate' 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(billamt) AS amt FROM $purchfile WHERE vouchdt = '$mfrmdate' AND cashcred != 'C' group by vouchdt") or die(mysqli_error($conn));
    $cspur = mysqli_fetch_array($cpur2);

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


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

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

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

    $bpaid2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $bpfile WHERE
                       vouchdt = '$mfrmdate' ") 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 = '$mfrmdate' 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 = '$mfrmdate' 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 = '$mfrmdate' 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 = '$mfrmdate' group by vouchdt") or die(mysqli_error($conn));
    $bpget = mysqli_fetch_array($bpaid);

    $bpamt = $bpget["amt"];


    $contents = "Cash Sale,Cash Purchase,Credit Sale,Credit Purchase,Total Sale,Total Purchase\n";

    $contents.=$cashsale . ",";
    $contents.=$cashpur . ",";
    $contents.=$creditsale . ",";
    $contents.=$creditpur . ",";
    $contents.=$totsale . ",";
    $contents.=$totpur . "\n";

    $totamt = $totamt + $row->subtotal;
    $billamt = $billamt + $row->netamt;

    if ($_POST["dtype"] == 2) {

        $style = "calibri";
        $size = '2px';

        $contents = " ,Cash Receipt, , , , \n";
        $contents = "Voucher No.,A/c. Name,Recd. Amount,Discount,Claim,Amount\n";


        $totrecd = 0;
        $totdisc = 0;
        $totclaim = 0;
        $totamt = 0;

        while ($row = mysqli_fetch_array($crecp2)) {

            $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row3[accode]'") or die(mysqli_error($conn));
            $name = mysqli_fetch_array($getname);

            $ctrdtl++;

            $vouch = $row->vouchno;
            $part = $name->name;
            $recd = $row->recdamt;
            $disc = $row->discount;
            $claim = $row->claim;
            $amt = $row->amount;

            $totrecd = $totrecd + $row[recdamt];
            $totdisc = $totdisc + $row[discount];
            $totclaim = $totclaim + $row[claim];
            $totamt = $totamt + $row[amount];



            $contents.=$vouch . ",";
            $contents.=$part . ",";
            $contents.=$recd . ",";
            $contents.=$disc . ",";
            $contents.=$claim . ",";
            $contents.=$amt . "\n";
        }
        $contents.="Total" . ",";
        $contents.=number_format($totrecd, 2) . ",";
        $contents.=number_format($totdisc, 2) . ",";
        $contents.=number_format($totclaim, 2) . ",";
        $contents.=number_format($totamt, 2) . "\n";


        $contents.=",Cash Payment, , , , \n";
        $contents.="Voucher No.,A/c. Name,Recd. Amount ,Discount ,Claim,Amount \n";

        $totrecd = 0;
        $totdisc = 0;
        $totclaim = 0;
        $totamt = 0;
        while ($row2 = mysqli_fetch_array($cpaid2)) {

            $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row2[accode]'") or die(mysqli_error($conn));
            $name = mysqli_fetch_array($getname);

            $ctrdtl++;

            $vouch = $row2->vouchno;
            $part = $name->name;
            $recd = $row2->recdamt;
            $disc = $row2->discount;
            $claim = $row2->claim;
            $amt = $row2->amount;

            $totrecd = $totrecd + $row2[recdamt];
            $totdisc = $totdisc + $row2[discount];
            $totclaim = $totclaim + $row2[claim];
            $totamt = $totamt + $row2[amount];



            $contents.=$vouch . ",";
            $contents.=$part . ",";
            $contents.=$recd . ",";
            $contents.=$disc . ",";
            $contents.=$claim . ",";
            $contents.=$amt . "\n";
        }

        $contents.=",Bank Receipt, , , , \n";
        $contents.="Voucher No.,A/c. Name,Recd. Amount ,Discount ,Claim,Amount \n";

        $totrecd = 0;
        $totdisc = 0;
        $totclaim = 0;
        $totamt = 0;
        while ($row3 = mysqli_fetch_array($brecp2)) {

            $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row3[accode]'") or die(mysqli_error($conn));
            $name = mysqli_fetch_array($getname);

            $ctrdtl++;

            $vouch = $row3->vouchno;
            $part = $name->name;
            $recd = $row3->recdamt;
            $disc = $row3->discount;
            $claim = $row3->claim;
            $amt = $row3->amount;

            $totrecd = $totrecd + $row3[recdamt];
            $totdisc = $totdisc + $row3[discount];
            $totclaim = $totclaim + $row3[claim];
            $totamt = $totamt + $row3[amount];



            $contents.=$vouch . ",";
            $contents.=$part . ",";
            $contents.=$recd . ",";
            $contents.=$disc . ",";
            $contents.=$claim . ",";
            $contents.=$amt . "\n";
        }
        $contents.=",Bank Payment, , , , \n";
        $contents.="Voucher No.,A/c. Name,Recd. Amount ,Discount ,Claim,Amount \n";

        $totrecd = 0;
        $totdisc = 0;
        $totclaim = 0;
        $totamt = 0;
        while ($row4 = mysqli_fetch_array($bpaid2)) {

            $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row4[accode]'") or die(mysqli_error($conn));
            $name = mysqli_fetch_array($getname);

            $ctrdtl++;

            $vouch = $row4->vouchno;
            $part = $name->name;
            $recd = $row4->recdamt;
            $disc = $row4->discount;
            $claim = $row4->claim;
            $amt = $row4->amount;

            $totrecd = $totrecd + $row4[recdamt];
            $totdisc = $totdisc + $row4[discount];
            $totclaim = $totclaim + $row4[claim];
            $totamt = $totamt + $row4[amount];



            $contents.=$vouch . ",";
            $contents.=$part . ",";
            $contents.=$recd . ",";
            $contents.=$disc . ",";
            $contents.=$claim . ",";
            $contents.=$amt . "\n";
        }
    }
    $contents.="Total" . ",";
    $contents.=number_format($totrecd, 2) . ",";
    $contents.=number_format($totdisc, 2) . ",";
    $contents.=number_format($totclaim, 2) . ",";
    $contents.=number_format($totamt, 2) . "\n";


    $contents = strip_tags($contents);

//header to make force download the file
    if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=SaleOrderRegisterSummary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment; filename=SaleOrderRegisterDetail" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
?>