<?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];
$spltype = $_SESSION["spltype_" . $dbname];     /// for "B"

$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 = "BankBook";
} else {
    $bgrp = "002";
    $msource1 = "CR";
    $msource2 = "CP";
    $dispname = 'Cash';
    $formname = "CashBook";
}

//browse($msource1);
//browse($msource2);

$acond = "";
if (!empty($_POST["name"])) {
    $code = $_POST["name"];
    $acond = "AND `accode`='$code'";
} else {
    if ($mpara == "B") {
        browse("Bank Name Cannot Be Empty.");
    } else {
        browse("Cash Name Cannot Be Empty.");
    }
    header("refresh: 1; bankbook.php");
    exit();
}
$partyname = trim($_POST["partyname"]);
$partycond = "";
if ($partyname) {
    $partycond = " and accode ='$partyname'";
}
$mfrmdate = trim($_POST["frmdate"]);
$mtodate = trim($_POST["todate"]);

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

if ($mfrmdate > $mtodate) {
    browse("To Date Cannot Be Less Than From Date");
    header("refresh: 1; ledger.php");
    exit();
}
$prntype = $_POST["dtype"];
$prnnarr = $_POST["prinarr"];
$prinop = $_POST["prinop"];
if ($prntype == 1) {
    $repname = $dispname . " Book";
} elseif ($prntype == 2) {
    $repname = $dispname . " Book - Only Debits";
} else {
    $repname = $dispname . " Book - Only Credits";
}
$newname = trim($_POST["partyname"]);

$getpartyprinname = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$newname' ") or die(mysqli_error($conn));
$fetpartyprinname = mysqli_fetch_array($getpartyprinname);

