<?php
session_start();
include './connection_sql.php';
if ($_SESSION["CURRENT_USER"] == "") { 
    exit('Please Logging Again..');
} 
if ($_SESSION['company'] == "") { 
    exit('Please Logging Again..');
} 
?>
<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>Trial Balance</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>
         <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';


   $sql_rspara = "select * from a_dep_mas where COMCODE='".$_SESSION['company']."'";
        $result_rspara = $conn->query($sql_rspara);  
        if (!$row_rspara = $result_rspara->fetch()) {
            exit();
        }
        
        
        $ayear = ac_year($_GET["dtfrom"]);
        $dtb = $ayear . "-04-01";
    
    
        $sql = "update a_ledger set L_AMOUNT1 = l_amount where l_flag1 = 'DEB'";
        
        $result = $conn->query($sql);
    
        $sql = "update a_ledger set L_AMOUNT1 = l_amount *-1 where l_flag1 = 'CRE'";
        $result = $conn->query($sql);

    $txtdes = "TB Report From : " . date("Y-m-d", strtotime($_GET["dtfrom"])) . "    To: " . date("Y-m-d", strtotime($_GET["dtto"]));
    
    
    $table = "";
      $table .=  "<h3>" .  $row_rspara['COMPANY']  . "</h3>";
      $table .=  "<h4>" .  $txtdes  . "</h4>";
      $table .=  "<h4>" .  $_GET["type"]  . "</h4>";

    
  
    
    if($_GET["type"]=="TrailBalance"){
        $table .= "<table class='table' >
		
      	<tr>
            <th class='center'  style='width: 90px;'>Ledger</th>
            <th class='center'  style='width: 300px;'>Account Description</th> 
            <th class='center'  style='width: 90px;'>Debit</th>
            <th class='center'  style='width: 90px;'>Credit</th>     		
        </tr>";
 
        $sql_rsPrInv = "select  SUM(IF(L_FLAG1='DEB',L_AMOUNT,0)) AS DEB , SUM(IF(L_FLAG1='CRE',L_AMOUNT,0)) AS CRE,L_CODE,C_NAME from view_ledger where L_REFNO <> '' ";
        $sql_rsPrInv .= "   and COMPANY = '" . $_SESSION['company'] .  "' and L_DATE>='" . $_GET["dtfrom"] . "' and L_DATE<='" . $_GET['dtto'] . "' GROUP BY L_CODE"; 
     
        foreach ($conn->query($sql_rsPrInv) as $row_rsPrInv) {
            
                    $url = "report_ledger_link.php?txtAccCode=" . $row_rsPrInv["L_CODE"] . "&txtAccName=" . $row_rsPrInv["C_NAME"] . "&repdatefrom=" . $_GET["dtfrom"] . "&repdateto=" . $_GET["dtto"];
    
                    $table .= "<tr><td><a target='_blank' href='" . $url . "'>" . $row_rsPrInv["L_CODE"] . "</a></td>
    			                    <td><a target='_blank' href='" . $url . "'>" . $row_rsPrInv["C_NAME"] . "</a></td>
    			                    <td  class='right'>" . number_format($row_rsPrInv['DEB'], 2, ".", ",") . "</td>
    			                    <td  class='right'>" . number_format($row_rsPrInv['CRE'], 2, ".", ",") . "</td>";
    
                     
    
                    $table .= "</tr>";
                
            $DEB +=$row_rsPrInv['DEB'];
            $CRE +=$row_rsPrInv['CRE'];
        }
     
    
    
         $table .= "<tr><td colspan=2>&nbsp;</td>
    		<th  class='right'>" . number_format($DEB, 2, ".", ",") . "</th>
    		<th  class='right'>" . number_format($CRE, 2, ".", ",") . "</th> </tr>";
    		
    		$table .= "<tr><td colspan=3>&nbsp;</td> 
    		<th  class='right'>" . number_format($DEB-$CRE, 2, ".", ",") . "</th> </tr>";
    		
    } else if($_GET["type"]=="DetailsTrailBalance"){
        $table .= "<table class='table' >
		
      	<tr>
            <th class='center'  style='width: 90px;'>Ledger</th>
            <th class='center'  style='width: 300px;'>Account Description</th> 
            <th class='center'  style='width: 90px;'>Debit</th>
            <th class='center'  style='width: 90px;'>Credit</th>     		
        </tr>";
  
        $sql_rsPrInv = "select  SUM(IF(L_FLAG1='DEB',L_AMOUNT,0)) AS DEB , SUM(IF(L_FLAG1='CRE',L_AMOUNT,0)) AS CRE,L_CODE,C_NAME from view_ledger where L_REFNO <> '' ";
        $sql_rsPrInv .= "   and COMPANY = '" . $_SESSION['company'] .  "' and L_DATE>='" . $_GET["dtfrom"] . "' and L_DATE<='" . $_GET['dtto'] . "' order BY C_NAME"; 
     
        foreach ($conn->query($sql_rsPrInv) as $row_rsPrInv) {
            
                    $url = "report_ledger_link.php?txtAccCode=" . $row_rsPrInv["L_CODE"] . "&txtAccName=" . $row_rsPrInv["C_NAME"] . "&repdatefrom=" . $_GET["dtfrom"] . "&repdateto=" . $_GET["dtto"];
    
                    $table .= "<tr><td><a target='_blank' href='" . $url . "'>" . $row_rsPrInv["L_CODE"] . "</a></td>
    			                    <td><a target='_blank' href='" . $url . "'>" . $row_rsPrInv["C_NAME"] . "</a></td>
    			                    <td  class='right'>" . number_format($row_rsPrInv['DEB'], 2, ".", ",") . "</td>
    			                    <td  class='right'>" . number_format($row_rsPrInv['CRE'], 2, ".", ",") . "</td>";
    
                     
    
                    $table .= "</tr>";
                
            $DEB +=$row_rsPrInv['DEB'];
            $CRE +=$row_rsPrInv['CRE'];
        }
     
    
    
         $table .= "<tr><td colspan=2>&nbsp;</td>
    		<th  class='right'>" . number_format($DEB, 2, ".", ",") . "</th>
    		<th  class='right'>" . number_format($CRE, 2, ".", ",") . "</th> </tr>";
    		
    		$table .= "<tr><td colspan=3>&nbsp;</td> 
    		<th  class='right'>" . number_format($DEB-$CRE, 2, ".", ",") . "</th> </tr>";
    		
    } else if($_GET["type"]=="EntryTrailBalance"){
        $table .= "<table class='table' >
		
      	<tr>
            <th class='center'  style='width: 90px;'>Date</th>
            <th class='center'  style='width: 90px;'>Refno</th>
            <th class='center'  style='width: 300px;'>Account Code</th>
            <th class='center'  style='width: 600px;'>Account Description</th> 
            <th class='center'  style='width: 90px;'>Debit</th>
            <th class='center'  style='width: 90px;'>Credit</th>     		
        </tr>";
  
        $sql_rsPrInv = "select  L_AMOUNT,L_CODE,C_NAME,L_LMEM,L_REFNO,L_FLAG1,L_DATE from view_ledger where L_REFNO <> '' ";
        $sql_rsPrInv .= "   and COMPANY = '" . $_SESSION['company'] .  "' and L_DATE>='" . $_GET["dtfrom"] . "' and L_DATE<='" . $_GET['dtto'] . "' ORDER BY L_DATE,L_REFNO,L_FLAG1"; 
      $refno="";
        foreach ($conn->query($sql_rsPrInv) as $row_rsPrInv) {
            
             
    
            if($refno !=$row_rsPrInv["L_REFNO"]){
                 $table .= "<tr><td colspan='6' class='right'></td></tr>"; 
                 $table .= "<tr><td colspan='6' class='right'></td></tr>"; 
            }
            
            $sql_ledger = "select SUM(IF(L_FLAG1='DEB',L_AMOUNT,0)) AS DEB , SUM(IF(L_FLAG1='CRE',L_AMOUNT,0)) AS CRE from a_ledger where L_REFNO='".$row_rsPrInv["L_REFNO"]."'";
            $result_ledger = $conn->query($sql_ledger);  
            $row_ledger = $result_ledger->fetch();
            
            
            if($row_ledger['DEB'] !==$row_ledger['CRE']){
                $table .= "<tr style='background-color:#ff7575'>";
            }else{
                $table .= "<tr>";
            }
            
            $table .= "<td style='border:none;'>" . $row_rsPrInv["L_DATE"] . "</td>
            <td style='border:none;'><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 style='border:none;'>" . $row_rsPrInv["L_CODE"] . " - " . $row_rsPrInv["C_NAME"] . "</a></td>
            <td style='border:none;'>" . $row_rsPrInv["L_LMEM"] . "</td>";
            if($row_rsPrInv["L_FLAG1"]=="DEB"){
                $table .= "  <td style='border:none;' class='right'>" . number_format($row_rsPrInv['L_AMOUNT'], 2, ".", ",") . "</td>";
                $table .= "  <td style='border:none;' class='right'></td>";
                
                $DEB +=$row_rsPrInv['L_AMOUNT'];
                
                $SUBDEB +=$row_rsPrInv['L_AMOUNT'];
            }else{
                $table .= "  <td style='border:none;' class='right'></td>";
                 $table .= "  <td style='border:none;' class='right'>" . number_format($row_rsPrInv['L_AMOUNT'], 2, ".", ",") . "</td>";
                 
                 $CRE +=$row_rsPrInv['L_AMOUNT'];
                 $SUBCRE +=$row_rsPrInv['L_AMOUNT'];
            }
            
            if($row_ledger['DEB'] !==$row_ledger['CRE']){
                $table .= "<td style='border:none;' class='right'>" . number_format(($row_ledger['DEB'] -$row_ledger['CRE']), 2, ".", ",") . "</td>";
            } 
                
                
                
                
		                   
            $table .= "</tr>";
                
            $refno=$row_rsPrInv["L_REFNO"];
            
        }
     
    
    
         $table .= "<tr><td colspan=4>&nbsp;</td>
    		<th  class='right'>" . number_format($DEB, 2, ".", ",") . "</th>
    		<th  class='right'>" . number_format($CRE, 2, ".", ",") . "</th> </tr>";
    		
    		$table .= "<tr><td colspan=5>&nbsp;</td> 
    		<th  class='right'>" . number_format($DEB-$CRE, 2, ".", ",") . "</th> </tr>";
    		
    }
    
    
    
    
    
    
    $table .= "</table>
 
</body>
</html>";

    echo $table;

    