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

function filterPay(Tval){
	var val=$(Tval).val();
	if(val.trim()==""){
		$(".MyTotalFee").parent().show();
		$(".MyTotalFee").parent().addClass('none');
		$(".dynamic").hide();
	}
	else
	if(!isNaN(val)){
		$(".dynamic").show();
		var allTot="";
		$(".MyTotalpaid").each(function(index, element) {
			var myVal=parseInt($(this).html());
			if(myVal>val)
			{
				$(this).parent().hide();
				$(this).parent().removeClass('none');
			}
			else
			{
				$(this).parent().show();
				$(this).parent().addClass('none');
			}
        });
		//alert(allTot);
	}else{
		alert("Please user only number");
		$(Tval).val('');
		$(".MyTotalFee").parent().show();
		$(".MyTotalFee").parent().addClass('none');
		$(".dynamic").hide();
	}
	var MyTotalFee=0,MyTotalpaid=0,MyTotalLeft=0,MyDiscount=0,MyTotalLeftDynamic=0;
	$(".none").each(function(index, element) {
		var totalleft=val-parseInt($(this).children('.MyTotalpaid').html());
		$(this).children('.MyTotalLeftDynamic').html(totalleft);
		MyTotalFee+=parseInt($(this).children('.MyTotalFee').html());
		MyTotalpaid+=parseInt($(this).children('.MyTotalpaid').html());
		MyTotalLeftDynamic+=parseInt($(this).children('.MyTotalLeftDynamic').html());
		MyTotalLeft+=parseInt($(this).children('.MyTotalLeft').html());
		MyDiscount+=parseInt($(this).children('.MyDiscount').html());
	});
	$("#MyTotalFee").html(MyTotalFee);
	$("#MyTotalpaid").html(MyTotalpaid);
	$("#MyTotalLeftDynamic").html(MyTotalLeftDynamic);
	$("#MyTotalLeft").html(MyTotalLeft);
	$("#MyDiscount").html(MyDiscount);
}
</script>
<?php
$pgMod="user_works";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);
if($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("student","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."'");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Promote", "page_id"=>$pgMod)));
}
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
}
function inst($str){
	$arr1 = str_split($str);
	if($arr1[1]=="p"){
		$i="Fee ";
		if($arr1[0]=="b")$i="Bus ";
		return $i." "." Installment ".$arr1[2];
	}
	else
		return $str;
}
function instFine($str){
	$arr1 = explode("-",$str);
	if($arr1[1]=="class")
		$i="Class Fine";
	elseif($arr1[1]=="bus")
		$i="Bus Fine";
	elseif($arr1[1]=="otherfee")
		$i="Other Fee";

	return $i." ("." Installment ".$arr1[0].")";
	
}
	?>
    <form method="get" action="">
    <input type="hidden" name="src" value="Promote" />
    <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
	<div class="row-fluid">
            <div class="span12">
            	<div class="top-bar">
                <button type="submit" style="float:right" class="btn btn-primary">Search</button>
                <h3><i class="icon-cog"></i>Report Section</div>
                
				<div class="well no-padding">
                	<div class="controls controls-row">
                        <div class="control-group span6">
                            <label class="control-label" for="class">Class</label>
                            <div class="controls">
                                <select onchange="changeIns()" name="class" id="class">
                                    <option value="">Class</option>
                                    <?php 
                                        $class=$db->getRows("select * from class  where school_id='".$_SESSION["admin"]["school_id"]."'");
                                        foreach($class as $cla){
                                            ?>
                                            <option <?php if($cla["class"]==$_GET["class"])echo 'selected';?> value="<?php echo $cla["class"] ?>">
                                                <?php echo $cla["class"] ?>
                                            </option>
                                            <?php
                                        }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="control-group span6">
                            <label class="control-label" for="medium">Users</label>
                            <div class="controls">
                            	<?php $users=$db->getRows("select * from users where school_id='".$_SESSION["admin"]["school_id"]."'"); 
								//print_r($cast);
								?>
                                <select name="added_by" id="cast">
                                	<option value="">select</option>
                                    <?php 
									if(is_array($users) && count($users)>0){
										foreach($users as $c){ ?>
										<option <?php if($c["id"]==$_GET["added_by"])echo 'selected';?> value="<?php echo $c["id"] ?>"><?php echo $c["userid"]."(".$c["name"].")" ?></option>
										<?php
										}
									}
									?>
                                    
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="controls controls-row">
                        <div class="control-group span6">
                            <label class="control-label" for="gender">Gender</label>
                            <div class="controls">
                                <select name="gender" id="gender">
                                    <option value="">All</option>
                                    <option <?php if($_GET["gender"]=="male")echo 'selected="selected"'; ?>  value="male">male</option>
                                    <option <?php if($_GET["gender"]=="female")echo 'selected="selected"'; ?> value="female">female</option>
                                </select>
                            </div>
                        </div>
                        <div class="control-group span6">
                            <label class="control-label" for="age">Age</label>
                            <div class="controls">
                                    <select class="span6" name="startage" id="startage">
                                    	<option value="">Start age</option>
                                        <?php
									    for($m = 1;$m <= 100; $m++){ 
                                            $selected="";if($_GET["startage"]==$m)$selected=" selected ";
											echo "<option $selected value='$m'>$m</option>";
                                        }
										?>
                                    </select>
                                    <select class="span6" name="endage" id="endage">
                                    	<option value="">End age</option>
                                        <?php
									    for($m = 1;$m <= 100; $m++){ 
                                            $selected="";if($_GET["endage"]==$m)$selected=" selected ";
											echo "<option $selected value='$m'>$m</option>";
                                        }
										?>
                                    </select>
                                    
                                </div>
                        </div>
                    </div>
                    <div class="controls controls-row">
                        <div class="control-group span6">
                            <label class="control-label" for="class">From Date</label>
                            <div class="controls">
                                    <select class="span3" name="fdate" id="date">
                                    	<option value="">Date</option>
                                        <?php
									    for($m = 01;$m <= 31; $m++){ 
                                            $selected="";if($_GET["fdate"]==$m)$selected=" selected ";
											echo "<option $selected value='$m'>$m</option>";
                                        }
										?> 
                                    </select>
                                    <select class="span5" name="fmonth" id="fmonth">
                                    	<option value="">Month</option>
                                     <?php
									    for($m = 1;$m <= 12; $m++){ 
                                            $month =  date("F", mktime(0, 0, 0, $m)); 
											$selected="";if($_GET["fmonth"]==$m)$selected=" selected ";
											echo "<option $selected value='$m'>$month</option>";
                                        }
									 ?> 
                                    </select>
                                    <select class="span4" name="fyear" id="fyear">
                                    	<option value="">Year</option>
                                        <?php
									    for($m = 2013;$m <= 2050; $m++){  
                                            $selected="";if($_GET["fyear"]==$m)$selected=" selected ";
											echo "<option $selected value='$m'>$m</option>";
                                        }
										?> 
                                    </select>
                                </div>
                        </div>
                        <div class="control-group span6">
                            <label class="control-label" for="medium">To date</label>
                            <div class="controls">
                                    <select class="span3" name="tdate" id="tdate">
                                    	<option value="">Date</option>
                                        <?php
									    for($m = 01;$m <= 31; $m++){ 
                                             $selected="";if($_GET["tdate"]==$m)$selected=" selected ";
											echo "<option $selected value='$m'>$m</option>";
                                        }
										?> 
                                    </select>
                                    <select class="span5" name="tmonth" id="tmonth">
                                    	<option value="">Month</option>
                                     <?php
									    for($m = 1;$m <= 12; $m++){ 
                                            $month =  date("F", mktime(0, 0, 0, $m)); 
											 $selected="";if($_GET["tmonth"]==$m)$selected=" selected ";
											echo "<option $selected value='$m'>$month</option>";
                                        }
									 ?> 
                                    </select>
                                    <select class="span4" name="tyear" id="tyear">
                                    	<option value="">Year</option>
                                        <?php
									    for($m = 2013;$m <= 2050; $m++){  
                                             $selected="";if($_GET["tyear"]==$m)$selected=" selected ";
											echo "<option $selected value='$m'>$m</option>";
                                        }
										?> 
                                    </select>
                                </div>
                        </div>
                    </div>
                    <br>
				</div>
                
				<!-- / Forms: Content -->

			</div>
    </div>
    </form>
	<?php
	$where=array();
	$qry="";
	
		$conwhere="";
		if(isset($_GET["fyear"]) && trim($_GET["fyear"])!="" && isset($_GET["fmonth"]) && trim($_GET["fmonth"])!="" && isset($_GET["fdate"]) && trim($_GET["fdate"])!="" && isset($_GET["tyear"]) && trim($_GET["tyear"])!="" && isset($_GET["tmonth"]) && trim($_GET["tmonth"])!="" && isset($_GET["tdate"]) && trim($_GET["tdate"])!=""){
			$fdate=$_GET["fyear"]."/".$_GET["fmonth"]."/".$_GET["fdate"];
			$tdate=$_GET["tyear"]."/".$_GET["tmonth"]."/".$_GET["tdate"];
			$where[]=" (rr.date between '$fdate' and '$tdate') ";
			$conwhere.=" and  (installment_fine.udate between '$fdate' and '$tdate') ";
		}
		if(isset($_GET["medium"]) && trim($_GET["medium"])!=""){
			$where[]=" s.medium='".$_GET["medium"]."' ";
			$conwhere.=" and student.medium='".$_GET["medium"]."' ";
		}
		if(isset($_GET["added_by"]) && trim($_GET["added_by"])!=""){
			$where[]=" rr.added_by='".$_GET["added_by"]."' ";
			$conwhere.=" and installment_fine.added_by='".$_GET["added_by"]."' ";
		}
		if(isset($_GET["class"]) && trim($_GET["class"])!=""){
			$where[]=" s.class='".$_GET["class"]."' ";
			$conwhere.=" and student.class='".$_GET["class"]."' ";
		}
		if(isset($_GET["gender"]) && trim($_GET["gender"])!=""){
			$where[]=" s.gender='".$_GET["gender"]."' ";
			$conwhere.=" and student.gender='".$_GET["gender"]."' ";
		}
		if(isset($_GET["cast"]) && trim($_GET["cast"])!=""){
			$where[]=" s.cast='".$_GET["cast"]."' ";
			$conwhere.=" and student.cast='".$_GET["cast"]."' ";
		}
		if(isset($_GET["startage"]) && trim($_GET["startage"])!="" && isset($_GET["endage"]) && trim($_GET["endage"])!=""){
			$start = date('Y-m-d', strtotime('-'.$_GET["startage"].' years'));
			$end = date('Y-m-d', strtotime('-'.$_GET["endage"].' years'));
			$where[]=" (s.dob between '".$end."' and '".$start."')";
			$conwhere.=" and (student.dob between '".$end."' and '".$start."') ";
		}
		$where[]=" rr.idno=s.idno and s.session='".$_SESSION["admin"]["session"]."' and s.status=1 ";
		$table=" reff_receipt as rr, student as s";
		$result=$db->getRows("SELECT rr.*,rr.id as recid,s.class as sclass,s.name,s.mname,s.fname,s.dob,s.gender FROM ".$table." where rr.school_id='".$_SESSION["admin"]["school_id"]."' AND ".implode(" AND ",$where)." group by rr.id order by rr.date ");
		//echo "<pre>";print_r($result);echo "</pre>";echo $db->getLastQuery();
		
		?>
		 <div class="row-fluid">
        <div class="span12">
            <div class="top-bar">
                <h3><i class="icon-cog"></i> Detail</h3>
                <div style="float:right; margin-top:5px">
                    <button class="btn btn-success" onclick="exportTableToExcel('tblData', 'User Summary')" style="margin-left:35px;">Excel</button>
                    <button onclick="printDiv('printDiv')" type="button" class="btn btn-primary">Print</button>
                </div>
            </div>
            <div class="well no-padding" id="printDiv">
            	<table class="table" id="tblData" border="1">
                    <thead>
                        <tr>
                            <th>Sr. No</th>
                            <th>Added by</th>
                            <th>Name</th>
                            <th>Class</th>
                            <th>Father Name</th>
                            <th>Mother Name</th>
                            <th>Receipt no</th>
                            <th>Quater</th>
                            <th>Fee</th>
                            <th>Date</th>
                        </tr>
                    </thead>
                    <tbody>
                    	<?php $ollTotal=0;
						if(is_array($result) && count($result)>0)
						{
							$j=0;
							foreach($result as $detail)
							{
								$added_by_id=$detail["added_by"];
								/*
								$alltransection=$db->getRows("select * from transaction where rid='". $detail["idno"] ."' and session='".$_SESSION["admin"]["session"]."' and school_id='".$_SESSION["admin"]["school_id"]."' and (detail='Student bus fee Payment in installment type' or detail='Student fee Payment in installment type')");
								 if(is_array($alltransection) && count($alltransection)>0){
									foreach($alltransection as $at){
										$tids=json_decode($at["tid"], true);
										foreach($tids as $key=>$value){
											if($key==$detail["ins_name"]){
												$added_by_id=$at["added_by"]; 
											}
										}
									}
								 }
								 if(!isset($_GET["added_by"]) || $_GET["added_by"]==$added_by_id)*/{
									 $j++;
								?>
                                <tr>
                                    <td><?php echo $j; ?></td>
                                     <td><?php echo $db->getVal("select name from users where id=".$added_by_id); ?></td>
                                    <td><?php echo ucfirst($detail["name"]); ?></td>
                                    <td><?php echo $detail["sclass"]; ?></td>
                                    <td><?php echo $detail["fname"]; ?></td>
                                    <td><?php echo $detail["mname"]; ?></td>
                                    <td><?php 
									$rids=array();
									$rrde=$db->getRows("select * from receipt where session='".$_SESSION["admin"]["session"]."' and school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$detail["idno"]."'");
									if(is_array($rrde) && count($rrde)>0){
										foreach($rrde as $rr){
											$rel_rrid=explode("-",$rr["rrid"]);
											foreach($rel_rrid as $rrids){
												if($rrids==$detail["recid"]){
													$rids[]=$rr["id"];
													//echo "select id from receipt where rrid='".$rrids."'<br />";
												}
											}
										}
									}
									echo implode(",",$rids) ?></td>
                                    
                                    <td><?php 
									if($detail["type"] == "fee"){
										echo $detail["inst_id"];
										echo "( School Fee)";
									}
									elseif($detail["type"] == "busfee"){
										echo $detail["inst_id"];
										echo " (Bus Fee)";
									}
									else{
										//echo ucfirst($detail["type"]);
										echo "other Fee (".$detail["type"].")";
										//$qua=$detail["ins_name"];echo instFine($qua);
									}
									//" (".($detail["type"] == "fee" ? "School Fee" : "Bus Fee").")"; ?></td>
                                    <td><?php echo $all=$detail["amount"];;
									$ollTotal=$ollTotal+$all; ?></td>
                                    <td><?php echo $detail["date"]; ?></td>
                                </tr>
								<?php
									 }
							}
						}else{
							?>
                            <tr>
                            	<td colspan="9">No Bus and Class record Found</td>
                            </tr>
							<?php
						}
						?>
                        <tr style="border-top:1px solid #F00; color:#F00">
                        <td colspan="6">Total</td><td></td><td></td><td id="completeFeeShw"><?php echo $ollTotal; ?></td><td></td>
                        </tr>
                    </tbody>
                    <tfoot>
                    	<tr>
                        	<th></th><th></th><th></th><th></th><th></th><th></th><th></th>
                        </tr>
                    </tfoot>
                 </table>
              </div>
          </div>
		<?php
	
	//echo "<pre>";print_r($result);echo $db->getLastQuery();
	
?>

<script>
    function exportTableToExcel(tableID, filename = '',e){
    
    var tableType = document.getElementById('tblData').getAttribute("data-tblname");

    var downloadLink;
    var dataType = 'application/vnd.ms-excel';
    var tableSelect = document.getElementById(tableID);
    var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');

    // Specify file name
    filename = filename?filename+'.xls':'excel_data.xls';
    
    // Create download link element
    downloadLink = document.createElement("a");
    
    document.body.appendChild(downloadLink);
    
    if(navigator.msSaveOrOpenBlob){
        var blob = new Blob(['\ufeff', tableHTML], {
            type: dataType
        });
        navigator.msSaveOrOpenBlob( blob, filename);
    }else{
        // Create a link to the file
        downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
    
        // Setting the file name
        downloadLink.download = filename;
        
        //triggering the function
        downloadLink.click();
    }
    e.preventDefault();
}
</script>
