MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: /home/cafsindia/cpaqua.cafsinfotech.in/dump/app/api_model.php
<?php
include('./dbconnect.php');
require('./vendor/autoload.php');
error_reporting(0);
date_default_timezone_set('Asia/Kolkata');
class api_model extends dbconnect{
	protected $app_url;
	private $enckey     = 'vDIa5JdknBqfrKOu8d7UpddnBMCH1vza'; //32 characters
	public function __construct(){
		$this->open_db();
		// $this->$app_url 	= ""; //API URL
		// $this->hrms_open_db();
		//$this->sql_open_db();
    }	
	// GET TOKEN
	public function get_token(){
		$curl = curl_init();
		curl_setopt_array($curl, array(
			CURLOPT_URL            => 'http://localhost/smart_hrms_8_3/api_interface/index.php',
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING       => '',
			CURLOPT_MAXREDIRS      => 68,
			CURLOPT_TIMEOUT        => 0,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST  => 'POST',
			CURLOPT_POSTFIELDS     => array('action' => 'get_token' , 'username' => 'marish', 'password' => 'marish2003'),
		));
		$response       		   = curl_exec($curl);
		if(curl_errno($curl)){
   			return  curl_error($curl);
		}else{
			$response = json_decode($response);
			if($response->status){
				return $response->rslt;	
				exit(0);
			}
		}
		curl_close($curl);
	}
	// GET EMPLOYEES 
	public function get_employees($token){
		$rslt                  = "";
		$qry_filter            = "";
		$punch_date            = ""; // ONLY FOR get_punched_data
		$log_count             = 0;
		$cron_status 		   = 'FAILED';
		$cron_status_id        = $this->insert_cron_status("GET EMPLOYEES","get_employees","Auto");//CRON INSERTION
		$hrms_emp_rslt         = json_decode($this->api_interface('get_employees',$token,"",$punch_date));
		$mysql_emp_rslt        = $this->get_mysql_emp("select employee_code from cw_employees ".$qry_filter);
		if($hrms_emp_rslt){
			foreach ($hrms_emp_rslt as $key => $value){
				$employee_code = $value->employee_code;
				$emp_name      = $value->emp_name;
				$dob           = $value->date_of_birth;
				$doj           = $value->date_of_joining;
				$gender        = $value->gender;
				$term_flag     = $value->termination_status;
				$term_date     = $value->resignation_date;
				$email_id      = $value->company_email_id;
				if((int)$term_flag === 0){
					$term_flag = 1;
				}else{
					$term_flag = 2;
				}
				if($employee_code){
					if($mysql_emp_rslt[$employee_code]['employee_code']){
						$prime_update_query  = 'UPDATE cw_employees SET emp_name = "'. $emp_name .'",user_name = "'. $employee_code .'",date_of_birth = "'.$dob.'",date_of_joining = "'.$doj.'",gender = "'.$gender.'",employee_status = "'.$term_flag.'",email = "'.$email_id.'",trans_updated_date = "'.date("Y-m-d H:i:s").'" WHERE employee_code = "'. $employee_code .'"';
						$rslt = $this->runQuery($prime_update_query);
					}else{
						$sql = "insert into cw_employees (employee_code,emp_name,user_name,password,date_of_birth,date_of_joining,gender,employee_status,inactive_date,email,trans_created_date) values ('".$employee_code."','".$emp_name."','". $employee_code ."','".md5($employee_code)."','". $dob ."','". $doj ."','". $gender ."','". $term_flag ."','". $term_date ."','". $email_id ."','".date("Y-m-d H:i:s")."')";
						$rslt = $this->runQuery($sql);
					}
				}		
				$log_count++;	
			}
			$cron_status  = "SUCCESS";		
		}    
		$this->update_cron_status($cron_status_id,$cron_status,$log_count);//CRON UPDATION
		return $rslt;	
	}

