<?php
include_once("../public/config.php");
$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date('Y-m-d');
$userid = $_SESSION['userid'];
if ($do == 'checkcolumn') {
  checkcolumn();
}else if ($do == 'getselectjoin') {
getselectjoin();
}else if ($do == 'removeteble') {
  removeteble();
}else if ($do == 'getselect') {
  getselect();
}else if ($do == 'getselectparent') {
getselectparent();
}else if ($do == 'getselectmultiple') {
  getselectmultiple();
}else if ($do == 'receivedata') {
  receivedata();
}else if ($do == 'receivestudent') {
  receivestudent();
}else if ($do == 'payed') {
   $today = date("Y-m-d H:i:s");
    $date = date("Y-m-d");
    $times = date("H:i:s");
    $day = date('l');
    $userid = $_SESSION['userid'];
    $branchiduser = $_SESSION['branchId'];
    $studentid = filter_input(INPUT_GET, 'studentid');
    $deviceid = filter_input(INPUT_GET, 'deviceid');
    $uniqid = time();
    
    if($studentid && !is_numeric($studentid)){
        $smarty->assign('notstudent', 3);   
    }else if($studentid && is_numeric($studentid)){
    $studentabsencescountday = R::count('studentabsences', "studentid = ?  and todaysend = '$date' and del < 2", [$studentid]);  
    $smarty->assign('studentabsencescountday', $studentabsencescountday); 
    $studentabsencescount = R::count('studentabsences', "studentid = ? and deviceid = ? and todaysend = '$date' and del < 2", [$studentid, $deviceid]);
     $students = R::findOne('students','id = ? and del < 2 ',[$studentid]);
     
     $smarty->assign('students', $students); 
     if ($students->branchid || $branchstudent == 0) {
           $branchstudent = R::getRow("SELECT * FROM `branch` WHERE branchId = $students->branchid ");
           if ($students->branchid == $branchiduser || $branchiduser == 0) {
               $smarty->assign('statusbranch', 1);
           }else{
               $branchuser = R::getRow("SELECT * FROM `branch` WHERE branchId = $branchiduser ");
               $smarty->assign('statusbranch', 2);
               $smarty->assign('branchuser', $branchuser);
           }
    }
    $smarty->assign('branchstudent', $branchstudent);
    $studentsubjects = R::findOne('studentsubjects','deviceid = ? ',[$deviceid]);
    $studentspays = R::findOne('studentspays',"studentid = ? and studentsubjectid = ? and del < 2 and payenddate >= '$date' order by id desc",[$studentid, $studentsubjects->id]);
    if ($studentspays->id < 1) {
        $smarty->assign('status', 2);
    }else{
        $smarty->assign('status', 1); 
    }
    $studentspayslast = R::findOne('studentspays',"studentid = ? and studentsubjectid = ? and del < 2  order by id desc",[$studentid, $studentsubjects->id]);
    $smarty->assign('studentspayslast', $studentspayslast);
    // && 
    if($studentabsencescount == 0  ){ 
           $studentaddsubjectdetails = R::findOne('studentaddsubjectdetails','studentid = ? and studentsubjectid = ? and del < 2  order by id desc',[$studentid, $studentsubjects->id]);
            $studentabsences = R::dispense('studentabsences');
            $studentabsences->onlys = 0;
            $studentabsences->del = 0;
            $studentabsences->arrivedtime = 0;
            $studentabsences->addtoday = $today;          
            $studentabsences->adduserid = $_SESSION['userid'];
            $studentabsences->uniqid = time();
            $studentabsences->studentid = $studentid;
            $studentabsences->todaysend = $date;
            $studentabsences->timesend = $times;
            $studentabsences->type = 1;
            $studentabsences->deviceid = $deviceid;
            $studentabsences->status = 1;
            $studentabsences->studentsubjectid = $studentaddsubjectdetails->studentsubjectid;
            $studentabsences->studentsubjectgroupid = $studentaddsubjectdetails->studentsubjectgroupid;
            $studentabsences->studentaddsubjectid = $studentaddsubjectdetails->studentaddsubjectid;
            $studentabsences->studentaddsubjectdetailid = $studentaddsubjectdetails->id;
            R::store($studentabsences); 
            
    }
     $smarty->assign('notstudent', 1);   
    }else{
      $smarty->assign('notstudent', 2);   
    }

 
    $smarty->display("studentview/notpayed.html"); 
}else if ($do == 'detailstudent') {

    $studentid = filter_input(INPUT_GET, 'studentid');
    $first_date = date('Y-m-d',strtotime('first day of this month'));
    
    $students = R::findOne('students','id = ? and del < 2 ',[$studentid]);
     $smarty->assign('students', $students); 
    $studentabsences = R::findAll('studentabsences', "studentid = $studentid and todaysend >= '$first_date' and del < 2");
    $smarty->assign('studentabsences', $studentabsences); 
    
    $studentspay = R::findOne('studentspays','studentid = ? and del < 2 order by id desc',[$studentid]);
     $smarty->assign('studentspay', $studentspay); 

  
    $smarty->display("studentview/studentparcode.html");     
}else if ($do == 'detailbranches') {
    $today = date("Y-m-d");
    $branchs = R::getAll('SELECT branch.* FROM `branch` WHERE branchId > 0 ');
    $saves = R::getAll('SELECT * FROM `save` WHERE savecurrentvalue > 0 and conditions = 0  and saveid != 1');
     $smarty->assign('saves', $saves);
    $branches = json_decode (json_encode ($branchs), FALSE);
     $allcgstudentabsencesd = 0;
    $allcstudentspaysd = 0;
    foreach($branches as $branch){
        $crightstudentspays = R::count('studentspays'," left join students on studentspays.studentid = students.id WHERE studentspays.del < 2 and students.branchid = ? 
        and studentspays.payenddate  >= '$today' ", [$branch->branchId]);
        $branch->crightstudentspays =  $crightstudentspays;
        
        $cgstudentabsences = R::getcell("SELECT COUNT(*) FROM ( SELECT DISTINCT studentid FROM `studentabsences` left join students on studentabsences.studentid = students.id WHERE students.branchid = $branch->branchId and studentabsences.todaysend = '$today' ) as totals ");
        $branch->cgstudentabsences =  $cgstudentabsences;
        $allcgstudentabsencesd += $cgstudentabsences;
        
        // $studentabsences = R::count('studentabsences'," left join students on studentabsences.studentid = students.id WHERE students.branchid = ? and studentabsences.todaysend = '$today' ", [$branch->branchId]);
        // $branch->cstudentabsences =  $studentabsences;
        
        $cstudentspays = R::count('studentspays'," left join students on studentspays.studentid = students.id WHERE studentspays.del < 2 and students.branchid = ? 
        and studentspays.addtoday  >= '$today 00-00-00' and studentspays.addtoday  <= '$today 23-59-55'  ", [$branch->branchId]);
        $branch->cstudentspays =  $cstudentspays;
        $allcstudentspaysd += $cstudentspays ;
        
        $totalstudentspays = R::getcell("SELECT SUM(studentspays.payprice) FROM `studentspays` LEFT JOIN students ON  studentspays.studentid = students.id WHERE studentspays.del < 2 
        and students.branchid = ?  and studentspays.addtoday  >= '$today 00-00-00' and studentspays.addtoday  <= '$today 23-59-55' ", [$branch->branchId]);
        $branch->totalstudentspays = (int)  $totalstudentspays;
    }
    $smarty->assign('branches', $branches);
     $smarty->assign('allcgstudentabsencesd', $allcgstudentabsencesd);
      $smarty->assign('allcstudentspaysd', $allcstudentspaysd);
    
    $month = filter_input(INPUT_POST, 'months')?: date('m');
    $year = filter_input(INPUT_POST, 'years') ?: date('Y');
    $smarty->assign("year", $year);
    $smarty->assign("month", $month);
    $start_date = "01-".$month."-".$year;
    $start_time = strtotime($start_date);
    $end_time = strtotime("+1 month", $start_time);
    $allmonth = array();
    //   $allscrightstudentspays = 0;
    // $allscgstudentabsences = 0;
    $allscstudentspays = 0;
    $allstotalstudentspays = 0;
    for($i=$start_time; $i<$end_time; $i+=86400)
    {
        $today = date('Y-m-d', $i); 
        $branchs = R::getAll('SELECT branch.* FROM `branch` WHERE branchId > 0 ');
        $branches = json_decode (json_encode ($branchs), FALSE);
      
        // $allcrightstudentspays = 0;
        $allcgstudentabsences = 0;
        $allcstudentspays = 0;
        $alltotalstudentspays = 0;
        foreach($branches as $branch){
            $crightstudentspays = R::count('studentspays'," left join students on studentspays.studentid = students.id WHERE studentspays.del < 2 and students.branchid = ? 
            and studentspays.payenddate  >= '$today' ", [$branch->branchId]);
            $branch->crightstudentspays =  $crightstudentspays;
            $allcrightstudentspays += $crightstudentspays;
            
            $cgstudentabsences = R::getcell("SELECT COUNT(*) FROM ( SELECT DISTINCT studentid FROM `studentabsences` left join students on studentabsences.studentid = students.id WHERE students.branchid = $branch->branchId and studentabsences.todaysend = '$today' ) as totals ");
            $branch->cgstudentabsences =  $cgstudentabsences;
            
            // $cstudentabsences = R::count('studentabsences'," left join students on studentabsences.studentid = students.id WHERE students.branchid = ? and studentabsences.todaysend = '$today' ", [$branch->branchId]);
            // $branch->cstudentabsences =  $cstudentabsences;
            
            $allcgstudentabsences += $cgstudentabsences;
            $cstudentspays = R::count('studentspays'," left join students on studentspays.studentid = students.id WHERE studentspays.del < 2 and students.branchid = ? 
            and studentspays.addtoday  >= '$today 00-00-00' and studentspays.addtoday  <= '$today 23-59-55'  ", [$branch->branchId]);
            $branch->cstudentspays =  $cstudentspays;
             $allcstudentspays += $cstudentspays ;
            
            $totalstudentspays = R::getcell("SELECT SUM(studentspays.payprice) FROM `studentspays` LEFT JOIN students ON  studentspays.studentid = students.id WHERE studentspays.del < 2 
            and students.branchid = ?  and studentspays.addtoday  >= '$today 00-00-00' and studentspays.addtoday  <= '$today 23-59-55' ", [$branch->branchId]);
            $branch->totalstudentspays = (int) $totalstudentspays;
            $branch->todays = $today;
             $alltotalstudentspays +=  (int) $totalstudentspays;
        }
        if($allcgstudentabsences > 0 || $allcstudentspays > 0  ){
            // $allscrightstudentspays += $allcrightstudentspays;
            // $allscgstudentabsences += $allcgstudentabsences;
            $allscstudentspays += $allcstudentspays;
            $allstotalstudentspays += $alltotalstudentspays;
             array_push($allmonth, $branches); 
        }

       
    }
    $smarty->assign("allmonth", $allmonth);
    $startOfThisMonth = date('Y-m-01', strtotime($start_date));
    $endOfThisMonth = date('Y-m-t', strtotime($start_date));
    $allscrightstudentspays = R::count('studentspays'," left join students on studentspays.studentid = students.id WHERE studentspays.del < 2 
            and studentspays.payenddate  >= '$startOfThisMonth'  ");
    $allscgstudentabsences = R::getcell("SELECT COUNT(*) FROM ( SELECT DISTINCT studentid FROM `studentabsences` left join students on studentabsences.studentid = students.id
    WHERE studentabsences.todaysend >= '$startOfThisMonth' and studentabsences.todaysend <= '$endOfThisMonth' ) as totals ");
    $smarty->assign("allscrightstudentspays", $allscrightstudentspays);
    $smarty->assign("allscgstudentabsences", $allscgstudentabsences);
     $smarty->assign("allscstudentspays", $allscstudentspays);
      $smarty->assign("allstotalstudentspays", $allstotalstudentspays);

    $smarty->display("studentview/branches.html");      
}else if ($do == 'getsetting') {
  getsetting();
}else if ($do == 'changeenddate') {
  changeenddate();
}
function checkcolumn() {
    $table = filter_input(INPUT_POST, 'table');
    $tablecol = filter_input(INPUT_POST, 'tablecol');
    $tablecolval = filter_input(INPUT_POST, 'tablecolval');
    $tableid = filter_input(INPUT_POST, 'tableid');
    $parentid = filter_input(INPUT_POST, 'parentid');
    $parentcol = filter_input(INPUT_POST, 'parentcol');

    if (!$tableid && !$parentid) {
    $checkinput = R::count($table, " $tablecol = ? and del < 2 ", [$tablecolval]);
    }else if ($tableid && !$parentid) {
    $checkinput = R::count($table, " $tablecol = ? and id != ? and del < 2 ", [$tablecolval, $tableid]); 
    }else if (!$tableid && $parentid) {
    $checkinput = R::count($table, " $tablecol = ? and $parentcol = ? and del < 2 ", [$tablecolval, $parentid]);
    }else if ($tableid && $parentid) {
    $checkinput = R::count($table, " $tablecol = ? and $parentcol = ? and id != ? and del < 2", [$tablecolval, $parentid, $tableid]);
    }  
    if ($checkinput > 0) {
         echo 1;
    } else {
         echo 0;
    }
       
}

function savestudaent() {
  include "../library/phpqrcode/qrlib.php";    
   
    $today = date("Y-m-d H:i:s");
    $date = date('Y-m-d');
    $userid = $_SESSION['userid'];
    $studentname = filter_input(INPUT_POST, 'studentname');
    $studentnumber = filter_input(INPUT_POST, 'studentnumber');
    $idnumber = filter_input(INPUT_POST, 'idnumber');
    $studentphone = filter_input(INPUT_POST, 'studentphone');
    $parentsphone = filter_input(INPUT_POST, 'parentsphone');
    $parentsjob = filter_input(INPUT_POST, 'parentsjob');
    $studentaddress = filter_input(INPUT_POST, 'studentaddress');
    $studentnote = filter_input(INPUT_POST, 'studentnote');
    $branchid = filter_input(INPUT_POST, 'branchid');
    $datebirth = filter_input(INPUT_POST, 'datebirth');
    $namepay = filter_input(INPUT_POST, 'namepay');
    $commentpay = filter_input(INPUT_POST, 'commentpay');
    $studentaddsubjectid = filter_input(INPUT_POST, 'studentaddsubjectid');
    $studentid = filter_input(INPUT_POST, 'studentid');
    $clientid = filter_input(INPUT_POST, 'clientid');

    if (!$studentid) {;
        $students = R::dispense('students');
        $students->del = 0;
        $students->addtoday = $today;          
        $students->adduserid = $userid;
        $students->deltoday = '';      
        $students->deluserid = '';
        $handle = new upload($_FILES['studentimage']);
        $studentimage = uploadImages($handle, "../views/default/images/students", 300, 300);
    }else{
        R::exec("UPDATE `client` SET `clientname`= '$studentname',`clientaddress`= '$studentaddress',`clientphone`= '$studentphone',`branchId`= $branchid WHERE clientid = $clientid");
        $students = R::load('students',$studentid);
        $students->del = 1;
        $students->updatetoday = $today;          
        $students->updateuserid = $userid; 
        $handle = new upload($_FILES['studentimage']);
        $studentimage = updateImages($handle, "oldstudentimage", "../views/default/images/students", 300, 300);
        unlink("../views/default/images/students" . $students->studentimage);
    }
    $students->studentname  = $studentname;
    $students->studentnumber = $studentnumber;
    $students->idnumber = $idnumber;
    $students->studentphone = $studentphone;
    $students->parentsphone = $parentsphone;
    $students->parentsjob = $parentsjob;
    $students->studentaddress = $studentaddress;
    $students->clientid = $clientid; 
    $students->branchid = $branchid; 
    $students->studentimage = $studentimage;
    $students->studentnote = $studentnote;
    $students->datebirth = $datebirth;
    $students->namepay = $namepay;
    $students->commentpay = $commentpay;
    try {
         $studentid = R::store($students);
            if (!$clientid) {
             R::exec("INSERT INTO `client`(`clientname`, `clientaddress`, `clientphone`, `clientdate`, `userid`, `branchId`, `clientStoreIds`, `typeclientid`, `priceTypeId`, `studentid`) VALUES 
             ('$studentname', '$studentaddress', '$studentphone','$date', $userid, $branchid, '-10', ',-20,', -1, $studentid)");
             $client = R::getCell("SELECT clientid FROM `client` WHERE studentid =  $studentid");
             $students = R::load('students',$studentid);
             $students->clientid = $client; 
             $studentid = R::store($students);
            }else{
               R::exec("UPDATE `client` SET `clientname`='$studentname',`clientaddress`='$studentaddress',`clientphone`='$studentphone',`branchId`= $branchid 
               WHERE clientid = $clientid");
            }
           if($studentaddsubjectid){
                $studentaddsubjects = R::load('studentaddsubjects',$studentaddsubjectid); 
                $studentaddsubjects->del = 1;
                $studentaddsubjects->updatetoday = $today;          
                $studentaddsubjects->updateuserid = $userid;
                $studentaddsubjects->studentid = $studentaddsubjects->studentid.','.$studentid;
                R::store($studentaddsubjects);
                $studentdata = R::load('students',$Explode[$i]); 
                $studentaddsubjectdetails = R::dispense('studentaddsubjectdetails');
                $studentaddsubjectdetails->studentid  = $studentid;
                $studentaddsubjectdetails->studentsubjectid  = $studentaddsubjects->studentsubjectid;
                $studentaddsubjectdetails->studentsubjectgroupid = $studentaddsubjects->studentsubjectgroupid;
                $studentaddsubjectdetails->del = 0;
                $studentaddsubjectdetails->studentaddsubjectid = $studentaddsubjectid;
                R::store($studentaddsubjectdetails); 
           }
           
            if ($students->del == 0) {
                $PNG_TEMP_DIR = "../views/default/images/studentsparcode/";
                $filename = $PNG_TEMP_DIR.$studentid.'student.png';
                $url = 'http://' . $_SERVER['HTTP_HOST'] . explode('controllers', $_SERVER['REQUEST_URI'])[0] . 'controllers/studentControllerAjax.php?do=detailstudent&studentid=' . $studentid;
                QRcode::png($url, $filename, 'L', 9, 2);    
            }
    
   
        echo 1;
    } catch (Exception $e) {
        echo 0 ;
    }
}





function getselectjoin() {
    $searchterm = $_POST['searchTerms']; 
    $tablesearch = $_POST['tablesearch'];
    $table = $_POST['table'];
    $tableid = $_POST['tableid'];
    $tablewhere = $_POST['tablewhere'];
    $parentcol = $_POST['parentcol'];
    $parentid = $_POST['parentid'];
    $join = $_POST['join'];
    $filter = $_POST['filter'];
    $search = '';
    if ($parentid) {
       $search .= " and $table.$parentcol = $parentid "; 
    }
    if ($filter) {
       $search .= $filter;
    }
    $row_array = array();
    $return_arr = array();
    $allData = R::getAll("SELECT $tableid as tabid ,CONCAT($tablesearch) as name FROM $table 
        $join WHERE  CONCAT($tablesearch) LIKE '%" . $searchterm . "%' $search $tablewhere limit 50");
    foreach ($allData as $data) {
        $row_array['id'] = $data['tabid'];
        $row_array['text'] = $data['name'];
        array_push($return_arr, $row_array);
    }
    echo json_encode($return_arr);
}



function removeteble() {
    $id = filter_input(INPUT_POST, 'id');
    $table = filter_input(INPUT_POST, 'table');
    $tables = R::load($table,$id);
    $tables->del = 2;
    $tables->deltoday = date("Y-m-d H:i:s");     
    $tables->deluserid = $_SESSION['userid']; 
    try {
        R::store($tables);
        echo 1;
    } catch (Exception $e) {
        echo 0 ;
    }
}












function getselect() {
    $searchterm = $_POST['searchTerms']; 
    $tablesearch = $_POST['tablesearch'];
    $table = $_POST['table'];
    $del = $_POST['del'];
    $filter = $_POST['filter'];
    $parentid = $_POST['parentid'];
    $parentcol = $_POST['parentcol'];

    $search = '';
    if ($parentid) {
    $search .= " and $table.del < 2"; 
    }
    if ($parentid) {
       $search .= " and $parentcol = $parentid "; 
    }

    if ($filter) {
       $search .= $filter;
    }

    $row_array = array();
    $return_arr = array();

     $allData = R::getAll("SELECT $table.id,CONCAT($tablesearch) as name
        FROM $table 
        WHERE  CONCAT($tablesearch) LIKE '%" . $searchterm . "%' $search limit 50");
    foreach ($allData as $data) {
        $row_array['id'] = $data['id'];
        $row_array['text'] = $data['name'];
        array_push($return_arr, $row_array);
    }

    echo json_encode($return_arr);
}


function getselectmultiple() {
    $searchterm = $_POST['searchTerms']; 
    $tablesearch = $_POST['tablesearch'];
    $table = $_POST['table'];
    $del = $_POST['del'];
    $studentsubjectid = $_POST['studentsubjectid'];
    
    $row_array = array();
    $return_arr = array();
    $search = '';
    if ($del) {
       $search .= " and $table.del < 2"; 
    }
    $studentid = '0';
    $studentstosubjects = R::findAll('studentaddsubjectdetails', 'studentsubjectid = ? and del < 2',[$studentsubjectid]);
    foreach($studentstosubjects as $data){
     $studentid .= ','.$data->studentid;
    }
    $search .= " and $table.id not in ($studentid) ";
    $allData = R::getAll("SELECT $table.id,CONCAT($tablesearch) as name
        FROM $table 
        WHERE  CONCAT($tablesearch) LIKE '%" . $searchterm . "%' $search limit 50");
    foreach ($allData as $data) {
        $row_array['id'] = $data['id'];
        $row_array['text'] = $data['name'];
        array_push($return_arr, $row_array);
    }

    echo json_encode($return_arr);
}






function getselectparent() {
    $searchterm = $_POST['searchTerms']; 
    $tablesearch = $_POST['tablesearch'];
    $table = $_POST['table'];
    $parentcol = $_POST['parentcol'];
    $parentid = $_POST['parentid'];
    $filter = $_POST['filter'];
    $search = '';
    
    $search .= " and $table.del < 2"; 
    if ($parentid) {
       $search .= " and $parentcol =  $parentid "; 
    }

    if ($filter) {
       $search .= $filter;
    }

    $row_array = array();
    $return_arr = array();

     $allData = R::getAll("SELECT $table.id,CONCAT($tablesearch) as name
        FROM $table 
        WHERE  CONCAT($tablesearch) LIKE '%" . $searchterm . "%' $search limit 50");
    foreach ($allData as $data) {
        $row_array['id'] = $data['id'];
        $row_array['text'] = $data['name'];
        array_push($return_arr, $row_array);
    }

    echo json_encode($return_arr);
}







function receivedata() {
    $today = date("Y-m-d H:i:s");
    $date = date("Y-m-d");
    $times = date("H:i:s");
    $day = date('l');
    $userid = $_SESSION['userid'];    
    $lists = explode("\n", $_POST['textfile']);
    foreach ($lists as $list) {
        $data = explode('?', $list);
        $studentabsencescount = R::count('studentabsences', "studentid = ? and deviceid = ? and todaysend = '$date' and del < 2", [$data[1], $data[5]]);
        if($studentabsencescount == 0 && $list){ 
            $studentaddsubjectdetails = R::findOne('studentaddsubjectdetails','studentid = ? and deviceid = ? and del < 2 order by id desc',[$data[1], $data[5]]); 
               $studentabsences = R::dispense('studentabsences');
                $studentabsences->onlys = 0;
                $studentabsences->del = 0;
                $studentabsences->arrivedtime = $arrivedtime;
                $studentabsences->addtoday = $today;          
                $studentabsences->adduserid = $_SESSION['userid'];
                $studentabsences->uniqid = $data[0];
                $studentabsences->studentid = $data[1];
                $studentabsences->todaysend = $data[2];
                $studentabsences->timesend = $data[3];
                $studentabsences->type = $data[4];
                $studentabsences->deviceid = $data[5];
                $studentabsences->status = $data[6];
                $studentabsences->studentsubjectid = $studentaddsubjectdetails->studentsubjectid;
                $studentabsences->studentsubjectgroupid = $studentaddsubjectdetails->studentsubjectgroupid;
                $studentabsences->studentaddsubjectid = $studentaddsubjectdetails->studentaddsubjectid;
                $studentabsences->studentaddsubjectdetailid = $studentaddsubjectdetails->id;
                R::store($studentabsences);
        }
    }
    
}






function receivestudent() {
    $today = date("Y-m-d H:i:s");
    $date = date("Y-m-d");
    $times = date("H:i:s");
    $day = date('l');
    $userid = $_SESSION['userid'];
   $brancheid = filter_input(INPUT_GET, 'brancheid');
   $studentid = filter_input(INPUT_GET, 'studentid');
  $studentabsencescount = R::count('studentabsences', "uniqid = ? and deviceid = ? and todaysend = '$date' and del < 2", [$data[0], $data[5]]);
    if($studentabsencescount == 0 && $studentid && $brancheid){ 
        $studentaddsubjectdetails = R::findOne('studentaddsubjectdetails','studentid = ? and deviceid = ? and del < 2 order by id desc',[$data[1], $data[5]]); 
     
   
   $studentabsences = R::dispense('studentabsences');
    $studentabsences->onlys = 0;
    $studentabsences->del = 0;
    $studentabsences->arrivedtime = $arrivedtime;
    $studentabsences->addtoday = $today;          
    $studentabsences->adduserid = $_SESSION['userid'];
    $studentabsences->uniqid = $data[0];
    $studentabsences->studentid = $data[1];
    $studentabsences->todaysend = $data[2];
    $studentabsences->timesend = $data[3];
    $studentabsences->type = $data[4];
    $studentabsences->deviceid = $data[5];
    $studentabsences->status = $data[6];
    $studentabsences->studentsubjectid = $studentaddsubjectdetails->studentsubjectid;
    $studentabsences->studentsubjectgroupid = $studentaddsubjectdetails->studentsubjectgroupid;
    $studentabsences->studentaddsubjectid = $studentaddsubjectdetails->studentaddsubjectid;
    $studentabsences->studentaddsubjectdetailid = $studentaddsubjectdetails->id;
    R::store($studentabsences);
    }
}

function getsetting() {
    $getdata = R::getRow("SELECT * FROM programsettings");
    echo json_encode($getdata);
}




function changeenddate() {
    $id = filter_input(INPUT_POST, 'payid');
    $dateend = filter_input(INPUT_POST, 'dateend');
    $datestart = filter_input(INPUT_POST, 'datestart');
    $tables = R::load(studentspays,$id);
    $tables->paystartdate = $datestart;
    $tables->payenddate = $dateend;
    try {
        R::store($tables);
        echo 1;
    } catch (Exception $e) {
        echo 0 ;
    }
}


?>