<?php 

// getting branch details to use every where
$m->where("branch_id", $_SESSION['branch']);
$brid=$m->get("edu_branch_config");

// this is the code to check weather the branch is active it not then redirect it to login

 if($brid[0]['Status']==1 || $brid[0]['Status']=="1"){

     //echo "$branch[0]['Status']==1";

    $_SESSION['reasons']=$brid[0]['reasons'];

    unset($_SESSION['user_id']);

    session_destroy();

  }


// getting user details to use every where
$m->where("user_id", $_SESSION['user_id']);
$userProfile=$m->get("edu_user_config");


function getMoreFee($chalan,$m)
{
  $m->where("challan_id", $chalan);
  $m->where("branch_id", $_SESSION['branch']);
  $m->where("month_id", date("m"));
  $data=$m->get("eduportal_again_paid_challan");
 // print_r($data);
  return $data[0]['paid_amount'];
}


function CheckString($string,$m)
{

  if (stripos($string, 'select') !== false || stripos($string, 'update') !== false || stripos($string, 'insert') !== false || stripos($string, 'delete') !== false || stripos($string, '*') !== false || stripos($string, ';') !== false || stripos($string, 'drop') !== false || stripos($string, 'empty') !== false || stripos($string, 'aqibmaje_edup') !== false) {

    return '1';

    } else {

      return '0';

    }
}

function is_user_loggedIn($m){

    $user=$_SESSION['user_id'];            
    $branch=$_SESSION['branch'];
   
 $qr="SELECT * FROM users_privilege_detail as priv, eduportal_modules_pages_detail as pag WHERE priv.user_id='$user' and priv.page_id=pag.page_id and priv.branch_id='$branch' and priv.module_id=10 order by priv.privilege_id asc limit 1";

    $pages = $m->query($qr);

   header("location:".$pages[0]['page_name']."?".session_id().session_id().session_id()."&successlogin=1");

}





 function time_ago_in_php($timestamp){
  
  date_default_timezone_set("Asia/Karachi");         
  $time_ago        = strtotime($timestamp);
  $current_time    = time();
  $time_difference = $current_time - $time_ago;
  $seconds         = $time_difference;
  
  $minutes = round($seconds / 60); // value 60 is seconds  
  $hours   = round($seconds / 3600); //value 3600 is 60 minutes * 60 sec  
  $days    = round($seconds / 86400); //86400 = 24 * 60 * 60;  
  $weeks   = round($seconds / 604800); // 7*24*60*60;  
  $months  = round($seconds / 2629440); //((365+365+365+365+366)/5/12)*24*60*60  
  $years   = round($seconds / 31553280); //(365+365+365+365+366)/5 * 24 * 60 * 60
                
  if ($seconds <= 60){

    return "Just Now";

  } else if ($minutes <= 60){

    if ($minutes == 1){

      return "one minute ago";

    } else {

      return "$minutes minutes ago";

    }

  } else if ($hours <= 24){

    if ($hours == 1){

      return "an hour ago";

    } else {

      return "$hours hrs ago";

    }

  } else if ($days <= 7){

    if ($days == 1){

      return "yesterday";

    } else {

      return "$days days ago";

    }

  } else if ($weeks <= 4.3){

    if ($weeks == 1){

      return "a week ago";

    } else {

      return "$weeks weeks ago";

    }

  } else if ($months <= 12){

    if ($months == 1){

      return "a month ago";

    } else {

      return "$months months ago";

    }

  } else {
    
    if ($years == 1){

      return "one year ago";

    } else {

      return "$years years ago";

    }
  }
}

 
function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}

function utf8_encode_all($dat) // -- It returns $dat encoded to UTF8 
{ 
  if (is_string($dat)) return utf8_encode($dat); 
  if (!is_array($dat)) return $dat; 
  $ret = array(); 
  foreach($dat as $i=>$d) $ret[$i] = utf8_encode_all($d); 
  return $ret; 
} 
/* ....... */ 

function utf8_decode_all($dat) // -- It returns $dat decoded from UTF8 
{ 
  if (is_string($dat)) return utf8_decode($dat); 
  if (!is_array($dat)) return $dat; 
  $ret = array(); 
  foreach($dat as $i=>$d) $ret[$i] = utf8_decode_all($d); 
  return $ret; 
} 


