<?php 
$pgMod="income";
$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
}

?>

<!-- nitish code start-->

<form method="post" action="">
    <div class="row-fluid">
        <div class="control-group">
           <!-- <div class="span3">
            	<select name="pmode">
                    <option value="">All Income</option>
                     <?php 
					$Expensemode=$db->getRows("select * from mode where school_id='".$_SESSION["admin"]["school_id"]."' and type='Income' and status=1");
					if(is_array($Expensemode) && count($Expensemode)>0){
					?>
                      <?php foreach($Expensemode as $Expense){
					  	?><option><?php echo $Expense["mode"] ?></option><?php
					  }?>
                    <?php } ?>
                    </select>
            </div> -->
            <div class="span3"><input required type="date" id="tfrom" name="tfrom" class="input-block-level" value="<?php if($_POST)echo $_POST["tfrom"];?>"></div>
            <div class="span3"> <input required type="date" id="tto" name="tto" class="input-block-level" value="<?php if($_POST)echo $_POST["tto"];?>"></div>
            
            <div class="control-group span2">
					<?php 
                                $user=$db->getRows("select * from users where school_id='".$_SESSION["admin"]["school_id"]."'");
                                if(is_array($user) && count($user)>0){
                                    foreach($user as $usr){
                                        ?>
					<input type="checkbox" name="user1[]" value="<?php echo $usr["id"] ?>"><?php echo $usr["name"] ?>
					<br>
					<?php
                                    }
                                }
                            ?>
					 </div>
            <div class="span3"> <!-- <input type="submit" class="btn" />  !-->
                <button type="submit" name="school_fee" class="btn-classic margin"> School Fee</button>
                <button type="submit" name="complete_fee" class="btn-classic margin"> complete Fee</button>
                <button type="submit" name="monthly_report" class="btn-classic margin"> Monthly Report</button>
                <button type="submit" name="mode_wise" class="btn-classic margin"> Payment Mode wise</button>


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

<!-- nitish code end-->
 
