HEX
Server: Apache
System: Linux server1.royalgt4.com 4.18.0-553.89.1.lve.el8.x86_64 #1 SMP Wed Dec 10 13:58:50 UTC 2025 x86_64
User: mostafedeg (1125)
PHP: 5.6.40
Disabled: mail,passthru,parse_ini_file,show_source,eval,assert,pcntl_exec,dl,putenv,proc_open,popen
Upload Files
File: /home/mostafedeg/public_html/erp/controllers/visitsApi.php
<?php


include_once("../public/config.php");

if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: *");
    //header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');    // cache for 1 day
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
        header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
    exit(0);
}
$post_data = file_get_contents("php://input");
if (empty($post_data)) {
    //for ios
    $get_request = json_decode(json_encode($_POST));
} else {
    //for android
    $get_request = json_decode($post_data);
}
########################################################
$do = $_GET['do'];





if (empty($do)) {
   
}else if ($do == "recordAttendance") {
    $user_id = isset($get_request->user_id) ? $get_request->user_id : '';
    $lat = isset($get_request->lat) ? $get_request->lat : '';
    $long = isset($get_request->long) ? $get_request->long : '';
    // $password = isset($get_request->password) ? $get_request->password : '';
    // $device_id = isset($get_request->device_id) ? $get_request->device_id : '';
    $today = date('Y-m-d');

    if (!$user_id || !$lat || !$long) { // || !$email
        $reason = 'ﺑﻌﺾ اﻟﺤﻘﻮﻝ اﻟﻤﻄﻠﻮﺑﺔ ﻓﺎﺭﻏﺔ';
        $status = 2; //0;
        echo json_encode(array('status' => $status, 'reason' => $reason), JSON_NUMERIC_CHECK);
        exit();
    }

    $checkrecordAttendance = R::count('recordattendance',"user_id = ? AND today = '$today' and conditions = 0 ", [$user_id]);
    if ($checkrecordAttendance > 0) {
        $reason = ' تم تسجيل الحضور من قبل ';
        $status = 2; //4;
        echo json_encode(array('status' => $status, 'reason' => $reason), JSON_NUMERIC_CHECK);
        exit();
    }
  
    
    // #phone
    // $check_mobile = R::count('webclients','mobile = ? AND is_active = 1', [$mobile]);
    // if ($check_mobile > 0) {
    //     $reason = 'ﺭﻗﻢ اﻟﻤﻮﺑﺎﻳﻞ ﻣﻜﺮﺭ';
    //     $status = 2; //6;
    //     echo json_encode(array('status' => $status, 'reason' => $reason), JSON_NUMERIC_CHECK);
    //     exit();
    // }

    
   try {
        $row = R::dispense('recordattendance');
        $row->user_id = $user_id;
        $row->lat = $lat;
        $row->long = $long;
        $row->conditions = 0;
        $row->today = $today;
        $row->addtoday = date('Y-m-d H:i:s');
        $id = R::store($row);
        if (!empty($id)) {
            R::exec("UPDATE user SET lastendvisit = '' WHERE userid = ?",[$user_id]);
            $reason = 'تم  تسجيل الحضور بنجاح';
            $status = 1;
        } else {
            $reason = 'Failed';
            $status = 2;
        }
    } catch (Exception $e) {
        $reason = $e->getMessage();
        $status = 2; //3;
    }

    echo json_encode(array('status' => $status, 'reason' => $reason, 'recordattendance_id' => $id));
}else if ($do == "login") {
    $username = isset($get_request->username) ? $get_request->username : '';
    $password = isset($get_request->password) ? $get_request->password : '';
    if (!empty($username) && !empty($password)) {
       
        $user =R::getROW("SELECT * FROM `user` WHERE username = '" . $username . "'  and password = '" . $password . "' ");
        // $user = R::findOne('user', " username = '" . $$username . "'  and password = '" . $password . "' ");
        if ($user['userid']> 0) {
            $send_data = array("status" => 1, "reason" =>   'ﺗﻢ اﻟﺪﺧﻮﻝ ﺑﻨﺠﺎﺡ'  , "user_id" => $user['userid'], "name" => $user['employeename']);
        } else {
            $send_data = array("status" => 2, "reason" => 'بيانات  التسجيل خطأ' , "user_id" => 0, "name" => '');
        }
    } else {
        $send_data = array("status" => 3, "reason" => 'ﺑﻌﺾ اﻟﺤﻘﻮﻝ اﻟﻤﻄﻠﻮﺑﺔ ﻓﺎﺭﻏﺔ', "user_id" => 0, "name" => '');
    }

    echo json_encode($send_data);

}else if ($do == "visits") {

    $user_id = isset($get_request->user_id) ? $get_request->user_id : '';
    $visit_id = isset($get_request->user_id) ? $get_request->visit_id : '';
    $today = date('Y-m-d');

    if (!$user_id) { // || !$email
        $reason = 'ﺑﻌﺾ اﻟﺤﻘﻮﻝ اﻟﻤﻄﻠﻮﺑﺔ ﻓﺎﺭﻏﺔ';
        $status = 2; //0;
        echo json_encode(array('status' => $status, 'reason' => $reason), JSON_NUMERIC_CHECK);
        exit();
    }
    $src = Host_URL . '/views/default/images/visits/';
    if(!$visit_id){

        $visits = R::getAll("select visits.*, clientname , CONCAT('$src', visits.imageendvisit) as image, government.governmentname, clientarea.name as clientareaname, cases from visits 
            LEFT JOIN client ON visits.clientid = client.clientid 
            LEFT JOIN clientarea ON client.clientareaid = clientarea.id 
            LEFT JOIN goverarea ON client.clientareaid = goverarea.clientareaid 
            LEFT JOIN government ON goverarea.governmentid = government.governmetid 
             LEFT JOIN visitcases ON visits.visitcaseid = visitcases.id
             where visits.conditions = 0 and visits.delegateid = ? and dateofvisit = '$today' ", [$user_id]);     
    }else{
        $visits = R::getROW("select visits.*, clientname , CONCAT('$src', visits.imageendvisit) as image, government.governmentname, clientarea.name as clientareaname, cases from visits 
            LEFT JOIN client ON visits.clientid = client.clientid 
            LEFT JOIN clientarea ON client.clientareaid = clientarea.id 
            LEFT JOIN goverarea ON client.clientareaid = goverarea.clientareaid 
            LEFT JOIN government ON goverarea.governmentid = government.governmetid
            LEFT JOIN visitcases ON visits.visitcaseid = visitcases.id
            where visits.id = ? ",[$visit_id]);    
    }
    echo json_encode($visits);

}else if ($do == "visitcases") {
    $visits = R::getAll("select visitcases.* from visitcases  where visitcases.conditions = 0 ");     
    echo json_encode($visits);
}else if ($do == "visitStart") {
    $visit_id = isset($get_request->visit_id) ? $get_request->visit_id : '';
    $clientid = isset($get_request->clientid) ? $get_request->clientid : '';
    $delegateid = isset($get_request->user_id) ? $get_request->user_id : '';
    $start_lat = isset($get_request->start_lat) ? $get_request->start_lat : '';
    $start_long = isset($get_request->start_long) ? $get_request->start_long : '';
     $img = isset($get_request->imageendvisit) ? $get_request->imageendvisit : '';
    $today = date('Y-m-d');
    $addtoday = date('Y-m-d H:i:s');


// $from_time = strtotime($datetime_1); 
// $to_time = strtotime($datetime_2); 
// $diff_minutes = round(abs($from_time - $to_time) / 60,2). " minutes";

// $hours = floor($diff_minutes / 60);
// $min = $diff_minutes - ($hours * 60);


    if (!$visit_id || !$start_lat || !$start_long || !$img) { // || !$email
        $reason = 'ﺑﻌﺾ اﻟﺤﻘﻮﻝ اﻟﻤﻄﻠﻮﺑﺔ ﻓﺎﺭﻏﺔ';
        $status = 2; //0;
        echo json_encode(array('status' => $status, 'reason' => $img), JSON_NUMERIC_CHECK);
        exit();
    }


    $checkrecordAttendance = R::count('recordattendance',"user_id = ? AND today = '$today' and conditions = 0 ", [$delegateid]);
    if ($checkrecordAttendance == 0) {
        $reason = ' يجب تسجيل الحضور اولا ';
        $status = 2; //4;
        echo json_encode(array('status' => $status, 'reason' => $reason), JSON_NUMERIC_CHECK);
        exit();
    }

    $checkvisits = R::getROW("select * from visits  where delegateid = ? AND dateofvisit = '$today' and newvisit = 1 and id != ?  ",[$delegateid, $visit_id]);    
    if ($checkvisits) {
        $reason = ' يجب  انهء الزياره رقم   ' .$checkvisits['id']. ' اولا';
        $status = 2; //4;
        echo json_encode(array('status' => $status, 'reason' => $reason), JSON_NUMERIC_CHECK);
        exit();
    }


    $checknewvisit = R::getROW("select * from visits where id = ?",[$visit_id]);    
    if ($checknewvisit['newvisit'] > 1) {
        $reason = 'تم انهاء الزياره';
        $status = 2; //4;
        echo json_encode(array('status' => $status, 'reason' => $reason), JSON_NUMERIC_CHECK);
        exit();
    }


    $imageendvisit = uniqid() . '.png';
    $success = file_put_contents('../views/default/images/visits/' . $imageendvisit, base64_decode($img));
    if (!$success) {
        echo json_encode(array('status' => null, 'image_path' => null, 'error' => $success), JSON_NUMERIC_CHECK);
    }


    $client = R::getROW("select * from client where clientid = ?",[$clientid]);  
    $user = R::getROW("select *, end_date  from user LEFT JOIN visits ON user.lastendvisit = visits.id  where user.userid = ? ",[$delegateid]); 
    $lastenddate = $user['end_date'];
    $before_visitid = $user['lastendvisit'];

    if($lastenddate){
        $from_time = strtotime($lastenddate); 
        $to_time = strtotime($addtoday); 
        $diff_minutes = abs($to_time - $from_time);
        $hours   = floor($diff_minutes / (60*60)); 
        $min  = floor(($diff_minutes - $hours*60*60)/ 60);
        $seconds = floor($diff_minutes - $hours*60*60 - $min*60); 
        $textdiffbetvisit =  $hours . ':'.$min.':'.$seconds;
    }
    $spaces = '';
    $spacestrue = 1;
    if($client['vlat']){
            
            $lat1 = $client['vlat'];
            $lon1 = $client['vlong'];
            $lat2 = $start_lat;
            $lon2 = $start_long;
            $unit = "K";
            
            $theta = $lon1 - $lon2;
            $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
            $dist = acos($dist);
            $dist = rad2deg($dist);
            $miles = $dist * 60 * 1.1515;
            $unit = strtoupper($unit);
            if ($unit == "K") {
              $spaces =  ($miles * 1.609344) * 1000;
            } else if ($unit == "N") {
              $spaces = ($miles * 0.8684);
            } else {
              $spaces = $miles;
            }
            if($spaces > 250){
               $spacestrue = 0; 
            }else{
               $spacestrue = 1; 
            }
    
    
    
    }
    
    // print_r($spaces * 1000);
    // exit();
    
    


    try {
        R::exec("UPDATE visits SET start_lat = '$start_lat', start_long = '$start_long', start_date = '$addtoday' , before_visitid = '$before_visitid', vlastenddate = '$lastenddate', textdiffbetvisit = '$textdiffbetvisit', 
        imageendvisit = '$imageendvisit', newvisit = 1, clat = '$lat1', clong = '$lon1', vspaces = '$spaces', vspacestrue = $spacestrue  WHERE id = ?",[$visit_id]);
        R::exec("UPDATE client SET vlat = '$start_lat', vlong = '$start_long', vimage = '$imageendvisit' WHERE clientid = ? and vlat is null ",[$clientid]);
         
        $reason = 'تم بدا الزيارة بنجاح';
        $status = 1;
      
    } catch (Exception $e) {
        $reason = $e->getMessage();
        $status = 2; //3;
    }

    echo json_encode(array('status' => $status, 'reason' => $reason));

}else if ($do == "base64ToImage") {
   
//  $date1 = "2009-12-07 22:44:00"; 

// $date2 = "2009-12-07 23:46:00"; 

// $diff = abs(strtotime($date2) - strtotime($date1)); 


// $hours   = floor($diff / (60*60)); 

// $minuts  = floor(($diff - $hours*60*60)/ 60); 

// $seconds = floor($diff - $hours*60*60 - $minuts*60); 

// printf($hours.":".$minuts.":".$seconds); 
//     if ($success) {
//         echo json_encode(array('image_name' => $file_name, 'image_path' => Host_URL . '/views/default/images/visits/' . $file_name, 'error' => 0), JSON_NUMERIC_CHECK);
//     } else {
//         echo json_encode(array('image_name' => null, 'image_path' => null, 'error' => 'Unable to save the file.'), JSON_NUMERIC_CHECK);
//     }
}else if ($do == "visitEnd") {
    $visit_id = isset($get_request->visit_id) ? $get_request->visit_id : '';
    $clientid = isset($get_request->clientid) ? $get_request->clientid : '';
    $delegateid = isset($get_request->user_id) ? $get_request->user_id : '';
    $end_lat = isset($get_request->end_lat) ? $get_request->end_lat : '';
    $end_long = isset($get_request->end_long) ? $get_request->end_long : '';
    $note = isset($get_request->note) ? $get_request->note : '';
    $visitcaseid = isset($get_request->visitcaseid) ? $get_request->visitcaseid : '';


    


    $today = date('Y-m-d');
    $addtoday = date('Y-m-d H:i:s');



    if (!$visit_id || !$end_lat || !$end_long || !$visitcaseid  ) { // || !$email
        $reason = 'ﺑﻌﺾ اﻟﺤﻘﻮﻝ اﻟﻤﻄﻠﻮﺑﺔ ﻓﺎﺭﻏﺔ';
        $status = 2; //0;
        echo json_encode(array('status' => $status, 'reason' => $reason), JSON_NUMERIC_CHECK);
        exit();
    }


   



    $checkvisits = R::getROW("select * from visits  where delegateid = ? AND dateofvisit = '$today' and newvisit = 1 and id != ? ",[$delegateid, $visit_id]);    
    if ($checkvisits) {
        $reason = ' يجب  انهء الزياره رقم   ' .$checkvisits['id']. ' اولا';
        $status = 2; //4;
        echo json_encode(array('status' => $status, 'reason' => $reason), JSON_NUMERIC_CHECK);
        exit();
    }



    $checknewvisit = R::getROW("select * from visits where id = ?",[$visit_id]);    
    if ($checknewvisit['newvisit'] == 0) {
        $reason = 'يجب بدا الزياره اولا ';
        $status = 2; //4;
        echo json_encode(array('status' => $status, 'reason' => $reason), JSON_NUMERIC_CHECK);
        exit();
    }


 




    $lastdebt = R::getROW("select * from clientdebtchange where clientid = ? order by clientdebtchangeid desc limit 1",[$clientid]);  
    $processname = "  زياره  بتاريخ  $today ";
    $clientdebtchangeafter = $lastdebt['clientdebtchangeafter'];
    $clientdebtchangebefore = $lastdebt['clientdebtchangebefore'];
    $clientdebtchangeamount = $lastdebt['clientdebtchangeamount'];
    $clientdebtchangetype =  $lastdebt['clientdebtchangetype'];
    
    
        $from_time = strtotime($checknewvisit['start_date']); 
        $to_time = strtotime($addtoday); 
        $diff_minutes = abs($to_time - $from_time);
        $hours   = floor($diff_minutes / (60*60)); 
        $min  = floor(($diff_minutes - $hours*60*60)/ 60);
        $seconds = floor($diff_minutes - $hours*60*60 - $min*60); 
        $textdiffbetvisit =  $hours . ':'.$min.':'.$seconds;
    
    

    try {
        R::exec("UPDATE visits SET end_lat = '$end_lat', end_long = '$end_long', end_date = '$addtoday', vdiffminutes = '$textdiffbetvisit', note = '$note', visitcaseid = $visitcaseid, newvisit = 2  WHERE id = ?",[$visit_id]);
        R::exec("UPDATE user SET lastendvisit = $visit_id WHERE userid = ?  ",[$delegateid]);
        R::exec("INSERT INTO `clientdebtchange`(`clientid`, `clientdebtchangebefore`, `clientdebtchangeamount`, `clientdebtchangetype`, `processname`,`clientdebtchangemodelid`,`clientdebtchangeafter`, `clientdebtchangedate`, `userid`, `tablename`) VALUES ($clientid,'$clientdebtchangebefore','$clientdebtchangeamount',$clientdebtchangetype,'$processname',$visit_id,'$clientdebtchangeafter','$addtoday','$delegateid','visitsController.php')");
        $reason = 'تم انهاء الزيارة بنجاح';
        $status = 1;
      
    } catch (Exception $e) {
        $reason = $e->getMessage();
        $status = 2; //3;
    }

    echo json_encode(array('status' => $status, 'reason' => $reason));

}else if ($do == "clients") {

    $user_id = isset($get_request->user_id) ? $get_request->user_id : '';
    $client_id = isset($get_request->user_id) ? $get_request->client_id : '';
    $search_name = isset($get_request->search_name) ? $get_request->search_name : '';
    $today = date('Y-m-d');

    if (!$user_id) { // || !$email
        $reason = 'ﺑﻌﺾ اﻟﺤﻘﻮﻝ اﻟﻤﻄﻠﻮﺑﺔ ﻓﺎﺭﻏﺔ';
        $status = 2; //0;
        echo json_encode(array('status' => $status, 'reason' => $reason), JSON_NUMERIC_CHECK);
        exit();
    }

    $searchQuery = ' ';
    if($search_name){
          $searchQuery .= ' and CONCAT(client.clientname,"/",clientcode)  LIKE "%'.$search_name.'%"  ';
    }

    if(!$client_id){
        $clients = R::getAll("select client.* from client where client.conditions = 0 and client.delegateid = ?  $searchQuery ", [$user_id]);     
    }else{
        $clients = R::getROW("select client.* from client where clientid = ? ",[$visit_id]);    
    }
    echo json_encode($clients);
}else if ($do == "clientReports") {

    $user_id = isset($get_request->user_id) ? $get_request->user_id : '';
    $client_id = isset($get_request->user_id) ? $get_request->client_id : '';
    $search_name = isset($get_request->search_name) ? $get_request->search_name : '';
    $clientdebtchange_id = isset($get_request->search_name) ? $get_request->clientdebtchange_id : '';
    $today = date('Y-m-d');

    if (!$client_id) { // || !$email
        $reason = 'ﺑﻌﺾ اﻟﺤﻘﻮﻝ اﻟﻤﻄﻠﻮﺑﺔ ﻓﺎﺭﻏﺔ';
        $status = 2; //0;
        echo json_encode(array('status' => $status, 'reason' => $reason), JSON_NUMERIC_CHECK);
        exit();
    }

    // $searchQuery = ' ';
    // if($search_name){
    //       $searchQuery .= ' and client.clientname LIKE "%'.$search_name.'%"  ';
    // }

    // if($client_id){
    //       $searchQuery .= ' and clientdebtchange.clientid = $client_id  ';
    // }


    // if($search_name){
    //       $searchQuery .= ' and clientdebtchange.clientname LIKE "%'.$search_name.'%"  ';
    // }

    // if(!$clientdebtchange_id){
        // $clientdebtchanges = R::getAll("select clientdebtchange.* from clientdebtchange  
        //     LEFT JOIN client ON clientdebtchange.clientid = client.clientid where client.conditions = 0 and  clientdebtchange.del = 0 and client.delegateid = ?  $searchQuery ", [$user_id]);     
        $client = R::getROW("select clientname, clientphone, clientdetails, clientdebt, debtlimit, debtlimit - clientdebt as residual from client where clientid = ? ",[$client_id]);    
        $clientdebtchanges = R::getAll("select processname, clientdebtchangeamount, clientdebtchangedate from clientdebtchange  where clientdebtchange.clientid  = ? order by clientdebtchangeid desc ", [ $client_id]);  
        $sellbilltotal = R::getcell("select sum(sellbillfinalbill) from sellbill where conditions = 0 and sellbillclientid = ? ",[$client_id]);
        $lastsellbill = R::getROW("select * from sellbill where conditions = 0 and sellbillclientid = ? ORDER BY sellbillid DESC LIMIT 1 ",[$client_id]);
        

    // }else{
    //     $clientdebtchanges = R::getROW("select clientdebtchange.* from clientdebtchange where clientdebtchangeid = ? ",[$clientdebtchange_id]);    
    // }
    $datas = [
        'client' => $client,
        'sellbilltotal' => $sellbilltotal,
        // 'lastsellbill' => $lastsellbill,
        'lastsellbilldate' => $lastsellbill['sellbilldate'],
        'clientdebtchanges' => $clientdebtchanges
    ];

    echo json_encode($datas);    
}else if ($do == "profile") {
    $user_id = isset($get_request->user_id) ? $get_request->user_id : '';

    if ($user_id) {
        $user = R::getRow("select * from user where userid = ? ", [$user_id]);
        // if ($projectclient['userid'] > 0) {
            echo json_encode($user);
        // } else {
        //     $send_data = array("status" => 2, "reason" => 'بيانات  التسجيل خطأ' , "userid" => 0, "name" => '');
        //      echo json_encode($send_data);
        // }
    } else {
        $send_data = array("status" => 3, "reason" => 'ﺑﻌﺾ اﻟﺤﻘﻮﻝ اﻟﻤﻄﻠﻮﺑﺔ ﻓﺎﺭﻏﺔ', "userid" => 0, "name" => '');
         echo json_encode($send_data);
    }
}else if ($do == "updateProfile") {
    $user_id = isset($get_request->user_id) ? $get_request->user_id : '';
    $password = isset($get_request->password) ? $get_request->password : '';
    
      if (!$user_id || !$password) { // || !$email
        $reason = 'ﺑﻌﺾ اﻟﺤﻘﻮﻝ اﻟﻤﻄﻠﻮﺑﺔ ﻓﺎﺭﻏﺔ';
        $status = 2; //0;
        echo json_encode(array('status' => $status, 'reason' => $reason), JSON_NUMERIC_CHECK);
        exit();
    }

    if ($user_id) {
         R::exec("UPDATE user SET password='$password' WHERE userid = $user_id" );
         $send_data = array("status" => 1, "reason" => 'تم تعديل كلمة المرور بنجاح');
         echo json_encode($send_data);
    } else {
        $send_data = array("status" => 3, "reason" => 'ﺑﻌﺾ اﻟﺤﻘﻮﻝ اﻟﻤﻄﻠﻮﺑﺔ ﻓﺎﺭﻏﺔ', "userid" => 0, "name" => '');
         echo json_encode($send_data);
    }    
}