<script src="<?php echo URL_ROOT; ?>js/jquery-1.10.2.js"></script>

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

if(isset($_GET['class']) && trim($_GET['class']) !='')
{
?>
<style>
    .st {
    	width: 55px;
    	font-size:10px;
    	display: inline-block;
    	color: #021EF0;
    }
    .size{
    	font-size:10px;
    }
    .recepit {
    	width: 717px !important;
    	height: 344px !important;
    }
    .unstyled{
    	margin:3px 0px 0px 0px;
    }
</style>
<?php
    $month=0;
    // if($_GET["month"]<=3){
    //     $month=$_GET["month"]+9;
    // }
    // else{
    //     $month=($_GET["month"]-3);
    // }
    
    $month = $_GET["month"];

    for($i=1; $i<=$month; $i++){
        $pm[]= "'".$i."'";
    }
    $pm1=implode(',',$pm);

    $where="";
    if(isset($_GET['idno']) && trim($_GET['idno']) !='')$where=" and idno=".$_GET['idno'];
    if(isset($_GET['section']) && trim($_GET['section']) !='')$where.=" and section='".$_GET['section']."'";
    $aryForms=$db->getRows("select *, REPLACE(sibling,idno,'') as sibling1 from student where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and (status=1 or status=2) and class='".$_GET["class"]."' $where order by fincome");

	if(is_array($aryForms) && count($aryForms)>0)
	{	$ij=0;
		foreach($aryForms as $aryForm)
		{ $ij++;
			$_GET["id"]=$aryForm["idno"]; $totalBus=0;$totalFee=0;
?>
<!-- Forms --> 
	
   
			<?php 
			//$sibling=$db->getVal("SELECT  concat(idno,sibling,'') as sibling1 from student  WHERE idno='".$aryForm['idno']."' and session='".$_SESSION["admin"]["session"]."' ");				
			$sibling=$aryForm['idno'];				
					
					//echo $sibling.'<br>'; ?>
                    <?php 
						
						$feeCount=array();$totalFee=0;$totalBus=0;$totalhostel=0;$extraFee=array();$totalExtra=0;$total=0;$dues=0;$duesother=0;$allmonths = array();
						$months = array();
						$totalother=$totalhostelother=0;
						//echo $month;
						for($i=1;$i<=$month;$i++){
							$iis="i".$i;
							$iip="ip".$i;
							
							
							$aryInstall=$db->getRow("select sum(".$iis.") as ".$iis." , sum(".$iip.") as ".$iip."  from installment where school_id='".$_SESSION["admin"]["school_id"]."' and idno in ($sibling) and session='".$_SESSION["admin"]["session"]."'");

							if($aryInstall[$iis]!="" && $aryInstall[$iis]!=0){
								//echo $aryInstall[$iis].'<br>';
								$totalclasspayfee=$totalclasspayfee+$aryInstall[$iip];
								$is_rr=false;
								// if($aryInstall[$iip]>0){}else{
									$totalFee+=$aryInstall[$iis]-$aryInstall[$iip];
									if(($aryInstall[$iis]-$aryInstall[$iip])>0){
									    array_push($allmonths,$i);
									}
								// }
							}
						}
						$ji=0;
						
						for($i=1;$i<=$month;$i++){
							$iis="b".$i;
							$iip="bp".$i;$is_rr=false;
							
							$aryInstall=$db->getRow("select sum(".$iis.") as ".$iis." , sum(".$iip.") as ".$iip."  from installment where school_id='".$_SESSION["admin"]["school_id"]."' and idno in ($sibling) and session='".$_SESSION["admin"]["session"]."'");
							if($aryInstall[$iis]!="" && $aryInstall[$iis]!=0){
								$totalbuspayfee=$totalbuspayfee+$aryInstall[$iip];$ji++;
								$is_rr=false;
								// if($aryInstall[$iip]>0){}else{
									  $totalBus+=$aryInstall[$iis]-$aryInstall[$iip];
									  if(($aryInstall[$iis]-$aryInstall[$iip])>0){
    									  if (!in_array($i, $allmonths)){
    									      array_push($allmonths,$i);
    									  }
									  }
								// }
							}
						}

						$aryInstall=$db->getRow("select * from hostelinstallment where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$aryForm["idno"]."' and session='".$_SESSION["admin"]["session"]."'");
						for($i=1;$i<=$month;$i++){
							$iis="i".$i;
							$iip="ip".$i;
							if($aryInstall[$iis]!="" && $aryInstall[$iis]!=0){
								$totalclasspayfee=$totalclasspayfee+$aryInstall[$iip];
								$is_rr=false;
								// if($aryInstall[$iip]>0){}else{
									$totalhostel+=$aryInstall[$iis]-$aryInstall[$iip];
									if(($aryInstall[$iis]-$aryInstall[$iip])>0){
    									  if (!in_array($i, $allmonths)){
    									      array_push($allmonths,$i);
    									  }
									  }
								// }
							}
						}

						$totalExt=0;

    $otherfeeary = array();

     $stype=$aryForm['stype'];
        if($stype=="old"){
            $aryother=$db->getRows("select  distinct(name),id,fee, month from otherfee_structure where (otype ='' or otype ='old' or otype='".$aryForm['idno']."') and month in($pm1) and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$aryForm["class"]."'");
        }
        elseif($stype=="new"){
        	 $aryother=$db->getRows("select  distinct(name),id,fee, month from otherfee_structure where (otype ='' or otype ='new' or otype='".$aryForm['idno']."') and month in($pm1) and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$aryForm["class"]."'");
        }

		if(is_array($aryother) && count($aryother)>0){
    		foreach($aryother as $other){

                $installOther=$db->getRows("select * from installment_fine where idno=".$aryForm['idno']." and reasion = '".$other['name']."'");
                $total_fine=0;
			    $installTution2=$db->getVal("select sum(discount) from otherfee_discount where idno=".$aryForm['idno']." and other_id='".$other['id']."' and class='".$aryForm["class"]."' and session='".$_SESSION["admin"]["session"]."'");
				$t=$other['fee'];
					//echo	$ss="select * from installment_fine where idno=".$aryForm['idno']." and reasion = '".$other['name']."'";
					//echo $fStudent['idno'];exit;	
					if($installTution2 >0){
                        $dis=$installTution2;

							if($other['fee']==$dis)
								$t=0;
							else 
								$t=$t-$dis;
					}
					if($t!=0 && $t<=$other['fee']){
							if(is_array($installOther) && count($installOther)>0){
									foreach($installOther as $installOther1){
										$total_fine+=$installOther1['fine'];	
										  if($total_fine==$t)	
												$duesother=0;
										   	
										  else
										  		$duesother=$t-$total_fine;	
							  		}
									if($total_fine!=$t)
										{$duesother=$t-$total_fine;}
									if(stripos($other["name"], "b") !== FALSE){
    									$installTution_remain=$db->getVal("select sum(fine) from installment_fine where idno=".$aryForm['idno']." and reasion like 'remain%' ");
    									$duesother=$duesother-$installTution_remain;
									}
							}
                            else
								$duesother=$t;
					}
					else{
						$duesother=0;
					}
						$totalother+=$duesother;

    	if($duesother!='0')
    	{
    	    $otherfeeary[$other['name']] = $duesother;
    	    
			  if (!in_array($other['month'], $allmonths)){
			      array_push($allmonths,$other['month']);
			  }

    	}
	} }

    // print_r($otherfeeary);
						?>
	<div id="hidediv_<?php echo $aryForm['idno']; ?>" style="width:48%; border:solid 1px #999;border-radius:10px;padding:10px;width:320px;display:inline-block;margin:10px;height:px;" >
<!--<div class="icardbg" style="border:solid 1px #999;border-radius:10px;padding:10px;width:250px;display:inline-block;margin:10px;">  !-->
<div class="headPrint" style="background: white; border-bottom: 4px solid black;"> 
<table>
        <tr>
          <td><img style="top:3px;position:relative;" src="<?php echo $LinksDetails["logo"] ?>" class="logo" height="50px" width="70px"></td>
           <td  style="text-align:center"><h5 style="margin:0; padding:0;"><?php echo ucfirst($LinksDetails["site_name"]) ?></h5>
            <span style="text-align:center;font-size:13px;"><?php echo ucfirst(nl2br($LinksDetails["contact_content"])); ?></span>
			</td>
        </tr>
      </table>
  </div>

    <table style='background: white;font-size:12px;'>

    <center><p><u>Demand Bill</u></p></center>

    <tr>
        <td><strong>Session</strong></td>
        <td><?php echo $_SESSION['admin']['session']; ?></td>
        <td><strong>Date</strong></td>
        <td><?php echo date('d-M-Y'); ?></td>
    </tr>
    <tr>
        <td><strong>Ledger No</strong></td><td><?php echo $aryForm["feducation"]; ?></td>

		<td><strong>Month</strong></td><td><?php
		  //$monthsList1 = array("4"=>"April",	"5"=>"May",	"6"=>"Jun",	"7"=>"July","8"=>"Aug","9"=>"Sep","10"=>"Oct","11"=>"Nov","12"=>"Dec","1"=>"Jan","2"=>"Feb","3"=>"March");
		foreach($allmonths as $m){
		  //  if($m <= 9){
		  //      $a = $m + 3 ;
		  //  } else { $a = $m - 9; }
    // 		echo $monthsList1[$a].", ";
        echo $db->getVal("select name from months where month='$m'").",";
		}
		
		?></td>
    </tr>
    <tr>
        <td><strong>Name</strong></td><td><?php echo $aryForm["name"]; ?></td>
        
       <td><strong>Class</strong></td><td><?php echo $aryForm["class"]; ?>-<?php echo $aryForm["section"]; ?></td>
    </tr>
    <tr>
        <td><strong>Father's</strong></td><td><?php echo $aryForm["fname"]; ?></td>
        <td><strong>Mother's</strong></td><td><?php echo $aryForm["mname"]; ?></td>
    </tr>
    
    <tr>
        <td><strong>Adm/No</strong></td><td><?php echo $aryForm["admissionno"]; ?></td>
        <td><strong>Roll No</strong></td><td><?php echo $aryForm["fincome"]; ?></td>
    </tr>
	<tr>
    <td colspan="4">					
    <?php
		$total_amount=0;
        echo "<table width='100%' border='1'>
    <tr>
    	<th>Sr. No</th><th>Fee Head</th><th>Payable amt</th>
    </tr>";

	echo "<th>1</th><th>Tuition</th><th>$totalFee</th></tr>";

	if($totalBus!='0')
	{
        $s=3;
        echo "<th>2</th><th>Transport</th><th>$totalBus</th></tr>";
	}
	else
	{
        $s=2;    
	}
	if($totalhostel!='0'){
	    echo "<th>3</th><th>Hostel</th><th>$totalhostel</th></tr>";
    }

  	$checkextFee=$db->getRows("select * from extrafee_type where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$aryForm["class"]."'");	
		if(is_array($checkextFee) && count($checkextFee)>0){
			foreach($checkextFee as $chkExtFee){
			   
				for($i=1;$i<=$month;$i++){
					$iip="p".$i;
					$mycheckInstall=$db->getRow("select fee,pay from extrafee where idno='".$_GET["id"]."' and type='".$chkExtFee["id"]."' and pid='".$iip."'");
					//echo "select fee,pay from extrafee where idno='".$_GET["id"]."' and type='".$chkExtFee["id"]."' and pid='".$iip."'";
					if($mycheckInstall["fee"]>0){
						$type=$chkExtFee["type"];
				// 		if($mycheckInstall["pay"]==0 || $mycheckInstall["pay"]=="")
				// 		{
						$extraFee[$type]=$extraFee[$type]+$mycheckInstall["fee"];
						//$totalExtra+=$totalExtra+$extraFee[$type];
					$totalExtra+=$mycheckInstall["fee"]-$mycheckInstall["pay"];
					$mycheckInstall["fee"];
					$mycheckInstall["pay"];
				// 		}
					}
				}
		 ?> <th><?php echo $s; ?></th><th><?php echo $type ?></th><th><?php echo $totalExtra; ?></th></tr>
		<?php
		$s++;
			    
			}
		}

/*
     $stype=$aryForm['stype'];
        if($stype=="old"){
            $aryother=$db->getRows("select  distinct(name),id,fee from otherfee_structure where (otype ='' or otype ='old' or otype='".$aryForm['idno']."') and month in($pm1) and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$aryForm["class"]."'");
        }
        elseif($stype=="new"){
        	 $aryother=$db->getRows("select  distinct(name),id,fee from otherfee_structure where (otype ='' or otype ='new' or otype='".$aryForm['idno']."') and month in($pm1) and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$aryForm["class"]."'");
        }

						if(is_array($aryother) && count($aryother)>0){$c=3;
					    		foreach($aryother as $other){$c++;

                                $installOther=$db->getRows("select * from installment_fine where idno=".$aryForm['idno']." and reasion = '".$other['name']."'");       	    $total_fine=0;
            				    $installTution2=$db->getVal("select sum(discount) from otherfee_discount where idno=".$aryForm['idno']." and other_id='".$other['id']."' and class='".$aryForm["class"]."' and  school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."'");
								$t=$other['fee'];
							//echo	$ss="select * from installment_fine where idno=".$aryForm['idno']." and reasion = '".$other['name']."'";
						//echo $fStudent['idno'];exit;	
							if($installTution2 >0){
										 $dis=$installTution2;

												if($other['fee']==$dis)
													$t=0;
												else 
													$t=$t-$dis;
							 }
					 if($t!=0 && $t<=$other['fee']){
							  if(is_array($installOther) && count($installOther)>0){
									foreach($installOther as $installOther1){
										$total_fine+=$installOther1['fine'];	
										  if($total_fine==$t)	
												$duesother=0;
										   	
										  else
										  		$duesother=$t-$total_fine;	
							  		}
									if($total_fine!=$t)
										{$duesother=$t-$total_fine;}
									if(stripos($other["name"], "b") !== FALSE){
									$installTution_remain=$db->getVal("select sum(fine) from installment_fine where idno=".$aryForm['idno']." and reasion like 'remain%' ");							  		
									$duesother=$duesother-$installTution_remain;
									}
							  }
							 
							   else 		
									$duesother=$t;
							
					 }
					 else{
						 $duesother=0;
					 }
						$totalother+=$duesother;

    	if($duesother!='0')
    	{
    		?>
    			<th><?php echo $c; ?></th><th><?php echo $other['name'] ?></th><th><?php echo $duesother; ?></th></tr>
    		<?php 
    	}
	} }

*/

        $c  =   3;
        foreach($otherfeeary as $key => $val){ $c++;
    		?>
    			<th><?php echo $c; ?></th><th><?php echo $key ?></th><th><?php echo $val; ?></th></tr>
    		<?php 
        }


    //hostelother
    $htype = $aryForm['htype'];
        if($htype=="old"){
            $aryhostelother=$db->getRows("select * from hostel_otherfee_structure where (otype ='' or otype ='old' or otype='".$aryForm['idno']."') and month <= '".$month."' and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$aryForm["class"]."'");
        }
	    elseif($htype=="new"){ 
            $aryhostelother=$db->getRows("select * from hostel_otherfee_structure where (otype ='' or otype ='new' or otype='".$aryForm['idno']."') and month <= '".$month."' and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$aryForm["class"]."'");
	    }
		else $aryhostelother=0;

        if(is_array($aryhostelother) && count($aryhostelother)>0){
	    	foreach($aryhostelother as $other){$c++;
				$installOther=$db->getRows("select * from installment_fine where idno=".$aryForm['idno']." and reasion = '".$other['name']."'");       	    $total_fine=0;
				$installTution2=$db->getRow("select * from otherfee_discount where idno=".$aryForm['idno']." and other_id='".$other['id']."' and class='".$aryForm["class"]."' and  school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."'");
				$t=$other['fee'];
					if(is_array($installTution2) && count($installTution2)>0){
                        $dis=$installTution2['discount'];
							if($other['fee']==$dis)
								$t=0;
							else 
								$t=$t-$dis;
                    }
                    if($t!=0 && $t<=$other['fee']){
                        if(is_array($installOther) && count($installOther)>0){
							foreach($installOther as $installOther1){
								$total_fine+=$installOther1['fine'];	
                                    if($total_fine==$t)	
										$duesother=0;
                                    else
								  		$duesother=$t-$total_fine;	
					  		}
							if($total_fine!=$t)
										{$duesother=$t-$total_fine;}
							if(stripos($other["name"], "b") !== FALSE){
									$installTution_remain=$db->getVal("select sum(fine) from installment_fine where idno=".$aryForm['idno']." and reasion like 'remain%' ");							  		$duesother=$duesother-$installTution_remain;
							}
					    }
					   else $duesother=$t;
					}
					 else{
						 $duesother=0;
					 }
						$totalhostelother+=$duesother;
						?>
		    			<th><?php echo $c; ?></th><th><?php echo $other['name'] ?></th><th><?php echo $duesother; ?></th></tr>
						<!--<th class="hostelother<?php echo $ij.$c ?>"> <?php echo $duesother; ?></th>-->
				     <?php } }

           $total_amount=$totalFee+$totalBus+$totalhostel+$totalother+$totalhostelother+$totalExtra;

	echo "<tr><th></th><th>Total  Amount</th><th>".$total_amount."</th></tr>";

if($total_amount <1){
    ?>
    <script>
        document.getElementById('hidediv_'+<?php echo $aryForm['idno']; ?>).style.display = "none";
    </script>
    <?php 
}

    echo "</table><hr/><div class='footerPrint'><span>Signature...............................</span></div>";//.$isAlreadyEntry;
?>
    	</td></tr>
    </table>					

</div>         
<?php
		}
	}
}
?>
<!-- / Forms -->
<script type="text/javascript">
window.print() ;
</script>