<?php 
$pgMod="total_outstanding";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);


if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{ 
	?>
	<div class="alert alert-success">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-thumbs-up-alt"></i> <b>Notice</b><?php echo $_SESSION['msg'] ?></i>
	</div>
	<?php
	unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
	?>
	<div class="alert alert-error">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-warning-sign"></i> <b>Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}

$allmonths = $db->getRows("select * from months");
?>

    <form method="get" action="">
        <input type="hidden" name="src" value="Account" />
        <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
        <div class="row-fluid">
            <div class="control-group">
                <div class="span3">
                    <select name="month">
                        <option value="">--Select Month--</option>
                        <?php
                                foreach($allmonths as $mn){
                                    $checked = "";
                                	    if($_GET["month"]=== trim($mn["month"]) ){
            						        $checked="selected='selected'";
                                	    }
                            	    echo "<option value='".$mn['month']."' $checked>$mn[name]</option>";
                        	    } 
                    	    ?>
                        <!--<option value="1" <?php if($_GET["month"]=="1"){echo "selected";} ?>>April</option>-->
                        <!--<option value="2" <?php if($_GET["month"]=="2"){echo "selected";} ?>>May</option>-->
                        <!--<option value="3" <?php if($_GET["month"]=="3"){echo "selected";} ?>>June</option>-->
                        <!--<option value="4" <?php if($_GET["month"]=="4"){echo "selected";} ?>>July</option>-->
                        <!--<option value="5" <?php if($_GET["month"]=="5"){echo "selected";} ?>>August</option>-->
                        <!--<option value="6" <?php if($_GET["month"]=="6"){echo "selected";} ?>>September</option>-->
                        <!--<option value="7" <?php if($_GET["month"]=="7"){echo "selected";} ?>>October</option>-->
                        <!--<option value="8" <?php if($_GET["month"]=="8"){echo "selected";} ?>>November</option>-->
                        <!--<option value="9" <?php if($_GET["month"]=="9"){echo "selected";} ?>>December</option>-->
                        <!--<option value="10" <?php if($_GET["month"]=="10"){echo "selected";} ?>>January</option>-->
                        <!--<option value="11" <?php if($_GET["month"]=="11"){echo "selected";} ?>>February</option>-->
                        <!--<option value="12" <?php if($_GET["month"]=="12"){echo "selected";} ?>>March</option>-->
                    </select>
                </div>
                <div class="span3">
                    <button type="submit" name="school_fee" class="btn-classic margin"> Single Month</button>
                </div>
                <div class="span3">
                    <button type="submit" name="complete_fee" class="btn-classic margin"> Total Outstanding</button>
                </div>
            </div>
        </div>
    </form>

<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
    	<div style="float:right; margin-top:5px">
            <a onclick="printDiv('printableDiv')" class="btn btn-primary" target="_blank">Print</a>
        </div>
    </div>
<div class="well no-padding" id="printableDiv">
<?php
$aryPgAct=array("src"=>"Account", "page_id"=>$pgMod,"action"=>"add");
$between="";
if(isset($_GET["school_fee"])){
    if(isset($_GET["month"]) && isset($_GET["month"])!=""){

    $session = explode("-", $_SESSION["admin"]["session"]);

        $icheck = $_GET["month"];
        
        if($icheck == 1){
			$year = $session[0];
            $newDate = $session[0]."-1-1";
			$d=cal_days_in_month(CAL_GREGORIAN,($icheck+3),$year);
			$todate = $year."-".($icheck+3)."-$d";
        }
        else {
            if($icheck<=9){
    			$year = $session[0];
    			$newDate = $year."-".($icheck+3)."-1";
    			$d=cal_days_in_month(CAL_GREGORIAN,($icheck+3),$year);
    			$todate = $year."-".($icheck+3)."-$d";
            }
            else{
                $year = $session[1];
                $newDate = $year."-".($icheck-9)."-1";
                $d=cal_days_in_month(CAL_GREGORIAN,($icheck-9),$year);
                $todate = $year."-".($icheck-9)."-$d";
            }
        }
    $todate1 = date('Y-m-d', strtotime($todate . ' +1 day'));
    $between=" and date between '$newDate' and '$todate1' ";

    $today_date = date('Y-m-d');
    $betweenOther = " and date between '$newDate' and '$today_date' ";

    if(isset($_GET['user1'])){
        $u=implode(',',$_GET["user1"]);
        $whereUser=" and added_by in ($u) ";
        $Username=$u;
    }

    $totaloutstanding = 0;
    $total="";

     $i=0;

    	$tndate = date('Y/m/d', strtotime('-1 days',strtotime($_POST["tto"])));
    $d=cal_days_in_month(CAL_GREGORIAN,2,2004);
    			?>
		<center><p><h2>Profit & Loss Detail</h2></p></center>
        <center><h5><p style="float:right"> Date: 
        <?php echo date("d-M-Y", strtotime($newDate)); ?> - <?php echo date("d-M-Y", strtotime($todate)); ?>
        
        <?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>
    	<div>
    
    	<?php
    // new code---------------------
    	$i=0;
    		$tndate = date('Y/m/d', strtotime('-1 days',strtotime($_POST["tto"])));
    	?>
    	</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="4" style="border-right:2px solid #FA0606;border-left:2px solid #FA0606;border-bottom: 2px solid #FA0606; text-align:center">Total Outstanding</td>
    						<td colspan="3" style="border-right:2px solid #FA0606;border-bottom: 2px solid #FA0606; text-align:center">Total Paid</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;">Outstanding 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;">Dis</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;">Paid Detail</td>
    						<td width="5%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;">Amount</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';
        				 echo "<br>";
        				 echo '6';
    				 ?></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>";

                    // $extrafeetypeary = array();				
                    $extrafee_type = $db->getRows("select * from extrafee_type where session='".$_SESSION["admin"]["session"]."' and status='1' group by type ");
                        foreach($extrafee_type as $extfee){
                            echo "$extfee[type],";
                            // $extrafeetypeary[]=$extfee['type'];
                        }
                    echo "<br>";    
    				 echo "Hostel Fee";
    				 echo "<br>";
    
    				 echo 'Hostel Other Fee';
    				?></td>
    				
    			<td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;"  align="right" valign="top"><?php 
        			$totalother = 0;
                    // for($i = 1; $i<=$_GET['month']; $i++){
    
                        $i = $_GET["month"];

            /* Tuition fee Starts */
    			$monthlytuitionfee = 0; $tuitiondis = 0;
    			$allclass = $db->getRows("select * from class");
    			foreach($allclass as $cls){
                    $fee = $cls["fee"]/$cls["installment"];
                    $stu = $db->getVal("select count(*) from student where class='$cls[class]' and session='".$_SESSION["admin"]["session"]."' and status='1' and bpl!='1' ");

        			$monthlytuitionfee+=$fee*$stu;
    			}

                // $tuitiondis = $db->getVal("select sum(discount_amount) from discount_detail where installment='i$i' and type='tution' and session='$session[0]' ");

                // $tuition = $monthlytuitionfee-$tuitiondis;
                $tuition = $monthlytuitionfee;
            /* Tuition fee end */

            /* Bus fee Starts */
                $monthlybusfee = 0; $busdis = 0;
                $allbus = $db->getRows("select * from bus"); 
                foreach($allbus as $bus){
                    $fee = $bus["fee"]/$bus["installment"];
                    $stu = $db->getVal("select count(*) from student where bus='$bus[id]' and session='".$_SESSION["admin"]["session"]."' and status='1' and bpl!='1' ");

        			$monthlybusfee+=$fee*$stu;
                }
                
                // $busdis = $db->getVal("select sum(discount_amount) from discount_detail where installment='b$i' and type='bus' and session='$session[0]' "); 

                // $bus = $monthlybusfee-$busdis; 
                $bus = $monthlybusfee;
            /* Bus fee end */

        /* Hostel fee Starts */
            $monthlyhostelfee = 0; $hosteldis = 0;
            $allhostel = $db->getRows("select * from hostel");
            foreach($allhostel as $hostel){
                $fee = $hostel["fee"]/$hostel["installment"];
                   $stu = $db->getVal("select count(s.idno) from hostelinstallment as h,student as s where s.school_id='".$_SESSION["admin"]["school_id"]."' and s.session='".$_SESSION["admin"]["session"]."' and s.idno=h.idno and  s.issue_TC=0 and s.status=1 and h.hid='$hostel[id]' ");
    			$monthlyhostelfee+=$fee*$stu;
            }

            // $hosteldis = $db->getVal("select sum(discount_amount) from discount_detail where installment='i$i' and type='hostel' and session='$session[0]' ");

            // $hostel = ($monthlyhostelfee*$_GET['month'])-$hosteldis;
            $hostel = $monthlyhostelfee;
        /* Hostel fee end */

        /* Extra fee Starts */
            $monthlyextrafee = 0; $extradis = 0;
            $extratype = $db->getRows("select * from extrafee_type where session='".$_SESSION["admin"]["session"]."' ");
            foreach($extratype as $extra){
                $fee = $extra["p1"];
                $stu = $db->getVal("select count(*) from student where class='$extra[class]' and session='".$_SESSION["admin"]["session"]."' and status='1' and bpl!='1' ");
                $monthlyextrafee+=$fee*$stu;
            }

            // $extradis+= $db->getVal("select sum(discount_amount) from discount_detail where installment='i$i' and type='extra' and (session='$session[0]' or session='$session[1]' ) ");

            // $totalextra = $monthlyextrafee-$extradis;
            $totalextra = $monthlyextrafee;
        /* Extra fee end */ 

        /* Other Fee Starts */
            $otherfee = $db->getRows("select * from otherfee_structure where session='".$_SESSION["admin"]["session"]."' and month='$i' ");

            foreach($otherfee as $fee){
                if($fee["otype"] == "new"){
                    $newstu = $db->getVal("select count(idno) from student where session='".$_SESSION["admin"]["session"]."' and class='$fee[class]' and stype='new' and status='1' ");
                    $total = $newstu*$fee['fee'];
                } elseif($fee["otype"] == "old"){
                    $oldstu = $db->getVal("select count(idno) from student where session='".$_SESSION["admin"]["session"]."' and class='$fee[class]' and stype='old' and status='1'  ");
                    $total = $oldstu*$fee['fee'];
                } elseif($fee["otype"] != ""){
                    $total = $fee['fee'];
                } else {
                    $stu = $db->getVal("select count(idno) from student where session='".$_SESSION["admin"]["session"]."' and class='$fee[class]' and status='1'  ");
                    $total = $stu*$fee['fee'];
                }
                $totalother+=$total;
            }

            // $otherdiscount = $db->getVal("select sum(discount) from otherfee_discount where month='$i' and session='".$_SESSION["admin"]["session"]."' ");
        /* Other Fee Ends */
    
        /* Hostel Other Fee starts */
    		$hosteltotalother = 0; $total = 0;

            $hostelotherfee = $db->getRows("select * from hostel_otherfee_structure where session='".$_SESSION["admin"]["session"]."' and month='$i' ");
                foreach($hostelotherfee as $fee){

                    if($fee["otype"] == "new"){
                        $newstu = $db->getVal("select count(s.idno) from hostelinstallment as h,student as s where s.school_id='".$_SESSION["admin"]["school_id"]."' and s.session='".$_SESSION["admin"]["session"]."' and s.idno=h.idno and  s.issue_TC=0 and s.status=1 and s.htype='new' and s.class='$fee[class]' ");
                        $total = $newstu*$fee['fee'];
                    } elseif($fee["otype"] == "old"){
                        $oldstu = $db->getVal("select count(s.idno) from hostelinstallment as h,student as s where s.school_id='".$_SESSION["admin"]["school_id"]."' and s.session='".$_SESSION["admin"]["session"]."' and s.idno=h.idno and  s.issue_TC=0 and s.status=1 and s.htype='old' and s.class='$fee[class]' ");
                        $total = $oldstu*$fee['fee'];
                    } elseif($fee["otype"] != ""){
                        $total = $fee['fee'];
                    } else {
                        $stu = $db->getVal("select count(s.idno) from hostelinstallment as h,student as s where s.school_id='".$_SESSION["admin"]["school_id"]."' and s.session='".$_SESSION["admin"]["session"]."' and s.idno=h.idno and  s.issue_TC=0 and s.status=1 and s.class='$fee[class]' ");
                        $total = $stu*$fee['fee'];
                    }
                    $hosteltotalother = $hosteltotalother+$total;
                }

            // $hostelotherdiscount = $db->getVal("select sum(discount) from hostelotherfee_discount where month='$i' and session='".$_SESSION["admin"]["session"]."' ");

        /* Hostel Other Fee end */
    
                    echo $totaltuition = number_format($tuition,2);
                    echo "<br>";
    
                    echo $totalbus = number_format($bus,2);
                    echo "<br>";
    
                    echo $totalotherfee = number_format($totalother,2);
                    echo "<br>";
    
                    echo $totalextrafee = number_format($totalextra,2);
                    echo "<br>";

                    echo number_format($hostel,2);
                    echo "<br>";

                    echo number_format($hosteltotalother,2);
                    echo "<br>";

                    // $totaloutstanding = $tuition+$bus+($totalother-$otherdiscount)+$totalextra;

            $totaloutstanding = $tuition+$bus+$totalother+$totalextra+$hostel+$hosteltotalother;    

    				?></td>
    				
                <td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;text-align:right;">
                    <?php 
        			$totalother = 0;
                    // for($i = 1; $i<=$_GET['month']; $i++){
    
                        $i = $_GET["month"];

        /* Tuition fee Starts */
            $tuitiondis = $db->getVal("select sum(discount_amount) from discount_detail where installment='i$i' and type='tution' and session='$session[0]' ");
        /* Tuition fee end */

        /* Bus fee Starts */
            $busdis = $db->getVal("select sum(discount_amount) from discount_detail where installment='b$i' and type='bus' and session='$session[0]' "); 
        /* Bus fee end */

        /* Hostel fee Starts */
            $hosteldis = $db->getVal("select sum(discount_amount) from discount_detail where installment='i$i' and type='hostel' and session='$session[0]' ");
        /* Hostel fee end */

        /* Extra fee Starts */
            $extradis+= $db->getVal("select sum(discount_amount) from discount_detail where installment='i$i' and type='extra' and (session='$session[0]' or session='$session[1]' ) ");
        /* Extra fee end */ 

        /* Other Fee Starts */
            $otherdiscount = $db->getVal("select sum(discount) from otherfee_discount where month='$i' and session='".$_SESSION["admin"]["session"]."' ");
        /* Other Fee Ends */
    
        /* Hostel Other Fee starts */
            $hostelotherdiscount = $db->getVal("select sum(discount) from hostelotherfee_discount where month='$i' and session='".$_SESSION["admin"]["session"]."' ");
        /* Hostel Other Fee end */
    
                    echo number_format($tuitiondis,2);
                    echo "<br>";
    
                    echo number_format($busdis,2);
                    echo "<br>";
    
                    echo number_format($otherdiscount,2);
                    echo "<br>";
    
                    echo number_format($extradis,2);
                    echo "<br>";

                    echo number_format($hosteldis,2);
                    echo "<br>";

                    echo number_format($hostelotherdiscount,2);
                    echo "<br>";

                    // $totaloutstanding = $tuition+$bus+($totalother-$otherdiscount)+$totalextra;

            $totaldiscount = $tuitiondis+$busdis+$otherdiscount+$extradis+$hosteldis+$hostelotherdiscount;    
?>
                </td>    				

                <td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;"><?php
                         echo '1';
        				 echo "<br>";
        				 echo '2';
        				 echo "<br>";
        				 echo '3';
        				 echo "<br>";
        				 echo '4';
        				 echo "<br>";
        				 echo '5';
        				 echo "<br>";
        				 echo '6';
        				 echo "<br>";

    		?>
    		</td>
			<td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;" align="left" >
			    <?php
    				 echo "Tuition Fee";
    				 echo "<br>";

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

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

                    $extrafee_type = $db->getRows("select * from extrafee_type where session='".$_SESSION["admin"]["session"]."' and status='1' group by type ");
                        foreach($extrafee_type as $extfee){
                            echo "$extfee[type],";
                        }
                    
                    echo "<br>";
                     echo "Hostel Fee";

                    echo "<br>";
                     echo "Hostel Other Fee";                        
			    ?>
			</td>
			<td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;"  align="right"><?php 
        			$totalother = 0;
                    // for($i = 1; $i<=$_GET['month']; $i++){
    
                        $i = $_GET["month"];

                        $notother = "";
                        $allotherfee = $db->getRows("select * from hostel_otherfee_structure where session='".$_SESSION["admin"]["session"]."' and month='$i' group by name ");
                        foreach($allotherfee as $ot){
                            $notother.= " and type!='$ot[name]' ";
                        }

                        $nothostelother = "";

                        // $tuitionpaid = $db->getVal("select sum(amount) from reff_receipt where type='fee' and session='".$_SESSION["admin"]["session"]."' $between ");
                        $tuitionpaid = $db->getVal("select sum(amount) from reff_receipt where type='fee' and session='".$_SESSION["admin"]["session"]."' $between ");

                        $buspaid = $db->getVal("select sum(amount) from reff_receipt where type='busfee' and session='".$_SESSION["admin"]["session"]."' $between ");

                        // $otherpaid = $db->getVal("select sum(amount) from reff_receipt where (type!='fee' and type!='bus' $notother and type NOT LIKE '%extrafee%') and session='".$_SESSION["admin"]["session"]."' $between ");
                        $otherpaid = $db->getVal("select sum(amount) from reff_receipt where (type not like '%extrafee%') and type not in ('busfee','fee','hostelfee') $notother and session='".$_SESSION["admin"]["session"]."' $between ");

                        $extrapaid = $db->getVal("select sum(amount) from reff_receipt where (type LIKE '%extrafee%') and session='".$_SESSION["admin"]["session"]."' $between ");
                    
                        $hostelpaid = $db->getVal("select sum(amount) from reff_receipt where type='hostelfee' and session='".$_SESSION["admin"]["session"]."' $between ");
                    
                        $hostelotherpaid = 0;
                        $hostelotherfee = $db->getRows("select * from hostel_otherfee_structure where session='".$_SESSION["admin"]["session"]."' and month='$i' group by name ");
                        foreach($hostelotherfee as $hostelfee){
                            $hostelotherpaid+= $db->getVal("select sum(amount) from reff_receipt where type='$hostelfee[name]' and (type!='fee' and type!='bus' and type NOT LIKE '%extrafee%') and session='".$_SESSION["admin"]["session"]."' $between ");
                        }

                    // }

                    echo $totaltuition = number_format($tuitionpaid,2);
                    echo "<br>";
    
                    echo $totalbus = number_format($buspaid,2);
                    echo "<br>";
    
                    echo $totalotherfee = number_format($otherpaid,2);
                    echo "<br>";

                    echo $totalextrafee = number_format($extrapaid,2);
                    echo "<br>";

                    echo number_format($hostelpaid,2);
                    echo "<br>";

                    echo number_format($hostelotherpaid,2);
                    echo "<br>";

                    // $totalpaid = $tuitionpaid+$buspaid+$otherpaid+$extrapaid+$hostelpaid;
                    $totalpaid = $tuitionpaid+$buspaid+$otherpaid+$extrapaid+$hostelpaid+$hostelotherpaid;

    		?>
    		
    		</td>
    
    	</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($totaloutstanding,2) ?></td>
				<td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;" align="right"><?php echo number_format($totaldiscount,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($totalpaid,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;">Remaining: <?php echo number_format(($totaloutstanding-$totalpaid-$totaldiscount),2); ?></span></center>
    </div>
    	
    	<?php

    } else {
        echo "Please Select Month.";
    }
}




if(isset($_GET["complete_fee"])){
    if(isset($_GET["month"]) && isset($_GET["month"])!=""){

    $session = explode("-", $_SESSION["admin"]["session"]);
    $newDate = $session[0]."-1-1";
        $icheck = $_GET["month"];

        if($icheck<=9){
			$year = $session[0];
            // $newDate = $year."-".($icheck+2)."-1";
			$d=cal_days_in_month(CAL_GREGORIAN,($icheck+3),$year);
			$todate = $year."-".($icheck+3)."-$d"; 
        } 
        else{
            $year = $session[1];
            // $newDate = $year."-".($icheck-10)."-1";
            $d=cal_days_in_month(CAL_GREGORIAN,($icheck-9),$year);
            $todate = $year."-".($icheck-9)."-$d";
        }



        $todate1 = date('Y-m-d', strtotime($todate . ' +1 day'));
        $between=" and date between '$newDate' and '$todate1' ";
    
        if(isset($_GET['user1'])){
            $u=implode(',',$_GET["user1"]);
            $whereUser=" and added_by in ($u) ";
            $Username=$u;
        }

    $totaloutstanding = 0;
    $total="";

     $i=0;

    	$tndate = date('Y/m/d', strtotime('-1 days',strtotime($_POST["tto"])));
    $d=cal_days_in_month(CAL_GREGORIAN,2,2004);
    			?>
		<center><p><h2>Profit & Loss Detail</h2></p></center>
        <center><h5><p style="float:right"> Date: 
        <?php echo date("d-M-Y", strtotime($newDate)); ?> - <?php echo date("d-M-Y", strtotime($todate)); ?>
        
        <?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>
    	<div>
    
    	<?php
    // new code---------------------
    	$i=0;
    		$tndate = date('Y/m/d', strtotime('-1 days',strtotime($_POST["tto"])));
    	?>
    	</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="4" style="border-right:2px solid #FA0606;border-left:2px solid #FA0606;border-bottom: 2px solid #FA0606; text-align:center">Total Outstanding</td>
    						<td colspan="3" style="border-right:2px solid #FA0606;border-bottom: 2px solid #FA0606; text-align:center">Total Paid</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;">Outstanding 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;">Dis</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;">Paid Detail</td>
    						<td width="5%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;">Amount</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>";

		    // $extrafee_type = $db->getRows("select * from extrafee_type where session='".$_SESSION["admin"]["session"]."' and status='1' group by type "); $ei = 3;
            //                 foreach($extrafee_type as $extfee){ $ei++;
            //                     echo $ei."<br>";
            //                 }

    				 echo '4';
    				 echo "<br>";
    				 echo '5';
     				 echo "<br>";
    				 echo '6';
    				 ?></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>";

                    // $extrafeetypeary = array();				
                    $extrafee_type = $db->getRows("select * from extrafee_type where session='".$_SESSION["admin"]["session"]."' and status='1' group by type ");
                        foreach($extrafee_type as $extfee){
                            echo "$extfee[type],";
                            // $extrafeetypeary[]=$extfee['type'];
                        }
                    echo "<br>";
    				
    				 echo "Hostel Fee";
    				 echo "<br>"; 

    				 echo 'Hostel Other Fee';
    				?></td>

    			<td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;"  align="right" valign="top"><?php
            /* Tuition fee Starts */
    			$monthlytuitionfee = 0; $tuitiondis = 0;
    			$allclass = $db->getRows("select * from class");
    			foreach($allclass as $cls){
                    $fee = $cls["fee"]/$cls["installment"];
                    $stu = $db->getVal("select count(*) from student where class='$cls[class]' and session='".$_SESSION["admin"]["session"]."' and status='1' and bpl!='1' ");

        			$monthlytuitionfee+=$fee*$stu;
    			}

                $tuition = ($monthlytuitionfee*$_GET['month']);
            /* Tuition fee end */
            
            /* Bus fee Starts */
                
                $monthlybusfee = 0; $busdis = 0;
                $allbus = $db->getRows("select * from bus"); 
                foreach($allbus as $bus){
                    $fee = $bus["fee"]/$bus["installment"];
                    $stu = $db->getVal("select count(*) from student where bus='$bus[id]' and session='".$_SESSION["admin"]["session"]."' and status='1' and bpl!='1' ");

        			$monthlybusfee+=$fee*$stu;
                }
                
                $bus = ($monthlybusfee*$_GET['month']);
            /* Bus fee end */
            
        /* Hostel fee Starts */
            $monthlyhostelfee = 0; $hosteldis = 0;
            $allhostel = $db->getRows("select * from hostel");
            foreach($allhostel as $hostel){
                $fee = $hostel["fee"]/$hostel["installment"];
                // $stu = $db->getVal("select count(*) from student where hostel='$hostel[id]' and session='".$_SESSION["admin"]["session"]."' and status='1' and bpl!='1' ");
                   $stu = $db->getVal("select count(s.idno) from hostelinstallment as h,student as s where s.school_id='".$_SESSION["admin"]["school_id"]."' and s.session='".$_SESSION["admin"]["session"]."' and s.idno=h.idno and  s.issue_TC=0 and s.status=1 and h.hid='$hostel[id]' ");
    			$monthlyhostelfee+=$fee*$stu;
            }
            $hostel = ($monthlyhostelfee*$_GET['month']);
        /* Hostel fee end */


            /* Extra fee Starts */
                $monthlyextrafee = 0; $extradis = 0;
                $extratype = $db->getRows("select * from extrafee_type where session='".$_SESSION["admin"]["session"]."' ");
                foreach($extratype as $extra){
                    $fee = $extra["p1"];
                    $stu = $db->getVal("select count(*) from student where class='$extra[class]' and session='".$_SESSION["admin"]["session"]."' and status='1' and bpl!='1' ");
                    $monthlyextrafee+=$fee*$stu;
                }

                $totalextra = ($monthlyextrafee*$_GET["month"]);
            /* Extra fee end */ 
            
        /* Other Fee starts */

		$totalother = 0;
        for($i = 1; $i<=$_GET['month']; $i++){
    
            $otherfee = $db->getRows("select * from otherfee_structure where session='".$_SESSION["admin"]["session"]."' and month='$i' ");
                foreach($otherfee as $fee){
                    if($fee["otype"] == "new"){
                        $newstu = $db->getVal("select count(idno) from student where session='".$_SESSION["admin"]["session"]."' and class='$fee[class]' and stype='new' and status='1' ");
                        $total = $newstu*$fee['fee'];
                    } elseif($fee["otype"] == "old"){
                        $oldstu = $db->getVal("select count(idno) from student where session='".$_SESSION["admin"]["session"]."' and class='$fee[class]' and stype='old' and status='1'  ");
                        $total = $oldstu*$fee['fee'];
                    } elseif($fee["otype"] != ""){
                        $total = $fee['fee'];
                    } else {
                        $stu = $db->getVal("select count(idno) from student where session='".$_SESSION["admin"]["session"]."' and class='$fee[class]' and status='1'  ");
                        $total = $stu*$fee['fee'];
                    }
                    $totalother+=$total;
                }

        }
        
        /* Other Fee end */

        /* Hostel Other Fee starts */
		$hosteltotalother = 0; $total = 0;
        for($i = 1; $i<=$_GET['month']; $i++){

            $hostelotherfee = $db->getRows("select * from hostel_otherfee_structure where session='".$_SESSION["admin"]["session"]."' and month='$i' ");
                foreach($hostelotherfee as $fee){
                    // echo $fee['fee'];

                    if($fee["otype"] == "new"){
                        $newstu = $db->getVal("select count(s.idno) from hostelinstallment as h,student as s where s.school_id='".$_SESSION["admin"]["school_id"]."' and s.session='".$_SESSION["admin"]["session"]."' and s.idno=h.idno and  s.issue_TC=0 and s.status=1 and s.htype='new' and s.class='$fee[class]' ");
                        // $newstu = $db->getVal("select count(idno) from student where session='".$_SESSION["admin"]["session"]."' and class='$fee[class]' and htype='new' and status='1' ");
                        $total = $newstu*$fee['fee'];
                    } elseif($fee["otype"] == "old"){
                        // $oldstu = $db->getVal("select count(idno) from student where session='".$_SESSION["admin"]["session"]."' and class='$fee[class]' and htype='old' and status='1'  ");
                        $oldstu = $db->getVal("select count(s.idno) from hostelinstallment as h,student as s where s.school_id='".$_SESSION["admin"]["school_id"]."' and s.session='".$_SESSION["admin"]["session"]."' and s.idno=h.idno and  s.issue_TC=0 and s.status=1 and s.htype='old' and s.class='$fee[class]' ");
                        $total = $oldstu*$fee['fee'];
                    } elseif($fee["otype"] != ""){
                        $total = $fee['fee'];
                    } else {
                        // $stu = $db->getVal("select count(idno) from student where session='".$_SESSION["admin"]["session"]."' and class='$fee[class]' and status='1'  ");
                        $stu = $db->getVal("select count(s.idno) from hostelinstallment as h,student as s where s.school_id='".$_SESSION["admin"]["school_id"]."' and s.session='".$_SESSION["admin"]["session"]."' and s.idno=h.idno and  s.issue_TC=0 and s.status=1 and s.class='$fee[class]' ");
                        $total = $stu*$fee['fee'];
                    }
                    $hosteltotalother = $hosteltotalother+$total;
                }

        }
        /* Hostel Other Fee end */

                    echo $totaltuition = number_format($tuition,2);
                    echo "<br>";

                    echo $totalbus = number_format($bus,2);
                    echo "<br>";

                    echo $totalotherfee = number_format(($totalother),2);
                    echo "<br>";

                    echo $totalextrafee = number_format($totalextra,2);
                    echo "<br>";
                    
                    echo number_format($hostel,2);
                    echo "<br>";

                    echo number_format($hosteltotalother,2);
                    echo "<br>";
    
                    // $totaloutstanding = $tuition+$bus+($totalother-$otherdiscount)+($totalextra);
                $totaloutstanding = $tuition+$bus+$totalother+$totalextra+$hostel+$hosteltotalother;

    				?></td>

    			<td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;"  align="right" valign="top"><?php
            /* Tuition fee Starts */
    			$tuitiondis = 0;
                for($i = 1; $i<=$_GET['month']; $i++){
                    $tuitiondis += $db->getVal("select sum(discount_amount) from discount_detail where installment='i$i' and type='tution' and session='$session[0]' "); 
                }
            /* Tuition fee end */
            
            /* Bus fee Starts */
                $busdis = 0;
                for($i = 1; $i<=$_GET['month']; $i++){
                    $busdis += $db->getVal("select sum(discount_amount) from discount_detail where installment='b$i' and type='bus' and session='$session[0]' "); 
                }
            /* Bus fee end */
            
            /* Hostel fee Starts */
                $hosteldis = 0;
                for($i = 1; $i<=$_GET['month']; $i++){
                    $hosteldis += $db->getVal("select sum(discount_amount) from discount_detail where installment='i$i' and type='hostel' and session='$session[0]' ");
                }
            /* Hostel fee end */
    

            /* Extra fee Starts */
                $extradis = 0;
                for($i = 1; $i<=$_GET['month']; $i++){
                    $extradis+= $db->getVal("select sum(discount_amount) from discount_detail where installment='i$i' and type='extra' and session='$session[0]' ");
                }
            /* Extra fee end */ 
            
            /* Other Fee starts */
            $otherdiscount = 0;
                for($i = 1; $i<=$_GET['month']; $i++){
                    $otherdiscount += $db->getVal("select sum(discount) from otherfee_discount where month='$i' and session='".$_SESSION["admin"]["session"]."' ");
                }
            /* Other Fee end */

            /* Hostel Other Fee starts */
            $hostelotherdiscount = 0;
                for($i = 1; $i<=$_GET['month']; $i++){
                    $hostelotherdiscount += $db->getVal("select sum(discount) from hostelotherfee_discount where month='$i' and session='".$_SESSION["admin"]["session"]."' ");
                }
            /* Hostel Other Fee end */

                    echo number_format($tuitiondis,2);
                    echo "<br>";

                    echo number_format($busdis,2);
                    echo "<br>";

                    echo number_format($otherdiscount,2);
                    echo "<br>";

                    echo number_format($extradis,2);
                    echo "<br>";
                    
                    echo number_format($hosteldis,2);
                    echo "<br>";

                    echo number_format($hostelotherdiscount,2);
                    echo "<br>";
    
                    // $totaloutstanding = $tuition+$bus+($totalother-$otherdiscount)+($totalextra);
                $totaldiscount = $tuitiondis+$busdis+$otherdiscount+$extradis+$hosteldis+$hostelotherdiscount;

    				?></td>    				
    				
                <td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;"><?php
                         echo '1';
        				 echo "<br>";
        				 echo '2';
        				 echo "<br>";
        				 echo '3';
        				 echo "<br>";
        				 echo '4';
        				 echo "<br>";
        				 echo '5';
        				 echo "<br>";
        				 echo '6';
        				 echo "<br>";

    		?>
    		</td>
			<td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;" align="left" >
			    <?php
    				 echo "Tuition Fee";
    				 echo "<br>";

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

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

                    $extrafee_type = $db->getRows("select * from extrafee_type where session='".$_SESSION["admin"]["session"]."' and status='1' group by type ");
                        foreach($extrafee_type as $extfee){
                            echo "$extfee[type],";
                        }
                    
                    echo "<br>";
                    
                    echo "Hostel Fee";
                    echo "<br>";

                    echo "Hostel Other Fee";
                    echo "<br>";

			    ?>
			</td>
			<td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;"  align="right"><?php 
        			$totalother = 0;
                    // for($i = 1; $i<=$_GET['month']; $i++){
    
                        $i = $_GET["month"];

                        $notother = "";
                        $allotherfee = $db->getRows("select * from hostel_otherfee_structure where session='".$_SESSION["admin"]["session"]."' and month='$i' group by name ");
                        foreach($allotherfee as $ot){
                            $notother.= " and type!='$ot[name]' ";
                        }

                        $tuitionpaid = $db->getVal("select sum(amount) from reff_receipt where type='fee' and session='".$_SESSION["admin"]["session"]."' $between ");

                        $buspaid = $db->getVal("select sum(amount) from reff_receipt where type='busfee' and session='".$_SESSION["admin"]["session"]."' $between ");

                        $otherpaid = $db->getVal("select sum(amount) from reff_receipt where  type not in ('busfee','fee','hostelfee') and type NOT LIKE '%extrafee%' $notother and session='".$_SESSION["admin"]["session"]."' $between ");

                        $extrapaid = $db->getVal("select sum(amount) from reff_receipt where (type LIKE '%extrafee%') and session='".$_SESSION["admin"]["session"]."' $between ");

                        $hostelpaid = $db->getVal("select sum(amount) from reff_receipt where type='hostelfee' and session='".$_SESSION["admin"]["session"]."' $between ");
                    
                        $hostelotherpaid = 0;
                        $hostelotherfee = $db->getRows("select * from hostel_otherfee_structure where session='".$_SESSION["admin"]["session"]."' and month='$i' group by name ");
                        foreach($hostelotherfee as $hostelfee){
                            $hostelotherpaid+= $db->getVal("select sum(amount) from reff_receipt where type='$hostelfee[name]' and (type!='fee' and type!='bus' and type NOT LIKE '%extrafee%') and session='".$_SESSION["admin"]["session"]."' $between ");
                        }

                    // }

                    echo $totaltuition = number_format($tuitionpaid,2);
                    echo "<br>";
    
                    echo $totalbus = number_format($buspaid,2);
                    echo "<br>";
    
                    echo $totalotherfee = number_format($otherpaid,2);
                    echo "<br>";

                    echo $totalextrafee = number_format($extrapaid,2);
                    echo "<br>";

                    echo number_format($hostelpaid,2);
                    echo "<br>";

                    echo number_format($hostelotherpaid,2);
                    echo "<br>";

                    $totalpaid = $tuitionpaid+$buspaid+$otherpaid+$extrapaid+$hostelpaid+$hostelotherpaid;

    		?>
    		
    		</td>
    
    	</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($totaloutstanding,2) ?></td>
				<td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;" align="right"><?php echo number_format($totaldiscount,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($totalpaid,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;">Remaining: <?php echo number_format(($totaloutstanding-$totalpaid-$totaldiscount),2); ?></span></center>
    </div>
    	
    	<?php

    } else {
        echo "Please Select Month.";
    }
}
?>




</div>
</div>
</div>
<?php

?>
<!-- / Forms -->
<style>
<!-- CSS goes in the document HEAD or added to your external stylesheet -->
<style type="text/css">
table.gridtable {
	font-family: verdana,arial,sans-serif;
	font-size:11px;
	color:#333333;
	border-width: 1px;
	border-color: #666666;
	border-collapse: collapse;
}
table.gridtable th {
	border-width: 1px;
	padding: 3px;
	border-style: solid;
	border-color: #666666;
	background-color: #dedede;
}
table.gridtable td {
	border-width: 1px;
	padding: 3px;
	border-style: solid;
	border-color: #666666;
	background-color: #ffffff;
}
</style>
<!-- Table goes in the document BODY -->


</style>