<?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];
$tourcode = $_SESSION['tourcode'];
$tourname = $_SESSION['tourname'];
//$admin = $_SESSION["admin_" . $dbname];
$maindb = 'accmast';

$trandb = "actran";
$bankrecp = "bankrecp";
$bankpay = "bankpay";
$cashrecp = "cashrecp";
$cashpay = "cashpay";
$contra = "contra";

$mpara = $_GET["para"];
if ($mpara == "B") {
    $bgrp = "001";
    $msource1 = "BR";
    $msource2 = "BP";
    $dispname = 'Bank';
    $formname = 'Bank Book';
} else {
    $bgrp = "002";
    $msource1 = "CR";
    $msource2 = "CP";
    $dispname = 'Cash';
    $formname = 'Cash Book';
}


//    $tourcode = $_POST["tourcode"];
$tourcond = " and tour = '$tourcode' ";

$acond = "";
if (!empty($_POST["name"])) {
    $code = $_POST["name"];
    $acond = "AND `accode`='$code'";
} elseif (!empty($_GET["acc"])) {
    $code = $_GET["acc"];
    $acond = "AND `accode`='$code'";
} else {
    if ($mpara == "B") {
        browse("Bank Name Cannot Be Empty.");
    } else {
        browse("Cash Name Cannot Be Empty.");
    }
    header("refresh: 1; ledger.php");
    exit();
}