if ($fetpartyprinname['name'] != "") {
    $partyprinname = $fetpartyprinname['name'];
} else {
    $partyprinname = "All";
}
$acc = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE accode!='' $acond") or die(mysqli_error($conn));
//    print table($acc);
if (isset($_POST["view"])) {
    include("includes/linkshead.php");
?>

    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 ">
                    <table class=" table table-condensed 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["init"];
                                $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 trnno != '001' and bankinit = '$mbankinit' and 
                                //                                                       date >= '$mfrmdate' and date <= '$mtodate' and     
                                //                                                       (source = '$msource1' or source = '$msource2') and  
                                //                                                       concat(source,vouchno) in (select concat(source,vouchno) from `actran` 
                                //                                                       where accode = '$maccode' and bankinit = '$mbankinit' and 
                                //                                                       (source = '$msource1' or source = '$msource2'))
                                //                                                       order by date, source, vouchno ") or die(mysqli_error($conn)) ;    


                                $ctran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `actran` 
                                                       WHERE trnno != '001' and bankinit = '$mbankinit' and 
                                                       (source = '$msource1' or source = '$msource2') $partycond and  
                                                       concat(source,vouchno) in (select concat(source,vouchno) from `actran` 
                                                       where accode = '$maccode' and bankinit = '$mbankinit' and 
                                                       date >= '$mfrmdate' and date <= '$mtodate' and     
                                                       (source = '$msource1' or source = '$msource2'))   $partycond
                                                       union
                                                       SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `actran` 
                                                       WHERE accode != '$maccode' $partycond and source = 'TR' and  
                                                       concat(source,vouchno) in (select concat(source,vouchno) from `actran` 
                                                       where accode = '$maccode'  and 
                                                       date >= '$mfrmdate' and date <= '$mtodate' and     
                                                       (source = 'TR' )) $partycond order by date, source, vouchno ") or die(mysqli_error($conn));

                                //    print table($ctran1);

                                $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE accode = '$maccode' AND 
                                                           `date` < '$mfrmdate' 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";
                                }

                                ///////////   as said by noor bhai from msg   ///  only in cash book and in "B"

                                if ($spltype == 0) {
                                    if ($mpara == 'C') {
                                        if ($partycond != "") {
                                            $totbal = 0;
                                        }
                                    }
                                }

                                echo "<tr style='height:10px;' class='mySearchTr'>";
                                echo "<th style=\"text-align:left;\"  colspan='8'><b>" . strtoupper($macname) . "</b></th>";
                                echo "</tr>";
                            ?>
                                <tr style="height:10px;width:100%" class="mySearchTr">
                                    <th style="text-align:left;width: 15%"><b>Date</b></th>
                                    <th style="text-align:left;width: 10%"><b>Voucher</b></th>
                                    <th style="text-align:left;width: 40%"><b>Description</b></th>
                                    <?php if ($mpara == "B") { ?>
                                        <th style="text-align:left;width: 10%"><b>Cheque No.</b></th>
                                        <th style="text-align:left;width: 10%"><b>Bill No.</b></th>
                                    <?php } ?>
                                    <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
                                if ($prinop == 1) {
                                    echo "<td style=\"text-align:left;\">$vdate</td>";
                                    echo "<td style=\"text-align:left;\">$mvouch</td>";
                                    echo "<td style=\"text-align:left;\">$mdescrip</td>";
                                    if ($mpara == "B") {
                                        echo "<td style=\"text-align:left;\"></td>";
                                        echo "<td style=\"text-align:left;\"></td>";
                                    }
                                    if ($prntype == 1) {

                                        echo "<td style=\"text-align:right;\">$mdebit</td>";
                                        echo "<td style=\"text-align:right;\">$mcredit</td>";
                                        echo "<td style=\"text-align:right;\">" . abs($totbal) . " " . $mdctot . "</td>";
                                    } elseif ($prntype == 2) {
                                        echo "<td style=\"text-align:right;\">$mdebit</td>";
                                        echo "<td style=\"text-align:right;\"></td>";
                                        echo "<td style=\"text-align:right;\"></td>";
                                    } else {
                                        echo "<td style=\"text-align:right;\"></td>";
                                        echo "<td style=\"text-align:right;\">$mcredit</td>";
                                        echo "<td style=\"text-align:right;\"></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;
                                    $mbilldet = "";

                                    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";
                                    } 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";
                                            $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, remark1, remark2
                                                                        FROM `$bankrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);

                                            $prbill = $nqry["chqno"];
                                            $prbilldet = $nqry["remark1"];
                                            $prbilldet1 = str_replace(['Received For Bill No.', 'Paid For Bill No.'], '', $prbilldet);
                                            if ($prnnarr == 1) {
                                                $mbill = $nqry["chqno"];
                                                $mbilldt = $nqry["mbilldate"];
                                                $mbilldet = $nqry["remark1"];
                                                $mnarr = $nqry["remark1"] . $nqry["remark2"];
                                                $mchqdtl = "Cheque No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                            }
                                            break;

                                        case "BP";
                                            $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, remark1 , remark2
                                                                              FROM `$bankpay` WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);

                                            $prbill = $nqry["chqno"];
                                            $prbilldet = $nqry["remark1"];
                                            $prbilldet1 = str_replace(['Received For Bill No.', 'Paid For Bill No.'], '', $prbilldet);

                                            if ($prnnarr == 1) {
                                                $mbill = $nqry["chqno"];
                                                $mbilldt = $nqry["mbilldate"];
                                                $mbilldet = $nqry["remark1"];
                                                $mnarr = $nqry["remark1"] . $nqry["remark2"];
                                                $mchqdtl = "Cheque No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                                            }
                                            break;

                                        case "CR";

                                            $nqry1 = mysqli_query($conn, "SELECT `remark1`,`remark2` FROM `$cashrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);

                                            $prbill = $nqry["chqno"];
                                            $prbilldet = $nqry["remark1"];
                                            $prbilldet1 = str_replace(['Received For Bill No.', 'Paid For Bill No.'], '', $prbilldet);

                                            if ($prnnarr == 1) {
                                                $mbill = '';
                                                $mbilldt = '';
                                                $mbilldet = $nqry["remark1"];
                                                $mnarr = $nqry["remark1"] . $nqry["remark2"];
                                            }

                                            break;

                                        case "CP";
                                            $nqry1 = mysqli_query($conn, "SELECT `remark1`, `remark2` FROM `$cashpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                                            $nqry = mysqli_fetch_array($nqry1);

                                            $prbill = $nqry["chqno"];
                                            $prbilldet = $nqry["remark1"];
                                            $prbilldet1 = str_replace(['Received For Bill No.', 'Paid For Bill No.'], '', $prbilldet);
                                            if ($prnnarr == 1) {
                                                $mbilldet = $nqry["remark1"];
                                                $mbill = $nqry[""];
                                                $mbilldt = $nqry[""];
                                                $mnarr = $nqry["remark1"] . $nqry["remark2"];
                                            }
                                            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'") or die(mysqli_error($conn));
                                                $nqry = mysqli_fetch_array($nqry1);

                                                $mnarr = $nqry["narr1"] . $nqry["narr2"];
                                            }
                                            break;
                                        default:
                                            $head = "Other";
                                    }

                                    echo "<tr style='height:10px'>";
                                    echo "<td style=\"text-align:left;\">$vdate</td>";
                                    echo "<td style=\"text-align:left;\">" . $msource . " " . $mvouch . "</td>";
                                    echo "<td style=\"text-align:left;\">$head";

                                    if ($prnnarr == 1) {
                                        if (!empty($mchqdtl)) {
                                            echo "<br>$mchqdtl";
                                            if (!empty($mnarr)) {
                                                echo "<br>";
                                                echo $mnarr;
                                            }
                                            echo "</td>";
                                        } else {
                                            if (!empty($mnarr)) {
                                                echo "<br>";
                                                echo $mnarr;
                                            }
                                        }
                                    }
                                    echo "</td>";

                                    if ($mpara == "B") {
                                        echo "<td style=\"text-align:left;\">$prbill</td>";
                                        echo "<td style=\"text-align:left;\">$prbilldet1</td>";
                                    }

                                    if ($prntype == 1) {
                                        echo "<td style=\"text-align:right;\">$mdebit</td>";
                                        echo "<td style=\"text-align:right;\">$mcredit</td>";
                                        echo "<td style=\"text-align:right;\"> " . abs($totbal) . " " . $mdctot . "</td>";
                                    } elseif ($prntype == 2) {
                                        echo "<td style=\"text-align:right;\">$mdebit</td>";
                                        echo "<td style=\"text-align:right;\"></td>";
                                        echo "<td style=\"text-align:right;\"></td>";
                                    } else {
                                        echo "<td style=\"text-align:right;\"></td>";
                                        echo "<td style=\"text-align:right;\">$mcredit</td>";
                                        echo "<td style=\"text-align:right;\"></td>";
                                    }
                                    echo "</tr>";
                                }

                                echo "<tr style='height:10px'>";
                                echo "<td style=\"text-align:left;\"></td>";
                                echo "<td style=\"text-align:left;\"></td>";

                                if ($mpara == "B") {
                                    echo "<td></td>";
                                    echo "<td></td>";
                                }
                                echo "<td style=\"text-align:left;\"><strong>Total</strong></td>";

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

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

