<?php
include("../public/impOpreation.php");
 // studentsaddsubject(); 
// function studentcontroller() {
//     $today = date("Y-m-d H:i:s");
//     $date = date('Y-m-d');
//     $userid = $_SESSION['userid'];
//     for ($i = 1; $i <= 5000; $i++) {
//         $students = R::dispense('students');
//         $students->del = 0;
//         $students->addtoday = $today;  
//         $students->adduserid = $userid;
//         $students->deltoday = '';  
//         $students->deluserid = '';
//         $students->studentname  = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ'),1,8). ' ' .substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ'),1,7);
//         $students->studentnumber = rand(100000, 999999);
//         $students->idnumber = rand(100000000, 1);
//         $students->studentphone = '010'.rand(100000000, 1);
//         $students->parentsphone = '011'.rand(100000000, 1);
//         $students->studentaddress = 'Address' . ' ' . $students->studentname;
//         $students->clientid = $clientid; 
//         $students->branchid = 1; 
//         $students->branchnameandphone = 'فرع  1';   
//         $studentid = R::store($students);
//         R::exec("INSERT INTO `client`(`clientname`, `clientaddress`, `clientphone`, `clientdate`, `userid`, `branchId`, `studentid`) VALUES ('$students->studentname', '$students->studentaddress', '$students->studentphone','$date', $userid, 1, $studentid)");
//         $client = R::getCell("SELECT clientid FROM `client` WHERE studentid =  $studentid");
//         $students = R::load('students',$studentid);
//         $students->clientid = $client; 
//         $studentid = R::store($students);
//     }
//     studentsubjectscontroller();
// }

// function studentsubjectscontroller() {
//     $today = date("Y-m-d H:i:s");
//     $date = date('Y-m-d');
//     $userid = $_SESSION['userid'];
//     for ($ni = 1; $ni <= 100; $ni++) {
//         $subjects = R::dispense('studentsubjects');
//         $subjects->del = 0;
//         $subjects->addtoday = $today;  
//         $subjects->adduserid = $userid;
//         $subjects->deltoday = '';  
//         $subjects->deluserid = '';
//         $subjects->subjectname  = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'),1,10);
//         $subjects->subjectprice = rand(100, 999);
//         $subjects->deviceid = '0'.$ni;
//         $subjects->useit  = 0;        
//         $subjectid = R::store($subjects);
//         for ($i1 = 1; $i1 <= 2; $i1++) {
//             $subjectgroupsdayitr = $i1;
//             $subjectgroups = R::dispense('studentsubjectgroups');
//             $subjectgroups->del = 0;
//             $subjectgroups->addtoday = $today;  
//             $subjectgroups->adduserid = $userid;
//             $subjectgroups->deltoday = '';  
//             $subjectgroups->deluserid = '';
//             $subjectgroups->studentsubjectid = $subjectid;
//             $subjectgroups->subjectgroupname =  substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ'),1,10);
//             $subjectgroups->subjectgroupprice = rand(100, 999);
//             $subjectgroups->deviceid = '0'.$ni;
//             $subjectgroups->useit  = 0;  
//             $subjectgroupid = R::store($subjectgroups);
//             for ($i2 = 1; $i2 <= 2; $i2++) {
//                 $dayar = ['','السبت','الأحد','الاثنين','الثلاثاء','الاربعاء','الخميس','الجمعة'];
//                 $dayen = ['','Saturday','Sunday','Monday','Tuesday','Wednesday','Thursday','Friday'];
//                 $subjectgroupdays = R::dispense('studentsubjectgroupdays');
//                 $subjectgroupdays->del = 0;
//                 $subjectgroupdays->addtoday = $today;  
//                 $subjectgroupdays->adduserid = $userid;
//                 $subjectgroupdays->deltoday = '';  
//                 $subjectgroupdays->deluserid = '';
//                 $subjectgroupdays->studentsubjectid = $subjectid;
//                 $subjectgroupdays->studentsubjectgroupid = $subjectgroupid;
//                 if ($i2 == 2) {
//                     $studentsubjectgroupold = R::findOne('studentsubjectgroupdays','studentsubjectgroupid = ?',[$subjectgroupid]);
//                     if ($studentsubjectgroupold->day > 4) {
//                         $subjectgroupdays->day = $studentsubjectgroupold->day - 3;
//                     }else{
//                        $subjectgroupdays->day = $studentsubjectgroupold->day + 3;   
//                     }
//                     $subjectgroupdays->timeform = $studentsubjectgroupold->timeto;  
//                     $subjectgroupdays->timeto = date("H:i:s", strtotime($studentsubjectgroupold->timeto."+1 hours"));
                    