	// GET EMPLOYEE PUNCHED DATA
	public function get_punched_data($token){
		$select_type    = ""; // ONLY FOR get_punched_data
		$qry_filter     = ""; 
		$query_filter   = "";
		$log_count      = 0;
		$cron_status    = 'FAILED';
		$cron_status_id = $this->insert_cron_status("GET PUNCHED DATA","get_punched_data","Auto");//CRON INSERTION
		if($select_type){
			$start_date = $from_date;
			$end        = new DateTime($from_date);
			$end_date   = $end->modify("+1 days")->format('Y-m-d');
		}else{
			$date       = new DateTime('11-07-2024');
			$start_date = $date->modify("-1 days")->format('Y-m-d');
			$end        = new DateTime('11-07-2024');
			$end_date   = $end->format('Y-m-d');			
		}
		
		$period = new DatePeriod(
		    new DateTime($start_date),
		    new DateInterval('P1D'),
		    new DateTime($end_date)
		);
		$punched_qry = "";
		$est_emp_qry = "";
		$det_emp_qry = "";
		foreach ($period as $key => $value){
		    $punch_date = $value->format('Y-m-d'); 
		    $rslt ="";
			$te_emp_rslt      = json_decode($this->api_interface("get_punched_data",$token,$query_filter,$punch_date));
			if($te_emp_rslt){
				//Get Employees based on 3 => detailer, 4 => teamleader,5=>PM for detailing
				$det_emp_rslt = $this->get_mysql_emp("select employee_code from cw_employees where trans_status = 1 and role in (3,4,5) ".$qry_filter);			
				//Get Employees based on 12 => estimator, 13 => checker or teamleader,14=>PM for detailing,15=> Partial Checker	
				$est_emp_rslt = $this->get_mysql_emp("select employee_code from cw_employees where trans_status = 1 and role in (12,13,14,15) ".$qry_filter);	
				//Get Detailing and Estimation Timesheet results
				$det_ts_rslt = $this->get_mysql_emp("select employee_code from cw_time_sheet where entry_date = '".$punch_date."' and cw_time_sheet.trans_status = 1 ".$qry_filter);	
				
				$est_ts_rslt = $this->get_mysql_emp("select employee_code from cw_est_time_sheet where entry_date = '".$punch_date."' and cw_est_time_sheet.trans_status = 1 ".$qry_filter);	

				foreach ($te_emp_rslt as $key => $value){
					$employee_code   = $value->employee_code;			
					$entry_date      = $value->att_date;
					$in_time         = $value->punch_in;
					$out_time        = $value->punch_out;
					$total_time      = $value->final_total_work_hours;
					$shift_name      = $value->shift_name;
					if($employee_code){
						if($est_emp_rslt[$employee_code]){ // check if estimation Employee
							if($est_ts_rslt[$employee_code]['employee_code']){
								$prime_update_query  = 'UPDATE cw_est_time_sheet SET entry_date = "'. $entry_date .'",in_time = "'. $in_time .'",out_time = "'.$out_time.'",total_time = "'.$total_time.'",shift_name = "'. $shift_name .'",trans_updated_by = "1",trans_updated_date = "'.date("Y-m-d H:i:s").'" WHERE employee_code = "'. $employee_code .'" and entry_date = "'. $entry_date .'"';
								$rslt = $this->runQuery($prime_update_query);
							}else{	
								$est_emp_qry .= "('".$employee_code."','".$entry_date."','". $in_time ."','".$out_time."','".$total_time."','".$shift_name."','1','".date("Y-m-d H:i:s") ."'),";
							}
						}else
						if($det_emp_rslt[$employee_code]){ // check if Detailer Employee
							if($det_ts_rslt[$employee_code]['employee_code']){
								$prime_update_query  = 'UPDATE cw_time_sheet SET entry_date = "'. $entry_date .'",in_time = "'. $in_time .'",out_time = "'.$out_time.'",total_time = "'.$total_time.'",trans_updated_by = "1",trans_updated_date = "'.date("Y-m-d H:i:s").'" WHERE employee_code = "'. $employee_code .'" and entry_date = "'. $entry_date .'"';
								$rslt = $this->runQuery($prime_update_query);
							}else{	
								$det_emp_qry .= "('".$employee_code."','".$entry_date."','". $in_time ."','".$out_time."','".$total_time."','1','".date("Y-m-d H:i:s") ."'),";
							}
						}	
						$log_count++;			
						$cron_status  = 'SUCCESS';		
					}					
				}				
			}
		}
		$est_emp_qry     = rtrim($est_emp_qry,",");
		$det_emp_qry     = rtrim($det_emp_qry,",");
		if($det_emp_qry){
			$sql = "insert into cw_time_sheet(employee_code,entry_date,in_time,out_time,total_time,trans_created_by,trans_created_date) values $det_emp_qry";
			$rslt = $this->runQuery($sql);
		}	
		if($est_emp_qry){
			$sql = "insert into cw_est_time_sheet(employee_code,entry_date,in_time,out_time,total_time,shift_name,trans_created_by,trans_created_date) values $est_emp_qry";
			$rslt = $this->runQuery($sql);
		}	
		$this->update_cron_status($cron_status_id,$cron_status,$log_count);//CRON UPDATION
		return $rslt;
	}
	