if (isset($_POST["excel"])) {
    include('includes/head_excel_rep.php');

    mysqli_select_db($conn, $zdb);
    $totbal = 0;

    while ($getac = mysqli_fetch_array($acc)) {
        $totbal = 0;
        $dtot = 0;
        $ctot = 0;

        $maccode = $getac["accode"];
        $mbankinit = $getac["init"];
        $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 trnno != '001' and bankinit = '$mbankinit' and 
                                                       (source = '$msource1' or source = '$msource2') $partycond and  
                                                       concat(source,vouchno) in (select concat(source,vouchno) from `actran` 
                                                       where accode = '$maccode' and bankinit = '$mbankinit' and 
                                                       date >= '$mfrmdate' and date <= '$mtodate' and     
                                                       (source = '$msource1' or source = '$msource2'))   $partycond
                                                       union
                                                       SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `actran` 
                                                       WHERE accode != '$maccode' $partycond and source = 'TR' and  
                                                       concat(source,vouchno) in (select concat(source,vouchno) from `actran` 
                                                       where accode = '$maccode'  and 
                                                       date >= '$mfrmdate' and date <= '$mtodate' and     
                                                       (source = 'TR' )) $partycond 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' ") 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";
        }
        if ($spltype == 0) {
            if ($mpara == 'C') {
                if ($partycond != "") {
                    $totbal = 0;
                }
            }
        }

        $macname = strtoupper($macname);
        $contents .= "$macname" . "\n";
        $contents .= "Date, Voucher, Description,";
        if ($mpara == "B") {
            $contents .= "Cheque No., Bill No.,";
        }
        $contents .= "Debit, Credit, Balance\n";

        if ($prinop == 1) {
            $contents .= "$vdate" . ",";
            $contents .= "$mvouch" . ",";
            $contents .= "$mdescrip" . ",";

            if ($mpara == "B") {
                $contents .= "" . ",";
                $contents .= "" . ",";
            }
            if ($prntype == 1) {
                $totbal = abs($totbal);
                $contents .= "$mdebit" . ",";
                $contents .= "$mcredit" . ",";
                $contents .= "$totbal" . " " . "$mdctot" . ",";
            } elseif ($prntype == 2) {
                $contents .= "$mdebit" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
            } else {
                $contents .= "" . ",";
                $contents .= "$mcredit" . ",";
                $contents .= "" . ",";
            }
            $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;
            $mbilldet = "";

            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";
            } 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";
                    $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, remark1, remark2
                                                                        FROM `$bankrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                    $nqry = mysqli_fetch_array($nqry1);

                    $prbill = $nqry["chqno"];
                    $prbilldet = $nqry["remark1"];
                    $prbilldet1 = str_replace(['Received For Bill No.', 'Paid For Bill No.'], '', $prbilldet);
                    $prbilldet2 = str_replace([','], '/', $prbilldet1);
                    if ($prnnarr == 1) {
                        $mbill = $nqry["chqno"];
                        $mbilldt = $nqry["mbilldate"];
                        $mbilldet = $nqry["remark1"];
                        $mnarr = $nqry["remark1"] . $nqry["remark2"];
                        $mchqdtl = "Cheque No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                    }
                    break;

                case "BP";
                    $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, remark1 , remark2
                                                                              FROM `$bankpay` WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                    $nqry = mysqli_fetch_array($nqry1);

                    $prbill = $nqry["chqno"];
                    $prbilldet = $nqry["remark1"];
                    $prbilldet1 = str_replace(['Received For Bill No.', 'Paid For Bill No.'], '', $prbilldet);
                    $prbilldet2 = str_replace([','], '/', $prbilldet1);
                    if ($prnnarr == 1) {
                        $mbill = $nqry["chqno"];
                        $mbilldt = $nqry["mbilldate"];
                        $mbilldet = $nqry["remark1"];
                        $mnarr = $nqry["remark1"] . $nqry["remark2"];
                        $mchqdtl = "Cheque No. : " . trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                    }
                    break;

                case "CR";

                    $nqry1 = mysqli_query($conn, "SELECT `remark1`,`remark2` FROM `$cashrecp`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                    $nqry = mysqli_fetch_array($nqry1);

                    $prbill = $nqry["chqno"];
                    $prbilldet = $nqry["remark1"];
                    $prbilldet1 = str_replace(['Received For Bill No.', 'Paid For Bill No.'], '', $prbilldet);
                    $prbilldet2 = str_replace([','], '/', $prbilldet1);
                    if ($prnnarr == 1) {
                        $mbill = '';
                        $mbilldt = '';
                        $mbilldet = $nqry["remark1"];
                        $mnarr = $nqry["remark1"] . $nqry["remark2"];
                    }

                    break;

                case "CP";
                    $nqry1 = mysqli_query($conn, "SELECT `remark1`, `remark2` FROM `$cashpay`
                                                                       WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
                    $nqry = mysqli_fetch_array($nqry1);

                    $prbill = $nqry["chqno"];
                    $prbilldet = $nqry["remark1"];
                    $prbilldet1 = str_replace(['Received For Bill No.', 'Paid For Bill No.'], '', $prbilldet);
                    $prbilldet2 = str_replace([','], '/', $prbilldet1);
                    if ($prnnarr == 1) {
                        $mbilldet = $nqry["remark1"];
                        $mbill = $nqry[""];
                        $mbilldt = $nqry[""];
                        $mnarr = $nqry["remark1"] . $nqry["remark2"];
                    }
                    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'") or die(mysqli_error($conn));
                        $nqry = mysqli_fetch_array($nqry1);

                        $mnarr = $nqry["narr1"] . $nqry["narr2"];
                    }
                    break;
                default:
                    $head = "Other";
            }

            $contents .= "$vdate" . ",";
            $contents .= "$msource" . " " . "$mvouch" . ",";
            $contents .= "$head" . ",";

            if ($mpara == "B") {
                $contents .= "$prbill" . ",";
                $contents .= "$prbilldet2" . ",";
            }

            if ($prntype == 1) {
                $totbal = abs($totbal);
                $contents .= "$mdebit" . ",";
                $contents .= "$mcredit" . ",";
                $contents .= "$totbal" . "" . "$mdctot" . ",";
            } elseif ($prntype == 2) {
                $contents .= "$mdebit" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
            } else {
                $contents .= "" . ",";
                $contents .= "$mcredit" . ",";
                $contents .= "" . ",";
            }
            $contents .= "" . "\n";
            if ($prnnarr == 1) {
                if (!empty($mchqdtl)) {
                    $contents .= "" . ",,,";
                    $contents .= "$mchqdtl" . "\n";
                    if (!empty($mnarr)) {
                        $contents .= "" . ",,,";
                        $mnarr1 = str_replace([','], '-', $mnarr);
                        $contents .= "$mnarr1" . "\n";
                    }
                } else {
                    if (!empty($mnarr)) {
                        $contents .= "" . ",,,";
                        $mnarr1 = str_replace([','], '-', $mnarr);
                        $contents .= "$mnarr1" . "\n";
                    }
                }
            }
        }

        $contents .= "" . ",";
        $contents .= "" . ",";

        if ($mpara == "B") {
            $contents .= "" . ",";
            $contents .= "" . ",";
        }
        $contents .= "Total" . ",";

        if ($prntype == 1) {
            $contents .= "$dtot" . ",";
            $contents .= "$ctot" . ",";
            if ($totbal < 0) {
                $totbal = abs($totbal);
                $contents .= "$totbal" . " " . "$mdctot" . ",";
            } else {
                $totbal = abs($totbal);
                $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;
    }
}
    ?>