<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($_POST["monthly_report"])){

        $fdate = $_POST["tfrom"];
	    $flag = 0;

		$fd = strtotime($fdate);
		$firstdate = date('Y/m/d', $fd);

		$fbd = $db->getVal("select back_date_fee from tbl_info");
        $s = strtotime($fbd);
        $backdate = date('Y/m/d', $s);

        if($firstdate == $backdate){
            $flag = 1;
        }

    if($flag == 0){

        if(isset($_POST["tfrom"]) && isset($_POST["tto"])){
        $between=" and udate between '".$_POST["tfrom"]."' and '".$_POST["tto"]."' ";

    if(isset($_POST['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 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"); 

    if($_POST["pmode"]!=""){
        $aryList=$db->getRows("SELECT * FROM transaction where 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");
    }
 
    $total="";

    $i=0;

	$tndate = date('Y/m/d', strtotime($_POST["tto"]));
    ?>
		<center><p><h2>Date Wise Collection Report</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>

	<?php
    	$i=0;
		$tndate = date('Y/m/d', strtotime('-1 days',strtotime($_POST["tto"])));

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

    function getDatesFromRange($start, $end, $format = 'Y-m-d') {
          
        // Declare an empty array
        $array = array();
          
        // Variable that store the date interval
        // of period 1 day
        $interval = new DateInterval('P1D');
      
        $realEnd = new DateTime($end);
        $realEnd->add($interval);
      
        $period = new DatePeriod(new DateTime($start), $interval, $realEnd);
      
        // Use loop to store date into array
        foreach($period as $date) {                 
            $array[] = $date->format($format); 
        }
      
        // Return the array elements
        return $array;
    }

    // Function call with passing the start date and end date
    $Date = getDatesFromRange($_POST["tfrom"], $_POST["tto"]);

    $otherfee = $db->getRows("select name from otherfee_structure where session='".$_SESSION["admin"]["session"]."' group by name ");

    $temp = array();
    foreach($otherfee as $ot){
        $amt = $db->getVal("select sum(amount) from reff_receipt as r INNER join student as s on s.idno=r.idno where r.type='$ot[name]' and (r.date >= '".$_POST["tfrom"]."' and r.date <= '".$_POST["tto"]."') and r.session='".$_SESSION["admin"]["session"]."' " );
        if($amt>0){
            $temp[]=$ot['name'];
        }
    }

	?>

				<table border="1" 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 width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;border-left:2px solid #FA0606;">Date</td> 
						<td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;">Tuition Fee</td>
						<td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;">Transport Fee</td>
						<td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;">Extra Fee</td>
						<?php  foreach($temp as $t){ ?>
                        <td width="7%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;"><?php echo $t; ?></td>
                        <?php }  ?>
						<td width="5%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;">Reception</td>
						<td width="5%" style="border-right:2px solid #FA0606;border-bottom: 1px solid #000;">Total</td>
					</tr>
					
					<?php
					$totaltuition = 0;
					$otherary = array();
					foreach($Date as $d){
					    $rowtotal = 0;

                        $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 like '$d%' and r.session='".$_SESSION["admin"]["session"]."' ");
    
                        $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 like '$d%' and r.session='".$_SESSION["admin"]["session"]."' ");

                        $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 like '$d%' and r.session='".$_SESSION["admin"]["session"]."' ");
                        
                        $reception = $db->getVal("select sum(amount) from enquiry_receipt where udate = '$d' and session='".$_SESSION["admin"]["session"]."' ");
					?>
					    <tr>
					        <td>
					            <?php echo date('d-m-Y', strtotime($d)); ?>
					        </td>
					        <td style="text-align:right;"><?php echo number_format($TUN,2); $totaltuition+=$TUN; $rowtotal+=$TUN; ?></td>
					        <td style="text-align:right;"><?php echo number_format($BUS,2); $totalbus+=$BUS; $rowtotal+=$BUS; ?></td>
					        <td style="text-align:right;"><?php echo number_format($extra,2); $totalextra+=$extra; $rowtotal+=$extra; ?></td>
					        <?php $i=0;  foreach($temp as $t){ $i++;
    					        $otheramt = $db->getVal("select sum(amount) from reff_receipt as r INNER join student as s on s.idno=r.idno where r.type='$t' and r.date like '$d%' and r.session='".$_SESSION["admin"]["session"]."' " );
    					        $otherary[$i]+=$otheramt;
					        ?>
					        <td style="text-align:right;"><?php echo number_format($otheramt,2); ?></td>
					        <?php $rowtotal+=$otheramt; } ?>
					        <td style="text-align:right;"><?php echo number_format($reception,2); $totalreception+=$reception; $rowtotal+=$reception; ?></td>
					        <td style="text-align:right;"><?php echo number_format($rowtotal,2); ?></td>
					    </tr>
					<?php } ?>
					<tbody>
    					<tr>
    					    <th>Total</th>
    					    <th style="text-align:right;"><?php echo number_format($totaltuition,2); $completetotal+=$totaltuition; ?></th>
    					    <th style="text-align:right;"><?php echo number_format($totalbus,2); $completetotal+=$totalbus; ?></th>
    					    <th style="text-align:right;"><?php echo number_format($totalextra,2); $completetotal+=$totalextra; ?></th>
    					    <?php foreach($otherary as $otf){ ?>
    					    <th style="text-align:right;"><?php echo number_format($otf,2); $completetotal+=$otf; ?></th>
    					    <?php } ?>
    					    <th style="text-align:right;"><?php echo number_format($totalreception,2); $completetotal+=$totalreception; ?></th>
    					    <th style="text-align:right;"><?php echo number_format($completetotal,2); ?></th>
    					</tr>
					</tbody>
					
					
				</table>

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

    }

}

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

    if(isset($_POST["tfrom"]) && isset($_POST["tto"])){
        $between = " and udate between '".$_POST["tfrom"]."' and '".$_POST["tto"]."' ";
        
        if(isset($_POST['user1'])){
            $u = implode(',',$_POST["user1"]);
            $whereUser =" and added_by in ($u) ";
            $Username = $u;
        }
        
        $feehead = $db->getRows("select type from reff_receipt where date between '".$_POST["tfrom"]."' and '".$_POST["tto"]."' and type!='busfee' group by type ");
        
        $modes = $db->getRows("select pay_type from reff_receipt where date between '".$_POST["tfrom"]."' and '".$_POST["tto"]."' and type!='busfee' group by pay_type ");

        $total="";
    
    	$tndate = date('Y/m/d', strtotime('-1 days',strtotime($_POST["tto"])));
        ?>
			<center><h2>Day Report Header Wise</h2></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>

    <table class="table">
        <thead>
            <tr>
                <th>S.No.</th>
                <th>Header</th>
                <?php foreach($modes as $m){ ?>
                    <th style="text-align:right;"><?php echo $m['pay_type']; ?></th>
                <?php } ?>
                <th style="text-align:right;">Total</th>
            </tr>
        </thead>
        <tbody>
            <?php $columntotal = [];
            foreach($feehead as $head){ $i++; $rowtotal = 0; ?>
            
            <tr>
                <td><?php echo $i; ?></td>
                <td><?php echo $head['type']; ?></td>
                <?php foreach($modes as $m){$j++; ?>
                    <td style="text-align:right;"><?php $amount = $db->getVal("select sum(amount) from reff_receipt where type='$head[type]' and pay_type='$m[pay_type]' and date between '".$_POST["tfrom"]."' and '".$_POST["tto"]."' ");
                        echo number_format($amount, 2);
                        $rowtotal+=$amount;
                        $columntotal[$m['pay_type']]+=$amount;
                    ?></td>
                <?php } ?>
                <td style="text-align:right;"><?php echo number_format($rowtotal,2); ?></td>
            </tr>
            
            <?php } ?>
        </tbody>
        <tfoot>
            <tr>
                <td></td>
                <td>Total</td>
                <?php foreach($columntotal as $ct){ ?>
                <td style="text-align:right;"><?php echo $ct; $total+=$ct; ?></td>
                <?php } ?>
                <td style="text-align:right;"><?php echo number_format($total,2); ?></td>
            </tr>
            
            <?php
            $busfee = $db->getRows("select type from reff_receipt where date between '".$_POST["tfrom"]."' and '".$_POST["tto"]."' and type='busfee' group by type ");
            ?>
            n
        </tfoot>
    </table>

<br>

<div style="width:100%;margin-top:150px;text-align:right;">Principal Sign.</div>

</div>
	<?php
}
}


	if(isset($_POST["school_fee"])){
if(isset($_POST["tfrom"]) && isset($_POST["tto"])){
$between=" and udate between '".$_POST["tfrom"]."' and '".$_POST["tto"]."' ";


if(isset($_POST['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

    $otherfee = $db->getRows("select name from otherfee_structure where session='".$_SESSION["admin"]["session"]."' and name!='Admission_Charge' and name!='Admission_Form' and name!='Exam_Fee_1st' and name!='Exam_Fee_2nd' and name!='Annual_Charge' and name!='Annual_fee' group by name ");
?>

	</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';
				 echo "<br>";
				 $i = 5;
				 foreach($otherfee as $ot){$i++;
    				 echo $i;
    				 echo "<br>";
				 }

				 echo $i=$i+1;
				 echo "<br>";
				 echo $i=$i+1;
				 echo "<br>";
				 echo $i=$i+1;
				 echo "<br>";

                $allmodes=$db->getRows("SELECT * FROM mode where school_id='".$_SESSION["admin"]["school_id"]."' and type='Income' ");
                foreach($allmodes as $mode){
                    $modeincome = $db->getVal("select sum(amount) from transaction where type='deposit' and (udate between '".$_POST["tfrom"]."' and '".$_POST["tto"]."') and (tfor='$mode[mode]') and session='".$_SESSION["admin"]["session"]."' ");
        			if($modeincome>0){$i++;
			 	        echo "$i<br>";
        			}
		 	    }
		 	    
		 	    echo $i=$i+1;
				echo "<br>";
			 	    
				 ?></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 "Admission Charge"; 
			     echo "<br>";
				 echo "Exam Fee"; 
			     echo "<br>";
			     echo "Annual Fee"; 
			     echo "<br>";
				$feeheadary = " and (type!='Admission_Charge')  and (type!='Admission_Form')  and (type!='Exam_Fee_1st')  and (type!='Exam_Fee_2nd') and (type!='Annual_Charge') and (type!='Annual_fee') ";
				foreach($otherfee as $ot){$i++;
    				 echo $ot["name"];
    				 echo "<br>";
    				$feeheadary.=" and ( type!='$ot[name]' )";
				 }
				 echo "other Fee"; 
				 echo "<br>";
				
				 echo "Extra Fee";	
				 
				 echo "<br>";
				 echo 'Reception';
				 echo "<br>";
				 
                 foreach($allmodes as $mode){
                    $modeincome = $db->getVal("select sum(amount) from transaction where type='deposit' and (udate between '".$_POST["tfrom"]."' and '".$_POST["tto"]."') and (tfor='$mode[mode]') and session='".$_SESSION["admin"]["session"]."' ");
        			if($modeincome>0){
			 	        echo "$mode[mode]<br>";
        			}
		 	    }
		 	    
		 	    echo 'Inventory';
				 echo "<br>";
				?></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 '".$_POST["tfrom"]."' and '".$_POST["tto"]."') and r.session='".$_SESSION["admin"]["session"]."' ");
	        echo number_format($TUN,2);
	        echo "<br>";
            $total+=$TUN;

			$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 '".$_POST["tfrom"]."' and '".$_POST["tto"]."') and r.session='".$_SESSION["admin"]["session"]."' ");
			 echo number_format($BUS,2);
			echo "<br>";
		    $total+=$BUS;

		    $admission=$db->getVal("select sum(amount) from reff_receipt as r INNER join student as s on s.idno=r.idno where (r.type='Admission_Charge' or r.type='Admission_Form') and (r.date between '".$_POST["tfrom"]."' and '".$_POST["tto"]."') and r.session='".$_SESSION["admin"]["session"]."' " );
                echo number_format($admission,2);
                echo "<br>";
    		$total+=$admission;

		    $exam=$db->getVal("select sum(amount) from reff_receipt as r INNER join student as s on s.idno=r.idno where (r.type='Exam_Fee_1st' or r.type='Exam_Fee_2nd') and (r.date between '".$_POST["tfrom"]."' and '".$_POST["tto"]."') and r.session='".$_SESSION["admin"]["session"]."' " );
                echo number_format($exam,2);
                echo "<br>";
    		$total+=$exam;

		    $annual=$db->getVal("select sum(amount) from reff_receipt as r INNER join student as s on s.idno=r.idno where (r.type='Annual_Charge' or r.type='Annual_fee') and (r.date between '".$_POST["tfrom"]."' and '".$_POST["tto"]."') and r.session='".$_SESSION["admin"]["session"]."' " );
                echo number_format($annual,2);
                echo "<br>";
    		$total+=$annual;


				foreach($otherfee as $ot){$i++;
                        $otherfee=$db->getVal("select sum(amount) from reff_receipt as r INNER join student as s on s.idno=r.idno where r.type='$ot[name]' and type not in ('busfee','fee','hostelfee') and (r.date between '".$_POST["tfrom"]."' and '".$_POST["tto"]."') and r.session='".$_SESSION["admin"]["session"]."' " );
                       echo number_format($otherfee,2);
                       echo "<br>";
                       $total+=$otherfee;
				 }

		    $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%') $feeheadary and type not in ('busfee','fee','hostelfee') and (r.date between '".$_POST["tfrom"]."' and '".$_POST["tto"]."') and r.session='".$_SESSION["admin"]["session"]."' " );
               echo number_format($other,2);
               echo "<br>";
                $total+=$other;

               $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 '".$_POST["tfrom"]."' and '".$_POST["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>";
                $total+=$extra;

				 $tot=$db->getVal("select sum(amount) from transaction where type='deposit' and (udate between '".$_POST["tfrom"]."' and '".$_POST["tto"]."') and (tfor='cash' or tfor='cheque' or tfor='hostel-income') and session='".$_SESSION["admin"]["session"]."' ");
				echo number_format($tot,2);
				echo "<br>";
				$total+=$tot;

                $totalmodeincome = 0;
				 foreach($allmodes as $mode){
    				 $modeincome = $db->getVal("select sum(amount) from transaction where type='deposit' and (udate between '".$_POST["tfrom"]."' and '".$_POST["tto"]."') and (tfor='$mode[mode]') and session='".$_SESSION["admin"]["session"]."' ");
                    if($modeincome>0){
        			echo number_format($modeincome,2);
        			echo "<br>";
        			$total+=$modeincome;
        			$totalmodeincome+=$modeincome;
                    }
		 	    }
		 	    
		 	    $inventory = $db->getVal("select sum(total_amount) from inventory_receipt where (issue_date between '".$_POST["tfrom"]."' and '".$_POST["tto"]."') and session='".$_SESSION["admin"]["session"]."' ");
		 	    echo number_format($inventory,2);
				echo "<br>";
				$total+=$inventory;


				?></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 //}   

		
		?>	
	</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>



<table class="table">
    <tr>
        <td>Payment Mode</td>
        <td>Amount</td>
    </tr>
<?php $dataPoints = []; $remtotal = 0;
    $tempary = [];
    $paymode = $db->getRows("select distinct(payment_mode) from receipt where payment_mode!='' and udate='$_POST[tfrom]' ");
        foreach($paymode as $mode){
            $amount = $db->getVal("select sum(amount) from receipt where payment_mode='$mode[payment_mode]' and udate='$_POST[tfrom]' ");
            if($mode['payment_mode']=="Cash"){
                $amount+=$totalmodeincome;
            } else {
                $remtotal+=$amount;
            }
            
            $tempary[$mode['payment_mode']] += $amount;
            // array_push($dataPoints,["label"=>$mode["payment_mode"], "y"=>$amount]);
        }
        
        $enquirymodes = $db->getRows("select distinct(payment_mode) from enquiry_receipt where payment_mode!='' and udate='$_POST[tfrom]' ");
        
        foreach($enquirymodes as $mode){
            $amount = $db->getVal("select sum(amount) from enquiry_receipt where payment_mode='$mode[payment_mode]' and udate='$_POST[tfrom]' ");
            if($mode['payment_mode']!="Cash"){
                $remtotal+=$amount;
            }
            $tempary[$mode['payment_mode']] += $amount;
        }
        
        $paymodebus = $db->getRows("select distinct(payment_mode) from inventory_receipt where payment_mode!='' and issue_date like '$_POST[tfrom]%' ");
        foreach($paymodebus as $mode){
            $amount = $db->getVal("select sum(total_amount) from inventory_receipt where payment_mode='$mode[payment_mode]' and issue_date like '$_POST[tfrom]%' ");
            if($mode['payment_mode']!="Cash"){
                $remtotal+=$amount;
            }
            $tempary[$mode['payment_mode']] += $amount;
        }
        
        foreach($tempary as $key=>$val){
            array_push($dataPoints,["label"=>$key, "y"=>$val]);
        }
        
        // print_r($dataPoints);

            foreach($dataPoints as $mode){ ?>
                    <tr>
                        <td style="color:red;"><?php echo $mode["label"]; ?></td>
                        <td><?php echo number_format($mode["y"],2); ?> </td>
                    </tr>
                    <?php
                }
                ?>
</table>

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


<div style="width:100%;margin-top:150px;text-align:right;">Principal Sign.</div>

</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
//}
}
}




if(isset($_POST["complete_fee"])){
if(isset($_POST["tfrom"]) && isset($_POST["tto"])){
$between=" and udate between '".$_POST["tfrom"]."' and '".$_POST["tto"]."' ";


if(isset($_POST['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 "Hostel Fee";
			     echo "<br>";
				 echo "other Fee"; 
				 echo "<br>";
				
				 echo "Extra Fee";	
				
				?></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 '".$_POST["tfrom"]."' and '".$_POST["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 '".$_POST["tfrom"]."' and '".$_POST["tto"]."') and r.session='".$_SESSION["admin"]["session"]."' ");
			 echo number_format($BUS,2);
			echo "<br>";
			
			$HOSTEL=$db->getVal("select sum(amount) from reff_receipt as r INNER join student as s on s.idno=r.idno where r.type='hostelfee' and (r.date between '".$_POST["tfrom"]."' and '".$_POST["tto"]."') and r.session='".$_SESSION["admin"]["session"]."' ");
			 echo number_format($HOSTEL,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 '".$_POST["tfrom"]."' and '".$_POST["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 '".$_POST["tfrom"]."' and '".$_POST["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);
               
				?></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+$HOSTEL;
		
		?>	
	</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>

</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>