	// GET LEAVE DATA
	public function get_leave_data($token){
		$select_type        = ""; // ONLY FOR get_leave_data		
		$qry_filter         = "";
		$team_filter        = "";
		$log_count          = 0;
		$cron_status        = 'FAILED';
		$cron_status_id     = $this->insert_cron_status("GET LEAVE DATA","get_leave_data","Auto");//CRON INSERTION
		if($select_type   === ""){
			$end_date 		= date('Y-m-d');
			$start_date 	= date('Y-m',strtotime($end_date))."-01";
		}
		//Get leave count from leave entry table
		$sql_leave_rslt = $this->get_mysql_emp('SELECT employee_code,date_format(leave_date,"%m-%Y") as l_month,SUM(leave_count) as l_count FROM `cw_leave_entry` where leave_date between "'.$start_date.'" and "'.$end_date.'" GROUP BY employee_code,date_format(leave_date,"%m-%Y")'.$qry_filter);

		//Get Holiday Working days from time entry table
		$sql_hw_rslt = $this->get_mysql_emp('SELECT employee_code,date_format(att_date,"%m-%Y") as att_month,COUNT(att_date) as hw_count FROM `cw_time_entry` where att_date between "'.$start_date.'" and "'.$end_date.'" and whole_day_status = "H" and final_total_work_hours > 0 GROUP BY employee_code,date_format(att_date,"%m-%Y")'.$qry_filter);
		$hw_arr = array();
		foreach ($sql_hw_rslt as $key => $value) {
			$hw_arr[$value->att_month][$value->employee_code] = $value->hw_count;
		}
		$get_emp_rslt   = $this->get_emp_data("select employee_code,team from cw_employees where trans_status = 1 ".$team_filter);
		$mysql_emp_rslt = $this->get_mysql_leave("select prime_employee_leaves_id as leave_id,employee_code,leave_month,leave_count from cw_employee_leaves where trans_status = 1 ".$qry_filter." GROUP BY employee_code,leave_month");
		$sql_insert = "";
		$sql_upd 	= "";
		foreach ($sql_leave_rslt as $key => $sql_leave) {
			$emp_code 	= $sql_leave->employee_code;
			$l_month 	= $sql_leave->l_month;
			$l_count 	= $sql_leave->l_count;
			$hw_count   = $hw_arr[$l_month][$emp_code];
			if($emp_code){
				$team 	= $get_emp_rslt[$emp_code];
				if($mysql_emp_rslt[$emp_code][$l_month]){
					$leave_id 	= $mysql_emp_rslt[$emp_code][$l_month]['leave_id'];
					$sql_upd   .= "('".$leave_id."','".$l_count."','".$team."','".$hw_count."'),";
				}else{
					if($emp_code && $l_month){
						$sql_insert .= "('".$emp_code."','".$l_count."','". $l_month ."','". $team ."','". $hw_count ."'),";
					}
				}
				$log_count++;
				$cron_status  = 'SUCCESS';		
			}		
		}
		$sql_val = rtrim($sql_insert,',');
		$sql_qry = "insert into cw_employee_leaves (employee_code,leave_count,leave_month,team,hw_count) values $sql_val";
		if($sql_val){
			$rslt = $this->runQuery($sql_qry);
		}
		$sql_upd_val = rtrim($sql_upd,',');
		$sql_upd_qry = "INSERT into `cw_employee_leaves` (prime_employee_leaves_id,leave_count,team,hw_count) VALUES $sql_upd_val ON DUPLICATE KEY UPDATE leave_count = VALUES(leave_count),team = VALUES(team),hw_count = VALUES(hw_count)";
		if($sql_upd_val){
			$rslt = $this->runQuery($sql_upd_qry);
		}
		$this->update_cron_status($cron_status_id,$cron_status,$log_count);//CRON UPDATION
		return $rslt;
	}	