if ($_GET["fdt"] != '') {
    $mfrmdate = trim($_GET["fdt"]);
} else {
    $mfrmdate = trim($_POST["frmdate"]);
    if (empty($_POST["frmdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; bankbook.php");
    }
}
if ($_GET["tdt"] != '') {
    $mtodate = trim($_GET["tdt"]);
} else {
    $mtodate = trim($_POST["todate"]);
    if (empty($_POST["todate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; bankbook.php");
    }
}

if ($_GET["rpt2"] != '') {
    $prntype = $_GET["rpt2"];
} else {
    $prntype = $_POST["dtype"];
}
$prnnarr = $_POST["prinarr"];

if ($prntype == 1) {
    $repname = $dispname . " Book";
} elseif ($prntype == 2) {
    $repname = $dispname . " Book - Only Debits";
} else {
    $repname = $dispname . " Book - Only Credits";
}

$acc = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE accode!='' $acond") or die(mysqli_error($conn));
if (isset($_POST["submit"]) || ($_GET["view"])) {
    include("includes/linkshead.php");
    ?>
    <head>
        <style>
            @media print {
                .noPrint {
                    display:none;
                }
            }

        </style>
    </head>
    <body>
        <div class="container">
            <?php include('includes/head_rep.php') ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class=" table table-condensed table-bordered table-striped" 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"];
//                                browse($mbankinit);
                                $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 `actran` 
                                                       WHERE accode != '$maccode'  $tourcond and 
                                                       (source = '$msource1' or source = '$msource2') and  
                                                       concat(source,vouchno) in (select concat(source,vouchno) from `actran` 
                                                       where accode = '$maccode'  $tourcond and 
                                                       date >= '$mfrmdate' and date <= '$mtodate' and `retdate` = '0000-00-00'    
                                                       and (source = '$msource1' or source = '$msource2'))  
                                                       union all
                                                       SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `actran` 
                                                       WHERE accode != '$maccode' and source = 'TR' $tourcond and  
                                                       concat(source,vouchno) in (select concat(source,vouchno) from `actran` 
                                                       where accode = '$maccode'  and `retdate` = '0000-00-00' $tourcond and
                                                       date >= '$mfrmdate' and date <= '$mtodate' and 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` = '0000-00-00' $tourcond ") 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";
                                    $colr = " color:black";
                                } else {
                                    $mdctot = "CR";
                                    $colr = " color:red";
                                }
                                echo "<tr style='height:10px;background-color: #BABFC2'>";
                                echo "<th style=\"text-align:left;\" scope=\"col\" width=\"500\" colspan='9'><b>" . strtoupper($macname) . "</b></th>";
                                echo "</tr>";
                                ?>
                                <tr style="height:10px; " class="mySearchTr">
                                    <th style="text-align:left;width:10%"><b>Date</b></th>
                                    <th style="text-align:left;width:10%"><b>Voucher</b></th>
                                    <th style="text-align:left;width:50%" colspan="2"><b>Description</b></th>
                                    <th style="text-align:right;width:10%"><b>Debit</b></th>
                                    <th style="text-align:right;width:10%"><b>Credit</b></th>
                                    <th style="text-align:right;width:10%"><b>Balance</b></th>
                                </tr>

                                <?php
                                echo "<td>$vdate</td>";
                                echo "<td>$mvouch</td>";
                                echo "<td colspan='2'>$mdescrip</td>";

                                if ($prntype == 1) {
                                    echo "<td class='text-right'>" . number_format($mdebit, 2) . "</td>";
                                    echo "<td class='text-right'>" . number_format($mcredit, 2) . "</td>";

                                    echo "<td class='text-right' style= '$colr'>" . number_format(abs($totbal), 2) . " " . $mdctot . "</td>";
                                } elseif ($prntype == 2) {
                                    echo "<td class='text-right'>" . number_format($mdebit, 2) . "</td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                } else {
                                    echo "<td></td>";
                                    echo "<td class='text-right'>" . number_format($mcredit, 2) . "</td>";
                                    echo "<td></td>";
                                }

                                if ($mcredit > 0) {
                                    $opening = round($mcredit);
                                } else {
                                    $opening = round($mdebit);
                                }
                                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;
                                        $totbal = $totbal - abs($mamount);
                                    } else {
                                        $mdc = "CR";
                                        $mcredit = abs($mamount);
                                        $ctot = $ctot + $mcredit;
                                        $totbal = $totbal + abs($mamount);
                                    }

                                    if ($totbal < 0) {
                                        $mdctot = "DR";
                                        $colr = " color:black";
                                    } else {
                                        $mdctot = "CR";
                                        $colr = " color:red";
                                    }
                                    $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 recpagnst,payfor,regnno,chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, note1,note2
                                                                              FROM `$bankrecp` WHERE vouchno='$mvouch' $tourcond ") or die(mysqli_error($conn));
                                                $nqry = mysqli_fetch_array($nqry1);

                                                if ($nqry["recpagnst"] == "U") {
                                                    $mbill = "Regn. No. " . $nqry["regnno"];
                                                } else {
                                                    $mbill = "FIT No. " . $nqry["regnno"];
                                                }
                                                if ($nqry["payfor"] == "U") {
                                                    $mbill .= ". Received Against Tour";
                                                } else {
                                                    $mbill .= ". Received Against Ticket";
                                                }
                                                $mchq = $nqry["chqno"];
                                                $mbilldt = $nqry["mbilldate"];
                                                $mnarr = $nqry["narr1"];
                                                $mchqdtl = trim($mbill) . " Cheque No. : " . trim($mchq) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                            }
                                            break;

                                        case "BP";

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

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

                                        case "CR";

                                            if ($prnnarr == 1) {
                                                $nqry1 = mysqli_query($conn, "SELECT recpagnst,payfor, regnno,vouchno,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt,`narr1`,narr2 FROM `$cashrecp`
                                                                                      WHERE vouchno='$mvouch' $tourcond") or die(mysqli_error($conn));
                                                $nqry = mysqli_fetch_array($nqry1);

                                                if ($nqry["recpagnst"] == "U") {
                                                    $mbill = "Regn. No. " . $nqry["regnno"];
                                                } else {
                                                    $mbill = "FIT No. " . $nqry["regnno"];
                                                }
                                                if ($nqry["payfor"] == "U") {
                                                    $mbill .= ". Received Against Tour";
                                                } else {
                                                    $mbill .= ". Received Against Ticket";
                                                }
                                                $mbilldt = $nqry["mydt"];
                                                $mnarr = $nqry["narr1"] . " " . $nqry["narr2"];
                                                $mchqdtl = trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                            }
                                            break;

                                        case "CP";

                                            if ($prnnarr == 1) {
                                                $nqry1 = mysqli_query($conn, "SELECT vouchno,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt,`narr1`,narr2 FROM `$cashpay`
                                                                       WHERE vouchno='$mvouch' $tourcond") or die(mysqli_error($conn));
                                                $nqry = mysqli_fetch_array($nqry1);

                                                $mbill = $nqry["vouchno"];
                                                $mbilldt = $nqry["mydt"];
                                                $mnarr = $nqry["narr1"] . " " . $nqry["narr2"];
                                                $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                            }
                                            break;

                                        case "TR";
//                                            $head = "Contra / Transfer";
                                            if ($mamount > 0) {
                                                $head = "To " . $head;
                                            } else {
                                                $head = "From " . $head;
                                            }
                                            if ($prnnarr == 1) {
                                                $nqry1 = mysqli_query($conn, "SELECT `narr1`, `narr2` FROM `$contra`
                                                                       WHERE vouchno='$mvouch' $tourcond") or die(mysqli_error($conn));
                                                $nqry = mysqli_fetch_array($nqry1);

                                                $mnarr = trim($nqry["narr1"]) . ' ' . trim($nqry["narr2"]);
                                            }
                                            break;
                                        default :
                                            $head = "Other";
                                    }
                                    echo "<tr style='height:10px;background-color:white'>";
                                    echo "<td>$vdate</td>";
                                    echo "<td>" . $msource . " " . $mvouch . "</td>";
                                    echo "<td  colspan='2'>$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 class='text-right'>" . number_format($mdebit, 2) . "</td>";
                                        echo "<td class='text-right'>" . number_format($mcredit, 2) . "</td>";
                                        echo "<td class='text-right' style='$colr'>" . number_format(abs($totbal), 2) . " " . $mdctot . "</td>";
                                    } elseif ($prntype == 2) {
                                        echo "<td class='text-right'>" . number_format($mdebit, 2) . "</td>";

                                        echo "<td ></td>";
                                        echo "<td ></td>";
                                    } else {

                                        echo "<td ></td>";
                                        echo "<td class='text-right'>" . number_format($mcredit, 2) . "</td>";
                                        echo "<td ></td>";
                                    }
                                    echo "</tr>";
                                }
                                echo "<tr style='height:10px'>";
                                echo "<td ></td>";
                                echo "<td ></td>";
                                echo "<td ></td>";
                                echo "<td class='text-right' style='width:35%'><strong>Total</strong></td>";
//                                echo "<td class='text-right' style='width:15%'><strong>Opening - $opening</strong></td>";

                                if ($prntype == 1) {
                                    echo "<td class='text-right'>$dtot</td>";
                                    echo "<td class='text-right'>$ctot</td>";

                                    if ($totbal < 0) {
                                        echo "<td class='text-right'><font color='green'><strong>" . number_format(abs($totbal), 2) . " " . $mdctot . "</strong></font></td>";
                                    } else {
                                        echo "<td class='text-right'><font color='red'><strong> -" . number_format(abs($totbal), 2) . " " . $mdctot . "</strong></font></td>";
                                    }
                                } elseif ($prntype == 2) {
                                    echo "<td class='text-right'>" . number_format($dtot, 2) . "</td>";
                                    echo "<td ></td>";
                                } else {

                                    echo "<td ></td>";
                                    echo "<td class='text-right'>" . number_format($ctot, 2) . "</td>";
                                }
                                echo "</tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php
        include("includes/linksfoot.php");
    }

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

        $totbal = 0;

        while ($getac = mysqli_fetch_array($acc)) {
            $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 `actran` 
                                   WHERE accode != '$maccode'  $tourcond and 
                                   (source = '$msource1' or source = '$msource2') and  
                                   concat(source,vouchno) in (select concat(source,vouchno) from `actran` 
                                   where accode = '$maccode'  $tourcond and 
                                   date >= '$mfrmdate' and date <= '$mtodate' and `retdate` = '0000-00-00'    
                                   and (source = '$msource1' or source = '$msource2'))  
                                   union all
                                   SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `actran` 
                                   WHERE accode != '$maccode' and source = 'TR' $tourcond and  
                                   concat(source,vouchno) in (select concat(source,vouchno) from `actran` 
                                   where accode = '$maccode'  and `retdate` = '0000-00-00' $tourcond and
                                   date >= '$mfrmdate' and date <= '$mtodate' and 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` = '0000-00-00' $tourcond ") 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);
            } else {
                $mdc = "CR";
                $mcredit = abs($mobal);
            }
            $vdate1 = strtotime($mfrmdate);
            $vdate = date('d-m-Y', $vdate1);
            $mvouch = "";
            $mdescrip = "Opening Balance";
            $totbal = $totbal + $mobal;

            if ($totbal < 0) {
                $mdctot = "DR";
                $colr = " color:black";
            } else {
                $mdctot = "CR";
                $colr = " color:red";
            }
            $contents .= "$macname\n";
            $contents .= "Date, Voucher, Description, Debit, Credit, Balance\n";

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

            if ($prntype == 1) {
                $contents .= "$mdebit" . ",";
                $contents .= "$mcredit" . ",";

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

            if ($mcredit > 0) {
                $opening = round($mcredit);
            } else {
                $opening = round($mdebit);
            }

            $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;
                    $totbal = $totbal - abs($mamount);
                } else {
                    $mdc = "CR";
                    $mcredit = abs($mamount);
                    $ctot = $ctot + $mcredit;
                    $totbal = $totbal + abs($mamount);
                }

                if ($totbal < 0) {
                    $mdctot = "DR";
                    $colr = " color:black";
                } else {
                    $mdctot = "CR";
                    $colr = " color:red";
                }
                $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 recpagnst,payfor,regnno,chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, note1,note2
                                                                              FROM `$bankrecp` WHERE vouchno='$mvouch' $tourcond ") or die(mysqli_error($conn));
                            $nqry = mysqli_fetch_array($nqry1);

                            if ($nqry["recpagnst"] == "U") {
                                $mbill = "Regn. No. " . $nqry["regnno"];
                            } else {
                                $mbill = "FIT No. " . $nqry["regnno"];
                            }
                            if ($nqry["payfor"] == "U") {
                                $mbill .= ". Received Against Tour";
                            } else {
                                $mbill .= ". Received Against Ticket";
                            }
                            $mchq = $nqry["chqno"];
                            $mbilldt = $nqry["mbilldate"];
                            $mnarr = $nqry["narr1"];
                            $mchqdtl = trim($mbill) . " Cheque No. : " . trim($mchq) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                        }
                        break;

                    case "BP";

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

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

                    case "CR";

                        if ($prnnarr == 1) {
                            $nqry1 = mysqli_query($conn, "SELECT recpagnst,payfor, regnno,vouchno,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt,`narr1`,narr2 FROM `$cashrecp`
                                                                                      WHERE vouchno='$mvouch' $tourcond") or die(mysqli_error($conn));
                            $nqry = mysqli_fetch_array($nqry1);

                            if ($nqry["recpagnst"] == "U") {
                                $mbill = "Regn. No. " . $nqry["regnno"];
                            } else {
                                $mbill = "FIT No. " . $nqry["regnno"];
                            }
                            if ($nqry["payfor"] == "U") {
                                $mbill .= ". Received Against Tour";
                            } else {
                                $mbill .= ". Received Against Ticket";
                            }
                            $mbilldt = $nqry["mydt"];
                            $mnarr = $nqry["narr1"] . " " . $nqry["narr2"];
                            $mchqdtl = trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                        }
                        break;

                    case "CP";

                        if ($prnnarr == 1) {
                            $nqry1 = mysqli_query($conn, "SELECT vouchno,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt,`narr1`,narr2 FROM `$cashpay`
                                                   WHERE vouchno='$mvouch' $tourcond") or die(mysqli_error($conn));
                            $nqry = mysqli_fetch_array($nqry1);

                            $mbill = $nqry["vouchno"];
                            $mbilldt = $nqry["mydt"];
                            $mnarr = $nqry["narr1"] . " " . $nqry["narr2"];
                            $mchqdtl = "Bill No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                        }
                        break;

                    case "TR";
                        if ($mamount > 0) {
                            $head = "To " . $head;
                        } else {
                            $head = "From " . $head;
                        }
                        if ($prnnarr == 1) {
                            $nqry1 = mysqli_query($conn, "SELECT `narr1`, `narr2` FROM `$contra`
                                                   WHERE vouchno='$mvouch' $tourcond") or die(mysqli_error($conn));
                            $nqry = mysqli_fetch_array($nqry1);

                            $mnarr = trim($nqry["narr1"]) . ' ' . trim($nqry["narr2"]);
                        }
                        break;
                    default :
                        $head = "Other";
                }
                $contents .= "$vdate" . ",";
                $contents .= "$msource" . "$mvouch" . ",";
                $contents .= "$head" . ",";

                if ($prnnarr == 1) {

                    if (!empty($mchqdtl)) {
                        $mchqdtl;
                        if (!empty($mnarr)) {
                            $mnarr;
                        }
                    }
                }
                if ($prntype == 1) {
                    $contents .= "$mdebit" . ",";
                    $contents .= "$mcredit" . ",";
                    $contents .= "$totbal" . "$mdctot" . ",";
                } elseif ($prntype == 2) {
                    $contents .= "$mdebit" . ",";
                    $contents .= "" . ",";
                    $contents .= "" . ",";
                } else {
                    $contents .= "" . ",";
                    $contents .= "$mcredit" . ",";
                    $contents .= "" . ",";
                }
                // $contents .="" . "\n";
            }
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "Total" . ",";

            if ($prntype == 1) {
                $contents .= "$dtot" . ",";
                $contents .= "$ctot" . ",";

                if ($totbal < 0) {
                    $contents .= "$totbal" . "$mdctot" . ",";
                } else {
                    $contents .= "$totbal" . "$mdctot" . ",";
                }
            } elseif ($prntype == 2) {
                $contents .= "$dtot" . ",";
                $contents .= "" . ",";
            } else {

                $contents .= "" . ",";
                $contents .= "$ctot" . ",";
            }
            $contents .= "" . "\n";
        }

        $contents = strip_tags($contents);

        if ($_POST["dtype"] == 1) {
            header("Content-Disposition: attachment;filename = $formname" . "_Summary" . date('d-m-Y') . ".csv");
            print $contents;
        } else {
            header("Content-Disposition: attachment;filename = $formname" . "_Detail" . date('d-m-Y') . ".csv");
            print $contents;
        }
    }

        