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

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

$formname = "LEDGER";
$grid = "ledger.php";
$file = "ledger.php";
$maindb = "accmast";
$usertype = $_SESSION["usertype_" . $dbname];

$zyear = right($zdb, 4);
$mfrdate = getzdates($zyear, 'S');
$mtodate = getzdates($zyear, 'E');

mysqli_select_db($conn, $zdb);

$trandb = "actran";

$fseries = $_SESSION["society_" . $dbname];
$zroot = $_SESSION["zdbroot"];
$para = $_POST['para'];

if ($_GET['grpcd'] == 10) {
    $grpcond = " and grpcode = '010' ";
} else if ($_GET['grpcd'] != 10) {
    $grpcond = " and grpcode != '010' ";
}



if ($para == 'M' || $_POST["flag"] == 'M') {
    $grpcond = " and grpcode = '010' ";
} else if ($para == 'O') {
    $grpcond = " and grpcode != '010' ";
}

if (empty($fseries)) {
    browse("Invalid Society Credentials Forced Logout.");
    print '<script type="text/javascript">';
    print " top.window.location = 'index.php'";
    PRINT '</script>';
}
$gcond = "";
$accond = "";
$custcond = "";
$flatcond = "";
$grpconds = "";
$partycond = "";


if (!empty($_POST['party'])) {
    $party = $_POST['party'];
    $partycond = " AND `accode` = '$party'";
}

if (!empty($_POST["group"])) {
    $grp = trim($_POST["group"]);
    $gcond = "AND `grpcode`='$grp'";
}
//    if (!empty($_GET["group"])) {
//        $grpname = trim($_GET["group"]);
//        $getgrp = mysqli_query($conn, "SELECT GRPCODE FROM accmast WHERE ") or die(mysqli_error($conn));
//        $gcond = "AND `grpcode`='$grp'";
//    }

if ($_POST["flag"] == 'M') {
    $acname = $_POST['acname'];
    $custcond = " AND `accode` = '$acname' ";
    $accond = " AND `accode` = '$acname' ";
} else {
    if (!empty($_POST['acname'])) {

        foreach ($_POST['acname'] as $cucheck2) {
            $ccheck2[$ccount2++] .= "OR `accode` = '" . $cucheck2 . "' ";
            $ccheckoth[$ccountoth++] .= "OR `accode` = '" . $cucheck2 . "' ";
        }

        foreach ($ccheck2 as $g => $cusvalues2) {
            $cuscheck2 .= $cusvalues2;
        }
        foreach ($ccheckoth as $g => $valuesoth) {
            $cuscheckoth .= $valuesoth;
        }

        $custcond = " AND (`accode` = '' " . $cuscheck2 . " )";
        $accond = " AND (`accode` = '' " . $cuscheckoth . " )";
    }
}


if (!empty($_POST["flatno"])) {
    $flatno = $_POST["flatno"];
    $flatcond = " AND flatno = '$flatno' ";
}

if ($_POST["frmdt"] != '') {
    $mfrmdate = $_POST["frmdt"];
}
if ($_POST["todt"] != '') {
    $mtodate = $_POST["todt"];
}


if ($mfrmdate > $mtodate) {
    browse("To Date Cannot Be Less Than From Date");
    header("refresh: 1; ledger.php");
    exit();
}


$prnzero = $_POST["zerobal"];



if ($_POST["dtype"] == 1) {
    $repname = "Ledger - Summary";
    $dtype = 1;
} else {
    $repname = "Ledger - Detail";
    $dtype = 2;
}


if ($_POST["scope"] != 2) {
    $wint = 1;
} else {
    $wint = 0;
}


//-----------------------------------------get

if ($_GET["frmdate"] != '') {
    $mfrmdate = $_GET["frmdate"];
}
if ($_GET["todate"] != '') {
    $mtodate = $_GET["todate"];
}


if ($_GET["prinnarr"] != '') {
    $prnnarr = 1;
} else {
    $prnnarr = $_POST["prinarr"];
}


if (!empty($_GET["party"])) {
    $party = $_GET['party'];

    $ggrpcode = mysqli_query($conn, "SELECT grpcode from accmast where accode='$party'")or die(mysqli_error($conn));
    $fgrpcode = mysqli_fetch_array($ggrpcode);

    if ($fgrpcode["grpcode"] == '010') {
        $wint = 1;
        $wintcond = " AND  `source`!='INT'";
//            $grpcond = " and grpcode = '010' ";
    } else {
        $wint = 0;
        $grpcond = "";
    }
    $partycond = " AND `accode` = '$party'";
}


if (!empty($_GET["grpcode"])) {
    $grpcode = $_GET['grpcode'];
    $grpcond = " AND `grpcode` = '$grpcode'";
}





//    $acc = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `series` = '$fseries' and `grpcode` != '001' and `grpcode` != '002'
//                      $grpcond $flatcond $gcond $accond $partycond order by mancode+0") or die(mysqli_error($conn));
//    browse($grpcond);
//    browse($flatcond);
//    browse($gcond);
//    browse($accond);
//    browse($partycond);
//    browse($fseries);

$acc = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `series` = '$fseries' and `grpcode` != '001' and `grpcode` != '002'
                       $grpcond $flatcond $gcond $accond $partycond order by mancode+0") or die(mysqli_error($conn));

