<?php
include("../db.php");
header("Access-Control-Allow-Origin: * ");
header('Content-Type: application/json');

date_default_timezone_set("Asia/Kolkata");

    $class = $_REQUEST['class'];
    $section = trim($_REQUEST['section']);
    $current_date = date('d-m-Y');

    $uid = $_REQUEST['uid'];
    $session = $_REQUEST['session'];

    $control_value = $db->getVal("SELECT online_class_fee_restriction from tbl_info");

    $sql = $db->getRows("select * from liveclass_meet where  class='$class'  and date >= '$current_date' order by date DESC");
    foreach($sql as $row)
	{
	 $d = $row["date"];
	 $t = $row["time"];
	 
	 $livetime = strtotime("$d $t");
	 $curtime  = time();
	 $difference = $curtime - $livetime;

	 $lower_limit = -120;
   //  $lower_limit = -21600; 

    if($difference >= $lower_limit && $difference <= /*2400*/28800) {
        $flag = 1;
    }
    else {
        $flag = 0;
    }


        $meet_account = $db->getRow("select * from meet_account where teacher_id='$row[teacher_id]' ");
	    
	    $sectionAry = $row['section'];
        if($section != ""){
            $sectionAry = json_decode($row['section'], true);
            
        if($sectionAry != ""){
            if(in_array("$section", $sectionAry)){
        	  $tname=  $row['teacher_id'];
            $techer=$db->getVal("select name from staff where idno='$tname'");

            	$jsonn[]=array("id" =>$row['id'],"teacher_id" =>$row['teacher_id'],"teacher_name" =>$techer,"class" =>$row['class'],"section" =>$row['section'],
                    "date" =>$row['date'],"time" =>$row['time'],"chapter" =>$row['chapter'],"topic" =>$row['topic'],"url" =>$row['url'],"status" =>$row['status'],"zoom_meeting_id" => $row['zoom_meeting_id'], "flag"=>$flag, 'sdk_key'=>$meet_account['sdk_key'], 'sdk_secret'=>$meet_account['sdk_secret']);

         	    $json['status']=1;
        		$json['msg']="successful";
        		$json['data']=$jsonn;

            }
        }
        else {
                    	  $tname=  $row['teacher_id'];
            $techer=$db->getVal("select name from staff where idno='$tname'");

            	$jsonn[]=array("id" =>$row['id'],"teacher_id" =>$row['teacher_id'],"teacher_name" =>$techer,"class" =>$row['class'],"section" =>$row['section'],
                    "date" =>$row['date'],"time" =>$row['time'],"chapter" =>$row['chapter'],"topic" =>$row['topic'],"url" =>$row['url'],"status" =>$row['status'],"zoom_meeting_id" => $row['zoom_meeting_id'], "flag"=>$flag, 'sdk_key'=>$meet_account['sdk_key'], 'sdk_secret'=>$meet_account['sdk_secret']);

         	    $json['status']=1;
        		$json['msg']="successful";
        		$json['data']=$jsonn;
        }

		}
		else {

        $tname=  $row['teacher_id'];
        $techer=$db->getVal("select name from staff where idno='$tname'");

    	$jsonn[]=array("id" =>$row['id'],"teacher_id" =>$row['teacher_id'],"teacher_name" =>$techer,"class" =>$row['class'],"section" =>$row['section'],
            "date" =>$row['date'],"time" =>$row['time'],"chapter" =>$row['chapter'],"topic" =>$row['topic'],"url" =>$row['url'],"status" =>$row['status'],"zoom_meeting_id" => $row['zoom_meeting_id'], "flag"=>$flag, 'sdk_key'=>$meet_account['sdk_key'], 'sdk_secret'=>$meet_account['sdk_secret']);

 	    $json['status']=1;
		$json['msg']="successful";
		$json['data']=$jsonn;
        }

	}
	
	    $monthsList1 = array(
        1   =>  'April'
    //     2   =>  'May',
    //     3   =>  'June',
    //     4   =>  'July ',
    //     5   =>  'August',
    //     6   =>  'September',
    //     7   =>  'October',
    //     8   =>  'November',
    //     9   =>  'December',
    // 	10  =>  'January',
    //     11  =>  'February',
    //     12  =>  'March'
    );

        // $tblinfo = $db->getRow("select due_month, resultview from tbl_info");    
        // $duemonth = $tblinfo['due_month'];
        
		$overAllTotal=0;$overAlltution=0;$overAllTransport=0;$overAllhotel=0;$overAllOther=0;$overAllOther_hostel=0;$overAllFine=0;$overAllextra=array();					
		$overAllNewextra=0;

		$aryFeeType=$db->getRows("select distinct(type) from extrafee_type where school_id='1' and session='$session' and status=1 order by type");

		$m=0;
