<?php
session_start();
include './connection_sql.php';
if ($_SESSION["CURRENT_USER"] == "") { 
    exit('Please Logging Again..');
} 
if ($_SESSION['company'] == "") { 
    exit('Please Logging Again..');
} 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Ledger Accounts Details</title>
        <style>
            @media print {
                a[href]:after {
                    content: none !important;
                }
            }
            a:link, a:visited {

                text-decoration: none;
                color:#000000;
            }


            a:hover {
                text-decoration: underline;
            }
            body {
                color: #333;
                font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
                font-size: 12px;
                line-height: 1.42857;
            }
            .table > tbody > tr > td, .table > tbody > tr > th, .table > tfoot > tr > td, .table > tfoot > tr > th, .table > thead > tr > td, .table > thead > tr > th {
                border-top:1px solid #DDDDDD;
                line-height:1.42857;
                padding:2px;
                vertical-align:top;
            }
            .right {
                text-align: right;
            }    
            .center {
                text-align: center;
            }
        </style><style>
            @media print {
                a[href]:after {
                    content: none !important;
                }
            }
            a:link, a:visited {

                text-decoration: none;
                color:#000000;
            }


            a:hover {
                text-decoration: underline;
            }
            body {
                color: #333;
                font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
                font-size: 12px;
                line-height: 1.42857;
            }
            .table > tbody > tr > td, .table > tbody > tr > th, .table > tfoot > tr > td, .table > tfoot > tr > th, .table > thead > tr > td, .table > thead > tr > th {
                border-top:1px solid #DDDDDD;
                line-height:1.42857;
                padding:2px;
                vertical-align:top;
            }
            .right {
                text-align: right;
            }    
            .center {
                text-align: center;
            }
        </style>
        <script language="javascript" type="text/javascript">
        <!--
            /****************************************************
             Author: Eric King
             Url: http://redrival.com/eak/index.shtml
             This script is free to use as long as this info is left in
             Featured on Dynamic Drive script library (http://www.dynamicdrive.com)
             ****************************************************/
             var win = null;
             function NewWindow(mypage, myname, w, h, scroll, pos) {
                if (pos == "random") {
                    LeftPosition = (screen.width) ? Math.floor(Math.random() * (screen.width - w)) : 100;
                    TopPosition = (screen.height) ? Math.floor(Math.random() * ((screen.height - h) - 75)) : 100;
                }
                if (pos == "center") {
                    LeftPosition = (screen.width) ? (screen.width - w) / 2 : 100;
                    TopPosition = (screen.height) ? (screen.height - h) / 2 : 100;
                } else if ((pos != "center" && pos != "random") || pos == null) {
                    LeftPosition = 0;
                    TopPosition = 20
                }
                settings = 'width=' + w + ',height=' + h + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
                win = window.open(mypage, myname, settings);
            }
            // -->
        </script>
    </head>
    <body>
        <?php
      
        require_once './gl_posting.php';
        date_default_timezone_set('Asia/Colombo');
       $sql_rspara = "select * from a_dep_mas where COMCODE='".$_SESSION['company']."'";
        $result_rspara = $conn->query($sql_rspara);  
        if (!$row_rspara = $result_rspara->fetch()) {
            exit();
        }

        $sql = "delete from a_ledprint ";
        $result = $conn->query($sql);
        $txtAccCode = $_GET["txtAccCode"];

        $sql = "select * from a_lcodes where c_code = '" . $txtAccCode . "'";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {
            $ac_type = $row['C_TYPE'];
        }


        $ayear = ac_year($_GET["repdatefrom"]);
        $dtb = $ayear . "-04-01";


        $OpDbAmu = 0;
        $OpCrAmu = 0;
        $OpBalAm = 0;
        $OpDbAmu = 0;
        $OpLnkAmt = 0;

        $sql_opCR = "select sum(l_amount)as ctot from a_ledger where  l_flag1='CRE' and COMPANY = '" . $_SESSION['company']  . "' and l_code='" . $txtAccCode . "' and (l_date<'" . $_GET["repdatefrom"] . "'  )";
        if ($ac_type != "B") {
            $sql_opCR .= " and AcYear ='" . $ayear . "'";
        }


        $result = $conn->query($sql_opCR);
        if ($row_opCR = $result->fetch()) {
            $OpCrAmu = $OpCrAmu + $row_opCR["ctot"];
        }

        $sql_opDb = "select sum(l_amount)as dtot from a_ledger where  l_flag1='DEB' and COMPANY = '" . $_SESSION['company']  . "' and l_code='" . $txtAccCode . "'  and (l_date<'" . $_GET["repdatefrom"] . "' ) ";
        if ($ac_type != "B") {
            $sql_opDb .= " and AcYear ='" . $ayear . "'";
        }
        $result = $conn->query($sql_opDb);
        if ($row_opDb = $result->fetch()) {
            $OpDbAmu = $OpDbAmu + $row_opDb["dtot"];
        }


        $bF = $OpBalAm + $OpDbAmu - $OpCrAmu + $OpLnkAmt;


        $txtdes = " Account : </b> " . $_GET["txtAccCode"] . " - " . $_GET["txtAccName"] . "   " . "<br><b>Date From</b> - " . $_GET["repdatefrom"] . " <b>To</b> " . $_GET["repdateto"];
        ;
        $table = "";
        $table .= "<h3>" . $row_rspara['COMPANY'] . "</h3>";
        $table .= "<h4>" . $txtdes . "</h4>";

        echo $table;



        $DEB_amt = 0;
        $CRE_amt = 0;



        echo "<table  class='table'>
		
      	<tr>
        <th width=\"70\">Date</th>
        <th width=\"200\">Refno</th>
        <th width=\"400\">Naration</th>
        <th width=\"100\">Debit</th>
	<th width=\"100\">Credit</th>
	<th width=\"100\">Balance</th>
        </tr>";
        $debamt = 0;
        $creamt = 0;
        $baltot = 0;

        $totdebamt = 0;
        $totcreamt = 0;
        $refno = "";

        $mst = 0;

        echo "<tr><td>" . $_GET["repdatefrom"] . "</td>
				<td>B/F</td>
				<td>Opening Balance</td>";

        if ($bF > 0) {
            echo "<td align=right>" . number_format($bF, 2, ".", ",") . "</td>";
            echo "<td></td>";
            $totdebamt = $totdebamt + $bF;
        } else {
            echo "<td></td>";
            echo "<td align=right>" . number_format($bF, 2, ".", ",") . "</td>";
            $totcreamt = $totcreamt + $bF;
        }

        if ($bF > 0) {
            echo "<td align=right>" . number_format($bF, 2, ".", ",") . "</td>";
        } else {
            echo "<td align=right>(" . number_format((-1 * $bF), 2, ".", ",") . ")</td>";
        }


        $baltot = $baltot + $bF;

        echo "</tr>";


        $sql_rsPrInv = "select * from a_ledger where l_flag != 'OPB' and COMPANY = '" . $_SESSION['company']  . "' and AcYear = '" . $ayear . "' and l_code = '" . $txtAccCode . "' and ( l_date >='" . $_GET["repdatefrom"] . "') and  ( l_date <= '" . $_GET["repdateto"] . "' ) order by L_DATE,ID";

        //$sql_rsPrInv = "select *  from ledprint order by sdate, refno";
        foreach ($conn->query($sql_rsPrInv) as $row_rsPrInv) {

            if ($row_rsPrInv["L_FLAG1"] == "DEB") {
                $debamt = $row_rsPrInv["L_AMOUNT"];
            } else {
                $debamt = 0;
            }

            if ($row_rsPrInv["L_FLAG1"] == "CRE") {
                $creamt = $row_rsPrInv["L_AMOUNT"];
            } else {
                $creamt = 0;
            }



            $bal = $debamt - $creamt;
            $baltot = $baltot + $bal;



            echo "<tr><td>" . $row_rsPrInv["L_DATE"] . "</td>
				<td><a onfocus=\"this.blur()\" onclick=\"NewWindow('ledger_view.php?refno=" . $row_rsPrInv["L_REFNO"] . "&amp;type=INV','mywin','900','700','yes','center');return false\" class=\"INV\" href=\"\">" . $row_rsPrInv["L_REFNO"] . "</a></td></td>
				<td>" . $row_rsPrInv["L_LMEM"] . "</td>";

            echo "<td align=right>" . number_format($debamt, 2, ".", ",") . "</td>";

            echo "<td align=right>" . number_format($creamt, 2, ".", ",") . "</td>";

            if ($baltot > 0) {
                echo "<td align=right>" . number_format($baltot, 2, ".", ",") . "</td>";
            } else {
                echo "<td align=right>(" . number_format((-1 * $baltot), 2, ".", ",") . ")</td>";
            }
            echo "</tr>";


            $totdebamt = $totdebamt + $debamt;
            $totcreamt = $totcreamt + $creamt;
        }
        echo "<tr><td colspan=3>&nbsp;</td><td align=right><b>" . number_format($totdebamt, 2, ".", ",") . "</b></td><td align=right><b>" . number_format($totcreamt, 2, ".", ",") . "</b></td>";
        if ($baltot > 0) {
            echo "<td align=right><b>" . number_format($baltot, 2, ".", ",") . "</b></td></tr>";
        } else {
            echo "<td align=right><b>(" . number_format((-1 * $baltot), 2, ".", ",") . ")</b></td></tr>";
        }
        ?>  



        </table>
    </body>
</html>