function createStudentMonthlyChallan($std,$session,$class,$m)
{

    $data = array(
      'class_id' => utf8_decode_all($_REQUEST['class_id']),
      'std_tution_fee' => utf8_decode_all($_REQUEST['tution_fee']),
      'std_transport_fee' => utf8_decode_all($_REQUEST['transport_fee']),
      'std_misc_fee' => utf8_decode_all($_REQUEST['std_misc_fee']),
      'branch_id' => utf8_decode_all($_SESSION['branch']),
      'session_id' => utf8_decode_all($_REQUEST['session_id']),
      'student_id' => utf8_decode_all($_REQUEST['student_id']),
      'std_outstanding_blc' => utf8_decode_all($_REQUEST['outstanding']),
      'discount_reason' => utf8_decode_all($_REQUEST['discount_reason'])
    );

     $result = $m->insert('eduportal_student_fee_structure',$data);

     $recent= $m->getInsertId();

  
}




function totalStudents($m)
{

  $branch=$_SESSION['branch'];
  
  $sql="SELECT COUNT(*) as total FROM edu_student_details WHERE branch_id='$branch' and status='0' ";
  $records=$m->query($sql);
  
  return $records[0]['total'];
}



function todayAdmissions($m)
{

  $branch=$_SESSION['branch'];
  $date2=date('Y-m-d');
  $session=$_SESSION['current_session'];

  $sql="SELECT * FROM edu_student_details WHERE branch_id='$branch' and session_id='$session' and status='0' and entrance_date like '$date2%'";
  $today_admission=$m->query($sql);
  
  return count($today_admission);
}


function newCode($m)
{

//alert("786");

  $branchID=$_SESSION['branch'];
  $sql = "SELECT MAX(CAST(family_code AS UNSIGNED)) AS family_code FROM edu_student_details WHERE branch_id='$branchID'";

  //print_r($sql);
      $evt =$m->query($sql);

      $prev= $evt[0]['family_code'];
         
      $numb=$prev+1;      

  return $numb;
}

function todayStuckOff($m)
{

  $branch=$_SESSION['branch'];
  $date2=date('Y-m-d');
  $session=$_SESSION['current_session'];

  $sql="SELECT * FROM edu_student_details WHERE branch_id='$branch' and session_id='$session' and status='1' and expel_date='$date2'";
//print_r($sql);
  $todayStuck_off=$m->query($sql);
  
  return count($todayStuck_off);
}

function currentMonthStuckOff($m)
{

  $branch=$_SESSION['branch'];
  $date2=date('Y-m');
  $session=$_SESSION['current_session'];

  $sql="SELECT * FROM edu_student_details WHERE branch_id='$branch' and session_id='$session' and status='1' and expel_date like '$date2%'";
//print_r($sql);
  $currentMonthStuck_off=$m->query($sql);
  
  return count($currentMonthStuck_off);
}

function currentMonthAdmissions($m)
{

  $branch=$_SESSION['branch'];
  $date2=date('Y-m');
  $session=$_SESSION['current_session'];

  $sql="SELECT * FROM edu_student_details WHERE branch_id='$branch' and session_id='$session' and status=0 and entrance_date like '$date2-%'";
//print_r($sql);
  $currentMonth_admission=$m->query($sql);
  
  return count($currentMonth_admission);
}


function currentSessionAdmissions($m)
{

  $branch=$_SESSION['branch'];
  $date2=date('Y');
  $session=$_SESSION['current_session'];

  $sql="SELECT * FROM edu_student_details WHERE branch_id='$branch' and session_id='$session' and status=0 and entrance_date like '$date2-%'";
//print_r($sql);
  $currentSession_admission=$m->query($sql);
  
  return count($currentSession_admission);
}


function totalVisitedStudnets($m)
{

  $branch=$_SESSION['branch'];
  $date2=date('Y');
  $session=$_SESSION['current_session'];

  $sql="SELECT * FROM edu_student_details WHERE branch_id='$branch'";
//print_r($sql);
  $totalVisitedStudents=$m->query($sql);
  
  return count($totalVisitedStudents);
}


function totalContacts($m)
{

  $branch=$_SESSION['branch'];
  $date2=date('Y');
  $session=$_SESSION['current_session'];

  $sql="SELECT * FROM eduportal_contacts WHERE branch_id='$branch' and status='0'";
//print_r($sql);
  $totalContacts=$m->query($sql);
  
  return count($totalContacts);
}


function total_classSection_Strength($cls,$sec,$m)
{

  $m->where("section_id", $sec);
  $m->where("class_id", $cls);
  $m->where("status", 0);
  $m->where("branch_id", $_SESSION['branch']);
  /*$m->where("session_id", $_SESSION['current_session']);*/
  $total=$m->get("edu_student_details");

  return count($total);
}