//                 }else{
//                     $subjectgroupdays->day = rand(1,7);  
//                     $timeform = date("H:i:s");  
//                     $timeto = date("H:i:s", strtotime("+1 hours"));
//                 }
                
//                 $subjectgroupdays->deviceid = '0'.$ni;
//                 $subjectgroupdays->dayar = $dayar[$subjectgroupdays->day]; 
//                 $subjectgroupdays->dayen = $dayen[$subjectgroupdays->day];  
//                 $subjectgroupdays->timeform = $timeform;  
//                 $subjectgroupdays->timeto = $timeto;
//                 $subjectgroupdays->useit  = 0; 
//                 $subjectgroupdayid = R::store($subjectgroupdays); 
//             } 
//         }  
//     }
//     studentsaddsubject(); 
     
   
// }


// function studentsaddsubject() {
//     $today = date("Y-m-d H:i:s");
//     $date = date('Y-m-d');
//     $userid = $_SESSION['userid'];  
//     $studentsubjectsdata = R::findAll('studentsubjects','id > 80 and id < 161 and useit = 0');
//     foreach($studentsubjectsdata as $studentsubjectdata){
//         R::exec("UPDATE `studentsubjects` SET `useit`= 1 WHERE  id = '" . $studentsubjectdata->id . "' ");
//         $studentsubjectgroupsdata = R::findAll('studentsubjectgroups','studentsubjectid = ? and useit = 0',[$studentsubjectdata->id]);
//         foreach($studentsubjectgroupsdata as $studentsubjectgroupdata){
//             R::exec("UPDATE `studentsubjectgroups` SET `useit`= 1 WHERE  id = '" . $studentsubjectgroupdata->id . "' ");
//             $studentdesc = R::findOne('studentaddsubjectdetails','order by id desc'); 
//             $stert = $studentdesc->id + 1;
//             $end = $studentdesc->id + 20;
//             $studentp = '0';
//             for ($ni = $stert; $ni <= $end; $ni++) {
//                 $studentp .= ','.$ni;
//             }
//             $Explode = explode(',', $studentp);
//             $studentsaddubjects = R::dispense('studentaddsubjects');
//             $studentsaddubjects->del = 0;
//             $studentsaddubjects->addtoday = $today;          
//             $studentsaddubjects->adduserid = $userid;
//             $studentsaddubjects->deltoday = '';  
//             $studentsaddubjects->deluserid = '';
//             $studentsaddubjects->studentsubjectid  = $studentsubjectdata->id;
//             $studentsaddubjects->deviceid  = $studentsubjectdata->deviceid;
//             $studentsaddubjects->subjectname  = $studentsubjectdata->subjectname;
//             $studentsaddubjects->subjectprice  = $studentsubjectdata->subjectprice;
//             $studentsaddubjects->studentsubjectgroupid = $studentsubjectgroupdata->id;
//             $studentsaddubjects->subjectgroupname = $studentsubjectgroupdata->subjectgroupname;
//             $studentsaddubjects->subjectgroupprice = $studentsubjectgroupdata->subjectgroupprice;
//             $studentsaddubjects->studentid = $studentp;
//             $studentsddubjectid = R::store($studentsaddubjects);
//             for ($i = 0; $i < count($Explode); $i++) { 
//                 $studentdata = R::load('students',$Explode[$i]); 
//                 if ($studentdata->id > 0) {
//                      $studentdata = R::load('students',$Explode[$i]); 
//                     $studentaddsubjectdetails = R::dispense('studentaddsubjectdetails');
//                     $studentaddsubjectdetails->del = 0;
//                     $studentaddsubjectdetails->addtoday = $today;          
//                     $studentaddsubjectdetails->adduserid = $userid;
//                     $studentaddsubjectdetails->deltoday = '';  
//                     $studentaddsubjectdetails->deluserid = '';
//                     $studentaddsubjectdetails->studentid  = $Explode[$i];
//                     $studentaddsubjectdetails->studentname  = $studentdata->studentname;
//                     $studentaddsubjectdetails->studentphone  = $studentdata->studentphone;
//                     $studentaddsubjectdetails->studentsubjectid  = $studentsubjectdata->id;
//                     $studentaddsubjectdetails->deviceid  = $studentsubjectdata->deviceid;
//                     $studentaddsubjectdetails->subjectname  = $studentsubjectdata->subjectname;
//                     $studentaddsubjectdetails->subjectprice  = $studentsubjectdata->subjectprice;
//                     $studentaddsubjectdetails->studentsubjectgroupid = $studentsubjectgroupdata->id;
//                     $studentaddsubjectdetails->subjectgroupname = $studentsubjectgroupdata->subjectgroupname;
//                     $studentaddsubjectdetails->subjectgroupprice = $studentsubjectgroupdata->subjectgroupprice;
//                     $studentaddsubjectdetails->studentaddsubjectid = $studentsddubjectid;
//                     R::store($studentaddsubjectdetails);
//                 }
//             }

            
           
