<?php
include("../../db.php");

$_POST['tfrom'] = $_REQUEST['tfrom'];
$_POST['tto'] = $_REQUEST['tto'];

$_SESSION["admin"]["session"] = $_REQUEST['session'];
$_SESSION["admin"]["school_id"] = $_REQUEST['school_id'];

$between = "";

if (isset($_GET["tfrom"]) && isset($_GET["tto"])) {

    $between = " and udate between '" . $_GET["tfrom"] . "' and '" . $_GET["tto"] . "' ";

    if (isset($_GET['user1'])) {

        $u = implode(',', $_POST["user1"]);
        $whereUser = " and added_by in ($u) ";
        $Username = $u;

    }

    $aryList = $db->getRows("SELECT *,sum(amount) as total_income FROM transaction where school_id='" . $_SESSION["admin"]["school_id"] . "' and type='deposit'  $whereUser and (tfor not like 'fee')  and (tfor not like 'bus') and status=1 and amount!=0  and tid!=0 and rid!=0  and session='" . $_SESSION["admin"]["session"] . "' $between group by tfor order by tfor asc");

//echo $ss="SELECT *,sum(amount) as total_income FROM transaction where school_id='".$_SESSION["admin"]["school_id"]."' and type='deposit'  $whereUser and (tfor not like 'fee')  and (tfor not like 'bus') and status=1 and amount!=0  and tid!=0 and rid!=0  and session='".$_SESSION["admin"]["session"]."' $between group by tfor order by tfor asc";


    $aryList_ex = $db->getRows("SELECT *,sum(amount) as total_ex FROM transaction where school_id='" . $_SESSION["admin"]["school_id"] . "'  $whereUser and type='withdraw' and status=1 and amount!=0  and session='" . $_SESSION["admin"]["session"] . "' $between group by tfor order by tfor asc");

//$aryList=$db->getRows("SELECT * FROM transaction where school_id='".$_SESSION["admin"]["school_id"]."' and type='deposit' and status=1 and amount!=0 and session='".$_SESSION["admin"]["session"]."'");
    if ($_POST["pmode"] != "") {
        $aryList = $db->getRows("SELECT * FROM transaction where school_id='" . $_SESSION["admin"]["school_id"] . "' and type='deposit' and status=1 and amount!=0 and session='" . $_SESSION["admin"]["session"] . "' $whereUser and detail like '%" . $_POST["pmode"] . "%' and amount!=0 $between order by udate asc");


        //$q="SELECT * FROM transaction where school_id='".$_SESSION["admin"]["school_id"]."' and type='deposit' and status=1 and amount!=0 and session='".$_SESSION["admin"]["session"]."' and detail like '%".$_POST["pmode"]."%' and amount!=0 $between order by udate asc";


    }


    $total = "";
    foreach ($aryList as $val) {


        //number_format($clist["total_income"],2); $other+=$val["total_income"];

    }

    $i = 0;
//if(is_array($aryList) && count($aryList)>0  )
//{
    $tndate = date('Y/m/d', strtotime('-1 days', strtotime($_POST["tto"])));
    //echo $tndate." ".$fdate
    ?>
    <center><p>
        <h2>Profit & Loss Detail</h2></p></center>
    <center>
        <h5><p style="float:right"> Date:
                <?php if (strtotime($tndate) == strtotime($_POST["tfrom"])) {
                    echo date("d-M-Y", strtotime($tndate)); ?> - <?php echo date("l", strtotime($tndate));
                } else echo date("d-M-Y", strtotime($_POST["tfrom"])) . " to " . date("d-M-Y", strtotime($tndate)); ?></p>
        </h5>
    </center>
    <?php $ac_name = $db->getVal("select name from users where id='$Username'"); ?>
    <center>
        <h5><p style="float:left">Account Id-<?php if ($ac_name) {
                    echo $ac_name;
                } else {
                    echo 'Overall';
                } ?>
            </p></h5>
    </center>

    <div>
        <table>


        </table>


        <?php
        //}
        // new code---------------------


        $i = 0;
        //if(is_array($aryList_ex) && count($aryList_ex)>0){

        $tndate = date('Y/m/d', strtotime('-1 days', strtotime($_POST["tto"])));
        //echo $tndate." ".$fdate
        ?>


    </div>

    <div style="font-size: 16px;clear: both;background-color: #D5DA13;font-weight: bold;padding: 5px;color: #000;text-align: center;  border-top: 1px solid #000;border-bottom: 2px solid #FA0606;border-left: 1px solid #000;border-right: 1px solid #000;">
        Profit & Loss Detail
    </div>
    <table style="width:100%; font-size:13px;margin:0;border-top:1px solid #FA0606 ;border-bottom:1px solid #000;text-align: center;color: #1224A5;font-weight: 800;"
           cellpadding="5" cellspacing="0">

        <tr style="background-color: #FFEA15;font-weight: 800;padding: 0px;color: #000;">
            <td colspan="3"
                style="border-right:2px solid #FA0606;border-left:2px solid #FA0606;border-bottom: 2px solid #FA0606; text-align:center">
                Income
            </td>
            <td colspan="3" style="border-right:2px solid #FA0606;border-bottom: 2px solid #FA0606; text-align:center">
                Expense
            </td>
        </tr>
        <tr style="background-color: #FFEA15;font-weight: 800;padding: 0px;color: #000;">
            <td width="7%"
                style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;border-left:2px solid #FA0606;">
                Sr.No
            </td>
            <td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;">Income Detail</td>
            <td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;">Amount</td>
            <td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;">Sr.No</td>
            <td width="12%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;">Expense Detail</td>
            <td width="5%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;">Amount</td>

            <!--<td width="7%" style="border-bottom: 2px solid #6F6868;">Grade Point</td>-->
        </tr>

        <tr style="background-color: #FFEA15;font-weight: 800;padding: 0px;color: #000;">
            <td width="7%"
                style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;border-left:2px solid #FA0606;"
                valign="top"><?php echo '1';
                echo "<br>";
                echo '2';
                echo "<br>";
                echo '3';
                echo "<br>";
                echo '4';
                echo "<br>";
                echo '5';
                ?></td>
            <td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;" align="left"
                valign="top"><?php


                echo "Tuition Fee";
                echo "<br>";
                echo "Transport Fee";
                echo "<br>";

                echo "other Fee";
                echo "<br>";

                echo "Extra Fee";
                echo "<br>";
                echo 'Reception';
                ?></td>

            <td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;" align="right"
                valign="top"><?php
                //	echo "select sum(amount) from reff_receipt as r INNER join student as s on s.idno=r.idno where r.type='fee' and (r.date between '".$_POST["tfrom"]."' and '".$_POST["tto"]."') and s.status=1";
                $TUN = $db->getVal("select sum(amount) from reff_receipt as r INNER join student as s on s.idno=r.idno where r.type='fee' and (r.date between '" . $_GET["tfrom"] . "' and '" . $_GET["tto"] . "') and r.session='" . $_SESSION["admin"]["session"] . "' ");
                echo number_format($TUN, 2);
                echo "<br>";
                $BUS = $db->getVal("select sum(amount) from reff_receipt as r INNER join student as s on s.idno=r.idno where r.type='busfee' and (r.date between '" . $_GET["tfrom"] . "' and '" . $_GET["tto"] . "') and r.session='" . $_SESSION["admin"]["session"] . "' ");
                echo number_format($BUS, 2);
                echo "<br>";
                $other = $db->getVal("select sum(amount) from reff_receipt as r INNER join student as s on s.idno=r.idno where (r.type not like '%extrafee%') and type not in ('busfee','fee','hostelfee') and (r.date between '" . $_GET["tfrom"] . "' and '" . $_GET["tto"] . "') and r.session='" . $_SESSION["admin"]["session"] . "'");

                echo number_format($other, 2);
                echo "<br>";
                $extra = $db->getVal("select sum(amount) from reff_receipt as r INNER join student as s on s.idno=r.idno where (r.type like '%extrafee%') and (r.date between '" . $_GET["tfrom"] . "' and '" . $_GET["tto"] . "') and r.session='" . $_SESSION["admin"]["session"] . "' ");

                // echo $ss="SELECT sum(amount) as total_income FROM transaction where school_id='1' and type='deposit' and rid='0' and tid='0' and status=1 and amount!=0 $whereUser and session='".$_SESSION["admin"]["session"]."' $between ";

                echo number_format($extra, 2);

                echo "<br>";

                $tot = $db->getVal("select sum(amount) from transaction where type='deposit' and (udate between '" . $_GET["tfrom"] . "' and '" . $_GET["tto"] . "') and (tfor='cash' or tfor='cheque' or tfor='hostel-income') and session='" . $_SESSION["admin"]["session"] . "' ");
                echo number_format($tot, 2);
                ?></td>
            <td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;"><?php
                $is = 0;

                foreach ($aryList_ex as $clist) {
                    $is++;
                    $aryPgAct["id"] = $clist['id'];

                    echo $is . "<br>";
                }
                ?>
            </td>
            <td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;" align="left"><?php
                foreach ($aryList_ex as $clist) {
                    $i++;
                    $aryPgAct["id"] = $clist['id'];


                    if ($clist["tfor"] == "other2_fee") echo "Annual Fee</br>";

                    elseif ($clist["tfor"] == "other4_fee") echo "App Money";
                    elseif ($clist["tfor"] == "other6_fee") echo "Admission Fee";
                    elseif ($clist["tfor"] == "fee") echo "Tuition Fee";
                    elseif ($clist["tfor"] == "bus") echo "Transport Fee";

                    else echo $clist["tfor"] . " </br> ";


                }
                ?></td>
            <td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;" align="right"><?php
                foreach ($aryList_ex as $clist) {
                    $i++;
                    $aryPgAct["id"] = $clist['id'];

                    echo number_format(($clist["total_ex"]), 2) . "</br>";
                    $total1 += $clist["total_ex"];
                }

                ?>

            </td>
            <?php //}
            // 		$total=$TUN+$BUS+$other+$extra;
            $total = $TUN + $BUS + $other + $extra + $tot;
            ?>
        </tr>

        <tr style="background-color: #FFEA15;font-weight: 800;padding: 0px;color: #000;">
            <td width="7%" style="border-bottom: 1px solid #000;border-left:2px solid #FA0606;"></td>
            <td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;" align="left">Total</td>
            <td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;"
                align="right"><?php echo number_format($total, 2) ?></td>
            <td width="7%" style="border-bottom: 1px solid #000;"></td>
            <td width="12%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;" align="left">Total
            </td>
            <td width="5%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;"
                align="right"><?php echo number_format($total1, 2) ?></td>

            <!--<td width="7%" style="border-bottom: 2px solid #6F6868;">Grade Point</td>-->
        </tr>
        </div>
    </table>


    <center><span
                style="font-size: 21px !important;  color:#F00;">Net Profit: <?php echo number_format(($total - $total1), 2); ?></span>
    </center>
    </div>

    <?php

//	}

// new code end.....................	


//else{
    ?>
    <div class="alert alert-info">
        <a class="close" data-dismiss="alert">×</a>
        <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
    </div>
    <?php
//}
}
?>
</div>
<style>.top-bar-minimize {
        display: none;
    }

    thead, tfoot {
        display: table-header-group;
    }

    .top-bar + .well, .accordion .accordion-heading + .accordion-body {
        display: inline-block !important;
        width: 100% !important;
        overflow: inherit !important;
    }

    .dynamic {
        display: none
    }

    table td {
        padding: 6px !important;
    }

    legend {
        line-height: 16px;
        font-weight: 100;
    }


    .gmaps {
        margin: 0;
        padding: 0;
        height: 400px;
        border-radius: 0 0 5px 5px;
        overflow: hidden;
        box-sizing: content-box
    }

    .gmaps > div > div:nth-child(3), .gmaps > div > div:nth-child(2) {
        display: none
    }

    .gmaps img {
        max-width: none
    }

    .gmaps.map-small {
        height: 200px
    }

    table.dataTable td, table.dataTable th {
        padding: 12px 0;
        line-height: 24px
    }

    .table-bordered.table {
        background-color: #eee;
        border: 1px solid #c2c2c2;
        box-shadow: 0 1px 0 white
    }

    .table-bordered.table tbody > tr:nth-child(odd) > td, .table-bordered.table .table tbody > tr:nth-child(odd) > td, .table-bordered.table .data-table tbody > tr:nth-child(odd) > td, .table-bordered.table .table-striped tbody > tr:nth-child(odd) > th, .table-bordered.table .table tbody > tr:nth-child(odd) > th, .table-bordered.table .data-table tbody > tr:nth-child(odd) > th, .table-bordered.table tbody tr, .table-bordered.table tbody td, .table-bordered.table th, .table-bordered.table td {
        border: 0;
        box-shadow: none
    }

    .table-bordered.table tbody > tr:nth-child(odd) > td, .table-bordered.table .table tbody > tr:nth-child(odd) > td, .table-bordered.table .data-table tbody > tr:nth-child(odd) > td, .table-bordered.table .table-striped tbody > tr:nth-child(odd) > th, .table-bordered.table .table tbody > tr:nth-child(odd) > th, .table-bordered.table .data-table tbody > tr:nth-child(odd) > th, .table-bordered.table tbody tr, .table-bordered.table tbody td, .table-bordered.table th, .table-bordered.table td {
        border-bottom: 1px solid #c2c2c2;
        border-top: 1px solid white
    }

    .table-bordered.table tbody tr:last-child td {
        border-bottom: 0
    }

    table.dataTable {
        margin: 0 auto;
        clear: both;
        width: 100%
    }

    table.dataTable thead th {
        cursor: pointer;
        *cursor: hand
    }

    table.dataTable thead tr {
        background: #3B5999
    }

    .table thead {
        background: crimson;
        color: #fff;
    }

    table.dataTable tfoot th, table.dataTable thead th, table.dataTable thead td {
        font-weight: bold;
        background-image: -moz-linear-gradient(bottom, rgba(0, 0, 0, 0.05) 0, rgba(255, 255, 255, 0.05) 100%);
        background-image: -o-linear-gradient(bottom, rgba(0, 0, 0, 0.05) 0, rgba(255, 255, 255, 0.05) 100%);
        background-image: -webkit-linear-gradient(bottom, rgba(0, 0, 0, 0.05) 0, rgba(255, 255, 255, 0.05) 100%);
        background-image: linear-gradient(bottom, rgba(0, 0, 0, 0.05) 0, rgba(255, 255, 255, 0.05) 100%);
        color: #fff;
        border-bottom: 1px solid #c2c2c2;
        -webkit-box-shadow: 0 2px 0 #fff;
        -moz-box-shadow: 0 2px 0 #fff;
        box-shadow: 0 2px 0 #fff
    }

    table.dataTable tfoot th:first-child, table.dataTable thead th:first-child {
        padding-left: 20px
    }

    table.dataTable thead th {
        border-top: 1px solid #c2c2c2;
        -webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.7), 0 2px 0 white;
        -moz-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.7), 0 2px 0 white;
        box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.7), 0 2px 0 white
    }

    table.dataTable td.center, table.dataTable td.dataTables_empty {
        text-align: center
    }

    .dataTables_length select {
        max-width: 50px
    }

    .dataTables_wrapper {
        position: relative;
        clear: both;
        *zoom: 1
    }

    th.center {
        text-align: center
    }

    .dataTables_processing {
        position: absolute;
        top: 50%;
        left: 50%;
        width: 250px;
        height: 30px;
        margin-left: -125px;
        margin-top: -15px;
        padding: 14px 0 2px 0;
        border: 1px solid #ddd;
        text-align: center;
        color: #999;
        background-color: white
    }

    table.dataTable thead th:active, table.dataTable thead td:active {
        outline: 0
    }

    .dataTables_scroll {
        clear: both
    }

    .dataTables_scrollBody {
        *margin-top: -1px;
        -webkit-overflow-scrolling: touch
    }

    .gallery {
        padding: 24px
    }

    .gallery .element {
        display: inline-block;
        padding: 0 1.5% 3% 1.5%;
        position: relative;
        text-align: center;
        width: 16%
    }

    .gallery .element .img-polaroid {
        padding: 5%;
        border-radius: 5px;
        border: 0;
        background-color: #fcfcfc;
        -webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 1), 0 2px 4px rgba(0, 0, 0, 0.08), inset 0 -1px 0 rgba(0, 0, 0, 0.25);
        -moz-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 1), 0 2px 4px rgba(0, 0, 0, 0.08), inset 0 -1px 0 rgba(0, 0, 0, 0.25);
        box-shadow: inset 0 1px 0 rgba(255, 255, 255, 1), 0 2px 4px rgba(0, 0, 0, 0.08), inset 0 -1px 0 rgba(0, 0, 0, 0.25)
    }

    .gallery .element a .icon-eye-open, .gallery .element a .icon-eye-open:hover {
        position: absolute;
        z-index: 1006;
        color: white;
        text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.3);
        vertical-align: center;
        background-color: rgba(0, 0, 0, 0.7);
        border-radius: 200em;
        left: 50%;
        top: 44%;
        margin-left: -26px;
        margin-top: -25px;
        width: 52px;
        height: 50px;
        padding: .70em .5em;
        box-sizing: border-box
    }

    .img-polaroid {
        padding: 10px;
        box-sizing: border-box
    }

    .gallery img {
        zoom: 1;
        filter: alpha(opacity=75);
        opacity: .75
    }

    .gallery img:hover {
        filter: alpha(opacity=100);
        opacity: 1
    }

    .table, .data-table {
        margin-bottom: 0
    }

    .table-striped, .table, .data-table {
        color: blue;
        font-weight: bolder;
        width: 100%
    }

    .table-striped th, .table th, .data-table th, .table-striped td, .table td, .data-table td {
        padding: 8px;
        text-align: left;
        vertical-align: top
    }

    .table-striped th, .table th, .data-table th {
        font-weight: bold
    }

    .table-striped thead th, .table thead th, .data-table thead th {
        vertical-align: bottom
    }

    .table-striped thead th:first-child, .table thead th:first-child, .data-table thead th:first-child {
        padding-left: 20px
    }

    .table-striped thead th:last-child, .table thead th:last-child, .data-table thead th:last-child {
        padding-right: 20px
    }

    .table-striped tbody i, .table tbody i, .data-table tbody i {
        color: #cacaca
    }

    .table-striped tbody td:first-child, .table tbody td:first-child, .data-table tbody td:first-child {
        padding-left: 20px
    }

    .table-striped tbody td:last-child, .table tbody td:last-child, .data-table tbody td:last-child {
        padding-right: 20px
    }

    .table-striped tbody td, .table tbody td, .data-table tbody td, .table-striped tbody th, .table tbody th, .data-table tbody th {
        background-image: -moz-linear-gradient(bottom, rgba(0, 0, 0, 0.01) 0, rgba(255, 255, 255, 0.01) 100%);
        background-image: -o-linear-gradient(bottom, rgba(0, 0, 0, 0.01) 0, rgba(255, 255, 255, 0.01) 100%);
        background-image: -webkit-linear-gradient(bottom, rgba(0, 0, 0, 0.01) 0, rgba(255, 255, 255, 0.01) 100%);
        background-image: linear-gradient(bottom, rgba(0, 0, 0, 0.01) 0, rgba(255, 255, 255, 0.01) 100%);
        border-bottom: 1px solid #c2c2c2;
        -webkit-box-shadow: 0 2px 0 #fff;
        -moz-box-shadow: 0 2px 0 #fff;
        font-weight: 800;
        box-shadow: 0 2px 0 #fff
    }

    .st {
        font-weight: 800;
    }

    .controls .input-block-level {
        font-weight: 800;
    }

    .table-striped tbody .small, .table tbody .small, .data-table tbody .small {
        text-align: right;
        color: #c3c3c3
    }

    .table-striped tbody > tr:nth-child(odd) > td, .table tbody > tr:nth-child(odd) > td, .data-table tbody > tr:nth-child(odd) > td, .table-striped tbody > tr:nth-child(odd) > th, .table tbody > tr:nth-child(odd) > th, .data-table tbody > tr:nth-child(odd) > th {
        background-color: #f9f9f9;
        background-image: -moz-linear-gradient(bottom, rgba(0, 0, 0, 0.03) 0, rgba(255, 255, 255, 0.03) 100%);
        background-image: -o-linear-gradient(bottom, rgba(0, 0, 0, 0.03) 0, rgba(255, 255, 255, 0.03) 100%);
        background-image: -webkit-linear-gradient(bottom, rgba(0, 0, 0, 0.03) 0, rgba(255, 255, 255, 0.03) 100%);
        background-image: linear-gradient(bottom, rgba(0, 0, 0, 0.03) 0, rgba(255, 255, 255, 0.03) 100%);
        border-bottom: 1px solid #c2c2c2
    }

    table .btn {
        padding: 0 6px;
        margin-right: 0;
        margin-left: 2px !important;
    }

    table .right .btn {
        float: right
    }

    table.dataTable thead th.right, table.dataTable tfoot th.right {
        text-align: right;
        padding-right: 20px
    }
</style>