function getClassName($m,$id)
{

  $m->where("branch_id", $_SESSION['branch']);
  $m->where("class_id", $id);
  $data=$m->get("edu_class_config");

  return $data[0]['Class_Name'];
}

function getSectionName($m,$id)
{

  $m->where("branch_id", $_SESSION['branch']);
  $m->where("section_id", $id);
  $data=$m->get("edu_adm_section_detail");

  return $data[0]['section_name'];
}

function getSessionName($m,$id)
{

  $m->where("session_id", $id);
  $data=$m->get("edu_session_config");

  return $data[0]['session_year'];
}


function getMonthName($m,$id)
{

  $m->where("Month_Id", $id);
  $data=$m->get("eduportal_month_config");

  return $data[0]['Month_Name'];
}

function getFeeOfOtherParameter($std,$chalan,$feetype,$m)
{
  $m->where("challan_id", $chalan);
  $m->where("feetype_id", $feetype);
  $m->where("student_id", $std);
  $data=$m->get("eduportal_monthly_challan_details");
  return $data[0]['fee_amount'];
 // print_r($data);
}

function getFeeAllOtherFee($std,$chalan,$m)
{

    $branch=$_SESSION['branch'];

    $qr="SELECT SUM(fee_amount) as total FROM eduportal_monthly_challan_details WHERE challan_id='$chalan'  and branch_id='$branch'
    AND student_id=".$std;

   $amount = $m->query($qr);
    return $amount[0]['total'];
}


function deleteFeeAllOtherFee($std,$chalan,$m)
{

    $branch=$_SESSION['branch'];

    $qr="Delete FROM eduportal_monthly_challan_details WHERE challan_id='$chalan'  and branch_id='$branch'
    AND student_id=".$std;

   $m->query($qr);
    
}





function getMonthlyRegularFee($session,$month,$feetype,$m)

{
    
 $branch=$_SESSION['branch'];
 
 $qr="SELECT SUM(fee.$feetype) as tution FROM eduportal_monthly_challan as fee,edu_student_details as std WHERE std.student_id=fee.student_id and std.status='0' and fee.month_id='$month'  and fee.branch_id='$branch' AND fee.session_id='$session'";
//print_r($qr);
  $amount = $m->query($qr);
  $fee=$amount[0]['tution'];

   return $fee;

}


function getMoreMonthsRegularFee($session,$from,$to,$feetype,$m)
{
    
 $branch=$_SESSION['branch'];
 
 $qr="SELECT SUM(fee.$feetype) as tution FROM eduportal_monthly_challan as fee,edu_student_details as std WHERE  std.student_id=fee.student_id and std.status='0'  and fee.branch_id='$branch' AND fee.session_id='$session' and fee.challan_duedate BETWEEN '$from' AND '$to'";
//print_r($qr);
  $amount = $m->query($qr);
  $fee=$amount[0]['tution'];

   return $fee;

}


function getTodayRegularFee($m)
{

 $session=$_SESSION['current_session'];
 $branch=$_SESSION['branch'];
 $todayDate=date("Y-m-d");
 
 $qr="SELECT SUM(fee.collected_amount) as collected FROM eduportal_monthly_challan as fee,edu_student_details as std WHERE std.student_id=fee.student_id and std.status='0' and fee.paying_date='$todayDate'  and fee.branch_id='$branch' AND fee.session_id='$session'";

  $amount = $m->query($qr);
  $fee=$amount[0]['collected'];

   return $fee;

}

function getAllotherFeeWithType_Summary($month,$feetype,$status,$m)
{
    $branch=$_SESSION['branch'];
    $qr="SELECT SUM(fee.fee_amount) as total FROM eduportal_monthly_challan_details as fee, eduportal_monthly_challan as cha, edu_student_details as std WHERE std.student_id=cha.student_id and std.status='0' and cha.month_id='$month' and cha.challan_id=fee.challan_id and cha.branch_id='$branch' and fee.feetype_id='$feetype' ";

   $amount = $m->query($qr);
    return $amount[0]['total'];
}


function getMoreAllotherFeeWithType_Summary($from,$to,$feetype,$status,$m)
{
    $branch=$_SESSION['branch'];
    $qr="SELECT SUM(fee.fee_amount) as total FROM eduportal_monthly_challan_details as fee, eduportal_monthly_challan as cha, edu_student_details as std WHERE std.student_id=cha.student_id and std.status='0' and cha.challan_id=fee.challan_id and cha.branch_id='$branch' and fee.feetype_id='$feetype' and cha.challan_duedate BETWEEN '$from' AND '$to'";

   $amount = $m->query($qr);
    return $amount[0]['total'];
}