//         }
//     }
//     studentsExamsController();
// }

// function studentsExamsController() {
//     $today = date("Y-m-d H:i:s");
//     $date = date('Y-m-d');
//     $userid = $_SESSION['userid'];
//  $studentaddsubjects = R::findAll('studentaddsubjects', "id < 160 and del < 2 ");  
//     foreach($studentaddsubjects as $studentaddsubject) {
//         $studentsexams = R::dispense('studentsexams');
//         $studentsexams->deltoday = '';
//         $studentsexams->deluserid = ''; 
//         $studentsexams->del = 0;
//         $studentsexams->addtoday = $today;          
//         $studentsexams->adduserid = $userid;
//         $studentsexams->studentaddsubjectid  = $studentaddsubject->id;
//         $studentsexams->studentsubjectid   = $studentaddsubject->studentsubjectid;
//         $studentsexams->subjectname  = $studentaddsubject->subjectname;
//         $studentsexams->subjectprice   = $studentaddsubject->subjectprice;
//         $studentsexams->studentsubjectgroupid  = $studentaddsubject->studentsubjectgroupid;
//         $studentsexams->subjectgroupname  = $studentaddsubject->subjectgroupname;
//         $studentsexams->subjectgroupprice   = $studentaddsubject->subjectgroupprice;
//         $studentsexams->studentid  = $studentaddsubject->studentid;
//         $studentsexams->examname  = 'اختبار  ' .' / '.$studentaddsubject->subjectname.' / '.$studentaddsubject->subjectgroupname;
//         $studentsexams->greatdegree  = rand(200, 1);
//         $studentsexams->examsstudent = 1;
//         $id = R::store($studentsexams);
//             $students = R::findAll('students', "del < 2 and id in ($studentaddsubject->studentid)");
//             foreach($students as $student){
//                 $studentsexamsval = R::dispense('studentsexamsval');
//                 $studentsexamsval->studentid  = $student->id;
//                 $studentsexamsval->studentname  = $student->studentname;
//                 $studentsexamsval->studentphone  = $student->studentphone;
//                 $studentsexamsval->examsvalue  = rand(200, 1);
//                 $studentsexamsval->examspresence = 1;
//                 $studentsexamsval->studentaddsubjectid  = $studentsexams->studentaddsubjectid;
//                 $studentsexamsval->subjectname  = $studentsexams->subjectname;
//                 $studentsexamsval->subjectprice   = $studentsexams->subjectprice;
//                 $studentsexamsval->studentsubjectgroupid  = $studentsexams->studentsubjectgroupid;
//                 $studentsexamsval->subjectgroupname  = $studentsexams->subjectgroupname;
//                 $studentsexamsval->subjectgroupprice   = $studentsexams->subjectgroupprice;
//                 $studentsexamsval->del = 0;
//                 $studentsexamsval->addtoday = $today;
//                 $studentsexamsval->adduserid = $_SESSION['userid']; 
//                 $studentsexamsval->deltoday = '';
//                 $studentsexamsval->deluserid = ''; 
//                 $studentsexamsval->studentsexamid  = $id;
//                 $studentsexamsval->examname  = $studentsexams->examname;
//                 $studentsexamsval->greatdegree  = $studentsexams->greatdegree;
//                 R::store($studentsexamsval);
//             }  
//     }
//     studentsPaymentSystems();
    
