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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$myyear = $_SESSION[$myyear];
$formname = "Bank Book";

$zyear = $_SESSION["tit_" . $dbname];
$maindb = 'accmast';
$zroot = $_SESSION["zdbroot"];

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

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


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

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

    $mfrmdate = trim($_POST["frmdate"]);
    $mtodate = trim($_POST["todate"]);

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

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

    if ($prntype == 1) {
        $repname = $dispname . " Book";
    } elseif ($prntype == 2) {
        $repname = $dispname . " Book - Only Debits";
    } else {
        $repname = $dispname . " Book - Only Credits";
    }
    $acc = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE accode!='' $acond") or die(mysqli_error($conn));
    include("includes/linkshead.php");
    ?>
    <body>
        <div class="container">
            <div class="row">
                <?php include("includes/head_rep.php"); ?>
                <div class="col-lg-12 pad">
                    <table class=" table table-bordered table-condensed table-hover table-striped" summary="Ledger - Detail" width='100%' border="0">    
                        <tbody>
                            <?php
                            mysqli_select_db($conn, $zdb);
                            $totbal = 0;

                            while ($getac = mysqli_fetch_array($acc)) {
                                /** Detail Part * */
                                $totbal = 0;
                                $dtot = 0;
                                $ctot = 0;

                                $maccode = $getac["accode"];
                                $mbankinit = $getac["bankinit"];
                                $macname = $getac["name"];
                                $mopbal = $getac["openbal"];
                                $mopdc = $getac["dc"];

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

                                $ctran1 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt, 0 as ret, date as newdate FROM `actran` 
                                                       WHERE trnno != '001' and bankinit = '$mbankinit' and 
                                                       (source = '$msource1' or source = '$msource2') and  
                                                       concat(source,vouchno) in (select concat(source,vouchno) from `actran` 
                                                       where accode = '$maccode' and bankinit = '$mbankinit' and 
                                                       date >= '$mfrmdate' and date <= '$mtodate' and     
                                                       (source = '$msource1' or source = '$msource2'))  
                                                       union
                                                       SELECT *, DATE_FORMAT(`date`, '%d-%m-%Y') as mydt, 0 as ret, date as newdate FROM `actran` 
                                                       WHERE accode != '$maccode' and source = 'TR' and  
                                                       concat(source,vouchno) in (select concat(source,vouchno) from `actran` 
                                                       where accode = '$maccode'  and 
                                                       date >= '$mfrmdate' and date <= '$mtodate' and source = 'TR' )  
                                                       union
                                                       SELECT *, DATE_FORMAT(`retdate`, '%d-%m-%Y') as mydt, 1 as ret, retdate as newdate FROM `actran` 
                                                       WHERE trnno != '001' and bankinit = '$mbankinit' and chqret = 'Y' and 
                                                       (source = '$msource1' or source = '$msource2') and  
                                                       concat(source,vouchno) in (select concat(source,vouchno) from `actran` 
                                                       where accode = '$maccode' and bankinit = '$mbankinit' and 
                                                       date >= '$mfrmdate' and date <= '$mtodate' and     
                                                       (source = '$msource1' or source = '$msource2'))  
                                                       order by newdate, source, vouchno ") or die(mysqli_error($conn));
                                
//                               print table ($ctran1) ;        
                                
                                $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE accode = '$maccode' AND 
                                                             `date` < '$mfrmdate' and (`retdate` != '0000-00-00') ") or die(mysqli_error($conn));
                                $getamt = mysqli_fetch_array($getbal);
                                $amt = $getamt['amt'];

                                $mobal = $mopbal + $amt;
                                $mdebit = 0;
                                $mcredit = 0;

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

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

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

                                if ($prntype == 1) {

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

                                    $vdate = $row["mydt"];
                                    $msource = $row["source"];
                                    $mvouch = $row["vouchno"];
                                    $mamount = $row["amount"];
                                    $daccode = $row["accode"];
                                    $ret     = $row["ret"];
                                    $mdebit = 0;
                                    $mcredit = 0;
                                    
                                if ($ret == 0) {
                                    if ($mamount > 0) {
                                        $mdc = "DR";
                                        $mdebit = abs($mamount);
                                        $dtot = $dtot + $mdebit;
                                        $totbal = $totbal - abs($mamount);
                                    } else {
                                        $mdc = "CR";
                                        $mcredit = abs($mamount);
                                        $ctot = $ctot + $mcredit;
                                        $totbal = $totbal + abs($mamount);
                                    }
                                } else {
                                    if ($mamount > 0) {
                                        $mdc = "CR";
                                        $mcredit = abs($mamount);
                                        $ctot = $ctot + $mcredit;
                                        $totbal = $totbal + abs($mamount);
                                    } else {
                                        $mdc = "DR";
                                        $mdebit = abs($mamount);
                                        $dtot = $dtot + $mdebit;
                                        $totbal = $totbal - abs($mamount);
                                    }
                                }    

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

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

                                    $head = $nqry["name"];
                                    $party = $head;

                                    switch ($msource) {

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

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

                                        case "BP";

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

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

                                        case "CR";

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

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

                                        case "CP";

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

                                                $mbill = $nqry[""];
                                                $mbilldt = $nqry[""];
                                                $mnarr = $nqry["remark1"];
                                            }
                                            break;

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

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

                                    if ($prnnarr == 1) {

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

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

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

                                if ($prntype == 1) {
                                    echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">$dtot</td>";
                                    echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">$ctot</td>";

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

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

                                    echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"></td>";
                                    echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">$ctot</td>";
                                }
                                echo "</tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php
        include("includes/linksfoot.php");
    }

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

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

        $mfrmdate = trim($_POST["frmdate"]);
        $mtodate = trim($_POST["todate"]);

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

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

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


        $totbal = 0;

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

            $maccode = $getac["accode"];
            $mbankinit = $getac["bankinit"];
            $macname = $getac["name"];
            $mopbal = $getac["openbal"];
            $mopdc = $getac["dc"];

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

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

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

            $mobal = $mopbal + $amt;
            $mdebit = 0;
            $mcredit = 0;

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

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

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

            if ($prntype == 1) {
                $contents.=$mdebit . ",";
                $contents.=$mcredit . ",";
                $contents.=abs($totbal) . " " . $mdctot . "\n";
            } elseif ($prntype == 2) {
                $contents.=$mdebit . ",";
                $contents.=" " . ",";
                $contents.=" " . "\n";
            } else {
                $contents.=" " . ",";
                $contents.=$mcredit . ",";
                $contents.=" " . "\n";
            }
            while ($row = mysqli_fetch_assoc($ctran1)) {

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

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

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

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

                $head = $nqry["name"];

                switch ($msource) {

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

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

                    case "BP";

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

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

                    case "CR";

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

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

                    case "CP";

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

                            $mbill = $nqry[""];
                            $mbilldt = $nqry[""];
                            $mnarr = $nqry["remark1"];
                        }
                        break;

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

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

                if ($prnnarr == 1) {

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

                    $contents.=" " . ",";
                    $contents.=$mcredit . ",";
                    $contents.=" " . "\n";
                }
 
            }
            $contents.=" " . ",";
            $contents.=" " . ",";
            $contents.="Total" . ",";
            if ($prntype == 1) {
                $contents.=$dtot . ",";
                $contents.=$ctot . ",";

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

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

        $mfrmdate = trim($_POST["frmdate"]);
        $mtodate = trim($_POST["todate"]);

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

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

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

        $totbal = 0;

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

            $maccode = $getac["accode"];
            $mbankinit = $getac["bankinit"];
            $macname = $getac["name"];
            $mopbal = $getac["openbal"];
            $mopdc = $getac["dc"];

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

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

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

            $mobal = $mopbal + $amt;
            $mdebit = 0;
            $mcredit = 0;

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

            if ($totbal < 0) {
                $mdctot = "DR";
            } else {
                $mdctot = "CR";
            }
            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');
                }

            }

            $pdf = new pdf('L', 'mm', 'A4');

            $pdf->AliasNbPages();

            $pdf->AddPage();
            $pdf->SetFont('Arial', 'I', 10);
            $pdf->SetTextColor(255, 255, 255);
            $pdf->SetFillColor(96, 130, 191);
            $pdf->Cell(278, 7, strtoupper($macname), 1, 0, '', 1);
            $pdf->SetFont('Arial', '', 1, 0);

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

            $pdf->SetFont('Arial', 'I', 10);
            $pdf->SetTextColor(255, 255, 255);
            $pdf->SetFillColor(96, 130, 191);
            $pdf->Cell(30, 7, "Date", 1, 0, '', 1);
            $pdf->Cell(30, 7, "Voucher", 1, 0, '', 1);
            $pdf->Cell(128, 7, "Description", 1, 0, '', 1);
            $pdf->Cell(30, 7, "Debit", 1, 0, '', 1, 0, 'R');
            $pdf->Cell(30, 7, "Credit", 1, 0, '', 1, 0, 'R');
            $pdf->Cell(30, 7, "Balance", 1, 0, '', 1, 0, 'R');
            $pdf->SetFont('Arial', '', 1, 0);

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

            $pdf->SetFont('Arial', '', 8);
            $pdf->Cell(30, 7, "$vdate", 1);
            $pdf->Cell(30, 7, "$mvouch", 1);
            $pdf->Cell(128, 7, "$mdescrip", 1);
            if ($prntype == 1) {
                $pdf->Cell(30, 7, "$mdebit", 1, 0, 'R');
                $pdf->Cell(30, 7, "$mcredit", 1, 0, 'R');
                $pdf->Cell(30, 7, abs($totbal) . " " . $mdctot, 1, 0, 'R');
                $pdf->Ln();
            } elseif ($prntype == 2) {
                $pdf->Cell(30, 7, "$mdebit", 1, 0, 'R');
                $pdf->Cell(30, 7, " ", 1);
                $pdf->Cell(30, 7, " ", 1);
                $pdf->Ln();
            } else {
                $pdf->Cell(30, 7, " ", 1);
                $pdf->Cell(30, 7, "$mcredit", 1, 0, 'R');
                $pdf->Cell(30, 7, " ", 1);
                $pdf->Ln();
            }
            while ($row = mysqli_fetch_assoc($ctran1)) {

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

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

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

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

                $head = $nqry["name"];

                switch ($msource) {

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

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

                    case "BP";

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

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

                    case "CR";

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

                            $mbill = '';
                            $mbilldt = '';
                            $mnarr = $nqry["remark1"];
                        }
                        break;

                    case "CP";

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

                            $mbill = $nqry[""];
                            $mbilldt = $nqry[""];
                            $mnarr = $nqry["remark1"];
                        }
                        break;

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

                            $mnarr = $nqry["narration"];
                        }
                        break;
                    default :
                        $head = "Other";
                }
                $pdf->SetFont('Arial', '', 8);
                $pdf->Cell(30, 7, "$vdate", 1);
                $pdf->Cell(30, 7, "$msource" . " " . "$mvouch", 1);
                $pdf->Cell(128, 7, "$head", 1);
                if ($prntype == 1) {
                    $pdf->Cell(30, 7, "$mdebit", 1, 0, 'R');
                    $pdf->Cell(30, 7, "$mcredit", 1, 0, 'R');
                    $pdf->Cell(30, 7, abs($totbal) . " " . $mdctot, 1, 0, 'R');
                } elseif ($prntype == 2) {
                    $pdf->Cell(30, 7, "$mdebit", 1, 0, 'R');
                    $pdf->Cell(30, 7, " ", 1);
                    $pdf->Cell(30, 7, " ", 1);
                } else {
                    $pdf->Cell(30, 7, " ", 1);
                    $pdf->Cell(30, 7, "$mcredit", 1, 0, 'R');
                    $pdf->Cell(30, 7, " ", 1);
                }
                $pdf->Ln();
                $pdf->Cell(30, 7, " ", 1);
                $pdf->Cell(30, 7, " ", 1);
                if ($prnnarr == 1) {

                    if (!empty($mchqdtl)) {
                        $pdf->Cell(128, 7, "$mchqdtl", 1);
                        if (!empty($mnarr)) {
                            
                        }
                    }
                }
                if ($prntype == 1) {
                    $pdf->Cell(30, 7, " ", 1, 0, 'R');
                    $pdf->Cell(30, 7, " ", 1, 0, 'R');
                    $pdf->Cell(30, 7, " ", 1, 0, 'R');
                } elseif ($prntype == 2) {
                    $pdf->Cell(30, 7, " ", 1, 0, 'R');
                    $pdf->Cell(30, 7, " ", 1);
                    $pdf->Cell(30, 7, " ", 1);
                } else {
                    $pdf->Cell(30, 7, " ", 1);
                    $pdf->Cell(30, 7, " ", 1, 0, 'R');
                    $pdf->Cell(30, 7, " ", 1);
                }
                $pdf->Ln();
                $pdf->Cell(30, 7, " ", 1);
                $pdf->Cell(30, 7, " ", 1);
                if ($prnnarr == 1) {

                    if (!empty($mchqdtl)) {
                        if (!empty($mnarr)) {
                            $pdf->Cell(128, 7, "$mnarr", 1);
                        }
                    }
                }
                if ($prntype == 1) {
                    $pdf->Cell(30, 7, " ", 1, 0, 'R');
                    $pdf->Cell(30, 7, " ", 1, 0, 'R');
                    $pdf->Cell(30, 7, " ", 1, 0, 'R');
                } elseif ($prntype == 2) {
                    $pdf->Cell(30, 7, " ", 1, 0, 'R');
                    $pdf->Cell(30, 7, " ", 1);
                    $pdf->Cell(30, 7, " ", 1);
                } else {
                    $pdf->Cell(30, 7, " ", 1);
                    $pdf->Cell(30, 7, " ", 1, 0, 'R');
                    $pdf->Cell(30, 7, " ", 1);
                }
                $pdf->Ln();
            }
            $pdf->SetFont('Arial', 'I', 10);
            $pdf->SetTextColor(255, 255, 255);
            $pdf->SetFillColor(255, 0, 0);
            $pdf->Cell(30, 7, " ", 1, 0, '', 1);
            $pdf->Cell(30, 7, " ", 1, 0, '', 1);
            $pdf->Cell(128, 7, "Total", 1, 0, '', 1);
            if ($prntype == 1) {
                $pdf->Cell(30, 7, "$dtot", 1, 0, '', 1, 0, 'R');
                $pdf->Cell(30, 7, "$ctot", 1, 0, '', 1, 0, 'R');
                if ($totbal < 0) {
                    $pdf->Cell(30, 7, abs($totbal) . " " . $mdctot, 1, 0, '', 1, 0, 'R');
                } else {
                    $pdf->Cell(30, 7, abs($totbal) . " " . $mdctot, 1, 0, '', 1, 0, 'R');
                }
            } elseif ($prntype == 2) {
                $pdf->Cell(30, 7, "$dtot", 1, 0, '', 1, 0, 'R');
                $pdf->Cell(30, 7, " ", 1, 0, '', 1);
            } else {
                echo $pdf->Cell(30, 7, " ", 1);
                echo $pdf->Cell(30, 7, "$ctot", 1, 0, '', 1, 0, 'R');
                $pdf->SetFont('Arial', '', 1, 0, '', 1, 0);

                $pdf->SetTextColor(0, 0, 0);
                $pdf->Ln();
            }
        }
        $pdf->Output("BankBook.pdf", "D");
    }
    ?>
    