// 		if($duemonth!=0){
// 			  if($duemonth<4){
// 				$m=($duemonth+9);
// 			  }
// 			  else{
// 				$m=$duemonth-3;
// 			  }
// 		}
// 		else{
// 			$m=12;
// 		}

        $m = 1;

            $fStudent = $db->getRow("select * from student where idno='$uid' and  school_id='1' and session='$session' and status=1 and issue_TC=0");

            	$i=0;$total_comp=0;
            	if(count($fStudent) > 1){
    //     			for($j=1; $j<=$m; $j++){
				// 		$md[]=$j;
				// 	}
				    $md = 1;
					 $mdf= implode(',',$md);

    				    $i++;
    					$mn=$m;

						$tution=0;$trasnport=0;$hostel=0;$otherExtra=0;$total=0;$fine=0;$total_full=0;
						$j=0;$other=0;$k=0;$total_extra=0;$count=0;
							$m1=$m+1;			
							foreach($monthsList1 as $key=>$val){$count++;

							if($mn===0){}
							else{ $mn--;
						$installTution=$db->getRow("select i".$key.",ip".$key." from installment where idno='".$fStudent["idno"]."'");
						if(is_array($installTution) && count($installTution)>0){
							if($installTution["ip$key"]=="" || $installTution["ip$key"]=="0")
							{
								$tution=$installTution["i$key"];
							}
							else
							{
                				if($m==$key)
                				{
                				$tution=$db->getVal("select (i".$m."-COALESCE(ip".$m.",0)) from installment where  idno='".$fStudent["idno"]."' ");
                				}
                                else
                                   {
                	             $tution=0;	
                                 }
							}
						}else{
							$tution="";
						}
						$overAlltution=$tution;

						$extrasum=array();
						if(is_array($aryFeeType) && count($aryFeeType)>0){
							foreach($aryFeeType as $ftype){  
								$sums=0;
								$Ftypeid=$db->getVal("select id from extrafee_type where type='".$ftype["type"]."' and class='".$fStudent["class"]."'");
        						$installTution=$db->getRow("select fee,pay from extrafee where type='".$Ftypeid."' and pid= 'p".$key."' and idno='".$fStudent["idno"]."'");
								$no=array();
								if(is_array($installTution) && count($installTution)>0){
									if($installTution["pay"]=="" || $installTution["pay"]==0)
									{
										$sums=$installTution["fee"];
									}
									else
									{
										if($m==$key)
                    				{
									$ext_last=$db->getVal("select sum(pay) from extrafee where idno='".$fStudent["idno"]."' and pid ='p".$key1."' ");
                                	if($ext_last==0)
									{	
									$sums=$db->getVal("select sum(fee-pay)  from extrafee where idno='".$fStudent["idno"]."' and pid ='p".$key."'  ");
									}
				}
                else
                   {
	             $sums=0;	
                     }
									}
									
								}else{
									$sums="";
								}
								$extrasum[]=$sums;
								$total_extra+=$sums;
								$tp=$ftype["type"];
								$overAllextra[$tp]+=$sums;
							}
						}

						 $overAllNewextra=0;
						for($ij=0; $ij<count($aryFeeType); $ij++){
						 $overAllNewextra+=$extrasum[$ij];
						}

						  $total=0;
						if($overAlltution=="0"&&$overAllNewextra==0)
						 {$total==0;}
						elseif($overAlltution==""&&$overAllNewextra=="")
						{$total="";}
						 else{ $total=$overAlltution+$overAllNewextra;}

		if($key=="12"){
		$total23=$db->getVal("select (COALESCE(i12,0)+COALESCE(b12,0)-COALESCE(ip12,0)-COALESCE(bp12,0)) from installment where idno='".$fStudent["idno"]."'");
		
		$total24=$db->getVal("select (COALESCE(i12,0)-COALESCE(ip12,0)) from hostelinstallment where idno='".$fStudent["idno"]."'");
		
		$te=$db->getVal("SELECT sum(fee-pay) FROM extrafee WHERE idno='$uid' and pid='p12' and session='$session'  ");
		
		$total22=$total23+$total24+$te;
		$total_full+=$total22;
			}
			else{ 
			$total_full=$total_full+$total;
    			 }
            }
		}
					$install=$db->getRows("select * from otherfee_structure where class='".$fStudent["class"]."' and  school_id='1' and (otype =  '' or otype = 'old' or otype = 'new' ) and month <= '".$m."' and session='$session'");

						if(is_array($install) && count($install)>0){
							$k=0;$c=0;$other2=0;
							foreach($install as $ins){$k+=2;$c++;
							if($fStudent['stype']=="old" && $ins['otype']=="new"){
								$over="";
							}
						   elseif($fStudent['stype']=="new" && $ins['otype']=="old"){
								$over="";
							}
							else{
							$installTution=$db->getRow("select * from installment_fine where idno=".$fStudent['idno']." and reasion = '".$ins['name']."'"); 
							$installTution1=$db->getRows("select * from installment_fine where idno=".$fStudent['idno']." and reasion = '".$ins['name']."' and id !=".$installTution['id']."");	$installTution2=$db->getRow("select * from otherfee_discount where idno=".$fStudent['idno']." and other_id='".$ins['id']."' and class='".$fStudent["class"]."' and  school_id='1' and session='$session' ");
							$t=0;$other1=0;$other_left=0;$dis=0;
							$t=$ins['fee'];
					 if(is_array($installTution2) && count($installTution2)>0){
									 $dis=$installTution2['discount'];
											if($t==$dis)
												$t=0;
											else 
												$t=$t-$dis;
							 }
							if(is_array($installTution) && count($installTution)>0){
								if($installTution['fine']!=""){
								if(is_array($installTution1) && count($installTution1)>0)
									{	foreach ($installTution1 as $instt)
											$other_left+=$instt['fine'];
									}
									 $other=$installTution['fine'];
																		
									if($other_left!=0 && $other!=$ins['fee'] && $installTution['id']!=$installTution1['id'])
									{$other+=$other_left;}
								}
								else
									$other=0;	
							}else{
								$other="";
							}
							$other1=$other;
														
							$overAllOther=$t - $other1;
						    $over=$overAllOther;
							$other2+=$over;
							
						}
							}	
						}
					$total_full+=$other2;

					$install=$db->getRow("select * from otherfee_structure where otype ='".$fStudent['idno']."' and name like 'b%' and  school_id='1' and session='$session' ");

					$discount= $db->getVal("SELECT discount from otherfee_discount  WHERE  idno='$fStudent[idno]' and other_id='$install[id]'   and session='".$session."' ");
					
					$install2=$db->getRow("select id from otherfee_structure where otype ='".$fStudent['idno']."' and name like 'Latefine%' and  school_id='1' and session='".$session."'");
					
					$discount2= $db->getVal("SELECT COALESCE(sum(discount),0) from otherfee_discount  WHERE  idno='$fStudent[idno]' and other_id in(select id from otherfee_structure where otype ='".$fStudent['idno']."' and name like 'Latefine%' and  school_id='1' and session='$session')   and session='$session' ");

							$dues=0;
							if(is_array($install) && count($install)>0){
								$installTution=$db->getRows("select * from installment_fine where idno=".$fStudent['idno']." and reasion = '".$install['name']."'"); 

								if(is_array($installTution) && count($installTution)>0){
									$total_dues=0;
									foreach($installTution as $ins){
										$total_dues+=$ins['fine'];
									}
									if($install['fee']==$total_dues)
										$dues=0;
									else $dues=$install['fee']-$total_dues-$discount;	

									$installTution_remain=$db->getVal("select sum(fine) from installment_fine where idno=".$fStudent['idno']." and reasion like 'remain%' ");
											$dues=$dues-$installTution_remain;
								}
							  else 		
									$dues=$install['fee']-$discount;
							}
							else
								$dues="";
							$alldues+=$dues;
							
							$total_full+=$dues;

					$install=$db->getRows("select * from hostel_otherfee_structure where class='".$fStudent["class"]."' and  school_id='1' and (otype =  '' or otype = 'old' or otype = 'new') and month <= '".$m."' and session='$session' ");

						if(is_array($install) && count($install)>0){
							$k=0;$b=0;$other2_hostel=0;
							foreach($install as $ins){$k+=2;$b++;
							if($fStudent['htype']=="old" && $ins['otype']=="new"){
								$over_hostel="";
							}
						   elseif($fStudent['htype']=="new" && $ins['otype']=="old"){
								$over_hostel="";
							}
							 elseif($fStudent['htype']==""){
								$over_hostel="";
							}
							else{
							$installTution=$db->getRow("select * from installment_fine where idno=".$fStudent['idno']." and reasion = '".$ins['name']."'"); 
							$installTution1=$db->getRows("select * from installment_fine where idno=".$fStudent['idno']." and reasion = '".$ins['name']."' and id !=".$installTution['id']."");	
							$installTution2=$db->getRow("select * from hostelotherfee_discount where idno=".$fStudent['idno']." and other_id='".$ins['id']."' and class='".$fStudent["class"]."' and  school_id='1' and session='$session' ");
							$t=0;$other1=0;$other_left=0;$dis=0;
							$t=$ins['fee'];
							 if(is_array($installTution2) && count($installTution2)>0){
									 $dis=$installTution2['discount'];
											if($t==$dis)
												$t=0;
											else 
												$t=$t-$dis;
							 }
							if(is_array($installTution) && count($installTution)>0){
								if($installTution['fine']!=""){
								if(is_array($installTution1) && count($installTution1)>0)
									{	foreach ($installTution1 as $instt) 
											$other_left+=$instt['fine'];
									}
									 $other=$installTution['fine'];
																		
									if($other_left!=0 && $other!=$ins['fee'] && $installTution['id']!=$installTution1['id'])
									{$other+=$other_left;}
								}
								else
									$other=0;	
							}else{
								$other="";
							}
							$other1=$other;
														
							$overAllOther_hostel=$t - $other1;
						    $over_hostel=$overAllOther_hostel;
							$other2_hostel+=$over_hostel;
							
						}
						}	
						}$total_full+=$other2_hostel; 
						$install=$db->getRows("select * from otherfee_structure where otype ='".$fStudent['idno']."' and name like 'Latefine%' and month <= '".$m."' and school_id='1' and session='$session' ");
							$dues=0;$total_dues=0;$fine_t=0;
							if(is_array($install) && count($install)>0){
								foreach($install as $ins){
									$fine_t+=$ins["fee"];
								$installTution=$db->getRows("select * from installment_fine where idno=".$fStudent['idno']." and reasion = '".$ins['name']."'");  
								      if(is_array($installTution) && count($installTution)>0){
											foreach($installTution as $ins){
												$total_dues+=$ins['fine'];
											}
									   }
									  else
										$dues=$ins['fee'];
								}
										if($fine_t==$total_dues)
												$dues=0;
								else $dues=$fine_t-$total_dues;	
										
						    }
							else
								$dues="";
							$alldues+=$dues;
							
							$total_full+=$dues-$discount2;
						
				 $install2=$db->getRow("SELECT otype,GROUP_CONCAT(id) as id FROM `otherfee_structure` where otype='".$fStudent['idno']."' and name like 'Latefine%'");

				$dis3= $db->getVal("SELECT sum(discount) from otherfee_discount  WHERE  idno='$fStudent[idno]' and other_id in($install2[id])   and session='$session' ");

				$to=$db->getVal("select sum(fee) from otherfee_structure where otype='".$fStudent['idno']."' and name!='Back_Dues' and name NOT like 'Latefine%' and month in ($mdf) and session='$session' ");

				$to1=$db->getVal("SELECT sum(amount) FROM reff_receipt WHERE idno='".$fStudent['idno']."' and session='$session' and type in (SELECT name FROM otherfee_structure WHERE otype='".$fStudent['idno']."' and name!='Back_Dues' and name NOT like 'Latefine%' and month in ($mdf))");

				$dis1=$db->getVal("SELECT sum(discount) FROM otherfee_discount WHERE idno='".$fStudent['idno']."' and session='$session' and other_id in (SELECT id FROM otherfee_structure WHERE otype='".$fStudent['idno']."' and name!='Back_Dues' and name NOT like 'Latefine%' and month in ($mdf))");

					 $t2=$to-$to1-$dis1;

					if($dues=="0")
					{
					 $finetotal=$dues;   
					}
					else
					{
					$finetotal=$dues-$dis3;    
					}
				 $total_comp+=$total_full+$t2;
        }
        $due = '';
        if($total_comp > 0) {
            $due = 1;
        } else {
            $due = 0;
        }

        //tbl_info control
        if ($control_value == 0){
            $due = 0;
        }

        $json['due'] = $due;
        $json['due_msg'] = "Dear Student, Please clear you dues of April to join class";

	if(is_array($jsonn) && count($jsonn)>0)
	{
		echo json_encode($json);
	}
	else
	{		
	 	echo json_encode(array('status' => 0, 'msg'=>'data not found..!'));
	}