// }

// function studentsPaymentSystems() {
//     $today = date("Y-m-d H:i:s");
//     $date = date('Y-m-d');
//     $userid = $_SESSION['userid'];
//     $studentaddsubjects = R::findAll('studentaddsubjects', "id < 161 and del < 2 ");  
//     foreach($studentaddsubjects as $studentaddsubject) {
//         $studentspaymentsystems = R::dispense('studentspaymentsystems');
//         $studentspaymentsystems->del = 0;
//         $studentspaymentsystems->addtoday = $today;          
//         $studentspaymentsystems->adduserid = $userid;
//         $studentspaymentsystems->deltoday = '';  
//         $studentspaymentsystems->deluserid = '';
//         $studentspaymentsystems->paymentname  = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'),1,14);
//         $studentspaymentsystems->paymentprice = rand(1000, 1);
//         $studentspaymentsystems->paymentmonth = rand(12, 1);
//         $studentspaymentsystems->studentaddsubjectid = $studentaddsubject->id;
//         $studentspaymentsystems->studentsubjectid = $studentaddsubject->studentsubjectid;
//         $studentspaymentsystems->subjectname = $studentaddsubject->subjectname;
//         $studentspaymentsystems->subjectprice = $studentaddsubject->subjectprice;
//         R::store($studentspaymentsystems);
//     }
//     studentsPay();
// }

// function studentsPay() {
//     $today = date("Y-m-d H:i:s");
//     $date = date('Y-m-d');
//     $userid = $_SESSION['userid'];
//     for ($ni = 1; $ni <= 18; $ni++) {
//     $studentaddsubjectall = R::findAll('studentaddsubjects', "id > 80 and id < 161 and del < 2 ");  
//         foreach($studentaddsubjectall as $studentaddsubjects) {
//           $limitend =  $studentaddsubjects->id * 20;
//           $limitstart =  $limitend - 20;
//           $studentid = rand($limitend, $limitstart);
//            $studentaddsubjectdetails = R::findOne('studentaddsubjectdetails', " studentaddsubjectid = $studentaddsubjects->id and del < 2 and studentid = $studentid ");  
//            $startdate = date('Y-m-d');
//           if ($ni == 1 || $ni == 4 || $ni == 7 || $ni == 10 || $ni == 13 || $ni == 16) {
//             $paymonths = 1;
//             $paytype = 'دفع عن طريق  الماده';
//             $payprice = $studentaddsubjects->subjectgroupprice;
//             $paytypenum = 1;
//           }else if ($ni == 2 || $ni == 5 || $ni == 8 || $ni == 11 || $ni == 14 || $ni == 17) {
//             $limit = rand(70, 1);
//             $studentspaymentsystems = R::load('studentspaymentsystems',$limit);
//             $paymonths = $studentspaymentsystems->paymentmonth;
//             $paytype = $studentspaymentsystems->paymentname;
//             $payprice = $studentspaymentsystems->paymentprice;
//             $paytypenum = 2;
//           }else{
//             $paymonths = rand(12, 1);
//             $paytype = 'دفع  خاص';
//             $payprice = rand(1000, 1);
//             $paytypenum = 3;
//             $countmonths = $paymonths;
//             $newprice = $payprice;
//           }