function getAllotherFeeType($month,$feetype,$m)
{   $branch=$_SESSION['branch'];

    $qr="SELECT SUM(fee_amount) as total FROM eduportal_monthly_challan_details WHERE month_id='$month' and branch_id='$branch' and feetype_id='$feetype' ";

   $amount = $m->query($qr);
    return $amount[0]['total'];
}

function getAllotherUnPaidFeeWithType($month,$feetype,$status,$m)
{
    $branch=$_SESSION['branch'];
    $qr="SELECT SUM(fee.fee_amount) as total FROM eduportal_monthly_challan_details as fee, eduportal_monthly_challan as cha, edu_student_details as std WHERE std.student_id=cha.student_id and std.status='0' and cha.month_id='$month' and cha.challan_id=fee.challan_id and cha.branch_id='$branch' and fee.feetype_id='$feetype' and cha.paid_status='$status' ";

   $amount = $m->query($qr);
    return $amount[0]['total'];
}

function getAllotherFeeWithType($month,$from,$to,$feetype,$status,$m)
{
    $branch=$_SESSION['branch'];
    $qr="SELECT SUM(fee.fee_amount) as total FROM eduportal_monthly_challan_details as fee, eduportal_monthly_challan as cha, edu_student_details as std WHERE std.student_id=cha.student_id and std.status='0' and cha.month_id='$month' and cha.challan_id=fee.challan_id and cha.branch_id='$branch' and fee.feetype_id='$feetype' and cha.paid_status='$status'  AND cha.paying_date BETWEEN '" .$from. "' AND '" .$to. "' ";

   $amount = $m->query($qr);
    return $amount[0]['total'];
}

function getAllotherPaidFee($month,$from,$to,$status,$m)
{

  $branch=$_SESSION['branch'];
    $qr="SELECT SUM(fee.fee_amount) as total FROM eduportal_monthly_challan_details as fee, eduportal_monthly_challan as cha, edu_student_details as std WHERE std.student_id=cha.student_id and std.status='0' and cha.month_id='$month' and cha.branch_id='$branch' and cha.challan_id=fee.challan_id and cha.paid_status='$status' AND cha.paying_date BETWEEN '" .$from. "' AND '" .$to. "' ";

   $amount = $m->query($qr);
    return $amount[0]['total'];
}


function getCompleteOtherMonthlyFee($month,$m)
{

$curr_session=$_SESSION['current_session'];

    $qr="SELECT SUM(fee.fee_amount) as total FROM eduportal_monthly_challan_details as fee, eduportal_monthly_challan as cha, edu_student_details as std WHERE std.student_id=cha.student_id and std.status='0' and cha.session_id='$curr_session' and cha.month_id='$month' and cha.challan_id=fee.challan_id and cha.branch_id='".$_SESSION['branch']."' ";
//print_r($qr);
   $amount = $m->query($qr);
    return $amount[0]['total'];
}

function getBranch($m)
{

 $m->where("branch_id", $_SESSION['branch']);
 $branch=$m->get("edu_branch_config");

  return $branch;
}


function getGrade($avg,$m)
{

    $query="SELECT grade_id,grade_title FROM eduportal_exam_school_grades WHERE percantage_start_range <=".round($avg)." and percantage_end_range >=".round($avg)." and branch_id=".$_SESSION['branch'];


      $grades;

        if(is_nan($avg)){
          $grades="";
        }  

        else {

            $average =  $m->query($query);
            $grades= $average[0]['grade_title'];
        }


  return $grades."";
}


function getGradeRemarks($avg,$m)
{

    $query="SELECT * FROM eduportal_exam_school_grades WHERE percantage_start_range <=".round($avg)." and percantage_end_range >=".round($avg)." and branch_id=".$_SESSION['branch'];

      $response;

        if(is_nan($avg)){
          $response="";
        }  

        else {

            $average =  $m->query($query);
            $response=$average[0]['grade_title'].",".$average[0]['remarks'].",".$average[0]['teacher_remarks'];
        }


  return $response;
}




function getEmployeeAttendance($date,$emp,$m)
{


    $query="SELECT * from eduportal_hrm_emp_attendance WHERE emp_id ='$emp' and date ='$date' and branch_id=".$_SESSION['branch'];

     $average =  $m->query($query);

    return $average[0]['attendance'];
}



function totalStaff($m)
{

  $branch=$_SESSION['branch'];
  
  $sql="SELECT COUNT(*) as total FROM edu_user_config WHERE branch_id='$branch' and status='0' ";
  $records=$m->query($sql);
  
  return $records[0]['total'];
}





