<script type="text/javascript" src="js/tableExport.js"></script>

<script type="text/javascript" src="js/jquery.base64.js"></script>

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

</style>

<?php
$pgMod="account_ledger";
$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"=>"Account", "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
}
	?>
    <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="span12">
            	<div class="top-bar">
                    <br />
                      <button type="submit" name="ledger" class="btn-classic margin"> Account Ledger</button>

				</div>
				<div class="well no-padding">
                	<div class="controls controls-row">
                        <div class="control-group span4">
                           <label class="control-label" for="class">Class</label>
                            <div class="controls span3">
                                <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 span4">
                            <label class="control-label" for="class">Section</label>
                            <div class="controls span3">
                               <select name="section"  id="section" >
                                <option value="">Section</option>
                                <?php 
                                    $class=$db->getRows("select * from section where school_id='".$_SESSION["admin"]["school_id"]."'");
                                    foreach($class as $cla){
                                        ?>
                                        <option <?php if($cla["name"]==$_GET["section"])echo 'selected';?> value="<?php echo $cla["name"] ?>">
                                            <?php echo $cla["name"] ?>
                                        </option>
                                        <?php
                                    }
                                ?>
                            </select>
                            </div>
                        </div>

                    </div>

                    
				</div>
			</div>
    </div>
    </form>
	<?php
	$where=array();
	$qry="";

	if(isset($_GET["fee"])){
		$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["stype"]) && trim($_GET["stype"])!=""){
			$where[]=" s.stype='".$_GET["stype"]."' ";
			$conwhere.=" and student.stype='".$_GET["stype"]."' ";
		}
		if(isset($_GET["medium"]) && trim($_GET["medium"])!=""){
			$where[]=" s.medium='".$_GET["medium"]."' ";
			$conwhere.=" and student.medium='".$_GET["medium"]."' ";
		}
		if(isset($_GET["class"]) && trim($_GET["class"])!=""){
			$where[]=" s.class='".$_GET["class"]."' ";
			$conwhere.=" and student.class='".$_GET["class"]."' ";
		}
		if(isset($_GET["section"]) && $_GET["section"]!=""){
			$where[]=" s.section='".$_GET["section"]."' ";
			$conwhere.=" and student.section='".$_GET["section"]."' ";
		}
		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 (rr.type='fee' or rr.type='busfee' or rr.type='hostelfee' or rr.type like 'extrafee%') 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.section,s.name,s.medium,s.fname,s.dob,s.mname FROM ".$table." where rr.school_id='".$_SESSION["admin"]["school_id"]."' and s.issue_TC=0  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 onclick="printDiv('printDiv')" type="button" class="btn btn-primary">Print</button>
                    <button onclick="SMSComplete('completeFeeShw','completeFeeShowSMS')" type="button" class="btn btn-primary">SMS</button>
                    <button class="btn btn-primary" onclick="exportTableToExcel('tblData', 'Fee Summary')" >Excel</button>
                </div>
            </div>
            <div class="well no-padding" id="printDiv">
            	<table class="table tableSummery" id="tblData" border="1">
                    <thead>
                        <tr>
                            <th>S. No</th>
                            <th>Name</th>
                            <th>Class</th>
                            <th>Section</th>
                            <th>Father</th>
                            <th>Mother</th>
                            <th>Receipt ID</th>
                            <th>Quater</th>
                            <th>Fee</th>
                            <th>Date</th>
                        </tr>
                    </thead>
                    <tbody>
                    	<?php $ollTotal=0;$ijk=0;
						if(is_array($result) && count($result)>0)
						{
							foreach($result as $detail)
							{$ijk++;
								?>
                                <tr>
                                    <td><?php echo $ijk; ?></td>
                                    <td><?php echo ucfirst($detail["name"]); ?></td>
                                    <td><?php echo $detail["sclass"]; ?></td>
                                    <td><?php echo $detail["section"]; ?></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"]."'");
									//echo "<pre>";print_r($rrde);echo "</pre>";
									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 implode(",",$rids) ?></td>
                                    <td><?php $ins_iods = $detail["inst_id"];//." (".($detail["type"] == "fee" ? "School Fee" : "Bus Fee").")" 
									if($detail["type"]=="fee")
										echo "<span style='color:#FF0000'>$ins_iods(School Fee)</span>";
									else if($detail["type"]=="busfee")
										echo "<span style='color:#00CC00'>$ins_iods(Transport Fee)</span>"; 
									else if($detail["type"]=="hostelfee") 
										echo "<span style='color:#FF00FF'>$ins_iods(Hostel Fee)</span>";
									else{
										$det=$db->getVal("select type from extrafee_type where id='".str_replace("extrafee","",$detail["type"])."'");
										echo "<span style='color:#FF0000'>$ins_iods($det)</span>";
									}?></td>
                                    <td><?php echo $all=$detail["amount"];
									$ollTotal=$ollTotal+$all; ?></td>
                                    <td><?php echo $detail["date"]; ?></td>
                                </tr>
								<?php
							}
						}
						$result=$db->getRows("select student.idno,student.name,student.class as sclass,student.medium,student.fname,student.dob,student.mname,installment_fine.* from installment_fine,student where student.school_id='".$_SESSION["admin"]["school_id"]."' and student.session='".$_SESSION["admin"]["session"]."' and student.issue_TC=0 and installment_fine.status=1 and installment_fine.fine!='' and installment_fine.idno=student.idno $conwhere order by student.name");
						//echo $db->getErMsg().$db->getLastQuery();
						if(is_array($result) && count($result)>0){
							foreach($result as $detail){$ijk++;
								?>
                                <tr>
                                    <td><?php echo $ijk; ?></td>
                                    <td><?php echo $detail["name"]; ?></td>
                                    <td><?php echo $detail["sclass"]; ?></td>
                                     <td><?php echo $detail["section"]; ?></td>
                                    <td><?php echo $detail["fname"]; ?></td>
                                    <td><?php echo $detail["mname"]; ?></td>
                                    <td><?php $detail["id"];
									//echo $db->getVal("select id from receipt where rrid=(select id from reff_receipt where inst_id=".$detail["id"].")"); 
									$rids=array();
									$recpID=$db->getVal("select id from reff_receipt where inst_id='".$detail["id"]."'");
									$rrde=$db->getRows("select rrid,id 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){
											//print_r($rr);
											$rel_rrid=explode("-",$rr["rrid"]);
											foreach($rel_rrid as $rrids){
												if($rrids==$recpID){
													$rids[]=$rr["id"];
													//echo "select id from receipt where rrid='".$rrids."'<br />";
												}
											}
										}
									}//echo $db->getLastQuery();
									echo implode(",",$rids).$db->getErMsg();
									?></td>
                                    <td><?php $qua=$detail["ins_name"];echo instFine($qua,$detail["reasion"]); ?></td>
                                    <td><?php $otfee=str_replace("_detail","_fee",$qua);
									echo $all=$detail["fine"];$ollTotal=$ollTotal+$all; ?></td>
                                    <td><?php echo $detail["udate"]; ?></td>
                                </tr>
								<?php
							}
						}
						$newwhere="";
						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"];
							$newwhere=" and (udate between '$fdate' and '$tdate') ";
						}
						$finalresult=$db->getRows("select * from transaction where type='deposit' $newwhere and rid=0 and tid=0");
						if(is_array($finalresult) && count($finalresult)>0){
							foreach($finalresult as $detail){$ijk++;
								?><tr>
                                    <td><?php echo $ijk; ?></td>
                                    <td>--</td>
                                    <td>--</td>
                                    <td>--</td>
                                    <td>--</td>
                                    <td>--</td>
									<td>--</td>
                                    <td><?php echo $detail["detail"] ?></td>
                                    <td><?php echo $detail["amount"];$ollTotal+=$detail["amount"]; ?></td>
                                    <td><?php echo $detail["udate"] ?></td>
									<?php
							}
						}
						?>
                        
                    </tbody>
                    <tfoot>
                    	<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>
                    </tfoot>
                 </table>
              </div>
          </div>
          </div>
		<?php
	}

	elseif(isset($_GET["ledger"])){
		$where="";

		if(isset($_GET["class"]) && trim($_GET["class"])!=""){
			$where.=" class='".$_GET["class"]."' and ";
		}

		if(isset($_GET["section"]) && $_GET["section"]!=""){
			$where.=" section='".$_GET["section"]."' and ";
		}
		?>
		<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 onclick="printDiv1('printableArea')" type="button" class="btn btn-primary">Print</button>
                    <button onclick="SMSComplete('completeFeeShw','completeFeeShowSMS')" type="button" class="btn btn-primary">SMS</button>
                    <button class="btn btn-primary" onclick="exportTableToExcel('tblData', 'Fee Summary')" >Excel</button>
                </div>
            </div>
            <div class="well no-padding" id="printableArea">
        <?php
		$allstudents = $db->getRows("select * from student where $where session='".$_SESSION["admin"]["session"]."'  and status='1' order by name");
		foreach($allstudents as $stu){
        ?>
            <div class="mydiv">
              <div class="headPrint" style="border: none;">
    			 <table style="width:100%;">
    			     <tr>
    			         <td><img src="<?php echo URL_ROOT ?>uploads/cbselogo.png" height="90px" width="110px" style="margin:0px 0px 3px 10px;"></td>
    			         <td style="text-align:center;margin-bottom:18px;"><h1 style="font-size:30px;margin:5px 0px 0px 0px;color: #EC1E1E;"><?php echo ucfirst($LinksDetails["site_name"]) ?></h1>
        			         <span style="font-size:16px;font-weight:800;  color: #186916;"> <?php echo ucfirst(nl2br($LinksDetails["contact_content"])) ?></span>
    			         </td>
    			         <td><img src="<?php echo $LinksDetails["logo"]; ?>" height="90px" width="110px" style="margin:0px 20px 3px 10px;float:right;"></td>
    			     </tr>
    			 </table>
    		  </div>
    		  <h4 style="text-align:center;border-bottom:solid 1px;">Student Fee Ledger</h4>
    		  <br>
    		  
    		  <table style="font-family:arial;font-size:12px;">
    		      <tr>
    		          <td>Admn. No. : </td>
    		          <td><?php echo $stu['admissionno']; ?></td>
    		      </tr>
    		      <tr>
    		          <td>Name of Student : </td>
    		          <td><?php echo $stu['name']; ?></td>
    		      </tr>
    		      <tr>
    		          <td>Name of Father : </td>
    		          <td><?php echo $stu['fname']; ?></td>
    		      </tr>
    		      <tr>
    		          <td>Name of Mother : </td>
    		          <td><?php echo $stu['mname']; ?></td>
    		      </tr>
    		      <tr>
    		          <td>Date of Admission : </td>
    		          <td><?php echo $stu['doa']; ?></td>
    		      </tr>
    		      <tr>
    		          <td>Mobile No. : </td>
    		          <td><?php echo $stu['mobile']; ?></td>
    		      </tr>
    		      <tr>
    		          <td>Date of Birth : </td>
    		          <td><?php echo $stu['dob']; ?></td>
    		      </tr>
    		      <tr>
    		          <td>Class : </td>
    		          <td><?php echo $stu['class']; ?></td>
    		      </tr>
    		  </table>
    		  <br>
    		  
    		  <table border="1" cellspacing="0" cellpadding="5" style="font-weight:normal;width:100%;">
    		      <tr>
    		          <td>S. No.</td>
    		          <td>Month Name</td>
    		          <td>Description <br> (if Any)</td>
    		          <td>Total Amt <br> (Rs.)</td>
    		          <td>Deposit <br> Amt (Rs.)</td>
    		          <td>Dues <br> (Rs.)</td>
    		          <td>Bill No</td>
    		          <td>Acct Sign</td>
    		      </tr>
    		      <tr>
    		          <td>1.</td>
    		          <td>Back_dues/Arrear</td>
    		          <?php for($j=1;$j<=6;$j++){ ?>
    		            <td></td>
    		          <?php } ?>
    		      </tr>
    		      <?php for($i=2;$i<=13;$i++){
    		            if($i <= 10){
    		                $monthNum  = $i+2;
    		            } else {
    		                $monthNum  = $i-10;
    		            }
                        
                        $monthName = date('F', mktime(0, 0, 0, $monthNum, 10));
    		      ?>
    		      <tr>
    		          <td><?php echo $i; ?>.</td>
    		          <td><?php echo $monthName; ?></td>
    		          <?php for($j=1;$j<=6;$j++){ ?>
    		            <td></td>
    		          <?php } ?>
    		      </tr>
    		      <?php } ?>
    		      
    		      <tr>
    		          <td colspan="8">
    		              Comment (if Any):
    		          </td>
    		      </tr>
    		  </table>
    		  
            </div>
            
            <style>
                @media print {
                    .mydiv {page-break-after: always;}
                }
            </style>
            
        <?php } ?>
        	    </div>
        	</div>
	    </div>
	<?php
	}

?>

    
    <script>
    
    function printDiv1(divName) {
        var printContents = document.getElementById(divName).innerHTML;
        var originalContents = document.body.innerHTML;
        
        document.body.innerHTML = printContents;
        
        window.print();
        
        document.body.innerHTML = originalContents;
    }
    
    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>
    
    
    
    