//         $studentspays = R::dispense('studentspays');
//         $studentspays->del = 0;
//         $studentspays->addtoday = $today;          
//         $studentspays->adduserid = $userid;
//         $studentspays->deltoday = '';  
//         $studentspays->deluserid = '';
        
//         $getstudentpaysubject = R::findOne('studentspays','studentid = ? and studentsubjectid = ? and del < 2 order by id desc',[$studentaddsubjectdetails->studentid,$studentaddsubjects->studentsubjectid]); 
//         if ($getstudentpaysubject->id < 1) {
//             $paystartdate = $startdate;
//             $payenddate = date('Y-m-d', strtotime("+".$paymonths." month", strtotime($startdate)));
//         }else if ($getstudentpaysubject->payenddate >= $startdate) {
//         $paystartdate = $getstudentpaysubject->payenddate;
//         $payenddate = date('Y-m-d', strtotime("+".$paymonths." month", strtotime($getstudentpaysubject->payenddate))); 
//         }else if ($getstudentpaysubject->payenddate < $startdate) {
//         $paystartdate = $startdate;
//         $payenddate = date('Y-m-d', strtotime("+".$paymonths." month", strtotime($startdate))); 
//         }

//         $studentspays->deviceid = $studentaddsubjects->deviceid;
//         $studentspays->studentaddsubjectid = $studentaddsubjects->id;
//         $studentspays->studentsubjectid = $studentaddsubjects->studentsubjectid;
//         $studentspays->subjectname = $studentaddsubjects->subjectname;
//         $studentspays->subjectprice = $studentaddsubjects->subjectprice;
//         $studentspays->studentsubjectgroupid = $studentaddsubjects->studentsubjectgroupid;
//         $studentspays->subjectgroupname = $studentaddsubjects->subjectgroupname;
//         $studentspays->subjectgroupprice = $studentaddsubjects->subjectgroupprice;
//         $studentspays->studentspaymentsystemid = $studentspaymentsystems->id;
//         $studentspays->paymentname = $studentspaymentsystems->paymentname;
//         $studentspays->paymentprice = $studentspaymentsystems->paymentprice;
//         $studentspays->paymentmonth = $studentspaymentsystems->paymentmonth;
//         $studentspays->studentaddsubjectdetailid = $studentaddsubjectdetails->id;
//         $studentspays->studentid = $studentaddsubjectdetails->studentid;
//         $studentspays->studentname  = $studentaddsubjectdetails->studentname;
//         $studentspays->studentphone  = $studentaddsubjectdetails->studentphone;
//         $studentspays->paymonths  = $paymonths;
//         $studentspays->paytype = $paytype;
//         $studentspays->paytypenum = $paytypenum;
//         $studentspays->payprice = $payprice;
//         $studentspays->countmonths = $countmonths;
//         $studentspays->newprice = $newprice;
//         $studentspays->paystartdate  = $paystartdate;
//         $studentspays->payenddate  = $payenddate;