function getExpectedMonthlyFee($m)
{
 $branch=$_SESSION['branch'];
 $curr_session=$_SESSION['current_session'];

  
  $qr="SELECT SUM(fee.tutionfee) as tution,SUM(fee.transportfee) as trans,SUM(fee.total_fine) as fine, SUM(fee.balance) as balance,SUM(fee.misc_fee) as misc, SUM(disc_amount) as disc FROM eduportal_monthly_challan as fee,edu_student_details as std WHERE std.student_id=fee.student_id and std.status='0' and fee.branch_id='$branch' AND fee.session_id='$curr_session' AND fee.month_id=".date('m');
//print_r($qr);
  $amount = $m->query($qr);
  $total_exp=$amount[0]['tution']+$amount[0]['trans']+$amount[0]['fine']+$amount[0]['balance']+$amount[0]['misc']-$amount[0]['disc'];

   return $total_exp;

}

function getCurrentMonthFeeReceived($session,$month,$m)
{

 $branch=$_SESSION['branch']; 
 $from=date("Y-m-01");
 $to=date("Y-m-t");
  $qr="SELECT SUM(fee.collected_amount) as received, SUM(fee.paid_again_amount) as againPay, SUM(fee.collected_fine) as total_fine FROM eduportal_monthly_challan as fee,edu_student_details as std WHERE std.student_id=fee.student_id  and fee.branch_id='$branch' AND fee.session_id='$session' ";

  $qr .= " AND (fee.paying_date BETWEEN '" .$from. "' AND '" .$to. "' OR fee.paid_again_date BETWEEN '" .$from. "' AND '" .$to. "' )";
//print_r($qr);
  $amount = $m->query($qr);
  $received_amount=$amount[0]['received']+$amount[0]['total_fine']+$amount[0]['againPay'];

   return $received_amount;

}


function getCurrentMonthFeeActiveStdReceived($session,$month,$m)
{

 $branch=$_SESSION['branch']; 
 $from=date("Y-m-01");
 $to=date("Y-m-t");
  $qr="SELECT SUM(fee.collected_amount) as received, SUM(fee.paid_again_amount) as againPay, SUM(fee.collected_fine) as total_fine FROM eduportal_monthly_challan as fee,edu_student_details as std WHERE std.student_id=fee.student_id  and fee.branch_id='$branch' AND fee.session_id='$session' and std.status='0' ";

  $qr .= " AND (fee.paying_date BETWEEN '" .$from. "' AND '" .$to. "' OR fee.paid_again_date BETWEEN '" .$from. "' AND '" .$to. "' )";
//print_r($qr);
  $amount = $m->query($qr);
  $received_amount=$amount[0]['received']+$amount[0]['total_fine']+$amount[0]['againPay'];

   return $received_amount;

}


function getCurrentMonthFeeInactiveStdReceived($session,$month,$m)
{

 $branch=$_SESSION['branch']; 
 $from=date("Y-m-01");
 $to=date("Y-m-t");
  $qr="SELECT SUM(fee.collected_amount) as received, SUM(fee.paid_again_amount) as againPay, SUM(fee.collected_fine) as total_fine FROM eduportal_monthly_challan as fee,edu_student_details as std WHERE std.student_id=fee.student_id  and fee.branch_id='$branch' AND fee.session_id='$session' and std.status='1' ";

  $qr .= " AND (fee.paying_date BETWEEN '" .$from. "' AND '" .$to. "' OR fee.paid_again_date BETWEEN '" .$from. "' AND '" .$to. "' )";
//print_r($qr);
  $amount = $m->query($qr);
  $received_amount=$amount[0]['received']+$amount[0]['total_fine']+$amount[0]['againPay'];

   return $received_amount;

}


function getCurrentMonthFeeReceivedWithStruckOff($session,$month,$m)
{

 $branch=$_SESSION['branch']; 
  $qr="SELECT SUM(fee.collected_amount) as received, SUM(fee.paid_again_amount) as againPay, SUM(fee.collected_fine) as total_fine FROM eduportal_monthly_challan as fee,edu_student_details as std WHERE std.student_id=fee.student_id  and fee.branch_id='$branch' AND fee.session_id='$session' AND fee.month_id='$month' ";
//print_r($qr);
  $amount = $m->query($qr);
  $received_amount=$amount[0]['received']+$amount[0]['total_fine']+$amount[0]['againPay'];

   return $received_amount;

}


