<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <?php
    if ($_GET['optionsRadios'] == "op_bs") {
        echo "<title>BS</title>";
    } else {
        echo "<title>PNL</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;
        }  
        .left {
            text-align: left;
        }
        .center {
            text-align: center;
        }
    </style>
</head>

<body> 
    <?php
    require_once ("./connection_sql.php");
    require_once './gl_posting.php';


    $sql_rspara = "select * from company_info";
    $result = $conn->query($sql_rspara);
    $row_rspara = $result->fetch();



        if ($_GET['optionsRadios'] == "op_pnl") {

            $txtdes = "Profit & Loss Statmenet For the Period : " . 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 .= "<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;'>Closing Balance</th>     		
        </tr>";


            $bal = 0;
            $mcl_bal = 0;

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


            $sql = "update ledger set l_amount1 = l_amount where l_flag1 = 'DEB'";
            $result = $conn->query($sql);

            $sql = "update ledger set l_amount1 = l_amount *-1 where l_flag1 = 'CRE'";
            $result = $conn->query($sql);





            $sql_rsPrInv = "select c_type,l_code,name,sum(l_amount1)as amo, c_subgro1,c_subgro2   from view_ledger_final  where c_type ='P' and (l_date>='" . $_GET["dtfrom"] . "' and l_date<='" . $_GET['dtto'] . "') group by c_type,l_code,name,c_subgro1,c_subgro2 order by  c_subgro1 desc,c_subgro2,l_code ";
            $bF = 0;
            $cgroup = "";
            $cgroup1 = "";
            $camo = 0;
            $camo1 = 0;
//            echo $sql_rsPrInv;
            foreach ($conn->query($sql_rsPrInv) as $row_rsPrInv) {

                //if (trim($row_rsPrInv['c_subgro1']) == "Revenue") {
                $pen = ($row_rsPrInv['amo']);
                //}
                if (trim($row_rsPrInv['c_subgro1']) == "Expenses") {
                    // $pen = abs($row_rsPrInv['amo']);
                    // $pen = $pen * -1;
                }

                if ($pen < 0) {
                    $pen = abs($row_rsPrInv['amo']);
                } else {
                    $pen = ($row_rsPrInv['amo'] * -1);
                }


                if ($cgroup != $row_rsPrInv['c_subgro1']) {


                    if ($cgroup1 != "") {
                        if ($camo1 >= 0) {
                            $table .= "<tr><th colspan='2'></th><th class='right'>" . number_format($camo1, 2, ".", ",") . "</th></tr>";
                        } else {
                            $table .= "<tr><th colspan='2'></th><th class='right'>(" . number_format(($camo1 * -1), 2, ".", ",") . ")</th></tr>";
                        }
                    }
                    if ($cgroup != "") {
                        if ($camo >= 0) {
                            $table .= "<tr><th colspan='2'>Total " . $cgroup . "</th><th class='right'>" . number_format($camo, 2, ".", ",") . "</th></tr>";
                        } else {
                            $table .= "<tr><th colspan='2'>Total " . $cgroup . "</th><th class='right'>(" . number_format(($camo * -1), 2, ".", ",") . ")</th></tr>";
                        }
                    }
                    $table .= "<tr><th class='left' colspan='3'>" . $row_rsPrInv['c_subgro1'] . "</th></tr>";
                    $camo = 0;
                    $camo1 = 0;
                    $cgroup1 = "";
                }
                if ($cgroup1 != $row_rsPrInv['c_subgro2']) {
                    if ($cgroup1 != "") {
                        if ($camo1 >= 0) {
                            $table .= "<tr><th colspan='2'></th><th class='right'>" . number_format($camo1, 2, ".", ",") . "</th></tr>";
                        } else {
                            $table .= "<tr><th colspan='2'></th><th class='right'>(" . number_format(($camo1 * -1), 2, ".", ",") . ")</th></tr>";
                        }
                    }
                    $table .= "<tr><th></th><th class='left' colspan='2'>" . $row_rsPrInv['c_subgro2'] . "</th></tr>";
                    $camo1 = 0;
                }




                $cgroup = $row_rsPrInv['c_subgro1'];
                $cgroup1 = $row_rsPrInv['c_subgro2'];

                $ac_type = $row_rsPrInv['c_type'];



                $camo = $camo + $pen;
                $camo1 = $camo1 + $pen;



                $url = "report_ledger_link.php?txtAccCode=" . $row_rsPrInv["l_code"] . "&txtAccName=" . $row_rsPrInv["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["name"] . "</a></td>";







                $cl_bal = $bF + $pen;

                if ($cl_bal >= 0) {
                    $table .= "<td class='right'>" . number_format($cl_bal, 2, ".", ",") . "</td>";
                } else {
                    $table .= "<td class='right'>(" . number_format(-1 * $cl_bal, 2, ".", ",") . ")</td>";
                }

                $mcl_bal = $mcl_bal + $cl_bal;

                $table .= "</tr>";
            }


            if ($camo1 >= 0) {
                $table .= "<tr><th colspan='2'></th><th class='right'>" . number_format($camo1, 2, ".", ",") . "</th></tr>";
            } else {
                $table .= "<tr><th colspan='2'></th><th class='right'>(" . number_format(($camo1 * -1), 2, ".", ",") . ")</th></tr>";
            }


            if ($camo >= 0) {
                $table .= "<tr><th colspan='2'>Total " . $cgroup . "</th><th class='right'>" . number_format($camo, 2, ".", ",") . "</th></tr>";
            } else {
                $table .= "<tr><th colspan='2'>Total " . $cgroup . "</th><th class='right'>(" . number_format(($camo * -1), 2, ".", ",") . ")</th></tr>";
            }


            $table .= "<tr><th  class='left' colspan=2>Current Year Income/Loss</th>
		 
		 
		<th  class='right'>" . number_format($mcl_bal, 2, ".", ",") . "</th></tr>";
            $table .= "</table>
 
</body>
</html>";

            echo $table;
        } else {

            $txtdes = "Balance Sheet As of : " . date("Y-m-d", strtotime($_GET["dtto"]));


            $table = "";
            $table .= "<h3>" . $row_rspara['COMPANY'] . "</h3>";
            $table .= "<h4>" . $txtdes . "</h4>";






            $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;'>Closing Balance</th>     		
        </tr>";


            $bal = 0;
            $mcl_bal = 0;

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


            $sql = "update ledger set l_amount1 = l_amount where l_flag1 = 'DEB'";
            $result = $conn->query($sql);

            $sql = "update ledger set l_amount1 = l_amount *-1 where l_flag1 = 'CRE'";
            $result = $conn->query($sql);


            $mearn = 0;
            $sql = "select  sum(l_amount1) as amo ,c_subgro1 from view_ledger_final where c_subgro1 <> '' ";
            $sql .= " and acyear <'" . $ayear . "' group by c_subgro1";
               
            foreach ($conn->query($sql) as $row_rsPrInv) {
                if (trim($row_rsPrInv['c_subgro1']) == "Revenue") {
                    $mearn = $mearn - $row_rsPrInv['amo'] * -1;
                }
                if (trim($row_rsPrInv['c_subgro1']) == "Expenses") {
                    $mearn = $mearn + $row_rsPrInv['amo'];
                }
            }
            $mearn1 = 0;
            $sql = "select  sum(l_amount1) as amo ,c_subgro1 from view_ledger_final where c_subgro1 <> '' ";
            $sql .= " and acyear ='" . $ayear . "' and l_date <='" . $_GET['dtto'] . "' group by c_subgro1";

            foreach ($conn->query($sql) as $row_rsPrInv) {
                if (trim($row_rsPrInv['c_subgro1']) == "Revenue") {
                    $mearn1 = $mearn1 + $row_rsPrInv['amo'] * -1;
                }
                if (trim($row_rsPrInv['c_subgro1']) == "Expenses") {
                    $mearn1 = $mearn1 - $row_rsPrInv['amo'];
                }
            }


            $sql_rsPrInv = "select c_type,l_code,name,sum(l_amount1)as amo, c_subgro1,c_subgro2   from view_ledger_final  where c_type ='B' and (l_date<='" . $_GET['dtto'] . "') group by c_type,l_code,name,c_subgro1,c_subgro2,pos order by  pos ,c_subgro2,l_code ";

            $bF = 0;
            $cgroup = "";
            $cgroup1 = "";
            $camo = 0;
            $camo1 = 0;
            foreach ($conn->query($sql_rsPrInv) as $row_rsPrInv) {
                $pen = ($row_rsPrInv['amo']);

                if ($row_rsPrInv['l_code'] == "3002") {
                    $pen = $pen + $mearn;
                }

                if ($row_rsPrInv['c_subgro1'] == "Equity") {
                    $pen = abs($pen);
                }

                if ($cgroup != $row_rsPrInv['c_subgro1']) {

                    if ($cgroup1 != "") {
                        if ($camo1 >= 0) {
                            $table .= "<tr><th colspan='2'></th><th class='right'>" . number_format($camo1, 2, ".", ",") . "</th></tr>";
                        } else {
                            $table .= "<tr><th colspan='2'></th><th class='right'>(" . number_format(($camo1 * -1), 2, ".", ",") . ")</th></tr>";
                        }
                    }
                    if ($cgroup != "") {
                        if ($camo >= 0) {
                            $table .= "<tr><th colspan='2'>Total " . $cgroup . "</th><th class='right'>" . number_format($camo, 2, ".", ",") . "</th></tr>";
                        } else {
                            $table .= "<tr><th colspan='2'>Total " . $cgroup . "</th><th class='right'>(" . number_format(($camo * -1), 2, ".", ",") . ")</th></tr>";
                        }
                    }


                    if ($cgroup == "Liabilities") {

                        if ($mcl_bal >= 0) {
                            $table .= "<tr><th colspan='2'>Net Assets & Liabilities</th><th class='right'>" . number_format($mcl_bal, 2, ".", ",") . "</th></tr>";
                        } else {
                            $table .= "<tr><th colspan='2'></th><th class='right'>(" . number_format(($mcl_bal * -1), 2, ".", ",") . ")</th></tr>";
                        }
                    }


                    $table .= "<tr><th class='left' colspan='3'>" . $row_rsPrInv['c_subgro1'] . "</th></tr>";
                    $camo = 0;
                    $camo1 = 0;
                    $cgroup1 = "";
                }
                if ($cgroup1 != $row_rsPrInv['c_subgro2']) {
                    if ($cgroup1 != "") {
                        if ($camo1 >= 0) {
                            $table .= "<tr><th colspan='2'></th><th class='right'>" . number_format($camo1, 2, ".", ",") . "</th></tr>";
                        } else {
                            $table .= "<tr><th colspan='2'></th><th class='right'>(" . number_format(($camo1 * -1), 2, ".", ",") . ")</th></tr>";
                        }
                    }
                    $table .= "<tr><th></th><th class='left' colspan='2'>" . $row_rsPrInv['c_subgro2'] . "</th></tr>";
                    $camo1 = 0;
                }




                $cgroup = $row_rsPrInv['c_subgro1'];
                $cgroup1 = $row_rsPrInv['c_subgro2'];

                $ac_type = $row_rsPrInv['c_type'];



                $camo = $camo + $pen;
                $camo1 = $camo1 + $pen;



                $url = "report_ledger_link.php?txtAccCode=" . $row_rsPrInv["l_code"] . "&txtAccName=" . $row_rsPrInv["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["name"] . "</a></td>";

                $cl_bal = $bF + $pen;

                if ($cl_bal >= 0) {
                    $table .= "<td class='right'>" . number_format($cl_bal, 2, ".", ",") . "</td>";
                } else {
                    $table .= "<td class='right'>(" . number_format(-1 * $cl_bal, 2, ".", ",") . ")</td>";
                }

                $mcl_bal = $mcl_bal + $cl_bal;

                $table .= "</tr>";
            }

            $table .= "<tr><td></td><td  class='left'>Current Year Income/Loss</td>
		   <td  class='right'>" . number_format($mearn1, 2, ".", ",") . "</td></tr>";
            $camo1 = $camo1 + $mearn1;
            $camo = $camo + $mearn1;


            if ($camo1 >= 0) {
                $table .= "<tr><th colspan='2'></th><th class='right'>" . number_format($camo1, 2, ".", ",") . "</th></tr>";
            } else {
                $table .= "<tr><th colspan='2'></th><th class='right'>(" . number_format(($camo1 * -1), 2, ".", ",") . ")</th></tr>";
            }
            if ($camo >= 0) {
                $table .= "<tr><th colspan='2'>Total " . $cgroup . "</th><th class='right'>" . number_format($camo, 2, ".", ",") . "</th></tr>";
            } else {
                $table .= "<tr><th colspan='2'>Total " . $cgroup . "</th><th class='right'>(" . number_format(($camo * -1), 2, ".", ",") . ")</th></tr>";
            }

            $table .= "</table>
            


</body>
</html>";

            echo $table;
        }