if (isset($_POST["view"]) || $_GET["view"]) {
    include("includes/linkshead.php");

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style>
        .bakc{
            background-color: #2FBEF3;
        }
        table td{
            font-size: 13px;
        }

    </style>
    </head>
    <body id="ledger">   
        <div class="container"> 
            <?php include("includes/head_rep.php"); ?>
            <table class="table table-bordered table-condensed table-bordered table-striped sortableTable responsive-table">
                <thead>
                    <tr class="mySearchTr" >
                        <?php
                        if ($dtype == 1) {
                            ?>

                            <th align="left">Account Name</th>
                            <th align="left">Flat No. / Man. Code</th>

                            <th align="right" style="text-align:right;">Balance</th>

                            <?php
                        }
                        ?>

                    </tr>

                </thead>

                <tbody>

                    <?php
                    $totbal = 0;
                    $url = '';
                    while ($getac = mysqli_fetch_array($acc)) {
                        if ($dtype == 1) {
                            echo '<tr>';

                            $maccode = $getac["accode"];

                            $mgrpcode = $getac["grpcode"];
                            $flatno = $getac["flatno"];
                            $mancode = $getac["mancode"];
                            $mopbal = $getac["openbal"];
                            $mprincipal = $getac["prinamt"];
                            $mopdc = $getac["dc"];

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

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


                            ############PARTY NAME####################


                            $getpartyname = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$maccode' and series = '$fseries'") or die(mysqli_error($conn));
                            $zname = mysqli_fetch_object($getpartyname);
                            $mname = $zname->name;

                            ############PARTY NAME####################

                            $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE `series`='$fseries' and `accode`='$maccode' AND  date < '$mtodate'") or die(mysqli_error($conn));
                            $getamt = mysqli_fetch_array($getbal);
                            $amt = $getamt["amt"];
                            $mbal = $mopbal + $amt;

                            if ($mbal < 0) {
                                $mdc = "DR";
                            } else {
                                $mdc = "CR";
                            }

                            $show = 1;
                            if ($prnzero == 1) {

                                $show = 1;
                            } else {

                                if ($mbal == 0) {

                                    $show = 0;
                                }
                            }

                            if ($show == 1) {
                                if ($mgrpcode == '010') {
                                    echo "<td style=\"text-align:left;\" scope=\"col\" width=\"200\">$mname </td>";
                                    echo "<td style=\"text-align:left;\" scope=\"col\" width=\"200\">$flatno</td>";
                                } else {
                                    echo "<td style=\"text-align:left;\" scope=\"col\" width=\"200\">$mname</td>";
                                    echo "<td style=\"text-align:left;\" scope=\"col\" width=\"200\">$mancode</td>";
                                }

                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">" . abs($mbal) . " " . $mdc . "</td>";

                                $totbal = $totbal + $mbal;
                            }

                            if ($totbal < 0) {

                                $mdctot = "DR";
                            } else {

                                $mdctot = "CR";
                            }
                        } else {

                            /** Detail Part* */
                            $dprin = 0;
                            $dint = 0;
                            $totbal = 0;
                            $dtot = 0;
                            $ctot = 0;

                            $maccode = $getac["accode"];
                            $ggrpcode = mysqli_query($conn, "SELECT grpcode from accmast where accode = '$maccode'")or die(mysqli_error($conn));
                            $fgrpcode = mysqli_fetch_array($ggrpcode);

                            if ($fgrpcode["grpcode"] == '010') {
                                $wintcond = " AND  `source`!='INT'";
                            }

                            $macname = $getac["name"];
                            $mgrpcode = $getac["grpcode"];
                            $flatno = $getac["flatno"];
                            $mancode = $getac["mancode"];

                            $mopbal = $getac["openbal"];
                            $principal = $getac["prinamt"];
                            $intamt = $getac["interest"];
                            $mopdc = $getac["dc"];
                            if ($mopdc == "D") {
                                $mopbal = $mopbal * - 1;
                            }

                            if ($mopdc == "C") {                //  C - reverse for D   -- just for calculation
                                $principal = $principal * - 1;
                            }


                            $dprin = $dprin + $principal;
                            $dint = $dint + $intamt;


//                                            $ctran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
//                                                                  WHERE accode = '$maccode' and series = '$fseries' AND  `date`>='$mfrmdate' AND `date`<='$mtodate'
//                                                                  and source != 'INT' ORDER BY `date`, vouchno") or die(mysqli_error($conn));
//                                        print table($ctran1);
//                                            $curl = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
//                                                                 WHERE accode = '$maccode' and series = '$fseries' AND `date`>='$mfrmdate' AND `date`<='$mtodate'
//                                                                 and source != 'INT' ORDER BY `date`, vouchno") or die(mysqli_error($conn));
                            $ctran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
                                                                  WHERE accode = '$maccode' and series = '$fseries' AND  `date`>='$mfrmdate'
                                                                  AND `date`<='$mtodate' $wintcond ORDER BY `date`, vouchno") or die(mysqli_error($conn));

//                                        print table($ctran1);
                            $curl = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
                                                                 WHERE accode = '$maccode' and series = '$fseries' AND `date`>='$mfrmdate' AND
                                                                 `date`<='$mtodate' $wintcond ORDER BY `date`, vouchno") or die(mysqli_error($conn));



                            while ($murl = mysqli_fetch_object($curl)) {
                                switch ($murl->source) {
                                    case "DN";

                                        $head = "Purchase Return";

                                        $url = "debnote.php?vouchno=$murl->vouchno&method=ledger";

                                        break;

                                    case "SL";

                                        $head = "Sale";

                                        $url = "sale.php?vouchno=$murl->vouchno&method=ledger";

                                        //   browse($murl->vouchno." - SALE URL - ".$url);

                                        break;

                                    case "BILL";

                                        $head = "Bill";

                                        $url = "sale.php?vouchno=$murl->vouchno&method=ledger";

                                        //   browse($murl->vouchno." - SALE URL - ".$url);

                                        break;



                                    case "CN";

                                        $head = "Credit Note";

                                        $url = "crednote.php?vouchno=$murl->vouchno&method=ledger";

                                        break;



                                    case "BR";

                                        $url = "bankrecp.php?vouchno=$murl->vouchno&method=ledger";

                                        break;



                                    case "BP";

                                        $head = "Bank Payment";

                                        $url = "bankpay.php?$murl->vouchno&method=ledger";

                                        break;

                                    case "CR";

                                        $head = "Cash Receipt";
                                        $url = "cashrecp.php?$murl->vouchno&method=ledger";
                                        break;



                                    case "CP";

                                        $head = "Cash Payment";

                                        $url = "cashpay.php?$murl->vouchno&method=ledger";

                                        break;



                                    case "TR";

                                        $head = "Contra / Transfer";

                                        $url = "contra.php?vouchno=$murl->vouchno&method=ledger";

                                        break;



                                    case "JV";

                                        $head = "Journal Voucher";

                                        $url = "journal.php?$murl->vouchno&method=ledger";

                                        break;



                                    case "DG";

                                        $head = "Discount Given";



                                        break;



                                    case "DR";

                                        $head = "Discount Received";

                                        break;



                                    default :

                                        $head = "Other";
                                }
                            }


                            $rtran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`retdate`, '%d-%m-%Y') as mydt FROM `actran` WHERE
                                                           retdate >= '$mfrmdate' AND retdate <= '$mtodate' AND
                                                           accode = '$maccode' and series = '$fseries' ORDER BY date, vouchno") or die(mysqli_error($conn));


                            $getprin = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode='$maccode'
                                                              AND  date < '$mfrmdate' and series = '$fseries' and source != 'INT' ") or die(mysqli_error($conn));

                            $getint = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode='$maccode'
                                                              AND  date < '$mfrmdate' and series = '$fseries' and source = 'INT' ") or die(mysqli_error($conn));

                            $fetprin = mysqli_fetch_array($getprin);
                            $opprin = $fetprin['amt'];

                            $amt2 = $opprin + $opint;

                            if ($opprin < 0) {
                                $opprin = $opprin * -1;
                            }

                            $fetint = mysqli_fetch_array($getint);
                            $opint = $fetint['amt'];

                            if ($opint < 0) {
                                $opint = $opint * -1;
                            }

                            $principal = $principal + $opprin;
                            $intamt = $intamt + $opint;

                            $getopen = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode='$maccode'
                                                              AND  date < '$mfrmdate' and series = '$fseries'  ") or die(mysqli_error($conn));
                            $fetopen = mysqli_fetch_array($getopen);
                            $opbal = $fetopen['amt'];

                            $mobal = $mopbal + $opbal;

                            $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";
                            }

                            $lgetbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE accode='$maccode' and 
                                                        series = '$fseries' AND  date <= '$mtodate'") or die(mysqli_error($conn));
                            $lgetamt = mysqli_fetch_array($lgetbal);
                            $lamt = $lgetamt["amt"];
                            $lmbal = $mopbal + $lamt;

                            if ($lmbal < 0) {
                                $lmdc = "DR";
                            } else {
                                $lmdc = "CR";
                            }

                            echo "<tr class='mySearchTr' style='background-color:#EFE4B0'>";
                            if ($mgrpcode == '010') {
                                echo "<th style=\"text-align:left;\" scope=\"col\" width=\"500\" colspan='9'><b>" . strtoupper($macname) . ' - Flat No. : ' . $flatno . "</b></th>";
                                echo "<th style=\"text-align:right;\" scope=\"col\" width=\"500\" colspan='2'><b>" . abs($lmbal) . " " . $lmdc . "</b></th>";
                            } else {
                                echo "<th style=\"text-align:left;\" scope=\"col\" width=\"500\" colspan='9'><b>" . strtoupper($macname) . ' - Code : ' . $mancode . "</b></th>";
                                echo "<th style=\"text-align:right;\" scope=\"col\" width=\"500\" colspan='2'><b>" . abs($lmbal) . " " . $lmdc . "</b></th>";
                            }

                            echo "</tr>";
                            ?>

                            <tr style="color:black;" class=" mySearchTr2" >

                                <th style="text-align:left; width : 8%">Date</th>                                   
                                <th style="text-align:left; width : 3%">Source</th>                                   
                                <th style="text-align:left; width : 6%">Voucher</th>                                   
                                <?php // if($grp == '010'){               ?>
                                <th style="text-align:left; width : 8%">Receipt No</th>                                   
                                <?php // }            ?>
                                <th style="text-align:left; width : 22%">Description</th>                                   
                                <?php if ($wint == 1) { ?>
                                    <th style="text-align:right; width : 8%">Principal</th>                                   
                                    <th style="text-align:right; width : 8%">Interest</th>                                   
                                <? } ?>
                                <th style="text-align:right; width : 9%">Debit</th>                                   
                                <th style="text-align:right; width : 9%">Credit</th>                                   
                                <th style="text-align:right; width : 15%">Balance</th>                                   

                            </tr>

                            <?php
//                                        echo "<tr  class='clickableRow' href='$url'>";
                            echo "<tr  class='' href='$url'>";

                            echo "<td style=\"text-align:left;\" scope=\"col\">$vdate</td>";
                            echo "<td style=\"text-align:left;\" scope=\"col\"></td>";
                            echo "<td style=\"text-align:left;\" scope=\"col\">$mvouch</td>";
                            echo "<td style=\"text-align:left;\" scope=\"col\"></td>";
                            echo "<td style=\"text-align:left;\" scope=\"col\">$mdescrip</td>";
                            if ($wint == 1) {
                                echo "<td style=\"text-align:right;\" scope=\"col\">" . number_format($principal, 2) . "</td>";
                                echo "<td style=\"text-align:right;\" scope=\"col\">" . number_format($intamt, 2) . "</td>";
                            }
                            echo "<td style=\"text-align:right;\" scope=\"col\">" . number_format($mdebit, 2) . "</td>";
                            echo "<td style=\"text-align:right;\" scope=\"col\">" . number_format($mcredit, 2) . "</td>";

                            echo "<td style=\"text-align:right;\" scope=\"col\">" . number_format(abs($totbal), 2) . " " . $mdctot . "</td>";

                            echo "</tr>";


                            while ($row = mysqli_fetch_array($ctran1)) {
                                $vdate = $row["mydt"];
                                $msource = $row["source"];
                                $mvouch = $row["vouchno"];
                                $newbill = $row["newbill"];
                                $mamount = $row["amount"];
                                $billno = $row["billno"];
                                $flatno = $row["flatno"];
//                                                $mancode = $row["mancode"];
                                $recpno = $row["recpno"];

                                $principal = 0;
                                $intamt = 0;



                                $mgrpcode = $getac["grpcode"];
                                $mdiscprov = $getac["discount"];
                                $totdiscprov = 0;

                                $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 = "";
                                $mchqdtl2 = '';

                                switch ($msource) {
                                    case "BILL";
//                                                        $url = "purchase.php?vouchno=$mvouch&method=ledger";
//                                                        $nqry1 = mysqli_query($conn, "SELECT billno,billcycle,billcode
//                                                                              FROM `actran`   WHERE `vouchno`='$mvouch' 
//                                                                              and series = '$fseries'") or die(mysqli_error($conn));
//                                                        $nqry = mysqli_fetch_array($nqry1);
                                        $billno = 'Bill - ' . $row["newbill"];
                                        $billcode = $row["billcode"];
                                        $billcycle = $row["billcycle"];

                                        switch ($billcycle) {

                                            case 'M': $byname = 'MONTHLY';
                                                break;
                                            case 'BM': $byname = 'BI - MONTHLY';
                                                break;
                                            case 'Q': $byname = 'QUARTERLY';
                                                break;
                                            case 'H': $byname = 'HALF YEARLY';
                                                break;
                                            default : $byname = '';
                                        }

                                        $gbilcode = mysqli_query($conn, "SELECT descrip FROM $zroot.`billingcycle` Where  `billcycle`='$billcycle' and `code` = '$billcode' ") or die(mysqli_error($conn));
                                        $fetbilcode = mysqli_fetch_array($gbilcode);
                                        $descrip = $fetbilcode['descrip'];

                                        if ($billcode < 4) {
                                            $cdate = strtotime($mtodate);
                                        } else {
                                            $cdate = strtotime($mfrdate);
                                        }

                                        $chkdate = date("Y", $cdate);

                                        $head = "To Bill" . "-" . $descrip . " " . $chkdate;

                                        if ($prnnarr == 1 || $usertype == 3) {
                                            $mchqdtl = "";
                                        }

                                        if ($wint == 1) {
//                                                            browse($mvouch);
//                                                            browse($billcycle);
//                                                            browse($billcode);
//                                                            browse($maccode);
//                                                            
                                            $getint = mysqli_query($conn, "SELECT billamt, intamt from `billmain` Where  `billcycle`='$billcycle' and `billcode` = '$billcode' 
                                                                               and accode = '$maccode' and vouchno = '$mvouch' ") or die(mysqli_error($conn));
                                            $fetint = mysqli_fetch_array($getint);
                                            $principal = $fetint['billamt'];
                                            $intamt = $fetint['intamt'];

//                                                            browse($principal);

                                            $dprin = $dprin + $principal;
                                            $dint = $dint + $intamt;

                                            $mdebit = $principal + $intamt;
                                            $dtot = $dtot + $intamt;
                                            $totbal = $totbal + ($intamt * -1);
                                        }

                                        break;

                                    case "INT";
//                                                        $url = "debnote.php?vouchno=$mvouch&method=ledger";
//
//                                                        $totdiscprov = $totdiscprov + $mamount;
//                                                        $nqry1 = mysqli_query($conn, "SELECT billno,billcycle,billcode,arrears
//                                                                         FROM `billmain`   WHERE `vouchno`='$mvouch' and `billno`='$billno' and society = '$fseries'") or die(mysqli_error($conn));
//                                                        $nqry = mysqli_fetch_array($nqry1);
//
//                                                        $aramt = $nqry["arrears"];
//                                                        $head = "Interest On Arrears" . " - " . $aramt;
//
//                                                        if ($prnnarr == 1) {
//                                                            $mchqdtl = "";
//                                                        }
//
//                                                        break;

                                    case "DN";
//                                                        $head = "Debit Note";
                                        if ($mgrpcode == '010') {
                                            $nqry1 = mysqli_query($conn, "SELECT debparty FROM `deboth`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                                            $nqry = mysqli_fetch_array($nqry1);
                                            $tcredcode = $nqry["debparty"];
                                        } else {
                                            $nqry1 = mysqli_query($conn, "SELECT accode FROM `debmain`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                                            $nqry = mysqli_fetch_array($nqry1);
                                            $tcredcode = $nqry["accode"];
                                        }

                                        $getpart = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                                WHERE accode = '$tcredcode' and series = '$fseries'") or die(mysqli_error($conn));

                                        $fetpart = mysqli_fetch_array($getpart);
                                        $tcredname = $fetpart["name"];

                                        $nnar = mysqli_query($conn, "SELECT rem1,rem2 FROM `debmain`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));
                                        $fnar = mysqli_fetch_array($nnar);
                                        $rem1 = $fnar['rem1'] . " " . $fnar['rem2'];

                                        $head = $tcredname;
                                        $mnarr = $rem1;
                                        $mchqdtl = "";

                                        break;

                                    case "SL";

                                        $head = "Sale";

                                        if ($prnnarr == 1 || $usertype == 3) {

                                            $mnarr = "";

                                            $mchqdtl = "";
                                        }

                                        break;



                                    case "CN";
//                                                        $head = "Credit Note";

                                        $url = "crednote_print.php?vouchno=$mvouch&method=ledger";

                                        if ($mgrpcode == '010') {
                                            $nqry1 = mysqli_query($conn, "SELECT debparty FROM `credoth`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                                            $nqry = mysqli_fetch_array($nqry1);
                                            $tcredcode = $nqry["debparty"];
                                        } else {
                                            $nqry1 = mysqli_query($conn, "SELECT accode,rem1 FROM `credmain`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                                            $nqry = mysqli_fetch_array($nqry1);
                                            $tcredcode = $nqry["accode"];
                                        }

                                        $getpart = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                              WHERE accode = '$tcredcode' and series = '$fseries'") or die(mysqli_error($conn));

                                        $fetpart = mysqli_fetch_array($getpart);
                                        $tcredname = $fetpart["name"];

                                        $nnar = mysqli_query($conn, "SELECT rem1,rem2 FROM `credmain`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));
                                        $fnar = mysqli_fetch_array($nnar);
                                        $rem1 = $fnar['rem1'] . " " . $fnar['rem2'];

                                        $head = $tcredname;
                                        $mnarr = $rem1;
                                        $mchqdtl = "";
                                        $mchqdtl2 = "";

                                        break;

                                    case "BR";

                                        $head = "Bank Receipt";
                                        $url = "bankrecp.php?vouchno=$mvouch&method=ledger";

                                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                       `narr1`, `narr2`, bankcode FROM `bankrecp`
                                                                        WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                                        $nqry = mysqli_fetch_array($nqry1);
                                        $mbill = $nqry["chqno"];
                                        $mbilldt = $nqry["mbilldate"];
                                        $mnarr = $nqry["narr1"] . " " . $nqry["narr2"];
                                        if ($mbill != "") {
                                            $mchqdtl = "Cheque No. : " . + trim($mbill);
                                        }

                                        if ($mbilldt != '00-00-0000') {
                                            $mchqdtl2 = " &nbsp;&nbsp; Dated : " . $mbilldt;
                                        } else {
                                            $mchqdtl2 = '';
                                        }

                                        $baccode = $nqry["bankcode"];

                                        $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast`
                                                                              WHERE accode='$baccode' and series = '$fseries'") or die(mysqli_error($conn));
                                        $acfet = mysqli_fetch_array($acqry1);
                                        $acname = $acfet["name"];
                                        if ($acname != "") {
                                            $head = 'By ' . $acname;
                                        } else {
                                            $head = "";
                                        }
                                        break;

                                    case "BP";

                                        $head = "Bank Payment";

                                        $url = "bankpay.php?vouchno=$mvouch&method=ledger";


                                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                        `narr1`, `narr2` FROM `bankpay`
                                                                        WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));


                                        $nqry = mysqli_fetch_array($nqry1);
                                        $mbill = $nqry["chqno"];
                                        $mbilldt = $nqry["mbilldate"];
                                        $mnarr = $nqry["narr1"] . " " . $nqry["narr2"];
                                        if ($mbill != "") {
                                            $mchqdtl = "Cheque No. : " . + trim($mbill);
                                        }
                                        if (strtotime($mbilldt) > 0) {
                                            $mchqdtl2 = "&nbsp;&nbsp;Dated : " . $mbilldt;
                                        }
                                        $baccode = $nqry["bankcode"];

                                        $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast`
                                                                              WHERE accode='$baccode' and series = '$fseries'") or die(mysqli_error($conn));
                                        $acfet = mysqli_fetch_array($acqry1);
                                        $acname = $acfet["name"];

                                        if ($acname != "") {
                                            $head = 'To ' . $acname;
                                        } else {
                                            $head = "";
                                        }
                                        break;



                                    case "CR";

                                        $head = "Cash Receipt";

                                        $url = "cashrecp.php?vouchno=$mvouch&method=ledger";


                                        $nqry1 = mysqli_query($conn, "SELECT `narr1`, `narr2`, bankcode  FROM `cashrecp`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                                        $nqry = mysqli_fetch_array($nqry1);
                                        $mnarr = $nqry["narr1"];
                                        $baccode = $nqry["bankcode"];

                                        $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast`
                                                                              WHERE accode='$baccode' and series = '$fseries'") or die(mysqli_error($conn));
                                        $acfet = mysqli_fetch_array($acqry1);
                                        $acname = $acfet["name"];

                                        if ($acname != "") {
                                            $head = 'By ' . $acname;
                                        } else {
                                            $head = "";
                                        }
                                        break;

                                    case "CP";

                                        $head = "Cash Payment";

                                        $url = "cashpay.php?vouchno=$mvouch&method=ledger";


                                        $nqry1 = mysqli_query($conn, "SELECT `narr1`, `narr2`, bankcode FROM `cashpay`
                                                                             WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                                        $nqry = mysqli_fetch_array($nqry1);
                                        $mnarr = $nqry["narr1"];
                                        $baccode = $nqry["bankcode"];

                                        $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast`
                                                                              WHERE accode='$baccode' and series = '$fseries'") or die(mysqli_error($conn));
                                        $acfet = mysqli_fetch_array($acqry1);
                                        $acname = $acfet["name"];

                                        if ($acname != "") {
                                            $head = 'To ' . $acname;
                                        } else {
                                            $head = "";
                                        }
                                        break;

                                    case "TR";

                                        $head = "Contra / Transfer";

                                        $url = "contra.php?vouchno=$mvouch&method=ledger";

                                        if ($prnnarr == 1 || $usertype == 3) {

                                            $nqry1 = mysqli_query($conn, "SELECT `narr1`, `narr2` FROM `contra`
                                                                             WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));


                                            $nqry = mysqli_fetch_array($nqry1);
                                            $mnarr = $nqry["narr1"] . " " . $nqry["narr2"];
                                        }

                                        break;

                                    case "JV";

//                                                        $head = "Journal Voucher";
                                        if ($mgrpcode == '010') {
                                            $nqry1 = mysqli_query($conn, "SELECT accode FROM `jvtran`
                                                                              WHERE vouchno='$mvouch' and debit > 0 and series = '$fseries'") or die(mysqli_error($conn));

                                            $nqry = mysqli_fetch_array($nqry1);
                                            $tcredcode = $nqry["accode"];
                                        } else {
                                            $nqry1 = mysqli_query($conn, "SELECT accode FROM `jvtran`
                                                                              WHERE vouchno='$mvouch' and credit > 0 and series = '$fseries'") or die(mysqli_error($conn));

                                            $nqry = mysqli_fetch_array($nqry1);
                                            $tcredcode = $nqry["accode"];
                                        }

                                        $getpart = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                              WHERE accode = '$tcredcode' and series = '$fseries'") or die(mysqli_error($conn));

                                        $fetpart = mysqli_fetch_array($getpart);
                                        $tcredname = $fetpart["name"];

                                        $nnar = mysqli_query($conn, "SELECT rem1,rem2 FROM `jvmain`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));
                                        $fnar = mysqli_fetch_array($nnar);
                                        $rem1 = $fnar['rem1'] . " " . $fnar['rem2'];

                                        $head = $tcredname;
                                        $mnarr = $rem1;
                                        $mchqdtl = "";
                                        $mchqdtl2 = "";


                                        break;

                                    case "SUPP";

                                        $head = "SUPPLEMENTARY BILL";

                                        $url = "suppbill.php?vouchno=$mvouch&method=ledger";


                                        $nqry1 = mysqli_query($conn, "SELECT `firsttran`, `rem1`, `rem2` FROM `suppmain`
                                                                             WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                                        $nqry = mysqli_fetch_array($nqry1);
                                        $mnarr = $nqry["rem1"] . " " . $nqry['rem2'];
                                        $firsttran = $nqry["firsttran"];

                                        $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast`
                                                                           WHERE accode='$firsttran' and series = '$fseries'") or die(mysqli_error($conn));
                                        $acfet = mysqli_fetch_array($acqry1);
                                        $acname = $acfet["name"];

                                        if ($acname != "") {
                                            $head = 'To ' . $acname;
                                        } else {
                                            $head = "";
                                        }
                                        break;

                                    case "DG";

                                        $head = "Discount Given";

                                        break;

                                    case "DR";

                                        $head = "Discount Received";

                                        break;

                                    default :

                                        $head = "Other";
                                }


                                if ($msource == 'BILL') {
                                    $action = 'bill_print_fr.php';
                                } elseif ($msource == 'BR') {
                                    $action = 'bankrecp_fr.php';
                                } elseif ($msource == 'BP') {
                                    $action = 'bankpay_fr.php';
                                } elseif ($msource == 'CR') {
                                    $action = 'cashrecp_fr.php';
                                } elseif ($msource == 'CP') {
                                    $action = 'cashpay_fr.php';
                                } elseif ($msource == 'SUPP') {
                                    $action = 'suppbl_print.php';
                                } elseif ($msource == 'CN') {
                                    $action = '';
                                }



                                echo "<tr class='clickableRow clickable-row2' data-href='$action?view=view&mvouch=$mvouch&fdt=$vdate' [target]='_blank' href='$url'>";

                                echo "<td style=\"text-align:left;\" scope=\"col\">$vdate</td>";
                                echo "<td style=\"text-align:left;\" scope=\"col\">$msource</td>";

                                if ($msource == 'BILL') {
                                    echo "<td style=\"text-align:left;\" scope=\"col\">$newbill</td>";
                                } else {
                                    echo "<td style=\"text-align:left;\" scope=\"col\">$mvouch</td>";
                                }

                                echo "<td style=\"text-align:left;\" scope=\"col\">$recpno</td>";
                                if ($prnnarr == 1 || $usertype == 3) {
                                    echo "<td style=\"text-align:left;\" scope=\"col\">$head<br>$mchqdtl $mchqdtl2<br>$mnarr</td>";
                                } else {
                                    echo "<td style=\"text-align:left;\" scope=\"col\">$head</td>";
                                }
                                if ($wint == 1) {
                                    echo "<td style=\"text-align:right;\" scope=\"col\">" . number_format($principal, 2) . "</td>";
                                    echo "<td style=\"text-align:right;\" scope=\"col\">" . number_format($intamt, 2) . "</td>";
                                }
                                echo "<td style=\"text-align:right;\" scope=\"col\">" . number_format($mdebit, 2) . "</td>";
                                echo "<td style=\"text-align:right;\" scope=\"col\">" . number_format($mcredit, 2) . "</td>";

                                echo "<td style=\"text-align:right;\" scope=\"col\">" . number_format(abs($totbal), 2) . " " . $mdctot . "</td>";

                                echo "</tr>";
                            }

                            echo "<tr >";

                            echo "<td style=\"text-align:left;\" scope=\"col\"></td>";
                            echo "<td style=\"text-align:left;\" scope=\"col\"></td>";
                            echo "<td style=\"text-align:left;\" scope=\"col\"></td>";
                            echo "<td style=\"text-align:left;\" scope=\"col\"></td>";
                            echo "<td style=\"text-align:right;\" scope=\"col\"><strong>Total</strong></td>";

                            if ($wint == 1) {
                                echo "<td style=\"text-align:right;\" scope=\"col\">" . number_format($dprin, 2) . "</td>";
                                echo "<td style=\"text-align:right;\" scope=\"col\">" . number_format($dint, 2) . "</td>";
                            }
                            echo "<td style=\"text-align:right;\" scope=\"col\">" . number_format($dtot, 2) . "</td>";
                            echo "<td style=\"text-align:right;\" scope=\"col\">" . number_format($ctot, 2) . "</td>";

                            if ($totbal <= 0) {
                                echo "<td style=\"text-align:right;\" scope=\"col\"><font color='red'><strong>" . number_format(abs($totbal), 2) . " " . $mdctot . "</strong></font></td>";
                            } else {
                                echo "<td style=\"text-align:right;\" scope=\"col\"><font color='blue'><strong>" . number_format(abs($totbal), 2) . " " . $mdctot . "</strong></font></td>";
                            }
                            echo "</tr>";
                        }
                    }

                    if ($dtype == 1) {
                        echo '</tr>';
                        echo "<tr style='background-color:#F0AD4E'>";
                        echo "<td align='right'><strong></strong></td>";
                        echo "<td align='right'><strong>Total</strong></td>";
                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><strong>" . abs($totbal) . " " . $mdctot . "</strong></td>";
                        echo "<tr>";
                    }
                    ?>

                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="9">
                        </td>
                    </tr>

                </tfoot>

            </table>

        </div>

    </div>

    </div>

    </div>
    </div>
    <div class="footer">
    </div>
    </div>



    <?php include("includes/linksfoot.php"); ?>


    </body>

    </html>

    <?php
}
if (isset($_POST["excel"])) {
    $gcond = "";
    $accond = "";
    $custcond = "";
    $flatcond = "";
    $grpcond = "";

    $para = $_POST['para'];
    if ($para == 'M') {
        $grpcond = " and grpcode = '010' ";
    } else {
        $grpcond = " and grpcode != '010' ";
    }


    if (!empty($_POST["group"])) {
        $grp = trim($_POST["group"]);
        $gcond = "AND `grpcode`='$grp'";
    }

    $accond = '';
    if ($_POST["flag"] == 'M') {
        $acname = $_POST['acname'];
        $custcond = " AND `accode` = '$acname' ";
        $accond = " AND `accode` = '$acname' ";
    } else {
        if (!empty($_POST['acname'])) {

            foreach ($_POST['acname'] as $cucheck2) {
                $ccheck2[$ccount2++] .= "OR `accode` = '" . $cucheck2 . "' ";
                $ccheckoth[$ccountoth++] .= "OR `accode` = '" . $cucheck2 . "' ";
            }

            foreach ($ccheck2 as $g => $cusvalues2) {
                $cuscheck2 .= $cusvalues2;
            }
            foreach ($ccheckoth as $g => $valuesoth) {
                $cuscheckoth .= $valuesoth;
            }

            $custcond = " AND (`accode` = '' " . $cuscheck2 . " )";
            $accond = " AND (`accode` = '' " . $cuscheckoth . " )";
        }
    }

    if (!empty($_POST["flatno"])) {
        $flatno = $_POST["flatno"];
        $flatcond = " AND flatno = '$flatno' ";
    }

    $mfrmdate = $_POST["frmdt"];
    $mtodate = $_POST["todt"];


    if ($mfrmdate > $mtodate) {
        browse("To Date Cannot Be Less Than From Date");
        header("refresh: 1; ledger.php");
        exit();
    }


    $prnzero = $_POST["zerobal"];
    $prnnarr = $_POST["prinarr"];


    if ($_POST["dtype"] == 1) {
        $repname = "Ledger - Summary";
        $dtype = 1;
    } else {
        $repname = "Ledger - Detail";
        $dtype = 2;
    }

    if ($_POST["scope"] == 1) {
        $wint = 1;
        $wintcond = "AND source !='INT'";
    } else {
        $wint = 0;
    }


    $acc = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `series` = '$fseries' and `grpcode` != '001' and `grpcode` != '002'
                       $grpcond $flatcond $gcond $accond order by mancode+0") or die(mysqli_error($conn));

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


    if ($dtype == 1) {

        $contents .= "Account Name,  Flat No. / Man. Code, Balance \n";
    }
    $totbal = 0;
    $url = '';
    while ($getac = mysqli_fetch_array($acc)) {
        if ($dtype == 1) {

            $maccode = $getac["accode"];

            $mgrpcode = $getac["grpcode"];
            $flatno = $getac["flatno"];
            $mancode = $getac["mancode"];
            $mopbal = $getac["openbal"];
            $mprincipal = $getac["prinamt"];
            $mopdc = $getac["dc"];
            if ($mopdc == "D") {
                $mopbal = $mopbal * - 1;
            }

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


            ############PARTY NAME####################


            $getpartyname = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$maccode' and series = '$fseries'") or die(mysqli_error($conn));
            $zname = mysqli_fetch_object($getpartyname);
            $mname = $zname->name;

            ############PARTY NAME####################

            $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE `series`='$fseries' and `accode`='$maccode' AND  date < '$mtodate'") or die(mysqli_error($conn));
            $getamt = mysqli_fetch_array($getbal);
            $amt = $getamt["amt"];
            $mbal = $mopbal + $amt;

            if ($mbal < 0) {
                $mdc = "DR";
            } else {
                $mdc = "CR";
            }

            $show = 1;
            if ($prnzero == 1) {

                $show = 1;
            } else {

                if ($mbal == 0) {

                    $show = 0;
                }
            }

            if ($show == 1) {
                if ($mgrpcode == '010') {
                    $contents .= $mname . ",";
                    $contents .= $flatno . ",";
                } else {
                    $contents .= $mname . ",";
                    $contents .= $mancode . ",";
                }
                $contents .= abs($mbal) . " " . $mdc . "\n";

                $totbal = $totbal + $mbal;
            }

            if ($totbal < 0) {

                $mdctot = "DR";
            } else {

                $mdctot = "CR";
            }
        } else {

            /** Detail Part* */
            $dprin = 0;
            $dint = 0;
            $totbal = 0;
            $dtot = 0;
            $ctot = 0;

            $maccode = $getac["accode"];
            $macname = $getac["name"];
            $mgrpcode = $getac["grpcode"];
            $flatno = $getac["flatno"];
            $mancode = $getac["mancode"];

            $mopbal = $getac["openbal"];
            $principal = $getac["prinamt"];
            $intamt = $getac["interest"];
            $mopdc = $getac["dc"];
            if ($mopdc == "D") {
                $mopbal = $mopbal * - 1;
            }

            if ($mopdc == "C") {                //  C - reverse for D   -- just for calculation
                $principal = $principal * - 1;
            }

            $dprin = $dprin + $principal;
            $dint = $dint + $intamt;


            $ctran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
                                                                  WHERE accode = '$maccode' and series = '$fseries' AND  `date`>='$mfrmdate' AND `date`<='$mtodate'
                                                                  $wintcond ORDER BY `date`, vouchno") or die(mysqli_error($conn));

//                                        print table($ctran1);
            $curl = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
                                                                 WHERE accode = '$maccode' and series = '$fseries' AND `date`>='$mfrmdate' AND `date`<='$mtodate'
                                                                 $wintcond ORDER BY `date`, vouchno") or die(mysqli_error($conn));



            while ($murl = mysqli_fetch_object($curl)) {
                switch ($murl->source) {
                    case "DN";

                        $head = "Purchase Return";

                        $url = "debnote.php?vouchno=$murl->vouchno&method=ledger";

                        break;

                    case "SL";

                        $head = "Sale";

//                        $url = "sale.php?vouchno=$murl->vouchno&method=ledger";
                        //   browse($murl->vouchno." - SALE URL - ".$url);

                        break;

                    case "BILL";

                        $head = "Bill";

//                        $url = "sale.php?vouchno=$murl->vouchno&method=ledger";
                        //   browse($murl->vouchno." - SALE URL - ".$url);

                        break;



                    case "CN";

                        $head = "Sale Return";

                        $url = "crednote.php?vouchno=$murl->vouchno&method=ledger";

                        break;



                    case "BR";

                        $url = "bankrecp.php?vouchno=$murl->vouchno&method=ledger";

                        break;



                    case "BP";

                        $head = "Bank Payment";

                        $url = "bankpay.php?$murl->vouchno&method=ledger";

                        break;

                    case "CR";

                        $head = "Cash Receipt";
                        $url = "cashrecp.php?$murl->vouchno&method=ledger";
                        break;



                    case "CP";

                        $head = "Cash Payment";

                        $url = "cashpay.php?$murl->vouchno&method=ledger";

                        break;



                    case "TR";

                        $head = "Contra / Transfer";

                        $url = "contra.php?vouchno=$murl->vouchno&method=ledger";

                        break;



                    case "JV";

                        $head = "Journal Voucher";

//                        $url = "journal.php?$murl->vouchno&method=ledger";

                        break;



                    case "DG";

                        $head = "Discount Given";



                        break;



                    case "DR";

                        $head = "Discount Received";

                        break;



                    default :

                        $head = "Other";
                }
            }


            $rtran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`retdate`, '%d-%m-%Y') as mydt FROM `actran` WHERE
                                                           retdate >= '$mfrmdate' AND retdate <= '$mtodate' AND
                                                           accode = '$maccode' and series = '$fseries' ORDER BY date, vouchno") or die(mysqli_error($conn));


            $getprin = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode='$maccode'
                                                              AND  date < '$mfrmdate' and series = '$fseries' and source != 'INT' ") or die(mysqli_error($conn));

            $getint = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode='$maccode'
                                                              AND  date < '$mfrmdate' and series = '$fseries' and source = 'INT' ") or die(mysqli_error($conn));

            $fetprin = mysqli_fetch_array($getprin);
            $opprin = $fetprin[amt];

            $amt2 = $opprin + $opint;

            if ($opprin < 0) {
                $opprin = $opprin * -1;
            }

            $fetint = mysqli_fetch_array($getint);
            $opint = $fetint[amt];

            if ($opint < 0) {
                $opint = $opint * -1;
            }

            $principal = $principal + $opprin;
            $intamt = $intamt + $opint;

            $getopen = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode='$maccode'
                                                              AND  date < '$mfrmdate' and series = '$fseries'  ") or die(mysqli_error($conn));
            $fetopen = mysqli_fetch_array($getopen);
            $opbal = $fetopen[amt];

            $mobal = $mopbal + $opbal;

            $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";
            }

            $lgetbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE accode='$maccode' and 
                                                        series = '$fseries' AND  date <= '$mtodate'") or die(mysqli_error($conn));
            $lgetamt = mysqli_fetch_array($lgetbal);
            $lamt = $lgetamt["amt"];
            $lmbal = $mopbal + $lamt;

            if ($lmbal < 0) {
                $lmdc = "DR";
            } else {
                $lmdc = "CR";
            }

            if ($mgrpcode == '010') {
                $contents .= strtoupper($macname) . ' - Flat No. : ' . $flatno . ",";
                $contents .= abs($lmbal) . " " . $lmdc . "\n";
            } else {
                $contents .= strtoupper($macname) . ' - Code : ' . $mancode . ",";
                $contents .= abs($lmbal) . " " . $lmdc . "\n";
            }

            $contents .= "Date, Source, Voucher, Receipt No, Description, Principal, Interest, Debit, Credit, Balance\n";
//            if ($wint == 1) {
//                $contents.="Date, Source, Voucher, Receipt No, Description, Principal, Interest, Debit, Credit, Balance \n";
//            }
//            $contents .= $url . ",";
            $contents .= "" . ",";
            $contents .= $mvouch . ",";
            $contents .= "" . ",";
//            $contents .= $mdescrip . "\n";
            if ($wint == 1) {
                $content .= number_format($principal, 2) . ",";
                $content .= number_format($intamt, 2) . ",";
            }
//            $contents .= $url . ",";
            $contents .= $vdate . ",";
            $contents .= "" . ",";
            $contents .= $mvouch . ",";
            $contents .= "" . ",";
            $contents .= $mdescrip . ",";
            if ($wint == 1) {
                $contents .= number_format($principal, 2) . ",";
                $contents .= number_format($intamt, 2) . ",";
            }
            $contents .= number_format($mdebit, 2) . ",";
            $contents .= number_format($mcredit, 2) . ",";
            $contents .= number_format(abs($totbal), 2) . " " . $mdctot . "\n";

            while ($row = mysqli_fetch_array($ctran1)) {
                $vdate = $row["mydt"];
                $msource = $row["source"];
                $mvouch = $row["vouchno"];
                $newbill = $row["newbill"];
                $mamount = $row["amount"];
                $billno = $row["billno"];
                $flatno = $row["flatno"];
                $recpno = $row["recpno"];

                $principal = 0;
                $intamt = 0;



                $mgrpcode = $getac["grpcode"];
                $mdiscprov = $getac["discount"];
                $totdiscprov = 0;

                $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 = "";


                switch ($msource) {
                    case "BILL";
//                                                        $url = "purchase.php?vouchno=$mvouch&method=ledger";
//                                                        $nqry1 = mysqli_query($conn, "SELECT billno,billcycle,billcode
//                                                                              FROM `actran`   WHERE `vouchno`='$mvouch' 
//                                                                              and series = '$fseries'") or die(mysqli_error($conn));
//                                                        $nqry = mysqli_fetch_array($nqry1);
                        $billno = 'Bill - ' . $row["newbill"];
                        $billcode = $row["billcode"];
                        $billcycle = $row["billcycle"];

                        switch ($billcycle) {

                            case 'M': $byname = 'MONTHLY';
                                break;
                            case 'BM': $byname = 'BI - MONTHLY';
                                break;
                            case 'Q': $byname = 'QUARTERLY';
                                break;
                            case 'H': $byname = 'HALF YEARLY';
                                break;
                            default : $byname = '';
                        }

                        $gbilcode = mysqli_query($conn, "SELECT descrip FROM $zroot.`billingcycle` Where  `billcycle`='$billcycle' and `code` = '$billcode' ") or die(mysqli_error($conn));
                        $fetbilcode = mysqli_fetch_array($gbilcode);
                        $descrip = $fetbilcode['descrip'];

                        if ($billcode < 4) {
                            $cdate = strtotime($mtodate);
                        } else {
                            $cdate = strtotime($mfrdate);
                        }

                        $chkdate = date("Y", $cdate);

                        $head = "To Bill" . "-" . $descrip . " " . $chkdate;

                        if ($prnnarr == 1) {
                            $mchqdtl = "";
                        }

                        if ($wint == 1) {
//                                                            browse($mvouch);
//                                                            browse($billcycle);
//                                                            browse($billcode);
//                                                            browse($maccode);
//                                                            
                            $getint = mysqli_query($conn, "SELECT billamt, intamt from `billmain` Where  `billcycle`='$billcycle' and `billcode` = '$billcode' 
                                                                               and accode = '$maccode' and vouchno = '$mvouch' ") or die(mysqli_error($conn));
                            $fetint = mysqli_fetch_array($getint);
                            $principal = $fetint['billamt'];
                            $intamt = $fetint['intamt'];

//                                                            browse($principal);

                            $dprin = $dprin + $principal;
                            $dint = $dint + $intamt;

                            $mdebit = $principal + $intamt;
                            $dtot = $dtot + $intamt;
                            $totbal = $totbal + ($intamt * -1);
                        }

                        break;

                    case "INT";
//                                                        $url = "debnote.php?vouchno=$mvouch&method=ledger";
//
//                                                        $totdiscprov = $totdiscprov + $mamount;
//                                                        $nqry1 = mysqli_query($conn, "SELECT billno,billcycle,billcode,arrears
//                                                                         FROM `billmain`   WHERE `vouchno`='$mvouch' and `billno`='$billno' and society = '$fseries'") or die(mysqli_error($conn));
//                                                        $nqry = mysqli_fetch_array($nqry1);
//
//                                                        $aramt = $nqry["arrears"];
//                                                        $head = "Interest On Arrears" . " - " . $aramt;
//
//                                                        if ($prnnarr == 1) {
//                                                            $mchqdtl = "";
//                                                        }
//
//                                                        break;

                    case "DN";
//                                                        $head = "Debit Note";
                        if ($mgrpcode == '010') {
                            $nqry1 = mysqli_query($conn, "SELECT debparty FROM `deboth`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $tcredcode = $nqry["debparty"];
                        } else {
                            $nqry1 = mysqli_query($conn, "SELECT accode FROM `debmain`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $tcredcode = $nqry["accode"];
                        }

                        $getpart = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                                WHERE accode = '$tcredcode' and series = '$fseries'") or die(mysqli_error($conn));

                        $fetpart = mysqli_fetch_array($getpart);
                        $tcredname = $fetpart["name"];

                        $nnar = mysqli_query($conn, "SELECT rem1,rem2 FROM `debmain`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));
                        $fnar = mysqli_fetch_array($nnar);
                        $rem1 = $fnar['rem1'] . $fnar['rem2'];

                        $head = $tcredname;
                        $mnarr = $rem1;
                        $mchqdtl = "";

                        break;

                    case "SL";

                        $head = "Sale";

                        if ($prnnarr == 1) {

                            $mnarr = "";

                            $mchqdtl = "";
                        }

                        break;



                    case "CN";
//                                                        $head = "Credit Note";

                        if ($mgrpcode == '010') {
                            $nqry1 = mysqli_query($conn, "SELECT debparty FROM `credoth`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $tcredcode = $nqry["debparty"];
                        } else {
                            $nqry1 = mysqli_query($conn, "SELECT accode,rem1 FROM `credmain`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $tcredcode = $nqry["accode"];
                        }

                        $getpart = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                              WHERE accode = '$tcredcode' and series = '$fseries'") or die(mysqli_error($conn));

                        $fetpart = mysqli_fetch_array($getpart);
                        $tcredname = $fetpart["name"];

                        $nnar = mysqli_query($conn, "SELECT rem1,rem2 FROM `credmain`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));
                        $fnar = mysqli_fetch_array($nnar);
                        $rem1 = $fnar['rem1'] . $fnar['rem2'];

                        $head = $tcredname;
                        $mnarr = $rem1;
                        $mchqdtl = "";

                        break;

                    case "BR";

                        $head = "Bank Receipt";
                        $url = "bankrecp.php?vouchno=$mvouch&method=ledger";

                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                       `narr1`, `narr2`, bankcode FROM `bankrecp`
                                                                        WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                        $nqry = mysqli_fetch_array($nqry1);
                        $mbill = $nqry["chqno"];
                        $mbilldt = $nqry["mbilldate"];
                        $mnarr = $nqry["narr1"];
                        if ($mbill != '') {
                            $mchqdtl = "Cheque No. : " . + trim($mbill);
                        }
                        if (strtotime($mbilldt) > 0) {
                            $mchqdtl2 = " &nbsp;&nbsp;Dated : " . $mbilldt;
                        }
//                        $mchqdtl = "Cheque No. : " . + trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;

                        $baccode = $nqry["bankcode"];

                        $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast`
                                                                              WHERE accode='$baccode' and series = '$fseries'") or die(mysqli_error($conn));
                        $acfet = mysqli_fetch_array($acqry1);
                        $acname = $acfet["name"];

                        $head = 'By ' . $acname;

                        break;

                    case "BP";

                        $head = "Bank Payment";

                        $url = "bankpay.php?vouchno=$mvouch&method=ledger";


                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                        `narr1`, `narr2` FROM `bankpay`
                                                                        WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));


                        $nqry = mysqli_fetch_array($nqry1);
                        $mbill = $nqry["chqno"];
                        $mbilldt = $nqry["mbilldate"];
                        $mnarr = $nqry["narr1"];
                        if ($mbill != '') {
                            $mchqdtl = "Cheque No. : " . + trim($mbill);
                        }
                        if (strtotime($mbilldt) > 0) {
                            $mchqdtl2 = " &nbsp;&nbsp;Dated : " . $mbilldt;
                        }
//                        $mchqdtl = "Cheque No. : " . + trim($mbill) . " &nbsp;&nbsp;Dated : " . $mbilldt;
                        $baccode = $nqry["bankcode"];

                        $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast`
                                                                              WHERE accode='$baccode' and series = '$fseries'") or die(mysqli_error($conn));
                        $acfet = mysqli_fetch_array($acqry1);
                        $acname = $acfet["name"];

                        $head = 'To ' . $acname;

                        break;



                    case "CR";

                        $head = "Cash Receipt";

                        $url = "cashrecp.php?vouchno=$mvouch&method=ledger";


                        $nqry1 = mysqli_query($conn, "SELECT `narr1`, `narr2`, bankcode  FROM `cashrecp`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                        $nqry = mysqli_fetch_array($nqry1);
                        $mnarr = $nqry["narr1"];
                        $baccode = $nqry["bankcode"];

                        $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast`
                                                                              WHERE accode='$baccode' and series = '$fseries'") or die(mysqli_error($conn));
                        $acfet = mysqli_fetch_array($acqry1);
                        $acname = $acfet["name"];

                        $head = 'By ' . $acname;

                        break;

                    case "CP";

                        $head = "Cash Payment";

                        $url = "cashpay.php?vouchno=$mvouch&method=ledger";


                        $nqry1 = mysqli_query($conn, "SELECT `narr1`, `narr2`, bankcode FROM `cashpay`
                                                                             WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                        $nqry = mysqli_fetch_array($nqry1);
                        $mnarr = $nqry["narr1"];
                        $baccode = $nqry["bankcode"];

                        $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast`
                                                                              WHERE accode='$baccode' and series = '$fseries'") or die(mysqli_error($conn));
                        $acfet = mysqli_fetch_array($acqry1);
                        $acname = $acfet["name"];

                        $head = 'To ' . $acname;

                        break;

                    case "TR";

                        $head = "Contra / Transfer";

                        $url = "contra.php?vouchno=$mvouch&method=ledger";

                        if ($prnnarr == 1) {

                            $nqry1 = mysqli_query($conn, "SELECT `narr1`, `narr2` FROM `contra`
                                                                             WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));


                            $nqry = mysqli_fetch_array($nqry1);
                            $mnarr = $nqry["narr1"];
                        }

                        break;

                    case "JV";

//                                                        $head = "Journal Voucher";
                        if ($mgrpcode == '010') {
                            $nqry1 = mysqli_query($conn, "SELECT accode FROM `jvtran`
                                                                              WHERE vouchno='$mvouch' and debit > 0 and series = '$fseries'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $tcredcode = $nqry["accode"];
                        } else {
                            $nqry1 = mysqli_query($conn, "SELECT accode FROM `jvtran`
                                                                              WHERE vouchno='$mvouch' and credit > 0 and series = '$fseries'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $tcredcode = $nqry["accode"];
                        }

                        $getpart = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                              WHERE accode = '$tcredcode' and series = '$fseries'") or die(mysqli_error($conn));

                        $fetpart = mysqli_fetch_array($getpart);
                        $tcredname = $fetpart["name"];

                        $nnar = mysqli_query($conn, "SELECT rem1,rem2 FROM `jvmain`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));
                        $fnar = mysqli_fetch_array($nnar);
                        $rem1 = $fnar['rem1'] . $fnar['rem2'];

                        $head = $tcredname;
                        $mnarr = $rem1;
                        $mchqdtl = "";


                        break;


                    case "DG";

                        $head = "Discount Given";

                        break;

                    case "DR";

                        $head = "Discount Received";

                        break;

                    default :

                        $head = "Other";
                }


                if ($msource == 'BILL') {
                    $action = 'bill_print_fr.php';
                } elseif ($msource == 'BR') {
                    $action = 'bankrecp_fr.php';
                } elseif ($msource == 'BP') {
                    $action = 'bankpay_fr.php';
                } elseif ($msource == 'CR') {
                    $action = 'cashrecp_fr.php';
                } elseif ($msource == 'CP') {
                    $action = 'cashpay_fr.php';
                }

                $contents .= $vdate . ",";
                $contents .= $msource . ",";
//                echo "<tr class='clickableRow clickable-row2' data-href='$action?view=view&mvouch=$mvouch&fdt=$vdate' [target]='_blank' href='$url'>";

                if ($msource == 'BILL') {
                    $contents .= $newbill . ",";
                } else {
                    $contents .= $mvouch . ",";
                }
                $contents .= $recpno . ",";
                if ($prnnarr == 1) {
                    $contents .= "$head<br>$mchqdtl $mchqdtl2<br>$mnarr" . ",";
                } else {
                    $contents .= $head . ",";
                }
                if ($wint == 1) {
                    $contents .= number_format($principal, 2) . ",";
                    $contents .= number_format($intamt, 2) . ",";
                }
                $contents .= number_format($mdebit, 2) . ",";
                $contents .= number_format($mcredit, 2) . ",";
                $contents .= number_format(abs($totbal), 2) . " " . $mdctot . "\n";



//                                      **********************************check return*********************************
//                $retdate = mysqli_query($conn, "SELECT *, DATE_FORMAT(`retdate`, '%d-%m-%Y') as mydt  FROM actran where source = '$msource' and chqret = 'Y' 
//                                                                    and retdate != '0000-00-00' and accode = '$maccode'")or die(mysqli_error($conn));
//
//                $getno = mysqli_num_rows($retdate);
//                if ($getno > 0) {
//                    $freturn = mysqli_fetch_object($retdate);
//
//                    if ($msource == "BR") {
//                        $mdebit = $freturn->amount;
//                        $totbal = $totbal - $mdebit;
//                        $mcredit = 0;
//                    } else if ($msource == "BP") {
//                        $mcredit = $freturn->amount;
//                        $totbal = $totbal - $mcredit;
//                        $mdebit = 0;
//                    }
//
//                    
//                    $content.= $vdate . ",";
//                    $content.= $msource . ",";
//                    $content.= $mvouch . ",";
//                    $content.= $recpno . ",";
//                    $content.= $head . ",";
//                    $content.=  ",,";
//                    $content.=  $mdebit . ",";
//                    $content.=  $mcreit . ",";
//                    $content.=  $totbal.$mdctot . "\n";
//                    
////                    $content.=  ",,,,,Chq. No. : $freturn->chqno" . "\n";
////                    $content.=  ",,,,,Dated : $freturn->mydt" . "\n";
////                    $content.=  ",,,,,* * * ( CHEQUE RETURN ) * * * " . "\n";
//                    
//                }
//                                            ***************************cheque return***************************************
            }

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

            if ($wint == 1) {
                $contents .= number_format($dprin, 2) . ",";
                $contents .= number_format($dint, 2) . ",";
            }
            $contents .= number_format($dtot, 2) . ",";
            $contents .= number_format($ctot, 2) . ",";
            if ($totbal <= 0) {
                $contents .= number_format(abs($totbal), 2) . " " . $mdctot . "\n";
            } else {
                $contents .= number_format(abs($totbal), 2) . " " . $mdctot . "\n";
            }
        }
    }

    if ($dtype == 1) {
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= abs($totbal) . " " . $mdctot . "\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;
    }
}
if (isset($_POST["subpdf"])) {
    require("library/fpdf/fpdf.php");

    class pdf extends fpdf {

        function Footer() {



            //Position at 1.5 cm from bottom

            $this->SetY(-15);

            //Arial italic 8

            $this->SetFont('Arial', 'I', 8);

            //Page number

            $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }

    }

    $ln = 0;
    $pagerow = 25;
    $pdf = new pdf('L', 'mm', 'A4');
    $pdf->AliasNbPages();
    $pdf->AddPage();


    $pdf->SetFont('Arial', 'B', 9);

    $pdf->SetTextColor(0, 0, 0);
    
    include('includes/head_rep_pdf.php');
    $ht = 6;

    $pdf->SetTextColor(0, 0, 0);
//    $pdf->SetFillColor(239, 228, 176);
    
    if ($dtype == 1) {
        $pdf->Cell(200, $ht, 'Account Name', 1, 0, '', 1);
        $pdf->Cell(40, $ht, 'Flat No. / Man. Code', 1, 0, '', 1);
        $pdf->Cell(40, $ht, 'Balance', 1, 0, 'R', 1);
        $pdf->Ln();
        $ln++;
    }

    $totbal = 0;
    $url = '';
    while ($getac = mysqli_fetch_array($acc)) {
        if ($dtype == 1) {

            $maccode = $getac["accode"];

            $mgrpcode = $getac["grpcode"];
            $flatno = $getac["flatno"];
            $mancode = $getac["mancode"];
            $mopbal = $getac["openbal"];
            $mprincipal = $getac["prinamt"];
            $mopdc = $getac["dc"];

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

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

            ############PARTY NAME####################

            $getpartyname = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$maccode' and series = '$fseries'") or die(mysqli_error($conn));
            $zname = mysqli_fetch_object($getpartyname);
            $mname = $zname->name;

            ############PARTY NAME####################

            $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE `series`='$fseries' and `accode`='$maccode' AND  date < '$mtodate'") or die(mysqli_error($conn));
            $getamt = mysqli_fetch_array($getbal);
            $amt = $getamt["amt"];
            $mbal = $mopbal + $amt;

            if ($mbal < 0) {
                $mdc = "DR";
            } else {
                $mdc = "CR";
            }

            $show = 1;
            if ($prnzero == 1) {

                $show = 1;
            } else {

                if ($mbal == 0) {

                    $show = 0;
                }
            }
            if ($show == 1) {
                if ($mgrpcode == '010') {
                    $pdf->SetFont('Arial', '', 10);
                    $pdf->SetTextColor(0, 0, 0);
                    $pdf->Cell(200, $ht, $mname, 1, 0);
                    $pdf->Cell(40, $ht, $flatno, 1, 0);
                } else {
                    $pdf->SetFont('Arial', '', 10);
                    $pdf->SetTextColor(0, 0, 0);
                    $pdf->Cell(200, $ht, $mname, 1, 0);
                    $pdf->Cell(40, $ht, $mancode, 1, 0);
                }
                
                $pdf->Cell(40, $ht, number_format(abs($mbal),2) . " " . $mdc, 1, 0, "R");
                $pdf->Ln();
                $ln++;
                if ($ln > $pagerow) {
                    $ln = 0;
                    include('includes/head_rep_pdf.php');

                    $pdf->Cell(200, $ht, 'Account Name', 1, 0, '', 1);
                    $pdf->Cell(40, $ht, 'Flat No. / Man. Code', 1, 0, '', 1);
                    $pdf->Cell(40, $ht, 'Balance', 1, 0, 'R', 1);
                    $pdf->Ln();
                    $ln++;
                    
                }

                $totbal = $totbal + $mbal;
            }

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

            /** Detail Part* */
            $dprin = 0;
            $dint = 0;
            $totbal = 0;
            $dtot = 0;
            $ctot = 0;

            $maccode = $getac["accode"];
            $ggrpcode = mysqli_query($conn, "SELECT grpcode from accmast where accode = '$maccode'")or die(mysqli_error($conn));
            $fgrpcode = mysqli_fetch_array($ggrpcode);

            if ($fgrpcode["grpcode"] == '010') {
                $wintcond = " AND  `source`!='INT'";
            }

            $macname = $getac["name"];
            $mgrpcode = $getac["grpcode"];
            $flatno = $getac["flatno"];
            $mancode = $getac["mancode"];

            $mopbal = $getac["openbal"];
            $principal = $getac["prinamt"];
            $intamt = $getac["interest"];
            $mopdc = $getac["dc"];
            if ($mopdc == "D") {
                $mopbal = $mopbal * - 1;
            }

            if ($mopdc == "C") {                //  C - reverse for D   -- just for calculation
                $principal = $principal * - 1;
            }

            $dprin = $dprin + $principal;
            $dint = $dint + $intamt;


//                                            $ctran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
//                                                                  WHERE accode = '$maccode' and series = '$fseries' AND  `date`>='$mfrmdate' AND `date`<='$mtodate'
//                                                                  and source != 'INT' ORDER BY `date`, vouchno") or die(mysqli_error($conn));
//                                        print table($ctran1);
//                                            $curl = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
//                                                                 WHERE accode = '$maccode' and series = '$fseries' AND `date`>='$mfrmdate' AND `date`<='$mtodate'
//                                                                 and source != 'INT' ORDER BY `date`, vouchno") or die(mysqli_error($conn));
            $ctran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
                                                                  WHERE accode = '$maccode' and series = '$fseries' AND  `date`>='$mfrmdate'
                                                                  AND `date`<='$mtodate' $wintcond ORDER BY `date`, vouchno") or die(mysqli_error($conn));

//                                        print table($ctran1);
            $curl = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt FROM `$trandb` 
                                                                 WHERE accode = '$maccode' and series = '$fseries' AND `date`>='$mfrmdate' AND
                                                                 `date`<='$mtodate' $wintcond ORDER BY `date`, vouchno") or die(mysqli_error($conn));



            while ($murl = mysqli_fetch_object($curl)) {
                switch ($murl->source) {
                    case "DN";

                        $head = "Purchase Return";

                        $url = "debnote.php?vouchno=$murl->vouchno&method=ledger";

                        break;

                    case "SL";

                        $head = "Sale";

                        $url = "sale.php?vouchno=$murl->vouchno&method=ledger";

                        //   browse($murl->vouchno." - SALE URL - ".$url);

                        break;

                    case "BILL";

                        $head = "Bill";

                        $url = "sale.php?vouchno=$murl->vouchno&method=ledger";

                        //   browse($murl->vouchno." - SALE URL - ".$url);

                        break;



                    case "CN";

                        $head = "Credit Note";

                        $url = "crednote.php?vouchno=$murl->vouchno&method=ledger";

                        break;



                    case "BR";

                        $url = "bankrecp.php?vouchno=$murl->vouchno&method=ledger";

                        break;



                    case "BP";

                        $head = "Bank Payment";

                        $url = "bankpay.php?$murl->vouchno&method=ledger";

                        break;

                    case "CR";

                        $head = "Cash Receipt";
                        $url = "cashrecp.php?$murl->vouchno&method=ledger";
                        break;



                    case "CP";

                        $head = "Cash Payment";

                        $url = "cashpay.php?$murl->vouchno&method=ledger";

                        break;



                    case "TR";

                        $head = "Contra / Transfer";

                        $url = "contra.php?vouchno=$murl->vouchno&method=ledger";

                        break;



                    case "JV";

                        $head = "Journal Voucher";

                        $url = "journal.php?$murl->vouchno&method=ledger";

                        break;



                    case "DG";

                        $head = "Discount Given";



                        break;



                    case "DR";

                        $head = "Discount Received";

                        break;



                    default :

                        $head = "Other";
                }
            }


            $rtran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`retdate`, '%d-%m-%Y') as mydt FROM `actran` WHERE
                                                           retdate >= '$mfrmdate' AND retdate <= '$mtodate' AND
                                                           accode = '$maccode' and series = '$fseries' ORDER BY date, vouchno") or die(mysqli_error($conn));


            $getprin = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode='$maccode'
                                                              AND  date < '$mfrmdate' and series = '$fseries' and source != 'INT' ") or die(mysqli_error($conn));

            $getint = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode='$maccode'
                                                              AND  date < '$mfrmdate' and series = '$fseries' and source = 'INT' ") or die(mysqli_error($conn));

            $fetprin = mysqli_fetch_array($getprin);
            $opprin = $fetprin['amt'];

            $amt2 = $opprin + $opint;

            if ($opprin < 0) {
                $opprin = $opprin * -1;
            }

            $fetint = mysqli_fetch_array($getint);
            $opint = $fetint['amt'];

            if ($opint < 0) {
                $opint = $opint * -1;
            }

            $principal = $principal + $opprin;
            $intamt = $intamt + $opint;

            $getopen = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode='$maccode'
                                                              AND  date < '$mfrmdate' and series = '$fseries'  ") or die(mysqli_error($conn));
            $fetopen = mysqli_fetch_array($getopen);
            $opbal = $fetopen['amt'];

            $mobal = $mopbal + $opbal;

            $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";
            }

            $lgetbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE accode='$maccode' and 
                                                        series = '$fseries' AND  date <= '$mtodate'") or die(mysqli_error($conn));
            $lgetamt = mysqli_fetch_array($lgetbal);
            $lamt = $lgetamt["amt"];
            $lmbal = $mopbal + $lamt;

            if ($lmbal < 0) {
                $lmdc = "DR";
            } else {
                $lmdc = "CR";
            }

            $pdf->SetFont('Arial', 'B', 10);
            $pdf->SetTextColor(0, 0, 0);
            $pdf->SetFillColor(239, 228, 176);
            if ($mgrpcode == '010') {
                $pdf->Cell(240, $ht, $macname . ' - Flat No. : ' . $flatno, 'T' . 'L' . 'B', 0, '', 1);
                $pdf->Cell(40, $ht, abs($lmbal) . " " . $lmdc, 'T' . 'R' . 'B', 0, 'R', 1);
            } else {
                $pdf->Cell(240, $ht, $macname . ' - Code : ' . $mancode, 'T' . 'L' . 'B', 0, '', 1);
                $pdf->Cell(40, $ht, abs($lmbal) . " " . $lmdc, 'T' . 'R' . 'B', 0, 'R', 1);
            }
            $pdf->Ln();
            $ln++;
            if ($ln > $pagerow) {
                $ln = 0;
                include('includes/head_rep_pdf.php');
                $pdf->SetFont('Arial', 'B', 10);
            }


            $pdf->SetTextColor(0, 0, 0);
            $pdf->SetFillColor(204, 204, 204);
            if ($wint == 1) {
                $pdf->Cell(20, $ht, "Date", 1, 0, '', 1);
                $pdf->Cell(20, $ht, "Source", 1, 0, '', 1);
                $pdf->Cell(17, $ht, "Voucher", 1, 0, '', 1);
                $pdf->Cell(23, $ht, "Receipt No", 1, 0, '', 1);
                $pdf->Cell(90, $ht, "Description", 1, 0, '', 1);
                $pdf->Cell(20, $ht, "Principal", 1, 0, 'R', 1);
                $pdf->Cell(20, $ht, "Interest", 1, 0, 'R', 1);
                $pdf->Cell(20, $ht, "Debit", 1, 0, 'R', 1);
                $pdf->Cell(20, $ht, "Credit", 1, 0, 'R', 1);
                $pdf->Cell(30, $ht, "Balance", 1, 0, 'R', 1);
            } else {
                $pdf->Cell(20, $ht, "Date", 1, 0, '', 1);
                $pdf->Cell(20, $ht, "Source", 1, 0, '', 1);
                $pdf->Cell(17, $ht, "Voucher", 1, 0, '', 1);
                $pdf->Cell(23, $ht, "Recp.No", 1, 0, '', 1);
                $pdf->Cell(130, $ht, "Description", 1, 0, '', 1);
                $pdf->Cell(20, $ht, "Debit", 1, 0, 'R', 1);
                $pdf->Cell(20, $ht, "Credit", 1, 0, 'R', 1);
                $pdf->Cell(30, $ht, "Balance", 1, 0, 'R', 1);
            }
            $pdf->Ln();
            $ln++;
            if ($ln > $pagerow) {
                $ln = 0;
                include('includes/head_rep_pdf.php');
                $pdf->SetFont('Arial', 'B', 9);
            }


            $pdf->SetTextColor(0, 0, 0);


            if ($wint == 1) {
                $pdf->Cell(20, $ht, "$vdate", 1, 0, '', 1);
                $pdf->Cell(20, $ht, "", 1, 0, '', 1);
                $pdf->Cell(17, $ht, "$mvouch", 1, 0, '', 1);
                $pdf->Cell(23, $ht, "", 1, 0, '', 1);

                $pdf->SetFont('Arial', 'B', 8);
                $pdf->Cell(90, $ht, "$mdescrip", 1, 0, '', 1);

                $pdf->SetFont('Arial', 'B', 9);
                $pdf->Cell(20, $ht, number_format($principal,2), 1, 0, 'R', 1);
                $pdf->Cell(20, $ht, number_format($intamt,2), 1, 0, 'R', 1);
                $pdf->Cell(20, $ht, number_format($mdebit,2), 1, 0, 'R', 1);
                $pdf->Cell(20, $ht, number_format($mcredit,2), 1, 0, 'R', 1);
            } else {
                $pdf->Cell(20, $ht, "$vdate", 1, 0, '', 1);
                $pdf->Cell(20, $ht, "", 1, 0, '', 1);
                $pdf->Cell(17, $ht, "$mvouch", 1, 0, '', 1);
                $pdf->Cell(23, $ht, "", 1, 0, '', 1);

                $pdf->SetFont('Arial', 'B', 8);
                $pdf->Cell(130, $ht, "$mdescrip", 1, 0, '', 1);

                $pdf->SetFont('Arial', 'B', 8);
                $pdf->Cell(20, $ht, number_format($mdebit,2), 1, 0, 'R', 1);
                $pdf->Cell(20, $ht, number_format($mcredit,2), 1, 0, 'R', 1);
            }
            $pdf->Cell(30, $ht, number_format($totbal,2) . " " . $mdctot, 1, 0, 'R', 1);
            
            $pdf->Ln();
            $ln++;
            if ($ln > $pagerow) {
                $ln = 0;
                include('includes/head_rep_pdf.php');
                $pdf->SetFont('Arial', 'B', 9);
            }


            $pdf->SetTextColor(0, 0, 0);


            while ($row = mysqli_fetch_array($ctran1)) {
                $vdate = $row["mydt"];
                $msource = $row["source"];
                $mvouch = $row["vouchno"];
                $newbill = $row["newbill"];
                $mamount = $row["amount"];
                $billno = $row["billno"];
                $flatno = $row["flatno"];
//                                                $mancode = $row["mancode"];
                $recpno = $row["recpno"];

                $principal = 0;
                $intamt = 0;

                $mgrpcode = $getac["grpcode"];
                $mdiscprov = $getac["discount"];
                $totdiscprov = 0;

                $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 = "";
                $mchqdtl2 = '';

                switch ($msource) {
                    case "BILL";
//                                                        $url = "purchase.php?vouchno=$mvouch&method=ledger";
//                                                        $nqry1 = mysqli_query($conn, "SELECT billno,billcycle,billcode
//                                                                              FROM `actran`   WHERE `vouchno`='$mvouch' 
//                                                                              and series = '$fseries'") or die(mysqli_error($conn));
//                                                        $nqry = mysqli_fetch_array($nqry1);
                        $billno = 'Bill - ' . $row["newbill"];
                        $billcode = $row["billcode"];
                        $billcycle = $row["billcycle"];

                        switch ($billcycle) {

                            case 'M': $byname = 'MONTHLY';
                                break;
                            case 'BM': $byname = 'BI - MONTHLY';
                                break;
                            case 'Q': $byname = 'QUARTERLY';
                                break;
                            case 'H': $byname = 'HALF YEARLY';
                                break;
                            default : $byname = '';
                        }

                        $gbilcode = mysqli_query($conn, "SELECT descrip FROM $zroot.`billingcycle` Where  `billcycle`='$billcycle' and `code` = '$billcode' ") or die(mysqli_error($conn));
                        $fetbilcode = mysqli_fetch_array($gbilcode);
                        $descrip = $fetbilcode['descrip'];

                        if ($billcode < 4) {
                            $cdate = strtotime($mtodate);
                        } else {
                            $cdate = strtotime($mfrdate);
                        }

                        $chkdate = date("Y", $cdate);

                        $head = "To Bill" . "-" . $descrip . " " . $chkdate;

                        if ($prnnarr == 1 || $usertype == 3) {
                            $mchqdtl = "";
                        }

                        if ($wint == 1) {
//                                                            browse($mvouch);
//                                                            browse($billcycle);
//                                                            browse($billcode);
//                                                            browse($maccode);
//                                                            
                            $getint = mysqli_query($conn, "SELECT billamt, intamt from `billmain` Where  `billcycle`='$billcycle' and `billcode` = '$billcode' 
                                                                               and accode = '$maccode' and vouchno = '$mvouch' ") or die(mysqli_error($conn));
                            $fetint = mysqli_fetch_array($getint);
                            $principal = $fetint['billamt'];
                            $intamt = $fetint['intamt'];

//                                                            browse($principal);

                            $dprin = $dprin + $principal;
                            $dint = $dint + $intamt;

                            $mdebit = $principal + $intamt;
                            $dtot = $dtot + $intamt;
                            $totbal = $totbal + ($intamt * -1);
                        }

                        break;

                    case "INT";
//                                                        $url = "debnote.php?vouchno=$mvouch&method=ledger";
//
//                                                        $totdiscprov = $totdiscprov + $mamount;
//                                                        $nqry1 = mysqli_query($conn, "SELECT billno,billcycle,billcode,arrears
//                                                                         FROM `billmain`   WHERE `vouchno`='$mvouch' and `billno`='$billno' and society = '$fseries'") or die(mysqli_error($conn));
//                                                        $nqry = mysqli_fetch_array($nqry1);
//
//                                                        $aramt = $nqry["arrears"];
//                                                        $head = "Interest On Arrears" . " - " . $aramt;
//
//                                                        if ($prnnarr == 1) {
//                                                            $mchqdtl = "";
//                                                        }
//
//                                                        break;

                    case "DN";
//                                                        $head = "Debit Note";
                        if ($mgrpcode == '010') {
                            $nqry1 = mysqli_query($conn, "SELECT debparty FROM `deboth`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $tcredcode = $nqry["debparty"];
                        } else {
                            $nqry1 = mysqli_query($conn, "SELECT accode FROM `debmain`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $tcredcode = $nqry["accode"];
                        }

                        $getpart = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                                WHERE accode = '$tcredcode' and series = '$fseries'") or die(mysqli_error($conn));

                        $fetpart = mysqli_fetch_array($getpart);
                        $tcredname = $fetpart["name"];

                        $nnar = mysqli_query($conn, "SELECT rem1,rem2 FROM `debmain`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));
                        $fnar = mysqli_fetch_array($nnar);
                        $rem1 = $fnar['rem1'] . " " . $fnar['rem2'];

                        $head = $tcredname;
                        $mnarr = $rem1;
                        $mchqdtl = "";

                        break;

                    case "SL";

                        $head = "Sale";

                        if ($prnnarr == 1 || $usertype == 3) {

                            $mnarr = "";

                            $mchqdtl = "";
                        }

                        break;



                    case "CN";
//                                                        $head = "Credit Note";

                        $url = "crednote_print.php?vouchno=$mvouch&method=ledger";

                        if ($mgrpcode == '010') {
                            $nqry1 = mysqli_query($conn, "SELECT debparty FROM `credoth`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $tcredcode = $nqry["debparty"];
                        } else {
                            $nqry1 = mysqli_query($conn, "SELECT accode,rem1 FROM `credmain`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $tcredcode = $nqry["accode"];
                        }

                        $getpart = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                              WHERE accode = '$tcredcode' and series = '$fseries'") or die(mysqli_error($conn));

                        $fetpart = mysqli_fetch_array($getpart);
                        $tcredname = $fetpart["name"];

                        $nnar = mysqli_query($conn, "SELECT rem1,rem2 FROM `credmain`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));
                        $fnar = mysqli_fetch_array($nnar);
                        $rem1 = $fnar['rem1'] . " " . $fnar['rem2'];

                        $head = $tcredname;
                        $mnarr = $rem1;
                        $mchqdtl = "";
                        $mchqdtl2 = "";

                        break;

                    case "BR";

                        $head = "Bank Receipt";
                        $url = "bankrecp.php?vouchno=$mvouch&method=ledger";

                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                       `narr1`, `narr2`, bankcode FROM `bankrecp`
                                                                        WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                        $nqry = mysqli_fetch_array($nqry1);
                        $mbill = $nqry["chqno"];
                        $mbilldt = $nqry["mbilldate"];
                        $mnarr = $nqry["narr1"] . " " . $nqry["narr2"];
                        if ($mbill != "") {
                            $mchqdtl = "Cheque No. : " . + trim($mbill);
                        }

                        if ($mbilldt != '00-00-0000') {
                            $mchqdtl2 = " Dated : " . $mbilldt;
                        } else {
                            $mchqdtl2 = '';
                        }

                        $baccode = $nqry["bankcode"];

                        $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast`
                                                                              WHERE accode='$baccode' and series = '$fseries'") or die(mysqli_error($conn));
                        $acfet = mysqli_fetch_array($acqry1);
                        $acname = $acfet["name"];
                        if ($acname != "") {
                            $head = 'By ' . $acname;
                        } else {
                            $head = "";
                        }
                        break;

                    case "BP";

                        $head = "Bank Payment";

                        $url = "bankpay.php?vouchno=$mvouch&method=ledger";


                        $nqry1 = mysqli_query($conn, "SELECT chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as mbilldate, 
                                                                        `narr1`, `narr2` FROM `bankpay`
                                                                        WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));


                        $nqry = mysqli_fetch_array($nqry1);
                        $mbill = $nqry["chqno"];
                        $mbilldt = $nqry["mbilldate"];
                        $mnarr = $nqry["narr1"] . " " . $nqry["narr2"];
                        if ($mbill != "") {
                            $mchqdtl = "Cheque No. : " . + trim($mbill);
                        }
                        if (strtotime($mbilldt) > 0) {
                            $mchqdtl2 = " Dated : " . $mbilldt;
                        }
                        $baccode = $nqry["bankcode"];

                        $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast`
                                                                              WHERE accode='$baccode' and series = '$fseries'") or die(mysqli_error($conn));
                        $acfet = mysqli_fetch_array($acqry1);
                        $acname = $acfet["name"];

                        if ($acname != "") {
                            $head = 'To ' . $acname;
                        } else {
                            $head = "";
                        }
                        break;



                    case "CR";

                        $head = "Cash Receipt";

                        $url = "cashrecp.php?vouchno=$mvouch&method=ledger";


                        $nqry1 = mysqli_query($conn, "SELECT `narr1`, `narr2`, bankcode  FROM `cashrecp`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                        $nqry = mysqli_fetch_array($nqry1);
                        $mnarr = $nqry["narr1"];
                        $baccode = $nqry["bankcode"];

                        $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast`
                                                                              WHERE accode='$baccode' and series = '$fseries'") or die(mysqli_error($conn));
                        $acfet = mysqli_fetch_array($acqry1);
                        $acname = $acfet["name"];

                        if ($acname != "") {
                            $head = 'By ' . $acname;
                        } else {
                            $head = "";
                        }
                        break;

                    case "CP";

                        $head = "Cash Payment";

                        $url = "cashpay.php?vouchno=$mvouch&method=ledger";


                        $nqry1 = mysqli_query($conn, "SELECT `narr1`, `narr2`, bankcode FROM `cashpay`
                                                                             WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                        $nqry = mysqli_fetch_array($nqry1);
                        $mnarr = $nqry["narr1"];
                        $baccode = $nqry["bankcode"];

                        $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast`
                                                                              WHERE accode='$baccode' and series = '$fseries'") or die(mysqli_error($conn));
                        $acfet = mysqli_fetch_array($acqry1);
                        $acname = $acfet["name"];

                        if ($acname != "") {
                            $head = 'To ' . $acname;
                        } else {
                            $head = "";
                        }
                        break;

                    case "TR";

                        $head = "Contra / Transfer";

                        $url = "contra.php?vouchno=$mvouch&method=ledger";

                        if ($prnnarr == 1 || $usertype == 3) {

                            $nqry1 = mysqli_query($conn, "SELECT `narr1`, `narr2` FROM `contra`
                                                                             WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));


                            $nqry = mysqli_fetch_array($nqry1);
                            $mnarr = $nqry["narr1"] . " " . $nqry["narr2"];
                        }

                        break;

                    case "JV";

//                                                        $head = "Journal Voucher";
                        if ($mgrpcode == '010') {
                            $nqry1 = mysqli_query($conn, "SELECT accode FROM `jvtran`
                                                                              WHERE vouchno='$mvouch' and debit > 0 and series = '$fseries'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $tcredcode = $nqry["accode"];
                        } else {
                            $nqry1 = mysqli_query($conn, "SELECT accode FROM `jvtran`
                                                                              WHERE vouchno='$mvouch' and credit > 0 and series = '$fseries'") or die(mysqli_error($conn));

                            $nqry = mysqli_fetch_array($nqry1);
                            $tcredcode = $nqry["accode"];
                        }

                        $getpart = mysqli_query($conn, "SELECT name FROM `accmast`
                                                                              WHERE accode = '$tcredcode' and series = '$fseries'") or die(mysqli_error($conn));

                        $fetpart = mysqli_fetch_array($getpart);
                        $tcredname = $fetpart["name"];

                        $nnar = mysqli_query($conn, "SELECT rem1,rem2 FROM `jvmain`
                                                                              WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));
                        $fnar = mysqli_fetch_array($nnar);
                        $rem1 = $fnar['rem1'] . " " . $fnar['rem2'];

                        $head = $tcredname;
                        $mnarr = $rem1;
                        $mchqdtl = "";
                        $mchqdtl2 = "";


                        break;

                    case "SUPP";

                        $head = "SUPPLEMENTARY BILL";

                        $url = "suppbill.php?vouchno=$mvouch&method=ledger";


                        $nqry1 = mysqli_query($conn, "SELECT `firsttran`, `rem1`, `rem2` FROM `suppmain`
                                                                             WHERE vouchno='$mvouch' and series = '$fseries'") or die(mysqli_error($conn));

                        $nqry = mysqli_fetch_array($nqry1);
                        $mnarr = $nqry["rem1"] . " " . $nqry['rem2'];
                        $firsttran = $nqry["firsttran"];

                        $acqry1 = mysqli_query($conn, "SELECT `name`  FROM `accmast`
                                                                           WHERE accode='$firsttran' and series = '$fseries'") or die(mysqli_error($conn));
                        $acfet = mysqli_fetch_array($acqry1);
                        $acname = $acfet["name"];

                        if ($acname != "") {
                            $head = 'To ' . $acname;
                        } else {
                            $head = "";
                        }
                        break;

                    case "DG";

                        $head = "Discount Given";

                        break;

                    case "DR";

                        $head = "Discount Received";

                        break;

                    default :

                        $head = "Other";
                }


                if ($msource == 'BILL') {
                    $action = 'bill_print_fr.php';
                } elseif ($msource == 'BR') {
                    $action = 'bankrecp_fr.php';
                } elseif ($msource == 'BP') {
                    $action = 'bankpay_fr.php';
                } elseif ($msource == 'CR') {
                    $action = 'cashrecp_fr.php';
                } elseif ($msource == 'CP') {
                    $action = 'cashpay_fr.php';
                } elseif ($msource == 'SUPP') {
                    $action = 'suppbl_print.php';
                } elseif ($msource == 'CN') {
                    $action = '';
                }



                if ($wint == 1) {
                    $pdf->Cell(20, $ht, $vdate, 1);
                    $pdf->Cell(20, $ht, $msource, 1, 0);

                    if ($msource == 'BILL') {
                        $pdf->Cell(17, $ht, $newbill, 1, 0);
                    } else {
                        $pdf->Cell(17, $ht, $mvouch, 1, 0);
                    }

                    $pdf->Cell(23, $ht, $recpno, 1, 0);

                    $pdf->SetFont('Arial', 'B', 8);
                    
                    if ($prnnarr == 1 || $usertype == 3) {
                        $pdf->Cell(90, $ht, "$head" . "$mchqdtl" . "$mchqdtl2" . "$mnarr", 1, 0);
                    } else {
                        $pdf->Cell(90, $ht, $head, 1, 0);
                    }
                    $pdf->SetFont('Arial', 'B', 9);

                    $pdf->Cell(20, $ht, number_format($principal,2), 1, 0, 'R');
                    $pdf->Cell(20, $ht, number_format($intamt,2), 1, 0, 'R');
                    $pdf->Cell(20, $ht, number_format($mdebit,2), 1, 0, 'R', 1);
                    $pdf->Cell(20, $ht, number_format($mcredit,2), 1, 0, 'R');
                } else {
                    $pdf->Cell(20, $ht, $vdate, 1, 0);
                    $pdf->Cell(20, $ht, $msource, 1, 0);

                    if ($msource == 'BILL') {
                        $pdf->Cell(17, $ht, $newbill, 1, 0);
                    } else {
                        $pdf->Cell(17, $ht, $mvouch, 1, 0);
                    }

                    $pdf->Cell(23, $ht, $recpno, 1, 0);

                    $pdf->SetFont('Arial', 'B', 8);
                    
                    if ($prnnarr == 1 || $usertype == 3) {
                        $pdf->Cell(130, $ht, "$head" . "$mchqdtl" . "$mchqdtl2" . "$mnarr", 1, 0);
                    } else {
                        $pdf->Cell(130, $ht, $head, 1, 0);
                    }
                    
                    $pdf->SetFont('Arial', 'B', 9);
                    
                    $pdf->Cell(20, $ht, number_format($mdebit,2), 1, 0, 'R');
                    $pdf->Cell(20, $ht, number_format($mcredit,2), 1, 0, 'R');
                }
                $pdf->Cell(30, $ht, number_format(abs($totbal),2) . " " . $mdctot, 1, 0, 'R');
                
                $pdf->Ln();
                $ln++;
                if ($ln > $pagerow) {
                    $ln = 0;
                    include('includes/head_rep_pdf.php');
                    $pdf->SetFont('Arial', 'B', 9);
                }
            }
            if ($wint == 1) {
                $pdf->Cell(20, $ht, "", 1, 0);
                $pdf->Cell(20, $ht, "", 1, 0);
                $pdf->Cell(17, $ht, "", 1, 0);
                $pdf->Cell(23, $ht, "", 1, 0);
                $pdf->Cell(90, $ht, "Total", 1, 0, 'R');

                $pdf->Cell(20, $ht, number_format($dprin,2), 1, 0, 'R');
                $pdf->Cell(20, $ht, number_format($dint,2), 1, 0, 'R');
                $pdf->Cell(20, $ht, number_format($dtot,2), 1, 0, 'R');
                $pdf->Cell(20, $ht, number_format($ctot,2), 1, 0, 'R');
            } else {
                $pdf->Cell(20, $ht, "", 1, 0);
                $pdf->Cell(20, $ht, "", 1, 0);
                $pdf->Cell(17, $ht, "", 1, 0);
                $pdf->Cell(23, $ht, "", 1, 0);
                $pdf->Cell(130, $ht, "Total", 1, 0, 'R');

                $pdf->Cell(20, $ht, number_format($dtot,2), 1, 0, 'R');
                $pdf->Cell(20, $ht, number_format($ctot,2), 1, 0, 'R');
            }
            $pdf->Cell(30, $ht, number_format($totbal,2) . " " . $mdctot, 1, 0, 'R');

            $pdf->Ln();
            $ln++;
            if ($ln > $pagerow) {
                $ln = 0;
                include('includes/head_rep_pdf.php');
                $pdf->SetFont('Arial', 'B', 9);
            }
        }
    }

    if ($dtype == 1) {
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->SetFillColor(239, 228, 176);

        $pdf->Cell(200, $ht, "", 1, 0, "", 1);
        $pdf->Cell(40, $ht, "Total", 1, 0, "", 1);
        $pdf->Cell(40, $ht, number_format(abs($totbal),2) . " " . $mdctot, 1, 0, "R", 1);
        $pdf->Ln();
        $ln++;
    }

    $pdf->Output("Ledger.pdf", "D");
}
?>
