<?php
//insert_class_routine.php
date_default_timezone_set("Asia/Dhaka");
include 'config.php';




if(isset($_POST["st_class"])){

    //st_class:st_class,toMonth:toMonth ,toYear:toYear,item_name:item_name,fld_item:fld_item,datetxt:datetxt
    
    $month_to=$_POST['toMonth'];  
$year_to=$_POST['toYear'];

$to_month_year= $month_to."-".$year_to;

$st_class=$_POST['st_class'];
    $item_name=$_POST['item_name'];
    $fld_item=$_POST['fld_item'];
    $datetxt=$_POST['datetxt'];


    $date = date_parse($month_to);
    $month_number=$date['month'];
  //echo $month_number;


  //$month = date('m');
  //echo $month;



    function item_val($stid){

        include 'config.php';
        $fld_item=$_POST['fld_item'];
        for($j=0;$j<count($fld_item);$j++){
          if($fld_item[$j]=='due_payments'){
            $due='due';
            $fld_it.=$due.",";
          }else{
            $fld_it.=$fld_item[$j].",";
          }  
            
        }   
        $fld_itm=rtrim($fld_it,",");
        //echo $fld_itm;
        //$fld_itmmm="tuition_fee,class_absent_fee,session_charge";

      $query="SELECT 
      $fld_itm 
      FROM
      student_list_ju
      WHERE
      st_id='$stid'
      ";
    $result=mysqli_query($link,$query);
    $numrow=mysqli_num_rows($result);
    if ($numrow>0){
        $roww = mysqli_fetch_array($result);
        return $roww;
    }else{
        return 0;
    }
    }
    




    function item_month($stid){

        include 'config.php';
        $month_to=$_POST['toMonth'];  
        $year_to=$_POST['toYear'];
        $date = date_parse($month_to);
        $to_month_num=$date['month'];

    $query="SELECT 
    last_month_of_tuiton_fee,
    last_paid_year,absent_per_month,st_name,father_mobile,st_class
    FROM
    student_list_ju
    WHERE
    st_id='$stid'
      ";
    $result=mysqli_query($link,$query);
    $numroww=mysqli_num_rows($result);
    if ($numroww>0){
        $rows = mysqli_fetch_array($result);
        $from_month=$rows[0];
        $datee = date_parse($from_month);
        $from_month_num=$datee['month'];

        $from_year=$rows[1];

        $totalMonth=($to_month_num-$from_month_num) +($year_to- $from_year)*12;
        $thismonth = date('m');
        $thisyear =date("Y");
        $totalLatemonth=($thismonth-$from_month_num-1) +($thisyear-$from_year)*12;
if ($totalMonth<0){
$total_month=0;
$total_latemonth=0;
}else{
    $total_month=$totalMonth;
    if($totalLatemonth<0){
        $total_latemonth=0;
    }else{
        $total_latemonth=$totalLatemonth;
    }
    
}

$total_absent=$rows[2];
$st_name=$rows[3];
$st_mobile=$rows[4];
$st_class=$rows[5];
$total_month_latemonth_absent_array=array($total_month,$total_latemonth,$total_absent,$st_name,$st_mobile,$st_class);

        return $total_month_latemonth_absent_array;
    }else{
        return 0;
    }
    }







    $st_clas='';
    for($i=0;$i<count($st_class);$i++){
        
        $st_clas.="'".$st_class[$i]."'".",";
    
    }
    $fld_class=rtrim($st_clas,",");
    //$fld_class=rtrim($st_clas,",");
    //echo $fld_class;

///-------student id collect--------------------

$query="SELECT
st_id
FROM
student_list_ju
WHERE
st_class IN ($fld_class)
AND active_inactive='ACTIVE'
ORDER BY st_class ASC,st_group DESC, st_id ASC
";
$result=mysqli_query($link,$query);

$numrow=mysqli_num_rows($result);
if ($numrow > 0 ) {

$data=[];
while($row = mysqli_fetch_array($result)){

$data[] = $row;

}
}
///----------------------------

//print_r($data);
//echo $data[5][0];
$nCount=0;
for($i=0;$i<count($data);$i++){
    $dataval=$data[$i][0];
    //echo $dataval;
    $fldarry=item_val($dataval);
    //print_r($fldarry) ;
    $total_month_latemonth_absent_array=item_month($dataval);
    //echo $total_month_num;
    $sum=0;
    
    for($j=0;$j<count($fld_item);$j++){

        $fld_itemname=$fld_item[$j];
if($fld_itemname=='tuition_fee'){
    $tuition_fee=$fldarry[$fld_itemname]*$total_month_latemonth_absent_array[0];
    $tuition_fee_format=sprintf('%0.2f', $tuition_fee);
    $item_nam.=$item_name[$j]."=".$tuition_fee_format."\n";
    $sum+= $tuition_fee;
}else if($fld_itemname=='late_fee'){
    $late_fee=$fldarry[$fld_itemname]*$total_month_latemonth_absent_array[1];
    $late_fee_format=sprintf('%0.2f', $late_fee);
    $item_nam.=$item_name[$j]."=".$late_fee_format."\n";
    $sum+= $late_fee;
}else if($fld_itemname=='class_absent_fee'){
    $class_absent_fee=$fldarry[$fld_itemname]*$total_month_latemonth_absent_array[2];
    $class_absent_fee_format=sprintf('%0.2f', $class_absent_fee);
    $item_nam.=$item_name[$j]."=".$class_absent_fee_format."\n";
    $sum+= $class_absent_fee;
}else if($fld_itemname=='due_payments'){
    $fld_val=$fldarry['due'];
    $item_nam.=$item_name[$j]."=".$fld_val."\n";
    $sum+= $fld_val;
}else if($fld_itemname=='online_charge'){
        $online_fee=$fldarry[$fld_itemname]*$total_month_latemonth_absent_array[0];
        $online_fee_format=sprintf('%0.2f', $online_fee);
        $item_nam.=$item_name[$j]."=".$online_fee_format."\n";
        $sum+= $online_fee;

}else{
$fld_val=$fldarry[$fld_itemname];
    $item_nam.=$item_name[$j]."=".$fld_val."\n";
    $sum+= $fld_val;
}



        //echo $fld_itemname;
        
    } 
    $sum_format=sprintf('%0.2f', $sum);
    $fld_sms=rtrim($item_nam,"\n"); 
    $sms_fees= "ID: ".$dataval."\nName: ".$total_month_latemonth_absent_array[3]."\nDue Until ".$to_month_year."\n".$fld_sms."\nTotal=".$sum_format."\nPayment Date:".$datetxt;
    
    $nCount++;
       //<td>'.$nCount.'</td>
        echo '<tr>
        
        <td><input type="checkbox" id="checkAll" name="selected_id[]" class="checkboxsms" data-st_mobile="'.$total_month_latemonth_absent_array[4].'" data-txtsms="'.$sms_fees.'" value="'.$total_month_latemonth_absent_array[4].'"> </td>
        <td>'.$dataval.'</td>
        <td>'.$total_month_latemonth_absent_array[3].'</td>
        <td>'.$total_month_latemonth_absent_array[5].'</td>
        <td class="st_absent">'.$total_month_latemonth_absent_array[4].'</td>
        <td>'.$nCount.'</td>
        
        </tr>';
    
    
    
    
    
    
    
    
    //echo $fldarry[0];
    $item_nam='';
    $fldarry=[];
    $total_month_latemonth_absent_array=[];
    $sum_format=0;
    //$total_month_num=[];
}



}
?>