    //check data
	public function get_mysql_hrms_emp($hrms_emp_qry){
		$hrms_emp_info     = $this->hrms_runQuery("$hrms_emp_qry");
		$hrms_emp_result   = $this->hrms_result($hrms_emp_info);
		return $hrms_emp_result;
	}
	public function get_mysql_emp($mysql_emp_qry){
		$mysql_emp_info    = $this->runQuery("$mysql_emp_qry");
		$mysql_emp_result  = $this->result($mysql_emp_info);
		$mysql_emp_result  = json_decode(json_encode($mysql_emp_result),true);		
		$emp_result = array_reduce($mysql_emp_result, function($result, $arr){			
		    $result[$arr['employee_code']] = $arr;
		    return $result;
		}, array());
		return $emp_result;
	}
	public function get_mysql_leave($mysql_emp_qry){
		$mysql_emp_info    = $this->runQuery("$mysql_emp_qry");
		$mysql_emp_result  = $this->result($mysql_emp_info);
		$mysql_emp_result  = json_decode(json_encode($mysql_emp_result),true);		
		$emp_result = array_reduce($mysql_emp_result, function($result, $arr){			
		    $result[$arr['employee_code']][$arr['leave_month']] = $arr;
		    return $result;
		}, array());
		return $emp_result;
	}
	public function get_emp_data($mysql_emp_qry){
		$mysql_emp_info    = $this->runQuery("$mysql_emp_qry");
		$mysql_emp_result  = $this->result($mysql_emp_info);
		$mysql_emp_result  = json_decode(json_encode($mysql_emp_result),true);		
		$emp_result = array_reduce($mysql_emp_result, function($result, $arr){			
		    $result[$arr['employee_code']] = $arr['team'];
		    return $result;
		}, array());
		return $emp_result;
	}
	public function get_month_cycle(){
		$today             = date("Y-m-d");
		$month             = date('m-Y',strtotime($today));
		# Retrieve month cycle
		$mon_list_qry      = 'SELECT month_year,start_date,end_date,days FROM cw_month_cycle WHERE trans_status = 1 AND month_year = "'.$month.'" ';
		$mon_list_info     = $this->runQuery("$mon_list_qry");
		$mon_list_rslt     = $this->result($mon_list_info);
		$mon_result = array_reduce($mon_list_rslt, function($result, $arr){			
		    $result[$arr->month_year] = $arr;
		    return $result;
		}, array());

		# Retrieve est leave
		$est_lve_qry       = 'SELECT * FROM cw_est_leaves WHERE trans_status = 1';
		$est_lve_info      = $this->runQuery("$est_lve_qry");
		$est_lve_rslt      = $this->result($est_lve_info);
		$leave_rslt = array_reduce($est_lve_rslt, function($result, $arr){			
		    $result[$arr->month_year][$arr->employee_code] = $arr;
		    return $result;
		}, array());

		# Get leave count from leave entry table
		$leave_ent_rslt    = json_decode($this->api_interface("leave_entry","","",""));

		# Get Holiday Working days from time entry table
		$time_ent_rslt     = json_decode($this->api_interface("time_entry","","",""));
		$result = [];
		foreach($mon_result as $mon_year => $month){
			$start_date = strtotime($month->start_date);
			$end_date   = strtotime($month->end_date);
			if(!isset($result[$mon_year])){
				$result[$mon_year] = [];
			}
			# Process leave entries
			foreach($leave_ent_rslt as $leave){
				$lve_date = strtotime($leave->leave_date);
				if($lve_date >= $start_date && $lve_date <= $end_date){
					if(!isset($result[$mon_year][$leave->employee_code])){
						$result[$mon_year][$leave->employee_code] = ['leave_count' => 0, 'hw_count' => 0];
					}
					$result[$mon_year][$leave->employee_code]['leave_count'] += $leave->leave_count;
				}
			}
			# Process time entries
			foreach($time_ent_rslt as $time){
				$att_date  = strtotime($time->att_date);
				if($att_date >= $start_date && $att_date <= $end_date){
					if(!isset($result[$mon_year][$time->employee_code])){
						$result[$mon_year][$time->employee_code] = ['leave_count' => 0, 'hw_count' => 0];
					}
					$result[$mon_year][$time->employee_code]['hw_count']++;
				}
			}
		}
		$sql_insert  = "";
		$sql_upd 	 = "";
		foreach($result as $month => $value){
			foreach($value as $emp_code => $data){
				if($leave_rslt[$month][$emp_code]){
					$leave_id  = $leave_rslt[$month][$emp_code]['prime_est_leaves_id'];
					$sql_upd  .= "('".$leave_id."','".$data['leave_count']."','". $data['hw_count']."''),";
				}else{
					$sql_insert .= "('".$month."','".$emp_code."','".$data['leave_count']."','".$data['hw_count']."'),";
				}
			}
		}
		$sql_val     = rtrim($sql_insert,',');
		$sql_qry     = "INSERT INTO cw_est_leaves(leave_month,employee_code,leave_count,hw_count) VALUES $sql_val";
		if($sql_val){
			$rslt    = $this->runQuery($sql_qry);
		}
		$sql_upd_val = rtrim($sql_upd,',');
		$sql_upd_qry = "INSERT INTO cw_est_leaves(prime_est_leaves_id,leave_count,hw_count) VALUES $sql_upd_val ON DUPLICATE KEY UPDATE leave_count = VALUES(leave_count),hw_count = VALUES(hw_count)";
		if($sql_upd_val){
			$rslt    = $this->runQuery($sql_upd_qry);
		}
		return $rslt;
	}