//             $studentspayid = R::store($studentspays);
//             $students2 = R::load('students',$studentaddsubjectdetails->studentid);
//             $students2->studentaddsubjectid = $studentaddsubjects->id;
//             $students2->studentsubjectid = $studentaddsubjects->studentsubjectid;
//             $students2->subjectname = $studentaddsubjects->subjectname;
//             $students2->subjectprice = $studentaddsubjects->subjectprice;
//             $students2->studentsubjectgroupid = $studentaddsubjects->studentsubjectgroupid;
//             $students2->subjectgroupname = $studentaddsubjects->subjectgroupname;
//             $students2->subjectgroupprice = $studentaddsubjects->subjectgroupprice;
//             $students2->studentspaymentsystemid = $studentspaymentsystems->id;
//             $students2->paymentname = $studentspaymentsystems->paymentname;
//             $students2->paymentprice = $studentspaymentsystems->paymentprice;
//             $students2->paymentmonth = $studentspaymentsystems->paymentmonth;
//             $students2->studentaddsubjectdetailid = $studentaddsubjectdetails->id;
//             $students2->paymonths  = $paymonths;
//             $students2->paytype = $paytype;
//             $students2->paytypenum = $paytypenum;
//             $students2->payprice = $payprice;
//             $students2->countmonths = $countmonths;
//             $students2->newprice = $newprice;
//             $students2->paystartdate  = $paystartdate;
//             $students2->payenddate  = $payenddate;
//             $students2->studentspayid  = $studentspayid;
//             R::store($students2);
//         }
//     }
//     studentAbsence();
// }

// function studentAbsence() {
//     $today = date("Y-m-d H:i:s");
//     $date = date('Y-m-d');
//     $userid = $_SESSION['userid'];
//     for ($ni = 1; $ni <= 2000; $ni++) {
//             $uniq_id = rand(1000000000, 1);
//             $id = rand(1999, 1);
//             $studentabsencesadd = R::count('studentabsences','uniq_id = ? ',[$uniq_id]);
//             $studentaddsubjectdetails = R::findOne('studentaddsubjectdetails',' id = ? ',[$id]); 
//             $studentabsences = R::dispense('studentabsences');
//             $studentabsences->del = 0;
//             $studentabsences->onlys = $id;
//             $studentabsences->addtoday = $today;          
//             $studentabsences->adduserid = $_SESSION['userid'];
//             $studentabsences->uniqid = $uniq_id;
//             $studentabsences->studentid = $studentaddsubjectdetails->studentid;
//             $studentabsences->todaysend =  date('Y-m-d', strtotime( '+'.mt_rand(0,100).' days'));
//             $studentabsences->timesend =  date("H:i:s", strtotime('+'.mt_rand(0,100).' hour'));
//             $studentabsences->type = 0;
//             $studentabsences->deviceid = $studentaddsubjectdetails->deviceid;
//             $studentabsences->status = 1;
//             $studentabsences->studentname = $studentaddsubjectdetails->studentname;
//             $studentabsences->studentphone = $studentaddsubjectdetails->studentphone;
//             $studentabsences->studentsubjectid = $studentaddsubjectdetails->studentsubjectid;
//             $studentabsences->subjectname = $studentaddsubjectdetails->subjectname;
//             $studentabsences->subjectprice = $studentaddsubjectdetails->subjectprice;
//             $studentabsences->studentsubjectgroupid = $studentaddsubjectdetails->studentsubjectgroupid;
//             $studentabsences->subjectgroupname = $studentaddsubjectdetails->subjectgroupname;
//             $studentabsences->subjectgroupprice = $studentaddsubjectdetails->subjectgroupprice;
//             $studentabsences->studentaddsubjectid = $studentaddsubjectdetails->studentaddsubjectid;
//             $studentabsences->studentaddsubjectdetailid = $studentaddsubjectdetails->id;
//             $studentabsenceid2 = R::store($studentabsences);
//     }
//     R::exec("UPDATE `studentabsences` SET `onlys`= 1, `uniqid`= '', `todaysend`= '', `timesend`= '', `type`= '', `status`= '' WHERE onlys < 1000 ");
//     R::exec("UPDATE `studentabsences` SET `onlys`= 0 WHERE onlys > 999 ");

//     faxs();
// }
// function faxs() {
//     $studentsubjectgroupdays = R::findAll('studentsubjectgroupdays');
//     foreach($studentsubjectgroupdays as $data){
//         $studentsubjectgroupold = R::findOne('studentsubjectgroupdays',"id < $data->id order by id desc ");
//         $data->timeform = $studentsubjectgroupold->timeto;  
//         $data->timeto = date("H:i:s", strtotime("+$data->id hour"));
//         R::store($data);  
//     }
// }

?>