<?php 
if($_POST['update_ins']){
$aryData=$db->getRows("Select * from reff_receipt where session='2015-2016' ");
$i=0;
foreach($aryData as $ary){$i++;
$rem=0;$ins='';$next_ins=0;$val=0;
	$ary_ins=$db->getRow("Select * from installment where session='2015-2016' and idno=".$ary['idno']."");
	//print_r($ary);echo "<br/>";print_r($ary_ins);echo "<br/>";exit;
	if(is_array($ary_ins) && count($ary_ins)!=0){
		$ins_no_str=$ary['inst_id'];
		$ins_noary=explode(",",$ins_no_str);
		//print_r($ins_noary);
		$rem=0;
		foreach($ins_noary as $ins_no){
			$amount=0;
			if($ary['type']=="fee"){
						$ins='i'.$ins_no;
						$ins1='ip'.$ins_no;
					if($ary_ins[$ins1]==""){
									
									if($ary['amount']<=$ary_ins[$ins]){
										$rem=$ary_ins[$ins]-$ary['amount'];
										$next_ins=$ins_no+1;
										$val=$db->getval("select i".$next_ins." from installment where session='2015-2016' and idno=".$ary['idno']."");
										$val+=$rem;
										$ins_ary=array('i'.$next_ins=>$val);
										$inup=$db->updateAry("installment",$ins_ary," where session='2015-2016' and idno=".$ary["idno"]."");
										//echo $db->getLastQuery();	
									}
									if($ary['amount']>$ary_ins[$ins]){
										$ary['amount']=$ary['amount']-$ary_ins[$ins];
										$amount=$ary_ins[$ins];
									}
									elseif($ary['amount']<$ary_ins[$ins])
										$amount=$ary['amount'];
									else{
										$amount=$ary['amount'];
									}
										
								
								$reffarray=array('ip'.$ins_no=>$amount);
								//print_r($reffarray);
								$inup1=$db->updateAry("installment",$reffarray," where session='2015-2016' and idno=".$ary["idno"]."");		
								//echo $db->getLastQuery();	
					}
			}
			
			elseif($ary['type']=="busfee"){
				
						$ins='b'.$ins_no;
						$ins1='bp'.$ins_no;
					if($ary_ins[$ins1]==""){
									
									if($ary['amount']<=$ary_ins[$ins]){
										$rem=$ary_ins[$ins]-$ary['amount'];
										$next_ins=$ins_no+1;
										$val=$db->getval("select b".$next_ins." from installment where session='2015-2016' and idno=".$ary['idno']."");
										$val+=$rem;
										$ins_ary=array('b'.$next_ins=>$val);
										$inup=$db->updateAry("installment",$ins_ary," where session='2015-2016' and idno=".$ary["idno"]."");
										//echo $db->getLastQuery();	
									}
									if($ary['amount']>$ary_ins[$ins]){
										$ary['amount']=$ary['amount']-$ary_ins[$ins];
										$amount=$ary_ins[$ins];
									}
									elseif($ary['amount']<$ary_ins[$ins])
										$amount=$ary['amount'];
									else{
										$amount=$ary['amount'];
									}
										
								
								$reffarray=array('bp'.$ins_no=>$amount);
								//print_r($reffarray);
								$inup1=$db->updateAry("installment",$reffarray," where session='2015-2016' and idno=".$ary["idno"]."");		
								//echo $db->getLastQuery();	
					}
			
			
			}
	  }
	}

}
echo "data updated successfully..!";
}
elseif($_POST['make_ins']){
	
	$aryList=$db->getRows("select idno from student where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 and issue_TC=0 order by name");
   //echo "<pre>";print_r($aryList);echo "total-".count($aryList);exit;
if(is_array($aryList) && count($aryList)>0)
{
	foreach($aryList as $idList){
		
		$insTall=$db->getRow("select * from installment where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$idList['idno']."'");
		
		
		if(is_array($insTall) && count($insTall)>0){
		
		}else{
			
			$insAry=array(	"idno"=>$idList['idno'],
							"session"=>$_SESSION["admin"]["session"],
							"discount"=>0,
							"busdiscount"=>0
			);
			$total=0;
			$installDetail=$db->getRow("select * from class where school_id='".$_SESSION["admin"]["school_id"]."' and class=(select class from student where idno='".$idList['idno']."')");
			if(is_array($installDetail) && count($installDetail)>0)
			{
				$devidedFee=(int)($installDetail["fee"]-$_GET["dis"])/(int)$installDetail["installment"];
				for($i=1; $i<=$installDetail["installment"]; $i++){
					$total=$total+$devidedFee;
					$insAry["i".$i]=$devidedFee;
				}
				$insAry["total"]=$total;
			}else{
				echo "Class May Be delete or remove From database, Please re allocate class to this student<br />
				<br />
				Click <a href='?page_id=pay_fee'>here</a> to ho back<br />
				".$db->getLastQuery().$db->getErMsg();
				exit;
			}
			$total=0;
			$installDetail=$db->getRow("select * from bus where school_id='".$_SESSION["admin"]["school_id"]."' and id=(select bus from student where idno='".$idList['idno']."')");
			if(is_array($installDetail) && count($installDetail)>0)
			{
				$devidedFee=(int)($installDetail["fee"]-$_GET["bus"])/(int)$installDetail["installment"];
				for($i=1; $i<=$installDetail["installment"]; $i++){
					$insAry["b".$i]=$devidedFee;
					$total=$total+$devidedFee;
				}
				$insAry["bus"]=$total;
				//$db->insertAry("installment",$insAry);
			}
			$insAry["school_id"]=$_SESSION["admin"]["school_id"];
	$insAry["added_by"]=$_SESSION["admin"]["uid"];
			$db->insertAry("installment",$insAry);//echo $db->getErMsg();
			
			
			
			//echo "<pre>";print_r($insAry);echo "</pre>";
		}
		
	}
	echo "All students installment created successfully..!";
}
}

elseif($_POST['student_bpl']){
    $students = $db->getRows("select * from student where session='".$_SESSION["admin"]["session"]."' and bpl='1' ");

    foreach($students as $stu){
        // echo "$stu[idno] - $stu[name] - $stu[class] <br>";
        $checkins = $db->getRow("select * from installment where idno='$stu[idno]' and session='".$_SESSION["admin"]["session"]."' ");
        // if(count($checkins) > 1){ }else{
            // print_r($checkins);
            
            $classfee = $db->getRow("select * from class where class='$stu[class]'");
            
            $installmentfee = $classfee['fee']/$classfee['installment'];
            if($checkins["i1"]>0){
                for($i = 1; $i<=12; $i++){
                    $ins = $checkins["i$i"];
                    $updary = array("i$i"   =>  0);
                    $db->updateAry("installment", $updary, " where idno='$stu[idno]' ");
                    
            		$arySave = array(
                            "idno"              =>  $stu["idno"],
        					"discount_amount"   =>  $ins,
        					"installment"       =>  "i$i",
        					"type"              =>  "tution",
        					"other_id"          =>  "",
        					"session"           =>  $_SESSION["admin"]["session"],
                            "school_id"         =>  $_SESSION["admin"]["school_id"],
                            "added_by"          =>  $_SESSION["admin"]["uid"]
        	    		);
            		$db->insertAry("discount_detail",$arySave);

                }
                
            
            $all_other = $db->getRows("select * from otherfee_structure where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$stu["class"]."' and (otype='' OR otype='".$stu["stype"]."' OR otype='".$stu["idno"]."')");

            foreach($all_other as $other){
                $checkdis = $db->getVal("select sum(discount) from otherfee_discount where other_id='$other[id]' and idno='$stu[idno]' and session='".$_SESSION["admin"]["session"]."' ");
                if($checkdis > 0){ }else{
                    $aryData=array(
    						"other_id"  =>  $other["id"],
    						"idno"      =>  $stu["idno"],
    						"discount"  =>  $other["fee"],
    						"month"     =>  $other["month"],
    						"class"     =>  $stu["class"],
    						"section"   =>  $stu["section"],
    						"session"   =>  $_SESSION["admin"]["session"],
    		      			"school_id" =>  $_SESSION["admin"]["school_id"],
            				"added_by"  =>  $_SESSION["admin"]["uid"]						
    					 );
    			    $flgIn=$db->insertAry("otherfee_discount",$aryData);
    
            		$arySave=array(
                            "idno"              =>  $stu["idno"],
    						"discount_amount"   =>  $other["fee"],
    						"installment"       =>  "",
    						"type"              =>  "otherfee",
    						"other_id"          =>  $other["id"],
    						"session"           =>  $_SESSION["admin"]["session"],
      	                    "school_id"         =>  $_SESSION["admin"]["school_id"],
    	                    "added_by"          =>  $_SESSION["admin"]["uid"]
    		    		);
    	    		$db->insertAry("discount_detail",$arySave);
                }
            }
            
            $stuextrafee = $db->getRows("select * from extrafee where idno='".$stu['idno']."' and session='".$_SESSION["admin"]["session"]."' and school_id='".$_SESSION["admin"]["school_id"]."' and type in (select id from extrafee_type where class='".$stu["class"]."')");
                foreach($stuextrafee as $exfee){
                    $exins = str_replace("p","i",$exfee["pid"]);
                    $extracheck = $db->getVal("select discount_amount from discount_detail where idno='".$stu['idno']."' and installment='$exins' and other_id='$exfee[type]' and type='extra' and session='".$_SESSION["admin"]["session"]."' ");
                    if($extracheck>0){}else{

            		$arySave=array(
                            "idno"              =>  $stu["idno"],
    						"discount_amount"   =>  $exfee["fee"],
    						"installment"       =>  $exins,
    						"type"              =>  "extra",
    						"other_id"          =>  $exfee["type"],
    						"session"           =>  $_SESSION["admin"]["session"],
      	                    "school_id"         =>  $_SESSION["admin"]["school_id"],
    	                    "added_by"          =>  $_SESSION["admin"]["uid"]
    		    		);
    	    		$db->insertAry("discount_detail",$arySave);
                }
                
                $upary = array("fee"=>"0");

					$flgUp1 = $db->updateAry("extrafee", $upary, "where type='$exfee[type]' and pid='$exfee[pid]' and idno='".$stu['idno']."' and session='".$_SESSION["admin"]["session"]."' and school_id='".$_SESSION["admin"]["school_id"]."' ");
                }
            
            }

            
        // }
    }
}

elseif($_POST['gen_admin']){
	$aryList=$db->getRows("select idno from student");
	//print_r($aryList);echo "total-".count($aryList);exit;
	if(is_array($aryList) && count($aryList)>0)
	{	$i=0;
		foreach($aryList as $idList){$i++;
		  $reffarray=array('admissionno'=>$i);
									//print_r($reffarray);
									$inup1=$db->updateAry("student",$reffarray," where idno=".$idList["idno"]."");		
			}
	}
}
elseif($_POST['gen_pass']){
    $allStudents = $db->getRows("select * from student");
    foreach($allStudents as $stu){
	$db->updateAry("student",array("password"=>"mces".mt_rand(1000,9999)), "where idno='$stu[idno]' ");
    }
	echo "password updated successfully..!";
}elseif($_POST['hstl_update']){
	
	$liststd=$db->getRows("select idno,hid from hostelinstallment where session='".$_SESSION["admin"]["session"]."'");
$i=0;

foreach ($liststd as $list){$i++;;
	$insAry=array("hostel"=>$list['hid'],"session"=>$_SESSION["admin"]["session"]);
	//$ins=$db->updateAry();
$db->updateAry("student",$insAry,"where idno='".$list["idno"]."'");
//echo "<pre>";
//echo $db->getLastQuery();
	//echo "<pre>";print_r($insAry);
	
}	
echo "Updated successfully..!";}
elseif($_POST['make_hstl_ins']){

$liststd=$db->getRows("select idno,class,session,hostel from student where session='".$_SESSION["admin"]["session"]."' and hostel!=''");
$i=0;


foreach ($liststd as $list){$i++;
	//$hsdetil[]=$db->getRow("select id,fee,installment from hostel where id='".$list['hostel']."'");	
	//echo $db->getLastQuery();
	//echo "<br>".$list['hostel'];	
	//echo "<pre>";print_r($hsdetil);exit;
	//echo $list["idno"];
	$total=$db->getVal("select fee from hostel where id='".$list['hostel']."'");
						 
	  $instalmenthostelAry=array("idno"=>$list["idno"],
						 "class"=>$list["class"],
						 "hid"=>$list['hostel'],
						 "total"=>$total,
						 "discount"=>0,
						 'session'=>$_SESSION["admin"]["session"],
						 'school_id'=>$_SESSION["admin"]["school_id"],
						 'added_by'=>$_SESSION["admin"]["uid"],
						 "status"=>1
	);
			
			$installDetail=$db->getVal("select installment from hostel where id='".$list['hostel']."'");
				if(is_array($list) && count($list)>0)
			{
				$devidedFee=(int)($total)/(int)$installDetail;
				for($i=1; $i<=$installDetail; $i++){
					$instalmenthostelAry["i".$i]=$devidedFee;
					$total=$total+$devidedFee;
				}
				
				//$db->insertAry("installment",$insAry);
		
				
				$flgInstall=$db->insertAry("hostelinstallment",$instalmenthostelAry);
				
				$hostelName=$db->getVal("select name from hostel where id='".$list['hostel']."'");
				$checkRoom=$db->getRow("select id,seat from hostelroom where hostel='".$hostelName."'");
				$roomid=$checkRoom["id"];
				if(is_array($checkRoom) && count($checkRoom)>0){
					$db->updateAry("hostelroom",array("seat"=>($checkRoom["seat"]+1))," where id='".$roomid."'");
				}else{
					
					$roomid=$db->insertAry("hostelroom",array("seat"=>1,"hostel"=>$hostelName,"noofroom"=>100,"status"=>1,"school_id"=>$_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"]));
				}
				$roomAryD=array("roomid"=>$roomid,"idno"=>$_POST["idno"],"status"=>1,"school_id"=>$_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"]);
				$db->insertAry("roomallocation",$roomAryD);
			//echo $db->getLastQuery();
		
	
	}
	}
		echo "Hostel installment created successfully..!";
		
		}

elseif($_POST['gen_omr']){
	$aryList=$db->getRows("select idno from student");
	if(is_array($aryList) && count($aryList)>0)
	{	$i=300;
		foreach($aryList as $idList){$i++;
		        $reffarray=array('omr'=>$i);
				$inup1=$db->updateAry("student",$reffarray," where idno=".$idList["idno"]."");
		    }
	}
}

?>
<form method="post" action="">
    <input type="submit" name="make_ins" class="btn btn-new" value="make installment" />
    <input type="submit" name="update_ins" class="btn btn-new" value="Update installment" />
    <input type="submit" name="gen_admin" class="btn btn-new" value="Generate Admission No" />
    <input type="submit" name="gen_pass" class="btn btn-new" value="Update Password" />
    
    <input type="submit" name="hstl_update" class="btn btn-new" value="Hostel update" />
    <input type="submit" name="make_hstl_ins" class="btn btn-new" value="Make Hostel installment" />

    <input type="submit" name="gen_omr" class="btn btn-new" value="Generate OMR Number" />
    
    <input type="submit" name="student_bpl" class="btn btn-new" value="BPL Students" />
</form>