	# PASSWORD UPDATE BASED ON ENCRYPTION
	public function update_password(){
		$select_qry             = 'SELECT employee_code,`password` FROM cw_employees WHERE trans_status = 1';
		$select_info            = $this->runQuery($select_qry);
		$select_rslt            = $this->result_array($select_info);	
		foreach($select_rslt as $val){
			$employee_code      = $val['employee_code'];
			$password           = $val['password'];
			$enc_password       = $this->cryptoEncrypt($password);
			$upd_qry            = 'UPDATE cw_employees SET  `password` = "'.$enc_password.'" WHERE employee_code = "'.$employee_code.'"';
			$upd_info           = $this->runQuery("$upd_qry");
		}
		if($upd_info){
			return true;
		}else{
			return false;
		}
	}

	//Five STAGES OF ENCRYPTION AND DECRYPTION 
	public function cryptoEncrypt($data){
		try {
			// For Password Encryption
			$hash1     = hash('sha512', $data);
			$hash2     = hash('sha1', $hash1);
			$Hash3     = hash('haval160,4', $hash2); 
			$Hash4     = hash('haval160,5', $Hash3); 
			// Generate the HMAC hash
			$finalhash = hash_hmac('sha256', $Hash4, $this->enckey);		
			return $finalhash;	
		} catch (Exception $e) {
			// Log the error or handle it as needed
			error_log("Encryption Error: " . $e->getMessage()); // Log the error for debugging
			return false;
		}
	}

	# API INTERFACE
	public function api_interface($action,$token,$query_filter,$punch_date){
		$post_arr  = array('action' => $action);
		if($punch_date){
			$post_arr['punch_date'] = $punch_date;
		}
		if($query_filter){
			$post_arr['query_filter'] = $query_filter;
		}
		$curl	   = curl_init();
		curl_setopt_array($curl, array(
			CURLOPT_URL            => 'http://localhost/smart_hrms_8_3/api_interface/index.php',
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING       => '',
			CURLOPT_MAXREDIRS      => 68,
			CURLOPT_TIMEOUT        => 0,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST  => 'POST',
			CURLOPT_POSTFIELDS     => $post_arr,
			CURLOPT_HTTPAUTH       => CURLAUTH_BASIC,
			CURLOPT_HTTPHEADER     => array(
			  'username: CAMS_USER@',
			  'password: CRfQ3bxbSyOdo',
			  "Authorization: $token"
			),
		));
		$response       		   = curl_exec($curl);
		if(curl_errno($curl)){
   			return  curl_error($curl);
		}else{
			$response = json_decode($response,true);
			if(!$response['status']){
				echo json_encode($response);exit(0);
			}else{
				return $response['rslt'];
			}
		}
		curl_close($curl);
	}

	//INSERT CRON STATUS 
	public function insert_cron_status($source,$frm,$process_type){
		$sql_qry      = 'INSERT into cw_cron_status(cron_source,cron_for,cron_status,start_time,process_type,process_date,trans_created_date) values ("'.$source.'","'.$frm.'","Inprogress","'.date("Y-m-d H:i:s").'","'.$process_type.'","'.date("Y-m-d").'","'.date("Y-m-d H:i:s").'")';
		$mysql_info   = $this->runQuery_insert_id("$sql_qry");// changed by marish [runQuery => runQuery_insert_id and process_count => count];
		mysqli_next_result($this->db);	
		return $mysql_info;
	}

	//UPDATE CRON STATUS AFTER CRON INSERTION
	public function update_cron_status($cron_status_id,$cron_status,$log_count){
		$trans_updated_date     = date("Y-m-d H:i:s");
		$end                    = date("Y-m-d H:i:s");
		$cron_update_qry        = 'UPDATE cw_cron_status SET end_time = "'.$end.'",cron_status = "'.$cron_status.'",processed_count = "'.$log_count.'",trans_updated_date = "'.$trans_updated_date.'" WHERE prime_cron_status_id    = "'.$cron_status_id.'"';
		$cron_update_query      = $this->runQuery("$cron_update_qry");
	}
}
?>