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

$zyear = $_SESSION["tit_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];
$maindb = 'accmast';

//if ($admin == 1) {

    $trandb = "actran";
    $bankrecp = "bankrecp";
    $bankpay = "bankpay";
    $cashrecp = "cashrecp";
    $cashpay = "cashpay";
    $contra = "contra";
//} else {
//
//    $trandb = "actran_d";
//    $bankrecp = "bankrecp_d";
//    $bankpay = "bankpay_d";
//    $cashrecp = "cashrecp_d";
//    $cashpay = "cashpay_d";
//    $contra = "contra_d";
//}
$mpara = $_GET["para"];

if ($mpara == "B") {

    $bgrp = "001";
    $msource1 = "BR";
    $msource2 = "BP";
} else {

    $bgrp = "002";
    $msource1 = "CR";
    $msource2 = "CP";
}



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

    $acond = "";

    if (!empty($_POST["name"])) {

        $code = trim($_POST["name"]);
        $acond = "AND `accode`='$code'";
    }
    $mfrmdate = trim($_POST["frmdate"]);
    $mtodate = trim($_POST["todate"]);

    if ($mfrmdate > $mtodate) {

        browse("To Date Cannot Be Less Than From Date");
        header("refresh: 1; ledger.php");
        exit();
    }
    $prntype = $_POST["dtype"];
    $prnnarr = $_POST["prinarr"];

    if ($prntype == 1) {
        $repname = "Bank Book";
    } elseif ($prntype == 2) {
        $repname = "Bank Book - Only Debits";
    } else {
        $repname = "Bank Book - Only Credits";
    }
    $acc = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE accode!='' $acond") or die(mysqli_error($conn));
    ?>
    <!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='#039'><b>$zcomp[compname]</font></b></font><br />";
              echo "<font size='2px' color='#039'><b> $zcomp[add1]. $zcomp[add2].  $zcomp[add3] </b></font><br /><br />";
              echo "<b><font color='#039'>$repname &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size='3px'>Date : " . date("M-d-Y") . "</b><br />";
              echo "From Date - " . date("d-m-Y", strtotime($mfrmdate)) . "<br/>To Date &nbsp;&nbsp;&nbsp;&nbsp;-  " . date("d-m-Y", strtotime($mtodate)) . "<br/>";
              echo "<br />"; */
            ?>
            <!--<table class="table table-bordered" summary="Ledger - Detail" width='100%' border="0">-->
            <table class=" table-bordered" summary="Ledger - Detail" width='100%' border="0">    
                <tbody>
                    <?php
                    mysqli_select_db($conn, $zdb);
                    $totbal = 0;

                    while ($getac = mysqli_fetch_array($acc)) {
                        /** Detail Part* */
                        $totbal = 0;
                        $dtot = 0;
                        $ctot = 0;
                        $maccode = $getac["accode"];
                        $mbankinit = $getac["bankinit"];
                        $macname = $getac["name"];
                        $mopbal = $getac["openbal"];
                        $mopdc = $getac["dc"];

                        if ($mopdc == "D") {
                            $mopbal = $mopbal * - 1;
                        }
                        $ctran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` WHERE
                                                                  trnno != '001' AND bankinit='$mbankinit' AND
                                                                  (source = '$msource1' or source = '$msource2' or source = 'TR') AND 
                                                                  vouchno+source IN (SELECT vouchno+source FROM $trandb WHERE 
                                                                  accode = '$maccode' AND `date`>='$mfrmdate' AND `date`<='$mtodate'
                                                                  AND (source = '$msource1' or source = '$msource2' or source = 'TR'))
                                                                  ORDER BY date, source, vouchno ") or die(mysqli_error($conn));

                        $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE accode='$maccode' AND 
                                                           date < '$mfrmdate' and retdate != '' and retdate != '0000-00-00' ") or die(mysqli_error($conn));
                        $getamt = mysqli_fetch_array($getbal);

                        $amt = $getamt["amt"];
                        $mobal = $mopbal + $amt;
                        $mdebit = 0;
                        $mcredit = 0;

                        if ($mobal < 0) {
                            $mdc = "DR";
                            $mdebit = abs($mobal);
                            $dtot = $dtot + $mdebit;
                        } else {
                            $mdc = "CR";
                            $mcredit = abs($mobal);
                            $ctot = $ctot + $mcredit;
                        }
                        $vdate1 = strtotime($mfrmdate);
                        $vdate = date('d-m-Y', $vdate1);
                        $mvouch = "";
                        $mdescrip = "Opening Balance";
                        $totbal = $totbal + $mobal;

                        if ($totbal < 0) {
                            $mdctot = "DR";
                        } else {
                            $mdctot = "CR";
                        }
                        echo "<tr style='height:10px'>";
                        echo "<th style=\"text-align:left;\" scope=\"col\" width=\"500\" colspan='9'><b>" . strtoupper($macname) . "</b></th>";
                        echo "</tr>";
                        ?>
                        <tr style="height:10px">
                            <th style="text-align:left;" scope="col" width="200"><b>Date</b></th>
                            <th style="text-align:left;" scope="col" width="50"><b>Voucher</b></th>
                            <th style="text-align:left;" scope="col" width="600"><b>Description</b></th>
                            <th style="text-align:right;" scope="col" width="130"><b>Debit</b></th>
                            <th style="text-align:right;" scope="col" width="130"><b>Credit</b></th>
                            <th style="text-align:right;" scope="col" width="180"><b>Balance</b></th>
                        </tr>

                        <?php
                        echo "<td style=\"text-align:left;\" scope=\"col\" width=\"150\">$vdate</td>";
                        echo "<td style=\"text-align:left;\" scope=\"col\" width=\"80\">$mvouch</td>";
                        echo "<td style=\"text-align:left;\" scope=\"col\" width=\"400\">$mdescrip</td>";

                        if ($prntype == 1) {

                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"180\">$mdebit</td>";
                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"180\">$mcredit</td>";
                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">" . abs($totbal) . " " . $mdctot . "</td>";
                        } elseif ($prntype == 2) {
                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"180\">$mdebit</td>";
                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"180\"></td>";
                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"></td>";
                        } else {
                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"180\"></td>";
                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"180\">$mcredit</td>";
                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"></td>";
                        }
                        while ($row = mysqli_fetch_assoc($ctran1)) {

                            $vdate = $row["mydt"];
                            $msource = $row["source"];
                            $mvouch = $row["vouchno"];
                            $mamount = $row["amount"];
                            $daccode = $row["accode"];
                            $mdebit = 0;
                            $mcredit = 0;

                            if ($mamount < 0) {
                                $mdc = "DR";
                                $mdebit = abs($mamount);
                                $dtot = $dtot + $mdebit;
                            } else {
                                $mdc = "CR";
                                $mcredit = abs($mamount);
                                $ctot = $ctot + $mcredit;
                            }
                            $totbal = $totbal + $mamount;

                            if ($totbal < 0) {
                                $mdctot = "DR";
                            } else {
                                $mdctot = "CR";
                            }
                            $mdescrip = "";
                            $mchqdtl = "";
                            $mnarr = "";

                            $nqry1 = mysqli_query($conn, "SELECT name FROM `accmast` WHERE accode = '$daccode'") or die(mysqli_error($conn));
                            $nqry = mysqli_fetch_array($nqry1);

                            $head = $nqry["name"];

                            switch ($msource) {

                                case "BR";
                                    if ($prnnarr == 1) {
                                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                       `narration` FROM `$bankrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                        $nqry = mysqli_fetch_array($nqry1);

                                        $mbill = $nqry["chqno"];
                                        $mbilldt = $nqry["mbilldate"];
                                        $mnarr = $nqry["narration"];
                                        $mchqdtl = "Cheque No. : " . + trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                    }
                                    break;

                                case "BP";

                                    if ($prnnarr == 1) {
                                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                        `narration` FROM `$bankpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                        $nqry = mysqli_fetch_array($nqry1);

                                        $mbill = $nqry["chqno"];
                                        $mbilldt = $nqry["mbilldate"];
                                        $mnarr = $nqry["narration"];
                                        $mchqdtl = "Cheque No. : " . + trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                    }
                                    break;

                                case "CR";

                                    if ($prnnarr == 1) {
                                        $nqry1 = mysqli_query($conn, "SELECT `narration` FROM `$cashrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                        $nqry = mysqli_fetch_array($nqry1);

                                        $mbill = '';
                                        $mbilldt = '';
                                        $mnarr = $nqry["narration"];
                                        //$mchqdtl = "Cheque No. : ".+ trim($mbill)." &nbsp;&nbsp;Dated : ".$mbilldt;  
                                    }
                                    break;

                                case "CP";

                                    if ($prnnarr == 1) {
                                        $nqry1 = mysqli_query($conn, "SELECT `narration` FROM `$cashpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                        $nqry = mysqli_fetch_array($nqry1);

                                        $mbill = $nqry[""];
                                        $mbilldt = $nqry[""];
                                        $mnarr = $nqry["narration"];
                                        //$mchqdtl = "Cheque No. : ".+ trim($mbill)." &nbsp;&nbsp;Dated : ".$mbilldt;  
                                    }
                                    break;

                                case "TR";
                                    $head = "Contra / Transfer";
                                    if ($prnnarr == 1) {
                                        $nqry1 = mysqli_query($conn, "SELECT `narration` FROM `$contra`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                        $nqry = mysqli_fetch_array($nqry1);

                                        $mnarr = $nqry["narration"];
                                    }
                                    break;
                                default :
                                    $head = "Other";
                            }
                            echo "<tr style='height:10px'>";
                            echo "<td style=\"text-align:left;\" scope=\"col\" width=\"120\" valign='top'>$vdate</td>";
                            echo "<td style=\"text-align:left;\" scope=\"col\" width=\"80\" valign='top'>" . $msource . " " . $mvouch . "</td>";
                            echo "<td style=\"text-align:left;\" scope=\"col\" width=\"400\" valign='top'>$head";

                            if ($prnnarr == 1) {

                                if (!empty($mchqdtl)) {
                                    echo "<br/>$mchqdtl";
                                    if (!empty($mnarr)) {
                                        echo "<br/>";
                                        echo $mnarr;
                                    }
                                    echo"</td>";
                                }
                            }
                            echo "</td>";
                            if ($prntype == 1) {
                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"180\" valign='top'>$mdebit</td>";
                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"180\" valign='top'>$mcredit</td>";
                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\" valign='top'> " . abs($totbal) . " " . $mdctot . "</td>";
                            } elseif ($prntype == 2) {
                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"180\" valign='top'>$mdebit</td>";

                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"180\" valign='top'></td>";
                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\" valign='top'></td>";
                            } else {

                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"180\" valign='top'></td>";
                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"180\" valign='top'>$mcredit</td>";
                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\" valign='top'></td>";
                            }
                            echo "</tr>";
                        }
                        echo "<tr style='height:10px'>";
                        echo "<td style=\"text-align:left;\" scope=\"col\" width=\"200\"></td>";
                        echo "<td style=\"text-align:left;\" scope=\"col\" width=\"200\"></td>";
                        echo "<td style=\"text-align:left;\" scope=\"col\" width=\"200\"><strong>Total</strong></td>";

                        if ($prntype == 1) {

                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">$dtot</td>";
                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">$ctot</td>";

                            if ($totbal < 0) {
                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><font color='red'><strong>" . abs($totbal) . " " . $mdctot . "</strong></font></td>";
                            } else {
                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><font color='green'><strong>" . abs($totbal) . " " . $mdctot . "</strong></font></td>";
                            }
                        } elseif ($prntype == 2) {

                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">$dtot</td>";
                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"></td>";
                        } else {

                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"></td>";
                            echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">$ctot</td>";
                        }
                        echo "</tr>";
                    }
                    ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="9">
                            <hr style="border-top: 1px solid #039;"/>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </body>
    </html>
    <?php
}

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

    $acond = "";

    if (!empty($_POST["name"])) {

        $code = trim($_POST["name"]);
        $acond = "AND `accode`='$code'";
    }
    $mfrmdate = trim($_POST["frmdate"]);
    $mtodate = trim($_POST["todate"]);

    if ($mfrmdate > $mtodate) {

        browse("To Date Cannot Be Less Than From Date");
        header("refresh: 1; ledger.php");
        exit();
    }
    $prntype = $_POST["dtype"];
    $prnnarr = $_POST["prinarr"];

    if ($prntype == 1) {
        $repname = "Bank Book";
    } elseif ($prntype == 2) {
        $repname = "Bank Book - Only Debits";
    } else {
        $repname = "Bank Book - Only Credits";
    }
    $acc = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE accode!='' $acond") or die(mysqli_error($conn));

    $totbal = 0;
    while ($getac = mysqli_fetch_array($acc)) {
        /** Detail Part* */
        $totbal = 0;
        $dtot = 0;
        $ctot = 0;
        $maccode = $getac["accode"];
        $mbankinit = $getac["bankinit"];
        $macname = $getac["name"];
        $mopbal = $getac["openbal"];
        $mopdc = $getac["dc"];

        if ($mopdc == "D") {
            $mopbal = $mopbal * - 1;
        }
        $ctran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` WHERE
                                                                  trnno != '001' AND bankinit='$mbankinit' AND
                                                                  (source = '$msource1' or source = '$msource2' or source = 'TR') AND 
                                                                  vouchno+source IN (SELECT vouchno+source FROM $trandb WHERE 
                                                                  accode = '$maccode' AND `date`>='$mfrmdate' AND `date`<='$mtodate'
                                                                  AND (source = '$msource1' or source = '$msource2' or source = 'TR'))
                                                                  ORDER BY date, source, vouchno ") or die(mysqli_error($conn));

        $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE accode='$maccode' AND 
                                                           date < '$mfrmdate' and retdate != '' and retdate != '0000-00-00' ") or die(mysqli_error($conn));
        $getamt = mysqli_fetch_array($getbal);

        $amt = $getamt["amt"];
        $mobal = $mopbal + $amt;
        $mdebit = 0;
        $mcredit = 0;

        if ($mobal < 0) {
            $mdc = "DR";
            $mdebit = abs($mobal);
            $dtot = $dtot + $mdebit;
        } else {
            $mdc = "CR";
            $mcredit = abs($mobal);
            $ctot = $ctot + $mcredit;
        }
        $vdate1 = strtotime($mfrmdate);
        $vdate = date('d-m-Y', $vdate1);
        $mvouch = "";
        $mdescrip = "Opening Balance";
        $totbal = $totbal + $mobal;

        if ($totbal < 0) {
            $mdctot = "DR";
        } else {
            $mdctot = "CR";
        }
        $contents.=strtoupper($macname) . "\n";

        $contents.= "Date,Voucher,Description,debit,Credit,Balance\n";

        $contents.=$vdate . ",";
        $contents.=$mvouch . ",";
        $contents.=$mdescrip . ",";

        if ($prntype == 1) {

            $contents.=$mdebit . ",";
            $contents.=$mcredit . ",";
            $contents.=abs($totbal) . " " . $mdctot . "\n";
            
        } elseif ($prntype == 2) {

            $contents.=$mdebit . ",";
            $contents.="" . ",";
            $contents.="" . "\n";
        } else {

            $contents.="" . ",";
            $contents.=$mcredit . ",";
            $contents.="" . "\n";
        }
        while ($row = mysqli_fetch_assoc($ctran1)) {

            $vdate = $row["mydt"];
            $msource = $row["source"];
            $mvouch = $row["vouchno"];
            $mamount = $row["amount"];
            $daccode = $row["accode"];
            $mdebit = 0;
            $mcredit = 0;

            if ($mamount < 0) {
                $mdc = "DR";
                $mdebit = abs($mamount);
                $dtot = $dtot + $mdebit;
            } else {
                $mdc = "CR";
                $mcredit = abs($mamount);
                $ctot = $ctot + $mcredit;
            }
            $totbal = $totbal + $mamount;

            if ($totbal < 0) {
                $mdctot = "DR";
            } else {
                $mdctot = "CR";
            }
            $mdescrip = "";
            $mchqdtl = "";
            $mnarr = "";

            $nqry1 = mysqli_query($conn, "SELECT name FROM `accmast` WHERE accode = '$daccode'") or die(mysqli_error($conn));
            $nqry = mysqli_fetch_array($nqry1);

            $head = $nqry["name"];

            switch ($msource) {

                case "BR";
                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                       `narration` FROM `$bankrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);

                        $mbill = $nqry["chqno"];
                        $mbilldt = $nqry["mbilldate"];
                        $mnarr = $nqry["narration"];
                        $mchqdtl = "Cheque No. : " . + trim($mbill) . " Dated : " . $mbilldt;
                    }
                    break;

                case "BP";

                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                        `narration` FROM `$bankpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);

                        $mbill = $nqry["chqno"];
                        $mbilldt = $nqry["mbilldate"];
                        $mnarr = $nqry["narration"];
                        $mchqdtl = "Cheque No. : " . + trim($mbill) . " Dated : " . $mbilldt;
                    }
                    break;

                case "CR";

                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT `narration` FROM `$cashrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);

                        $mbill = '';
                        $mbilldt = '';
                        $mnarr = $nqry["narration"];
                        //$mchqdtl = "Cheque No. : ".+ trim($mbill)." &nbsp;&nbsp;Dated : ".$mbilldt;  
                    }
                    break;

                case "CP";

                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT `narration` FROM `$cashpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);

                        $mbill = $nqry[""];
                        $mbilldt = $nqry[""];
                        $mnarr = $nqry["narration"];
                        //$mchqdtl = "Cheque No. : ".+ trim($mbill)." &nbsp;&nbsp;Dated : ".$mbilldt;  
                    }
                    break;

                case "TR";
                    $head = "Contra / Transfer";
                    if ($prnnarr == 1) {
                        $nqry1 = mysqli_query($conn, "SELECT `narration` FROM `$contra`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);

                        $mnarr = $nqry["narration"];
                    }
                    break;
                default :
                    $head = "Other";
            }
            if ($prnnarr == 1) {

                if (!empty($mchqdtl)) {
                    $dtl = $mchqdtl ;
                    if (!empty($mnarr)) {
                    $dtl =  $mnarr ;
                    }
                }
            }
            $contents.=$vdate . ",";
            $contents.=$msource . " " . $mvouch . ",";
            $contents.=$head . $dtl. ",";            
            
            if ($prntype == 1) {
                $contents.=$mdebit . ",";
                $contents.=$mcredit . ",";
                $contents.=abs($totbal) . " " . $mdctot . "\n";
            } elseif ($prntype == 2) {
                $contents.=$mdebit . ",";
                $contents.="" . ",";
                $contents.="" . "\n";
            } else {

                $contents.="" . ",";
                $contents.=$mcredit . ",";
                $contents.="" . "\n";
            }
        }
        $contents.="" . ",";
        $contents.="" . ",";
        $contents.="Total" . ",";

        if ($prntype == 1) {

            $contents.=$dtot . ",";
            $contents.=$ctot . ",";

            if ($totbal < 0) {
                $contents.=abs($totbal) . " " . $mdctot . "\n";
            } else {
                $contents.=abs($totbal) . " " . $mdctot . "\n";
            }
        } elseif ($prntype == 2) {

            $contents.=$dtot . ",";
            $contents.="" . "\n";
        } else {

            $contents.="" . ",";
            $contents.=$ctot . "\n";
        }
    }
    $contents = strip_tags($contents);

//header to make force download the file
    header("Content-Disposition: attachment; filename=BankBook$mpara" . date('d-m-Y') . ".csv");
    print $contents;
}
?>        



