<?php
include("../public/impOpreation.php");

$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");

if (empty($do)) {  
$today = date("Y-m-d H:i:s");
    $date = date('Y-m-d');
    $userid = $_SESSION['userid'];
    $trainerssettingid = filter_input(INPUT_POST, 'trainerssettingid');
    $teacherortrainer = filter_input(INPUT_POST, 'teacherortrainer');
    // $paymentsystems1 = filter_input(INPUT_POST, 'paymentsystems1');
    // $paymentsystems2 = filter_input(INPUT_POST, 'paymentsystems2');
    // $paymentsystems3 = filter_input(INPUT_POST, 'paymentsystems3');
    // $paythreemonths = filter_input(INPUT_POST, 'paythreemonths');
    // $paysixmonths = filter_input(INPUT_POST, 'paysixmonths');
    // $payonemonths = filter_input(INPUT_POST, 'payonemonths');
    // $paytwomonths = filter_input(INPUT_POST, 'paytwomonths');
    // $payfourmonths = filter_input(INPUT_POST, 'payfourmonths');
    // $payeightmonths = filter_input(INPUT_POST, 'payeightmonths');
    $welcomemessage = filter_input(INPUT_POST, 'welcomemessage');
    $showpayedit =$_POST['showpayedit'];
    $showpaydel =$_POST['showpaydel'];

    // $payyear = filter_input(INPUT_POST, 'payyear');
    // $payapplied = filter_input(INPUT_POST, 'payapplied');
    // $studentaddsubjectid = filter_input(INPUT_POST, 'studentaddsubjectid');
    $showdel = filter_input(INPUT_POST, 'showdel');
    // $studentsettingpaysitr = filter_input(INPUT_POST, 'studentsettingpaysitr');
    $trainerssetting = R::load('trainerssetting',1);
    
    $usergroup = R::getAll('select * from usergroup where 1');
    $smarty->assign("usergroup",$usergroup);

    
    
    //  for ($i1 = 1; $i1 <= $studentsettingpaysitr; $i1++) {
    //         $studentsettingpayid = filter_input(INPUT_POST, 'studentsettingpayid' . $i1);
    //         $name = filter_input(INPUT_POST, 'name' . $i1);
    //         $countmonths = filter_input(INPUT_POST, 'countmonths' . $i1);
    //         $price = filter_input(INPUT_POST, 'price' . $i1);
    //         $stopping = filter_input(INPUT_POST, 'stopping' . $i1);

            
    //         if (!$studentsettingpayid) {
    //             $studentsettingpayssave = R::dispense('studentsettingpays');
    //             $studentsettingpayssave->del = 0;
    //             $studentsettingpayssave->addtoday = $today;  
    //             $studentsettingpayssave->adduserid = $userid;
    //             $studentsettingpayssave->deltoday = '';  
    //             $studentsettingpayssave->deluserid = '';

    //         }else{
    //           $studentsettingpayssave = R::load('studentsettingpays',$studentsettingpayid); 
    //             $studentsettingpayssave->del = 1;
    //             $studentsettingpayssave->updatetoday = $today;          
    //             $studentsettingpayssave->updateuserid = $userid;

    //         }
    //         if (!$name  || $price < 1) {continue;}
    //         $studentsettingpayssave->name = $name;
    //         $studentsettingpayssave->countmonths = $countmonths;
    //         $studentsettingpayssave->price = $price;  
    //         $studentsettingpayssave->stopping = $stopping;
    //         R::store($studentsettingpayssave);
    //  }
    if($trainerssettingid == 1){
        $trainerssetting->updateuserid = $userid; 
        $trainerssetting->teacherortrainer  = $teacherortrainer;
        // if (!$paymentsystems1 && !$paymentsystems2 && !$paymentsystems3) {$paymentsystems1 = 1;}
        // $studentsetting->paymentsystems1 = $paymentsystems1;
        // $studentsetting->paymentsystems2 = $paymentsystems2;
        // $studentsetting->paymentsystems3 = $paymentsystems3;
        // $studentsetting->studentaddsubjectid = $studentaddsubjectid;
        $trainerssetting->showdel = $showdel;
        // $studentsetting->paythreemonths = $paythreemonths;
        // $studentsetting->paysixmonths = $paysixmonths;
        // $studentsetting->payeightmonths = $payeightmonths;
        // $studentsetting->paytwomonths = $paytwomonths;
        // $studentsetting->payonemonths = $payonemonths;
        // $studentsetting->payfourmonths = $payfourmonths;
        // $studentsetting->payapplied = $payapplied;
        // $studentsetting->payyear = $payyear;
        $trainerssetting->welcomemessage = $welcomemessage;
        $trainerssetting->showpayedit = 'aa,'.implode(",",$showpayedit);
        $trainerssetting->showpaydel =  'aa,'.implode(",",$showpaydel);
        R::store($trainerssetting);
    }
    // $studentaddsubjects = R::load('studentaddsubjects',$studentsetting->studentaddsubjectid);
    // $studentsubjects = R::load('studentsubjects',$studentaddsubjects->studentsubjectid);
    // $studentsubjectgroups = R::load('studentsubjectgroups',$studentaddsubjects->studentsubjectgroupid);
    // $subjectgroups =$studentsubjects->subjectname.'/'.$studentsubjectgroups->subjectgroupname.'/'.$studentsubjectgroups->subjectgroupprice;
    // $smarty->assign("subjectgroups",$subjectgroups);
    // $smarty->assign("studentaddsubjects",$studentaddsubjects);
    
    // $studentsettingpays = R::findAll('studentsettingpays');
    // $smarty->assign("studentsettingpays",$studentsettingpays);
    $smarty->assign("showpayedit",explode(",",$trainerssetting->showpayedit));
    $smarty->assign("showpaydel",explode(",",$trainerssetting->showpaydel));
    $smarty->assign("trainerssetting",$trainerssetting);
    $smarty->display("header.html");
    $smarty->display("trainerssettingview/edit.html");
    $smarty->assign("trainers", 1);
    $smarty->display("footer.html");
    
} else if ($do == "pays") { 
$itr = filter_input(INPUT_POST, 'itr');  
$smarty->assign('itr', $itr);
$smarty->assign('student', 1);
$smarty->display("studentsettingview/pays.html");

} else if ($do == "removeappend") { 
$id = filter_input(INPUT_POST, 'id');  
 $tables = R::load('studentsettingpays',$id);
        R::trash($tables);
        echo '1';
        exit();
}else if($do == "excel"){
// include '../Classes/PHPExcel/PHPExcel.php';
// include '../Classes/PHPExcel/IOFactory.php';


//          if(isset($_FILES["excel_file"]["name"])){
//             $path = $_FILES["excel_file"]["tmp_name"];
//             $object = PHPExcel_IOFactory::load($path);
//             foreach($object->getWorksheetIterator() as $worksheet)
//             {
//                 $highestRow = $worksheet->getHighestRow();
//                 $highestColumn = $worksheet->getHighestColumn();
//                 for($row=3; $row<=$highestRow; $row++)
//                 {
//                     $t0 = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
//                     $t1 = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
//                     $t2 = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
//                     $t3 = $worksheet->getCellByColumnAndRow(3, $row)->getValue();
//                     $t4 = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
//                     $t5 = $worksheet->getCellByColumnAndRow(5, $row)->getValue();
//                     $t6 = $worksheet->getCellByColumnAndRow(6, $row)->getValue();    
//                     $t7 = $worksheet->getCellByColumnAndRow(7, $row)->getValue();    
//                     $t8 = $worksheet->getCellByColumnAndRow(8, $row)->getValue();    
//                     $t9 = $worksheet->getCellByColumnAndRow(9, $row)->getValue();    
//                     $t10 = $worksheet->getCellByColumnAndRow(10, $row)->getValue();    
//                     $t11 = $worksheet->getCellByColumnAndRow(11, $row)->getValue();    
//                     if($t1){
                        
                        
//                     $students = R::dispense('students');
//                     $students->del = 0;
//                     $students->addtoday = $today;          
//                     $students->adduserid = $userid;
//                     $students->deltoday = '';      
//                     $students->deluserid = '';    
//                     $students->studentname  = $t1." ".$t2;
//                     $students->studentdatebirth  = $t5;
//                     $students->studentnumber = $t0;
//                     $students->idnumber = $t0;
//                     $students->studentphone = $t0;
//                     $students->parentsname = $t2;
//                     $students->parentsphone = $t8;
//                     $students->studentaddress = $studentaddress;
//                     $students->clientid = $clientid; 
//                     $students->branchid = $t7; 
//                     $students->studentimage = $studentimage;
//                     $students->studentnote = $studentnote;
//                     $id = R::store($students);
//                     R::exec("INSERT INTO `client`(`clientname`, `clientaddress`, `clientphone`, `clientdate`, `userid`, `branchId`, `studentid`) VALUES 
//                     ('$students->studentname', '$t1', '$t0','$date', $userid, $t7, $id)");
//                     $client = R::getCell("SELECT clientid FROM `client` WHERE studentid =  $id");
//                     $students = R::load('students',$id);
//                     $students->clientid = $client; 
//                     $studentid = R::store($students);
//                     }
//                 }
                
//             }
             
//          }
        // studentsPay();
       
       
    }


