<?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 Expense</option>
                     <?php 
					$Expensemode=$db->getRows("select * from mode where school_id='".$_SESSION["admin"]["school_id"]."' and type='Expense' 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="span3"><input type="submit" class="btn" /></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">
    <button class="btn btn-success" onclick="exportTableToExcel('tblData', 'Complete Expenses Report')" style="margin-left:35px;">Excel</button>
        <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["tfrom"]) && isset($_POST["tto"])){
$between=" and udate between '".$_POST["tfrom"]."' and '".$_POST["tto"]."' ";

$aryList=$db->getRows("SELECT *,sum(amount) as total_income FROM transaction where school_id='".$_SESSION["admin"]["school_id"]."' and type='withdraw' and status=1 and amount!=0 and session='".$_SESSION["admin"]["session"]."' $between group by tfor order by udate 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='withdraw' and status=1 and amount!=0 and session='".$_SESSION["admin"]["session"]."' and tfor like '%".$_POST["pmode"]."%' and amount!=0 $between order by udate asc");
}
 $i=0;$total="";
if(is_array($aryList) && count($aryList)>0)
{

	$tndate = date('Y/m/d', strtotime('-1 days',strtotime($_POST["tto"])));
			//echo $tndate." ".$fdate
			?>
            <center><h5><p style="float:right">Expense 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> <p>Expense Detail</p>	</h5></center>
	
	<table class="table" id="tblData" border="1">
		<thead>
			<tr>
				<th>Serial No</th>
				<th>Expense detail</th>
				
				<th>Expense amount</th>
				<!--<th>Income date</th>
                <th>Payment for</th> -->
				
			</tr>
		</thead>
		<tbody>
		<?php foreach($aryList as $clist){ $i++; 
		$aryPgAct["id"]=$clist['id'];
		?>
			<tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
				<td><?php echo $i; ?></td>
				<td><?php 

				if($clist["tfor"]=="other2_fee" ) echo "Annual Fee";
				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"];
				
					
				
				?></td>
				<td><?php if($_POST["pmode"]!=""){echo number_format($clist["amount"],2);} else echo number_format($clist["total_income"],2);  $total+=$clist["amount"]; ?></td>
				
				<!--<td><?php //echo $clist["detail"] ?></td> 
				<td><?php 
				/*
				$date= $clist["udate"];

              $date1= explode(' ', $date);

echo $date1[0];			  
				
				*/
				?></td>
				<td><?php //echo $clist["amount"];$total+=$clist["amount"]; ?></td>
                <td><?php //echo $clist["tfor"] ?></td>-->
			</tr>
		<?php } ?>
		</tbody>
		<tfoot style="color:#F00">
			<tr>
				<th></th>
				
				<th>Total</th>
                <th><?php echo number_format($total,2) ?></th>
              
			</tr>
		</tfoot>
	</table>
	<?php
}
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></div>
<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>