function getCommitment($session,$month,$m)
{

 $branch=$_SESSION['branch'];

 $date=date("Y-m-d");
   
  $qr="SELECT count(*) as total FROM eduportal_monthly_challan WHERE month_id='$month' and session_id='$session' and branch_id='$branch' and commitment_date > $date and commitment!=''"; 
 //print_r($qr);
   $result=$m->query($qr);
   $commitment=$result[0]['total'];
   
   return $commitment;

}

function getCurrentMonthFeeDiscount($session,$month,$m)
{

 $branch=$_SESSION['branch']; 
  $qr="SELECT SUM(fee.disc_amount) as disc FROM eduportal_monthly_challan as fee,edu_student_details as std WHERE std.student_id=fee.student_id and std.status='0' and fee.branch_id='$branch' AND fee.session_id='$session' AND fee.month_id='$month' ";

  $amount = $m->query($qr);
  $received_amount=$amount[0]['disc'];

   return $received_amount;

}



function today_Fee($m)
{
 $branch=$_SESSION['branch'];
 $curr_session=$_SESSION['current_session'];
 $date=date('Y-m-d');
  
  $qr="SELECT SUM(fee.tutionfee) as tution, SUM(fee.transportfee) as trans,SUM(fee.collected_fine) as fine, SUM(fee.balance) as balance, SUM(fee.collected_amount) as coll  FROM eduportal_monthly_challan as fee, edu_student_details as std WHERE fee.student_id=std.student_id and fee.branch_id='$branch' AND fee.session_id='$curr_session' AND fee.paying_date='$date'";
//print_r($qr);
  $amount = $m->query($qr);
  
  $recieved=$amount[0]['coll']+$amount[0]['fine'];

   return $recieved;

}


function today_PaidAgainFee($m)
{
 $branch=$_SESSION['branch'];
 $curr_session=$_SESSION['current_session'];
 $date=date('Y-m-d');
  
  $qr="SELECT SUM(fee.paid_again_amount) as againPay FROM eduportal_monthly_challan as fee, edu_student_details as std WHERE fee.student_id=std.student_id and fee.branch_id='$branch' AND fee.session_id='$curr_session' AND fee.paid_again_date='$date'";
//print_r($qr);
  $amount = $m->query($qr);
  
  $againrecieved=$amount[0]['againPay'];

   return $againrecieved;

}

function getCurrentMonthUsedSmsPercentage($m)
{
 $branch=$_SESSION['branch'];
 $curr_session=$_SESSION['current_session'];
 $date= date("Y-m");

 $m->where("branch_id", $_SESSION['branch']);
  $branch_record=$m->get("edu_branch_config");

  
  // getting other msgs
$qr_oth="SELECT SUM(msg_count) as msg_count FROM edu_messages_details WHERE branch_id='$branch' and other_msg='1' and msg_date like '$date-%' ";
$other_sms=$m->query($qr_oth);

// getting attendance msgs
$qr_att="SELECT SUM(msg_count) as msg_count FROM edu_messages_details WHERE branch_id='$branch' and attendance_msg='1' and msg_date like '$date-%' ";
$att_sms=$m->query($qr_att);

// getting result msgs
$qr_res="SELECT SUM(msg_count) as msg_count FROM edu_messages_details WHERE branch_id='$branch' and result_msg='1' and msg_date like '$date-%' ";
$result_sms=$m->query($qr_res);

// getting fee msgs
$qr_fee="SELECT SUM(msg_count) as msg_count FROM edu_messages_details WHERE branch_id='$branch' and fee_msg='1' and msg_date like '$date-%' ";
$fee_sms=$m->query($qr_fee);

  $total_sms=$att_sms[0]['msg_count']+$fee_sms[0]['msg_count']+$result_sms[0]['msg_count']+$other_sms[0]['msg_count'];

  $total_used_sms=$total_sms*100/$branch_record[0]['monthly_credit'];

   return $total_used_sms;

}

function getCurrentMonthFeeSms($m)
{
 $branch=$_SESSION['branch'];
 $date= date("Y-m");


// getting fee msgs
$qr_fee="SELECT SUM(msg_count) as msg_count FROM edu_messages_details WHERE branch_id='$branch' and fee_msg='1' and msg_date like '$date-%' ";
$fee_sms=$m->query($qr_fee);
$fees_sms=$fee_sms[0]['msg_count'];

   return $fees_sms;

}

function getCurrentMonthResultSms($m)
{
 $branch=$_SESSION['branch'];
 $date= date("Y-m");

// getting result msgs
$qr_res="SELECT SUM(msg_count) as msg_count FROM edu_messages_details WHERE branch_id='$branch' and result_msg='1' and msg_date like '$date-%' ";
$result_sms=$m->query($qr_res);
$results_sms=$result_sms[0]['msg_count'];
   return $results_sms;

}