// function studentsubjectscontroller() {
//     $today = date("Y-m-d H:i:s");
//     $date = date('Y-m-d');
//     $userid = $_SESSION['userid'];
//         $subjects = R::dispense('studentsubjects');
//         $subjects->del = 0;
//         $subjects->addtoday = $today;  
//         $subjects->adduserid = $userid;
//         $subjects->deltoday = '';  
//         $subjects->deluserid = '';
//         $subjects->subjectname  = "كورة";
//         $subjects->subjectprice = "0";
//         $subjects->deviceid = '01';
//         $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 = $ni . " مجموعه" ;
//             $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');
//     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 . "' ");
//             $students = R::findAll('students'); 
//             $studentp = '0';
//             foreach($students as $student){
//                 $studentp .= ','.$student->id;
//             }
//             $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);
//                 }
//             }

            
           
//         }
//     }
//     studentsPay();
// }
function studentsPay() {
    $today = date("Y-m-d H:i:s");
    $date = date('Y-m-d');
    $userid = $_SESSION['userid'];
    $students = R::findAll('students'); ;  
        foreach($students as $student) {
            $studentaddsubjectdetails = R::findOne('studentaddsubjectdetails', " studentaddsubjectid = 1 and del < 2 and studentid = $student->id ");  
            $startdate = date('Y-m-d');
            
            $limit = rand(70, 1);
            $studentspaymentsystems = R::load('studentspaymentsystems',$student->parentsphone);
            $paymonths = $studentspaymentsystems->paymentmonth;
            $paytype = $studentspaymentsystems->paymentname;
            $payprice = $studentspaymentsystems->paymentprice;
            $paytypenum = 2;


        $studentspays = R::dispense('studentspays');
        $studentspays->del = 0;
        $studentspays->addtoday = $today;          
        $studentspays->adduserid = $userid;
        $studentspays->deltoday = '';  
        $studentspays->deluserid = '';
        $paystartdate = $startdate;
        $payenddate = date('Y-m-d', strtotime("+1 month", strtotime($startdate)));
        
         $studentaddsubjects = R::findone('studentaddsubjects');
     $studentspays->studentaddsubjectid = $studentaddsubjects->id;
    $studentspays->studentsubjectid = $studentaddsubjects->studentsubjectid;
    $studentspays->studentsubjectgroupid = $studentaddsubjects->studentsubjectgroupid;
    $studentspays->studentspaymentsystemid = $studentspaymentsystemid;
    $studentspays->studentaddsubjectdetailid = $studentaddsubjectdetailid;
    $studentspays->studentid = $studentaddsubjectdetails->studentid;
    $studentspays->paymonths  = $paymonths;
    $studentspays->paytype = $paytype;
    $studentspays->paytypenum = $paytypenum;
    $studentspays->payprice = $payprice;
    $studentspays->countmonths = $countmonths;
    $studentspays->newprice = $newprice;
    $studentspays->paystartdate  = $paystartdate;
    $studentspays->payenddate  = $payenddate;
    $studentspays->paytypered  = $paytypered;
    $studentspays->paykind  = $paykind;
    
        $studentspayid2 = R::store($studentspays);
        $students2 = R::load('students',$studentaddsubjectdetails->studentid);
        $students2->studentaddsubjectid = $studentaddsubjects->id;
        $students2->studentsubjectid = $studentaddsubjects->studentsubjectid;
        $students2->studentsubjectgroupid = $studentaddsubjects->studentsubjectgroupid;
        $students2->studentspaymentsystemid = $studentspaymentsystemid;
        $students2->studentaddsubjectdetailid = $studentaddsubjectdetailid;
        $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  = $studentspayid2;
        R::store($students2);
        // $studentsubjects = R::load('studentsubjects',$studentaddsubjects->studentsubjectid);
        // $incomeName = $students2->studentname. 'دفع  ' . $textpay . 'تاريخ الانتهاء  ' .$payenddate;
        // if (!$studentspayid) {
        //      $send_data = array(
        //     'clientid' => $students2->clientid,
        //     'saveid' => $_SESSION['saveid'],
        //     'Costcenterid' => -1,
        //     'Value' => $payprice,
        //     'comment' => $paytype,
        //     'name' => $students2->studentname. 'دفع  ' . $textpay . 'تاريخ الانتهاء  ' .$payenddate,
        //     'parent' => $studentsubjects->incometypeid,
        //     );
        //     CURL_IT2($send_data, 'incomeController.php?do=add');
        // }else{
        //     $income = R::findOne("SELECT income.* FROM `income` WHERE clientid = $students2->clientid order by incomeId desc");
        //      $send_data = array(
        //     'clientid' => $students2->clientid,
        //     'incomeId' => $income->incomeId,
        //     'dailyentryid' => $income->dailyentryid,
        //     'saveid' => $_SESSION['saveid'],
        //     'Costcenterid' => -1,
        //     'Value' => $payprice,
        //     'comment' => $paytype,
        //     'name' =>  $students2->studentname. 'دفع  ' . $textpay . 'تاريخ الانتهاء  ' .$payenddate,
        //     'parent' => $studentsubjects->incometypeid,
        //     );
        //     CURL_IT2($send_data, 'incomeController.php?do=update');
        // }        
        // echo $studentspayid2;
            
        }
    
}






?>