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

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

$series = $_SESSION["society_" . $dbname];
mysqli_select_db($conn, $zdb);


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

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

    $maindb = "grpmast";
    $formname = "Income/Expenditure";
    $file = "profit.php";

    $actdb = 'actran';

    $getpmain1 = mysqli_query($conn, "select accode, name, init, grpcode from accmast where grpcode in (SELECT grpcode FROM `$maindb` WHERE `subtype`='TE' and grpcode != '019') and series = '$series' order by name ")or die(mysqli_error($conn));
    $getpmain2 = mysqli_query($conn, "select accode, name, init, grpcode from accmast where grpcode in (SELECT grpcode FROM `$maindb` WHERE `subtype`='TI' and grpcode != '008') and series = '$series' order by name ")or die(mysqli_error($conn));

    $getpmain11 = mysqli_query($conn, "select accode, name, init, grpcode from accmast where grpcode in (SELECT grpcode FROM `$maindb` WHERE `subtype`='E' and grpcode != '019') and series = '$series' order by grpcode, name ")or die(mysqli_error($conn));
    $getpmain22 = mysqli_query($conn, "select accode, name, init, grpcode from accmast where grpcode in (SELECT grpcode FROM `$maindb` WHERE `subtype`='I' and grpcode != '008') and series = '$series' order by grpcode, name ")or die(mysqli_error($conn));

    $getopstk = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE grpcode = '019' order by name ")or die(mysqli_error($conn));
    $getclost = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE grpcode = '008' order by name ")or die(mysqli_error($conn));

    $get11no = mysqli_num_rows($getpmain11);
    $get22no = mysqli_num_rows($getpmain22);

    $ext11no = $get22no - $get11no;
    $ext22no = $get11no - $get22no;

    $totamt1 = 0;
    $totamt2 = 0;

    $mfrom = $_POST['frdt'];
    $mto = $_POST['todt'];

    $mfdt = date("d-m-Y", strtotime($mfrom));
    $mtdt = date("d-m-Y", strtotime($mto));

    $diffname1 = 'Gross Profit';
    $diffname2 = 'Gross Loss';
    $diffamt = 0;

    $netname1 = 'Net Profit';
    $netname2 = 'Net Loss';
    $diffamt = '';

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

    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>    
            <div class="row">
                <div class="col-lg-12 pad">

                    <!--******************expense and income part *********************-->

                    <table class="table  table-condensed table-bordered" style="float: left; width: 48%;margin: 2px">
                        <tr  style="" class="mySearchTr" >
                            <th width="15%" class="text-right">Opening</th>                        
                            <th width="70%">Expense</th>                        
                            <th width="15%" class="text-right">Amount</th>                        
                        </tr>
                        <?php
                        $tgrp = '';
                        $ctr = 1;
                        while ($pmain11 = mysqli_fetch_array($getpmain11)) {
                            $pamt11 = 0;
                            $paccode = $pmain11[accode];
                            $grp = $pmain11[grpcode];

                            $qryac11 = mysqli_query($conn, "SELECT sum(amount) as acamt, accode FROM $actdb WHERE `series` = '$series' and date >= '$mfrom' and date <= '$mto' 
                                                and chqret != 'Y' and `accode` = '$paccode' ") or die(mysqli_error($conn));
                            $pacc11 = mysqli_fetch_array($qryac11);
                            $qamt11 = $pacc11[acamt];
                            $pamt11 = $qamt11 * -1;


                            if ($pamt11 != 0) {

                                if ($grp != $tgrp) {
                                    $grpqry = mysqli_query($conn, "select name from grpmast where grpcode = '$grp'") or die(mysqli_error($conn));
                                    $grpfet = mysqli_fetch_array($grpqry);
                                    $pgrpname = $grpfet[name];

                                    Echo "<tr class='mySearchTr2'>";
                                    Echo "<td></td>";
                                    Echo "<td style='font-size:15px; text-align:center;'><b>$pgrpname</b></td>";
                                    Echo "<td></td>";
                                    Echo "</tr>";
                                    $tgrp = $grp;
                                }

                                $opqry1 = mysqli_query($conn, "select openbal, dc from accmast where `series` = '$series' and accode = '$paccode' ") or die(mysqli_error($conn));
                                $opfet1 = mysqli_fetch_array($opqry1);
                                $popenbal1 = $opfet1['openbal'];
                                $pdc1 = $opfet1['dc'];

                                if ($pdc1 == 'D') {
                                    $popenbal1 = abs($popenbal1) * -1;
                                } else {
                                    $popenbal1 = abs($popenbal1);
                                }

                                Echo "<tr>";
                                Echo "<td width='15%' class='text-right'>$popenbal1</td>";
                                Echo "<td width='70%'>$pmain11[name]</td>";
                                Echo "<td width='15%' class='text-right'>$pamt11</td>";
                                Echo "</tr>";

                                $totamt1 = $totamt1 + $pamt11;
                                $ctr++;
                            }
                        }
                        ?>                    

                    </table>

                    <table class="table table-condensed table-bordered  " style="float: left; width: 48%;margin: 2px">
                        <tr  style=" " class="mySearchTr">
                            <th width="15%"style="text-align: right">Opening</th>                        
                            <th width="70%">Income</th>                        
                            <th width="15%"style="text-align: right">Amount</th>                        
                        </tr>
                        <?php
                        $ctr = 1;
                        $tgrp = '';
                        while ($pmain22 = mysqli_fetch_array($getpmain22)) {
                            $pamt22 = 0;
                            $paccode22 = $pmain22[accode];
                            $grp = $pmain22[grpcode];

                            $qryac22 = mysqli_query($conn, "SELECT sum(amount) as acamt FROM $actdb WHERE `series` = '$series' and date >= '$mfrom' and date <= '$mto' 
                                                and chqret != 'Y' and `accode` = '$paccode22' ") or die(mysqli_error($conn));
                            $pacc22 = mysqli_fetch_array($qryac22);
                            $qamt22 = $pacc22[acamt];
                            $pamt22 = $qamt22;

                            if ($pamt22 != 0) {
                                if ($grp != $tgrp) {
                                    $grpqry = mysqli_query($conn, "select name from grpmast where grpcode = '$grp'") or die(mysqli_error($conn));
                                    $grpfet = mysqli_fetch_array($grpqry);
                                    $pgrpname = $grpfet['name'];

                                    Echo "<tr class='mySearchTr2'>";
                                    Echo "<td></td>";
                                    Echo"<td style='font-size:15px; text-align:center;'><b>$pgrpname</b></td>";
                                    Echo "<td></td>";
                                    Echo "</tr>";
                                    $tgrp = $grp;
                                }

                                $opqry2 = mysqli_query($conn, "select openbal, dc from accmast where `series` = '$series' and accode = '$paccode22' ") or die(mysqli_error($conn));
                                $opfet2 = mysqli_fetch_array($opqry2);
                                $popenbal2 = $opfet2['openbal'];
                                $pdc2 = $opfet2['dc'];

                                if ($pdc2 == 'D') {
                                    $popenbal2 = abs($popenbal2) * -1;
                                } else {
                                    $popenbal2 = abs($popenbal2);
                                }

                                Echo "<tr>";
                                Echo "<td width='15%' class='text-right'>$popenbal2</td>";
                                Echo "<td width='70%'>$pmain22[name]</td>";
                                Echo "<td width='15%'class='text-right'>$pamt22</td>";
                                Echo "</tr>";

                                $totamt2 = $totamt2 + $pamt22;
                                $ctr++;
                            }
                        }
                        ?>
                    </table>

                    <table class="table table-bordered table-condensed " style="float: left; width: 96%;margin: 2px">

                        <?php
                        $netamt = $totamt2 - $totamt1;
                        if ($netamt > 0) {
                            $netname2 = '';
                        } else {
                            $netname1 = '';
                        }

                        Echo "<tr>";

                        if ($netamt > 0) {
                            Echo"<td width='43%'>$netname1</td>";
                            Echo"<td width='7%' class='text-right'>$netamt</td>";
                            Echo"<td width='43%'></td>";
                            Echo"<td width='7%'></td>";
                            $totamt1 = $totamt1 + $netamt;
                        } else {
                            Echo"<td width='43%'></td>";
                            Echo"<td width='7%'></td>";
                            Echo"<td width='42%'>$netname2</td>";
                            Echo"<td width='7%'>$netamt</td>";
                            $totamt2 = $totamt2 + $netamt;
                        }
                        Echo "</tr>";

                        Echo "<tr class='mySearchTr'>";
                        Echo"<td style=' color:white; '>TOTAL</td>";
                        Echo"<td class='text-right' style='color:white;' 'font-size:17px'><b>$totamt1</b></td>";
                        Echo"<td style='color:white;'></td>";
                        Echo"<td class='text-right'style='color:white;' 'font-size:17px' ><b>$totamt2</b></td>";
                        Echo "</tr>";

                        $qryprof = mysqli_query($conn, "select * from profit") or die(mysqli_error($conn));
                        $noprof = mysqli_num_rows($qryprof);
                        if ($noprof > 0) {
                            mysqli_query($conn, "update `profit` set amount = '$netamt'");
                        } else {
                            mysqli_query($conn, "insert into `profit` (amount) values ('$netamt')");
                        }
                        ?>
                    </table>

                </div>
            </div>
        </div>

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

    <?php
}

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

    $maindb = "grpmast";
    $formname = "Income/Expenditure";
    $file = "profit.php";

    $actdb = 'actran';

    $getpmain1 = mysqli_query($conn, "select accode, name, init, grpcode from accmast where grpcode in (SELECT grpcode FROM `$maindb` WHERE `subtype`='TE' and grpcode != '019') and series = '$series' order by name ")or die(mysqli_error($conn));
    $getpmain2 = mysqli_query($conn, "select accode, name, init, grpcode from accmast where grpcode in (SELECT grpcode FROM `$maindb` WHERE `subtype`='TI' and grpcode != '008') and series = '$series' order by name ")or die(mysqli_error($conn));

    $getpmain11 = mysqli_query($conn, "select accode, name, init, grpcode from accmast where grpcode in (SELECT grpcode FROM `$maindb` WHERE `subtype`='E' and grpcode != '019') and series = '$series' order by name ")or die(mysqli_error($conn));
    $getpmain22 = mysqli_query($conn, "select accode, name, init, grpcode from accmast where grpcode in (SELECT grpcode FROM `$maindb` WHERE `subtype`='I' and grpcode != '008') and series = '$series' order by name ")or die(mysqli_error($conn));

    $getopstk = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE grpcode = '019' order by name ")or die(mysqli_error($conn));
    $getclost = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE grpcode = '008' order by name ")or die(mysqli_error($conn));

    $get11no = mysqli_num_rows($getpmain11);
    $get22no = mysqli_num_rows($getpmain22);

    $ext11no = $get22no - $get11no;
    $ext22no = $get11no - $get22no;

    $totamt1 = 0;
    $totamt2 = 0;

    $mfrom = $_POST['frdt'];
    $mto = $_POST['todt'];

    $mfdt = date("d-m-Y", strtotime($mfrom));
    $mtdt = date("d-m-Y", strtotime($mto));

    $diffname1 = 'Gross Profit';
    $diffname2 = 'Gross Loss';
    $diffamt = 0;

    $netname1 = 'Net Profit';
    $netname2 = 'Net Loss';
    $diffamt = '';

    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->SetMargins(1,1,1);
//$pdf->SetXY(5,5);

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(96, 130, 191);
    $pdf->Cell(100, 8, 'Expense', 1, 0, '', 1);
    $pdf->Cell(50, 8, 'Ammount', 1, 0, '', 1);
    $pdf->Ln();

    $tgrp = '';
    $ctr = 1;
    while ($pmain11 = mysqli_fetch_array($getpmain11)) {
        $pamt11 = 0;
        $paccode = $pmain11[accode];
        $grp = $pmain11[grpcode];

        $qryac11 = mysqli_query($conn, "SELECT sum(amount) as acamt, accode FROM $actdb WHERE `series` = '$series' and date >= '$mfrom' and date <= '$mto' 
                                                and chqret != 'Y' and `accode` = '$paccode' ") or die(mysqli_error($conn));
        $pacc11 = mysqli_fetch_array($qryac11);
        $qamt11 = $pacc11[acamt];
        $pamt11 = $qamt11 * -1;


        if ($pamt11 != 0) {

            if ($grp != $tgrp) {
                $grpqry = mysqli_query($conn, "select name from grpmast where grpcode = '$grp'") or die(mysqli_error($conn));
                $grpfet = mysqli_fetch_array($grpqry);
                $pgrpname = $grpfet[name];

                $pdf->SetX("20");
                $y1 = $pdf->GetY();
                $pdf->SetFont('Arial', 'B', 10);
                $pdf->SetTextColor(0, 0, 0);
                $pdf->Cell("80", "0", "$pgrpname", "0");
                $pdf->Ln();
                $tgrp = $grp;
            }

            $pdf->SetFont('Arial', '', 10);
            $pdf->Cell(30, 8, $pmain11['name'], 1, 0);
            $pdf->Cell(30, 8, $pamt11, 1, 0);
            $pdf->Ln();
            $totamt1 = $totamt1 + $pamt11;
            $ctr++;
        }
    }

    $pdf->SetX("20");
    $y1 = $pdf->GetY();
    $pdf->SetFont('Arial', 'B', 10, '');
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(96, 130, 191);
    $pdf->Cell(30, 8, 'Income', 1, 0, '', 1);
    $pdf->Cell(30, 8, 'Amount', 1, 0, '', 1);
    $pdf->Ln();


    $ctr = 1;
    $tgrp = '';
    while ($pmain22 = mysqli_fetch_array($getpmain22)) {
        $pamt22 = 0;
        $paccode22 = $pmain22[accode];
        $grp = $pmain22[grpcode];

        $qryac22 = mysqli_query($conn, "SELECT sum(amount) as acamt FROM $actdb WHERE `series` = '$series' and date >= '$mfrom' and date <= '$mto' 
                                                and chqret != 'Y' and `accode` = '$paccode22' ") or die(mysqli_error($conn));
        $pacc22 = mysqli_fetch_array($qryac22);
        $qamt22 = $pacc22[acamt];
        $pamt22 = $qamt22;

        if ($pamt22 != 0) {
            if ($grp != $tgrp) {
                $grpqry = mysqli_query($conn, "select name from grpmast where grpcode = '$grp'") or die(mysqli_error($conn));
                $grpfet = mysqli_fetch_array($grpqry);
                $pgrpname = $grpfet[name];
                $pdf->SetX("20");
                $y1 = $pdf->GetY();
                $pdf->SetFont('Arial', 'BI', 8);
                $pdf->Cell(30, 8, "$pgrpname", 1, 0, '', 1);
                $pdf->Ln();
                $tgrp = $grp;
            }
            $pdf->SetX("20");
            $y1 = $pdf->GetY();
            $pdf->SetFont('Arial', 'BI', 8);
            $pdf->Cell(30, 8, "$pmain22[name]", 1, 0, '', 1);
            $pdf->Cell(30, 8, "$pamt22", 1, 0, '', 1);
            $pdf->Ln();
            $totamt2 = $totamt2 + $pamt22;
            $ctr++;
        }
    }



    $netamt = $totamt2 - $totamt1;
    if ($netamt > 0) {
        $netname2 = '';
    } else {
        $netname1 = '';
    }
    $pdf->Cell(10, 8, " ", 1, 0);
    if ($netamt > 0) {
        $pdf->Cell(10, 8, "$netname1", 1, 0);
        $pdf->Cell(10, 8, "$netamt", 1, 0);
        $pdf->Cell(10, 8, "", 1, 0);
        $pdf->Cell(10, 8, "", 1, 0);
        $totamt1 = $totamt1 + $netamt;
    } else {
        $pdf->Cell(10, 8, "", 1, 0);
        $pdf->Cell(10, 8, "", 1, 0);
        $pdf->Cell(10, 8, "$netname2", 1, 0);
        $pdf->Cell(10, 8, "$netamt", 1, 0);
        $totamt2 = $totamt2 + $netamt;
    }
    $pdf->Ln();
    $pdf->Cell(10, 8, "Total", 1, 0);
    $pdf->Cell(10, 8, "$totamt1", 1, 0);
    $pdf->Cell(10, 8, "", 1, 0);
    $pdf->Cell(10, 8, "$totamt2", 1, 0);
    $pdf->Ln();
    $qryprof = mysqli_query($conn, "select * from profit") or die(mysqli_error($conn));
    $noprof = mysqli_num_rows($qryprof);
    if ($noprof > 0) {
        mysqli_query($conn, "update `profit` set amount = '$netamt'");
    } else {
        mysqli_query($conn, "insert into `profit` (amount) values ('$netamt')");
    }






    $pdf->Output('Income/Expenditure.pdf', 'D');
}
?>