File: //home/cafsindia/hrms_allyindian_com/application_bk/controllers/My_team_punches.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class My_team_punches extends Action_controller{
public function __construct(){
parent::__construct('my_team_punches');
}
// LOAD PAGE QUICK LINK,FILTERS AND TABLE HEADERS
public function index(){
//PAGE INFO FUNCTION
$this->page_info();
$data['encKey'] = $this->generateKey();
$logged_emp_code = $this->session->userdata("logged_emp_code");
// Get unique reporting managers
$report_data_qry = "SELECT DISTINCT m.employee_code, m.emp_name FROM cw_employees e JOIN cw_employees m ON e.first_level_approval = m.employee_code WHERE e.trans_status = 1 UNION SELECT DISTINCT m.employee_code, m.emp_name FROM cw_employees e JOIN cw_employees m ON e.second_level_approval = m.employee_code WHERE e.trans_status = 1";
$report_data_info = $this->db->query("CALL sp_a_run('SELECT', '$report_data_qry')");
$report_data_rslt = $report_data_info->result_array();
$report_data_info->next_result();
// Build dropdown HTML
foreach($report_data_rslt as $row){
$reporting_manager_list[$row['employee_code']] = $row['emp_name'];
}
$report_status = false;
if(!empty($report_data_rslt)){
$manager_codes = array_column($report_data_rslt, 'employee_code');
if(in_array($logged_emp_code, $manager_codes)){
$report_status = true;
};
}
$data['report_status'] = $report_status;
$data['reporting_manager_list'] = $reporting_manager_list;
$this->load->view("$this->control_name/manage",$data);
}
// GET TEAMPUNCHES BASED ON LEAVE REPORTING MANAGER _ARN 10-10-2025
public function get_teampunches_data(){
$encString = file_get_contents('php://input');
$_POST = $this->cryptoDecrypt($encString);
if(!$_POST){
echo json_encode(array('success' => false,'message' => 'Url Expired.. Please refresh the page and try again....','table_data' => ""));
exit(0);
}
$report_code = $this->input->post("leave_reporting");
$date = $this->input->post("process_date");
if(empty($report_code) || empty($date)){
echo json_encode(["success"=>FALSE, "message"=>"Please select both Leave Reporting Manager and Process Date"]);
return;
}
// Format date properly
$date = date('Y-m-d', strtotime($date));
// Check-in employees
$checkin_qry = "SELECT cw_employees.employee_code, cw_employees.emp_name,cw_employees.device_code,COUNT(*) AS log_count FROM cw_time_log JOIN cw_employees ON cw_employees.device_code = cw_time_log.user_id WHERE cw_employees.termination_status != 1 AND cw_time_log.trans_status = 1 AND (first_level_approval = '".$report_code."' OR second_level_approval = '".$report_code."') AND DATE(cw_time_log.log_date) = '".$date."' GROUP BY cw_time_log.user_id, cw_employees.employee_code, cw_employees.emp_name";
// Unpunched employees
$unpunch_qry = "SELECT e.employee_code,e.emp_name,e.device_code FROM cw_employees e LEFT JOIN cw_time_log t ON e.device_code = t.user_id AND DATE(t.log_date) = '".$date."' AND t.trans_status = 1 WHERE e.termination_status != 1 AND (first_level_approval = '".$report_code."' OR second_level_approval = '".$report_code."') AND t.user_id IS NULL";
$checkin_qry_info = $this->db->query($checkin_qry);
$checkin_rslt = $checkin_qry_info->result_array();
$checkin_qry_info->next_result();
$unpunch_qry_info = $this->db->query($unpunch_qry);
$unpunch_rslt = $unpunch_qry_info->result_array();
$unpunch_qry_info->next_result();
if(empty($checkin_rslt) && empty($unpunch_rslt)){
echo json_encode(["success"=>FALSE, "message"=>"No records found."]);
return;
}
// Build two tables
$checkin_table = $this->generateTable($checkin_rslt, "checkin_table");
$unpunch_table = $this->generateTable($unpunch_rslt, "unpunch_table");
// Tabs with counts
$tabs_html = "
<ul class='nav nav-tabs' id='punchTabs'>
<li class='active'><a data-toggle='tab' href='#checkin_tab'>Check-In (".count($checkin_rslt).")</a></li>
<li><a data-toggle='tab' href='#unpunch_tab'>Unpunch (".count($unpunch_rslt).")</a></li>
</ul>
<div class='tab-content' style='margin-top:15px;'>
<div id='checkin_tab' class='tab-pane fade in active'>$checkin_table</div>
<div id='unpunch_tab' class='tab-pane fade'>$unpunch_table</div>
</div>
";
echo json_encode(["success"=>TRUE, "html"=>$tabs_html]);
}
// GENERATE THE TABLE AS SHOW THE CHECKIN & UNPUNCH DATA
public function generateTable($data, $id){
$table = "<div style='overflow-x:auto;'><table id='" . htmlspecialchars($id) . "' style='width:100% !important;'><thead><tr>";
// Determine headers
if(!empty($data)){
$headers = array_keys($data[0]);
}else{
$headers = ['Employee Code', 'Employee Name', 'Thumb No', 'Log Count'];
}
// Add table headers
foreach($headers as $col){
$table .= "<th>" . htmlspecialchars(ucwords(strtolower(str_replace('_', ' ', $col)))) . "</th>";
}
$table .= "</tr></thead><tbody>";
if(!empty($data)){
// Add table rows
foreach ($data as $row){
$table .= "<tr>";
foreach ($headers as $col){
$table .= "<td>" . (isset($row[$col]) ? htmlspecialchars($row[$col]) : '') . "</td>";
}
$table .= "</tr>";
}
}else{
$table .= "<tr>";
$colCount = count($headers);
for($i = 0; $i < $colCount; $i++){
if($i == 1){
// First cell shows the message
$table .= "<td style='text-align:center;'>No data found</td>";
}else{
// Other cells remain empty
$table .= "<td></td>";
}
}
$table .= "</tr>";
}
$table .= "</tbody></table></div>";
return $table;
}
// GET EMPLOYEE PUNCHES DETAILS _ARN 10-10-2025
public function get_employee_punches(){
$encString = file_get_contents('php://input');
$_POST = $this->cryptoDecrypt($encString);
if(!$_POST){
echo json_encode(array('success' => false,'message' => 'Url Expired.. Please refresh the page and try again....','table_data' => ""));
exit(0);
}
$device_code = $this->input->post('device_code');
$date = $this->input->post('process_date');
$date = date('Y-m-d', strtotime($date));
if(empty($device_code)){
echo json_encode(["success" => FALSE, "message" => "Invalid employee."]);
return;
}
$time_log_qry = "SELECT * FROM cw_time_log WHERE user_id = '" . $device_code . "' AND DATE(log_date) = '" . $date . "' ORDER BY log_date ASC ";
$time_log_info = $this->db->query($time_log_qry);
$result = $time_log_info->result_array();
$time_log_info->next_result();
if(empty($result)){
echo json_encode(["success" => FALSE, "message" => "No punches found."]);
return;
}
echo json_encode(["success" => TRUE, "data" => $result]);
}
// []-----------------ARN END 10-10-2025---------------[]
}
?>