function getCurrentMonthOtherSms($m)
{
 $branch=$_SESSION['branch'];
 $date= date("Y-m");

// getting other msgs
$qr_oth="SELECT SUM(msg_count) as msg_count FROM edu_messages_details WHERE branch_id='$branch' and other_msg='1' and msg_date like '$date-%' ";
$other_sms=$m->query($qr_oth);

$others_sms=$other_sms[0]['msg_count'];
   return $others_sms;

}

function getCurrentMonthAttendanceSms($m)
{
 $branch=$_SESSION['branch'];
 $date= date("Y-m");

// getting attendance msgs
$qr_att="SELECT SUM(msg_count) as msg_count FROM edu_messages_details WHERE branch_id='$branch' and attendance_msg='1' and msg_date like '$date-%' ";
$att_sms=$m->query($qr_att);

$attendance_sms=$att_sms[0]['msg_count'];
   return $attendance_sms;

}

function getTodayPresentStudentPercentage($m,$total_today)
{
 $branch=$_SESSION['branch'];
 $date=date('Y-m-d');


  $qr="SELECT count(*) as total FROM eduportal_attendance_detail WHERE attendance!='A'  and branch_Id='$branch' and date='$date'"; 
 
   $result=$m->query($qr);
   $p=$result[0]['total'];
   $present=($p*100/$total_today);

   return $present;

}

function todayPresentStudentsCount($m,$total_today)
{
 $branch=$_SESSION['branch'];
 $date=date('Y-m-d');
  
  $qr="SELECT COUNT(*) as total FROM eduportal_attendance_detail as att, edu_student_details as std WHERE std.student_id=att.student_id and std.status='0' and att.attendance!='A' and att.attendance!='L' and att.attendance!='LA' and att.branch_Id='$branch' and att.date='$date' and att.section_id!='0'"; 
 
   $result=$m->query($qr);
   $p=$result[0]['total'];
   
   return $p;

}

function getTodayEmployeeAbsentPercentage($m,$total_today)
{
 $branch=$_SESSION['branch'];
 $date=date('Y-m-d');
  
  $qr="SELECT count(*) as total FROM eduportal_hrm_emp_attendance WHERE attendance='A'  and branch_Id='$branch' and date='$date'"; 
 
   $result=$m->query($qr);
   $A=$result[0]['total'];
   $absent=($A*100/$total_today);

   return $absent;

}

function getTodayEmployeePresentPersentage($m,$total_today)
{
 $branch=$_SESSION['branch'];
 $date=date('Y-m-d');

  $qr="SELECT count(*) as total FROM eduportal_hrm_emp_attendance WHERE attendance='P'  and branch_Id='$branch' and date='$date'"; 
 
   $result=$m->query($qr);
   $p=$result[0]['total'];
   $present=($p*100/$total_today);

   return $present;

}


function getTodayAbsentStudentPercentage($m,$total_today)
{
 $branch=$_SESSION['branch'];
 $date=date('Y-m-d');

  $qr="SELECT count(*) as total FROM eduportal_attendance_detail WHERE attendance='A'  and branch_Id='$branch' and date='$date'"; 
 
   $result=$m->query($qr);
   $A=$result[0]['total'];
   $absent=($A*100/$total_today);

   return $absent;

}

function todayAbsentStudentsCount($m,$total_today)
{
 $branch=$_SESSION['branch'];
 $date=date('Y-m-d');
  
  $qr="SELECT count(*) as total FROM eduportal_attendance_detail WHERE attendance='A'  and branch_Id='$branch' and date='$date'"; 
 
   $result=$m->query($qr);
   $A=$result[0]['total'];
   
   return $A;

}

function todayLateStudentsCount($m,$total_today)
{
 $branch=$_SESSION['branch'];
 $date=date('Y-m-d');
  
  $qr="SELECT count(*) as total FROM eduportal_attendance_detail WHERE attendance='LA'  and branch_Id='$branch' and date='$date'"; 
 
   $result=$m->query($qr);
   $LA=$result[0]['total'];
   
   return $LA;

}

function todayLeaveStudentsCount($m,$total_today)
{
 $branch=$_SESSION['branch'];
 $date=date('Y-m-d');

   
  $qr="SELECT count(*) as total FROM eduportal_attendance_detail WHERE attendance='L'  and branch_Id='$branch' and date='$date'"; 
 
   $result=$m->query($qr);
   $L=$result[0]['total'];
   
   return $L;

}

function todayPresentEmployeesCount($m,$total_today)
{
 $branch=$_SESSION['branch'];
 $date=date('Y-m-d');

  
  $qr="SELECT count(*) as total FROM eduportal_hrm_emp_attendance WHERE attendance='P'  and branch_Id='$branch' and date='$date'"; 
 
   $result=$m->query($qr);
   $p=$result[0]['total'];
   
   return $p;

}

function todayPresentStaff($m,$total_today)
{
 $branch=$_SESSION['branch'];
 $date=date('Y-m-d');

  $qr="SELECT count(*) as total FROM eduportal_hrm_emp_attendance WHERE attendance='P'  and branch_Id='$branch' and date='$date'"; 
 
   $result=$m->query($qr);
   $present=$result[0]['total'];
   
   return $present;

}

function todayAbsentEmployees($m,$total_today)
{
 $branch=$_SESSION['branch'];
 $date=date('Y-m-d');

  $qr="SELECT count(*) as total FROM eduportal_hrm_emp_attendance WHERE attendance='A'  and branch_Id='$branch' and date='$date'"; 
 
   $result=$m->query($qr);
   $A=$result[0]['total'];
   
   return $A;

}

function todayLateEmployees($m,$total_today)
{
 $branch=$_SESSION['branch'];
 $date=date('Y-m-d');
  
  $qr="SELECT count(*) as total FROM eduportal_hrm_emp_attendance WHERE attendance='LA'  and branch_Id='$branch' and date='$date'"; 
 
   $result=$m->query($qr);
   $late=$result[0]['total'];
   
   return $late;

}

function getCurrentMonthTotalSms($m)
{
  $m->where("branch_id", $_SESSION['branch']);
  $branch_record=$m->get("edu_branch_config");
 
   
   $total_sms=$branch_record[0]['monthly_credit'];

   return $total_sms;

}
// function to get fee total of other fee paramerts

function getOtherFee_WithParam($param,$fe_date,$date,$branch,$m)
{
  $param="fe.".$param;
  $fe_date="fe.".$fe_date;
  $qr= "SELECT sum(".$param.") as adm FROM eduportal_student_fee_structure as fe, edu_student_details as st where fe.branch_id='$branch' and st.student_id=fe.student_id and st.status='0' and ".$fe_date." like '$date%'";
  $annual = $m->query($qr);
   
  return $annual[0]['adm'];
}




// this code is used to check the user pages access 

    $user=$_SESSION['user_id'];            
    $branch=$_SESSION['branch'];
    $pageName=basename($_SERVER['PHP_SELF']);

    $m->where("page_name", $pageName);
    $page=$m->get("eduportal_modules_pages_detail");
   //print_r($page);
  if(count($page)>0) {

     $qr="SELECT * FROM users_privilege_detail as priv, eduportal_modules_pages_detail as pag WHERE priv.user_id='$user' and priv.page_id=pag.page_id and pag.page_name='$pageName' and priv.branch_id='$branch'";
   //print_r($qr);
      $pagesss = $m->query($qr);
     // print_r(count($pagesss));
       if(count($pagesss)>0) {

      } else {
        header("location:access_denied.php");
      }
            

  } // ending first if



function randomPassword($m) {
    $alphabet = "0123456789";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 4; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}

/*function randomPassword($m) {
    $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 8; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}*/

function noOfParentsChild($number,$m)
{
  $branch=$_SESSION['branch'];

 $qr="SELECT count(*) as total FROM edu_student_details WHERE cell_no='$number' and status='0' and branch_id='$branch'";
 $result=$m->query($qr);
 $cell=$result[0]['total'];
  return $cell;
}

function getCurrentMonthExpense($session,$month,$m)
{
 $branch=$_SESSION['branch'];
 
  
  $qr="SELECT SUM(expense_amount) as expense FROM eduportal_expense_detail  WHERE branch_id='$branch' AND session_id='$session' AND month_id='$month' ";

  $amount = $m->query($qr);
  $total_expensse=$amount[0]['expense'];

   return $total_expensse;

}


function getTodayExpense($session,$month,$m)
{
 $branch=$_SESSION['branch'];
 
  $date=date('Y-m-d');
  $qr="SELECT SUM(expense_amount) as expense FROM eduportal_expense_detail  WHERE branch_id='$branch' AND session_id='$session' AND expense_date='$date' ";

  $amount = $m->query($qr);
  $today_expensse=$amount[0]['expense'];

   return $today_expensse;

}

function getPrintBottom($m)
{

 $data=$m->get("eduportal_setting");
  return $data[0]['bottom_print'];
}


?>