public function index()
{
try {
$this->load->model('employee/employee_model');
ini_set('max_execution_time', 0);
set_time_limit(0);
ini_set("memory_limit", "5024M");
$user_data = $this->session->userdata();
$check_exist = $this->db->where("company_id", $user_data['company_id'])->get("bf_attendance_sync");
if ($check_exist->num_rows() > 0) {
$last_sync_date = date("Y-m-d", strtotime($check_exist->row()->last_sync_date));
$current_date = date("Y-m-d");
$updated_date = $check_exist->row()->updated_on;
} else {
$last_sync_date = $current_date = date("Y-m-d");
}
$dateformat = $this->web_setting_model->get_value('dateformat');
if ($dateformat == 'BS') {
$picker_class = "npdatepicker";
$df = englishToNepali($last_sync_date);
$dt = englishToNepali($current_date);
if (isset($updated_date)) {
$updated_date = englishToNepali($updated_date);
}
} else {
$picker_class = "datepicker";
$df = $last_sync_date;
$dt = $current_date;
}
if (!empty($_POST)) {
if ($this->session->userdata('company_id') > 0) {
if ($dateformat == 'BS') {
$datefrom = nepaliToEnglish($this->input->post('df'));
$dateto = nepaliToEnglish($this->input->post('dt'));
} else {
$datefrom = $this->input->post('df');
$dateto = $this->input->post('dt');
}
$company_id = $this->session->userdata('company_id');
$company = $this->company_model->find($company_id);
$allemployee = $this->employee_model->where(array('company' => $company_id, 'status' => 'ACTIVE'))->find_all();
#displayArr($allemployee);
$start = strtotime($datefrom);
$attend2 = array();
if ($allemployee) {
foreach ($allemployee as $ae) {
$alldevicedata = $this->general_model->db->where(
array(
'enrollNumber' => $ae->id,
'companyid' => $company_id,
'status' => '0',
'date(signinDate) >=' => date('Y-m-d', strtotime($datefrom)),
'date(signinDate) <=' => date('Y-m-d', strtotime($dateto))
)
)->order_by('enrollNumber, signinDate, signinTime', 'ASC')
->group_by('signinTime')->get('devicelog')->result();
$i = 0;
$attend = array();
$checks = array();
//displayArr($alldevicedata);
if ($alldevicedata) {
foreach ($alldevicedata as $k => $add) {
$checks[$add->signinDate]['data'][] = $add->signinTime;
$checks[$add->signinDate]['branch_id'][] = $add->branch_id;
}
}
$empid = $ae->empid;
$attend[$empid] = $checks;
//$start = strtotime("+1 day", $start);
$start = strtotime($datefrom);
//displayArr($attend);
while ($start <= strtotime($dateto)) {
$shift_id = $this->shift_model->get_employee_shift(date('Y-m-d', $start), $ae->empid);
$shift_detail = $this->shift_model->find($shift_id);
if (empty($attend[$empid][date('Y-m-d', $start)]['data'])) {
// if there is no fingerprint
$attend2[$empid][date('Y-m-d', $start)] = null;
$attend2[$empid][date('Y-m-d', $start)]['shift_id'] = $shift_id;
} else {
// if there is fingerprint
$attend2[$empid][date('Y-m-d', $start)] = $attend[$empid][date('Y-m-d', $start)];
$attend2[$empid][date('Y-m-d', $start)]['shift'] = $shift_detail;
$attend2[$empid][date('Y-m-d', $start)]['shift_id'] = $shift_id;
//$attend2[$empid][date('Y-m-d', $start)]['branch_id'] = $
}
$start = strtotime("+1 day", $start);
}
ksort($attend2[$empid]);
}
}
$attendance = array();
if ($attend2) {
foreach ($attend2 as $empID => $emp_attend) {
foreach ($emp_attend as $att_date => $attval) {
//displayArr($attval['shift']);
$current_date = strtotime($att_date);
$breakout = '';
$breakin = '';
$checkin = '';
$checkout = '';
$remark = '';
$class = '';
$checkin_branch = '';
$checkout_branch = '';
$present_days = '0';
$extras = 0;
if (@$attval['shift']->single_punch == 'YES') {
if (count(@$attval['data']) > 0) {
$remark = "P";
$class = "present";
$checkin = strtotime($att_date . ' ' . $attval['data'][0]);
if (@$attval['branch_id'][0] != '') $checkin_branch = $attval['branch_id'][0];
if (count(@$attval['data']) <= 1) {
$checkout = '';
} else {
$checkout = strtotime($att_date . ' ' . $attval['data'][(count($attval['data']) - 1)]);
if (@$attval['branch_id'][count($attval['data']) - 1] != '') $checkout_branch = $attval['branch_id'][count($attval['data']) - 1];
}
$present_days = '1';
} else {
$remark = "A";
$class = "absent";
$checkin = $current_date;
$present_days = '0';
}
} else if (@$attval['shift_id'] == '-1') {
// employee has day off in the roster;
$remark = "DO";
$class = "dayoff";
$checkin = $current_date;
$present_days = '0';
} else if (empty($attval['data'])) {
$shift = $this->shift_model->find($attval['shift_id']);
// if there is no data it happens when: weekend, leave or holiday;
$dayofweek = date('w', $current_date);
$leave = $this->employee_model->check_leave($empID, $att_date, $company_id);
if ($dayofweek == @$shift->weekend) {
$remark = "W";
$class = 'weekend';
} else if ($this->employee_model->check_holiday($att_date)) {
$remark = "H";
$class = 'holiday';
} else if ($leave) {
$leave_type = $this->general_model->db->where('leave_typeID', @$leave->leave_type)->get('leave_type')->row();
$remark = (@$leave_type->leave_code != '') ? $leave_type->leave_code : 'OL';
$class = 'leave';
} else {
$remark = 'A';
$class = 'absent';
}
$out_checkin = date('Y-m-d', $current_date);
$outarr = explode('-', $out_checkin);
// echo $att_date . "::" . $remark . '<br>';
// if (!$this->employee_model->check_ret_present($empID, $outarr[0], $outarr[1], $outarr[2])) {
// $remark = 'A';
// $class = 'absent';
// }
$checkin = $current_date;
$present_days = '0';
} else {
// if there is data;
if ($attval['shift']->enable_break == 'YES') {
//break is there in the shift
/*
if (count($attval['data']) == 1) {
// === === === === === === === === === === ===
// === === === === === === === === === === ===
// === === === === === === === === === === ===
// === === === === === === === === === === ===
// === === === === === === === === === === ===
// === === === === === === === === === === ===
// === === === === === === === === === === ===
// === === === === === === === === === === ===
// === === === === === === === === === === ===
$checkin = date('H:i', strtotime('+' . $attval['shift']->late_arrival . " minutes", strtotime($attval['shift']->checkin)));
if ($attval['shift']->mark_single_punch == "P") {
$remark = 'P';
$class = 'present';
} elseif ($attval['shift']->mark_single_punch == "A") {
$remark = "A";
$class = "absent";
} elseif ($attval['shift']->mark_single_punch == "HP") {
$remark = "HP";
$class = "half_present";
} else {
$remark = "L";
$class = "late";
}
} else
*/
if (count($attval['data']) < 2) {
if ($att_date == date('Y-m-d')) {
$checkin = date('H:i', strtotime('+' . $attval['shift']->late_arrival . " minutes", strtotime($attval['shift']->checkin)));
if (strtotime($att_date . ' ' . $attval['data'][0]) >= strtotime($att_date . ' ' . $checkin)) {
$remark = 'P';
$class = 'present';
$checkin = strtotime($att_date . ' ' . $attval['data'][0]);
if (@$attval['branch_id'][0] != '') $checkin_branch = $attval['branch_id'][0];
}
} else {
$remark = 'L';
$class = 'late';
$checkin = strtotime($att_date . ' ' . $attval['data'][0]);
if (@$attval['branch_id'][0] != '') $checkin_branch = $attval['branch_id'][0];
$leave = $this->employee_model->check_leave($empID, $att_date, $company_id);
if ($leave) {
$leave_type = $this->general_model->db->where('leave_typeID', @$leave->leave_type)->get('leave_type')->row();
$remark = (@$leave_type->leave_code != '') ? $leave_type->leave_code : 'OL';
$class = 'leave';
}
}
} else if (count($attval['data']) == 2) {
$checkin = strtotime($att_date . ' ' . $attval['data'][0]);
$checkout = strtotime($att_date . ' ' . $attval['data'][1]);
if (@$attval['branch_id'][0] != '') $checkin_branch = $attval['branch_id'][0];
if (@$attval['branch_id'][1] != '') $checkout_branch = $attval['branch_id'][1];
$vcheckin = date('H:i', strtotime('+' . $attval['shift']->late_arrival . " minutes", strtotime($attval['shift']->checkin)));
$vcheckout = date('H:i', strtotime('-' . $attval['shift']->early_departure . " minutes", strtotime($attval['shift']->checkout)));
$shift_checkin = strtotime($att_date . ' ' . $vcheckin);
$shift_checkout = strtotime($att_date . ' ' . $vcheckout);
if (($checkin <= $shift_checkin) && ($checkout >= $shift_checkout)) {
$remark = "P";
$class = "present";
} else {
$remark = 'L';
$class = 'late';
$leave = $this->employee_model->check_leave($empID, $att_date, $company_id);
if ($leave) {
$leave_type = $this->general_model->db->where('leave_typeID', @$leave->leave_type)->get('leave_type')->row();
$remark = (@$leave_type->leave_code != '') ? $leave_type->leave_code : 'OL';
$class = 'leave';
}
}
} else if (count($attval['data']) > 2) {
$checkin = strtotime($att_date . ' ' . $attval['data'][0]);
if (@$attval['branch_id'][0] != '') $checkin_branch = $attval['branch_id'][0];
if (count($attval['data']) == 3) {
$breakout = strtotime($att_date . ' ' . $attval['data'][1]);
$checkout = strtotime($att_date . ' ' . $attval['data'][2]);
if (@$attval['branch_id'][2] != '') $checkout_branch = $attval['branch_id'][2];
} else {
$breakout = strtotime($att_date . ' ' . $attval['data'][1]);
$breakin = strtotime($att_date . ' ' . $attval['data'][2]);
$checkout = strtotime($att_date . ' ' . $attval['data'][(count($attval['data']) - 1)]);
if (@$attval['branch_id'][(count($attval['data']) - 1)] != '') $checkout_branch = $attval['branch_id'][(count($attval['data']) - 1)];
}
$vcheckin = date('H:i', strtotime('+' . $attval['shift']->late_arrival . " minutes", strtotime($attval['shift']->checkin)));
$vcheckout = date('H:i', strtotime('-' . $attval['shift']->early_departure . " minutes", strtotime($attval['shift']->checkout)));
$shift_checkin = strtotime($att_date . ' ' . $vcheckin);
$shift_checkout = strtotime($att_date . ' ' . $vcheckout);
if (($checkin <= $shift_checkin) && ($checkout >= $shift_checkout)) {
$remark = "P";
$class = "present";
} else {
$remark = 'L';
$class = 'late';
$leave = $this->employee_model->check_leave($empID, $att_date, $company_id);
if ($leave) {
$leave_type = $this->general_model->db->where('leave_typeID', @$leave->leave_type)->get('leave_type')->row();
$remark = (@$leave_type->leave_code != '') ? $leave_type->leave_code : 'OL';
$class = 'leave';
}
}
}
} // end if enable break is yes else {
else {
$checkin = strtotime($att_date . ' ' . $attval['data'][0]);
if (@$attval['branch_id'][0] != '') $checkin_branch = $attval['branch_id'][0];
$vcheckin = date('H:i', strtotime('+' . $attval['shift']->late_arrival . " minutes", strtotime($attval['shift']->checkin)));
$vcheckout = date('H:i', strtotime('-' . $attval['shift']->early_departure . " minutes", strtotime($attval['shift']->checkout)));
$shift_checkin = strtotime($att_date . ' ' . $vcheckin);
$shift_checkout = strtotime($att_date . ' ' . $vcheckout);
if ($att_date == date('Y-m-d')) {
if (count($attval['data']) > 1) {
$checkout = strtotime($att_date . ' ' . @$attval['data'][(count($attval['data']) - 1)]);
if (($checkin <= $shift_checkin) && ($checkout >= $shift_checkout)) {
$remark = "P";
$class = "present";
} else {
$remark = 'L';
$class = 'late';
$leave = $this->employee_model->check_leave($empID, $att_date, $company_id);
if ($leave) {
$leave_type = $this->general_model->db->where('leave_typeID', @$leave->leave_type)->get('leave_type')->row();
$remark = (@$leave_type->leave_code != '') ? $leave_type->leave_code : 'OL';
$class = 'leave';
}
}
} else {
if ($checkin >= $shift_checkin) {
$remark = 'P';
$class = 'present';
} else {
$remark = 'A';
$class = 'absent';
}
}
} else {
if (count($attval['data']) == 1) {
$remark = 'L';
$class = 'late';
} else if (count($attval['data']) >= 2) {
if ($attval['shift']->type == 'ROUNDTIME' && (strtotime($attval['shift']->checkin) > strtotime($attval['shift']->checkout))) {
//$checkout = strtotime($att_date . ' ' . @$attval['data'][(count($attval['data']) - 1)]);
$next_day = date('Y-m-d', strtotime($att_date . ' + 1 day'));
$checkin = strtotime($att_date . ' ' . @$attval['data'][(count($attval['data']) - 1)]);
$checkout = strtotime($emp_attend[$next_day]['data'][0]);
} else {
$checkout = strtotime($att_date . ' ' . @$attval['data'][(count($attval['data']) - 1)]);
}
// $checkout = strtotime($att_date . ' ' . @$attval['data'][(count($attval['data']) - 1)]);
if (@$attval['branch_id'][(count($attval['data']) - 1)] != '') $checkout_branch = $attval['branch_id'][(count($attval['data']) - 1)];
if (($checkin <= $shift_checkin) && ($checkout >= $shift_checkout)) {
$remark = "P";
$class = "present";
} else {
$remark = 'L';
$class = 'late';
$leave = $this->employee_model->check_leave($empID, $att_date, $company_id);
if ($leave) {
$leave_type = $this->general_model->db->where('leave_typeID', @$leave->leave_type)->get('leave_type')->row();
$remark = (@$leave_type->leave_code != '') ? $leave_type->leave_code : 'OL';
$class = 'leave';
}
}
} else {
if ($attval['shift']->type == 'ROUNDTIME' && (strtotime($attval['shift']->checkin) > strtotime($attval['shift']->checkout))) {
//$checkout = strtotime($att_date . ' ' . @$attval['data'][(count($attval['data']) - 1)]);
$next_day = date('Y-m-d', strtotime($att_date . ' + 1 day'));
$checkin = strtotime($att_date . ' ' . @$attval['data'][(count($attval['data']) - 1)]);
$checkout = strtotime($emp_attend[$next_day]['data'][0]);
} else {
$checkout = strtotime($att_date . ' ' . @$attval['data'][(count($attval['data']) - 1)]);
}
if (($checkin <= $shift_checkin) && ($checkout >= $shift_checkout)) {
$remark = "P";
$class = "present";
} else {
$remark = 'A';
$class = 'absent';
}
}
}
}
} //end if there is data
$empl = $this->employee_model->find($empID);
$eshift = $this->shift_model->find($empl->shift);
$half_day_hour = ($eshift->daily_attendance == '') ? 5 : $eshift->daily_attendance;
$leave = $this->employee_model->check_leave($empID, $att_date, $company_id);
//displayArr($leave);
//echo('<br>' . $empID . ',' . $att_date . ',' . $company_id);
if ($leave) {
$leave_type = $this->general_model->db->where('leave_typeID', @$leave->leave_type)->get('leave_type')->row();
$remark = (@$leave_type->leave_code != '') ? $leave_type->leave_code : 'OL';
$class = 'leave';
}
$att_temp = array();
$att_temp = array(
'company_id' => $company_id,
'employee_id' => $empl->id,
'checkin' => date('Y-m-d H:i:s', $checkin),
'remarks' => $remark,
'css_class' => $class
);
//displayArr($att_temp);
if ($remark != 'A' && @$checkout != '') {
//echo $checkin . "-" . $checkout . "<br>";
$kstart = date_create(date('Y-m-d H:i:s', @$checkin));
$kend = date_create(date('Y-m-d H:i:s', @$checkout));
$diff = date_diff($kstart, $kend);
// echo $diff->h . '>=' . $half_day_hour . ' =' . $empl->id . '--' . date('Y-m-d', $checkin) . '<br>';
if ($diff->h >= $half_day_hour) {
$present_days = '1';
} else {
$present_days = '0.5';
}
} else {
$present_days = '0';
}
$att_temp['present_days'] = $present_days;
if (@$checkout) $att_temp['checkout'] = date('Y-m-d H:i:s', $checkout);
if (@$breakout) $att_temp['break_out'] = date('Y-m-d H:i:s', $breakout);
if (@$breakin) $att_temp['break_in'] = date('Y-m-d H:i:s', $breakin);
if ($checkin_branch != '') $att_temp['checkin_branch_id'] = $checkin_branch;
if ($checkout_branch != '') $att_temp['checkout_branch_id'] = $checkout_branch;
if ($this->employee_model->check_holiday(date('Y-m-d', @$checkin), $company_id)) {
$att_temp['present_days'] = '0';
$att_temp['remarks'] = 'H';
}
//displayARr($att_temp);
$dayofweek = date('w', $current_date);
if ($dayofweek == @$eshift->weekend) {
$att_temp['present_days'] = '0';
$att_temp['remarks'] = 'W';
$att_temp['css_class'] = 'weekend';
}
// if ($att_temp['css_class'] == 'absent' || $att_temp['css_class'] == 'weekend' || $att_temp['css_class'] == 'holiday') {
// $myreturn = $this->employee_model->check_ret_present($empl->id, $att_date);
// //echo 'retrun: ' . $myreturn . "<br>";
// if (!$myreturn) {
// $att_temp['present_days'] = '0';
// $att_temp['remarks'] = 'A';
// $att_temp['css_class'] = 'absent';
// }
// }
$attendance_ins[] = $att_temp;
// if ($empl->id == '14') {
// displayArr($att_temp);
// }
}
}
}
if ($attendance_ins) {
foreach ($attendance_ins as $index => $ai) {
$early_attend = $this->general_model->db->where(
array(
'date(checkin)' => date('Y-m-d', strtotime($ai['checkin'])),
'company_id' => $company_id,
'employee_id' => $ai['employee_id']
)
)->get('attendance')->row();
$extras = 0;
// === === === === === === === === === === === === ===
// === === === === === === === === === === === === ===
// === === === === === === === === === === === === ===
// === === === === LATE CALCULATION === === === ===
// === === === === === === === === === === === === ===
// === === === === === === === === === === === === ===
// === === === === === === === === === === === === ===
$late_deduction = $this->web_setting_model->get_value("late_deduction");
$absentHour = null;
$shift_hour = null;
if ($late_deduction == "yes") {
$absentHour = 0;
$shiftCheckIn = new DateTime($eshift->checkin);
$shiftCheckOut = new DateTime($eshift->checkout);
$checkin = $ai['checkin'];
$checkout = $this->_check_checkout($ai['checkout'], $ai['checkin'], $shiftCheckOut);
$lateArrival = $eshift->late_arrival;
$earlyDeparture = $eshift->early_departure;
$half_attendance = $eshift->daily_attendance * 60;
$totalWorkedHour = strtotime($ai['checkout']) - strtotime($ai['checkin']);
$shiftArr = $shiftCheckIn->diff($shiftCheckOut);
$work_hour = $totalWorkedHour / 60;
$shift_hour = $shiftArr->h * 60 + $shiftArr->i;
$total_shift_hour = $shift_hour;
if ($lateArrival) {
$shift_hour = $shift_hour - $lateArrival;
}
if ($earlyDeparture) {
$shift_hour = $shift_hour - $earlyDeparture;
}
if ($work_hour > 0) {
if ($work_hour > $half_attendance && $work_hour <= $shift_hour) {
$absentHour = $shift_hour - $work_hour;
}
}
}
if ($early_attend) {
$dayofweek = date('w', strtotime($ai['checkin']));
if (($dayofweek == @$eshift->weekend) || ($this->employee_model->check_holiday($ai['checkin']))) {
if ($early_attend->checkin != '' && $early_attend->checkout != '') {
$kstart = date_create(date('Y-m-d H:i:s', strtotime($early_attend->checkin)));
$kend = date_create(date('Y-m-d H:i:s', strtotime($early_attend->checkout)));
$diff = date_diff($kend, $kstart);
if ($diff->h >= 3) {
$extras = 1;
} else if ($diff->h > 1.5) {
$extras = 0.5;
} else {
$extras = 0;
}
}
}
if ($early_attend->edited_by == '') {
//echo $ai['checkin'] . ':' .
$update_ai = array(
'remarks' => $ai['remarks'],
'css_class' => $ai['css_class'],
'checkin' => $ai['checkin'],
);
if (@$ai['checkout'] != '') $update_ai['checkout'] = $ai['checkout'];
if (@$ai['break_out'] != '') $update_ai['break_out'] = $ai['break_out'];
if (@$ai['break_in'] != '') $update_ai['break_in'] = $ai['break_in'];
if (@$ai['checkin_branch_id'] != '') $update_ai['checkin_branch_id'] = $ai['checkin_branch_id'];
if (@$ai['checkout_branch_id'] != '') $update_ai['checkout_branch_id'] = $ai['checkout_branch_id'];
if (@$ai['present_days'] != '') $update_ai['present_days'] = $ai['present_days'];
$update_ai['extra'] = $extras;
$update_ai['absent_hour'] = $absentHour;
$update_ai['shift_hour'] = $total_shift_hour;
$this->general_model->db->update('attendance', $update_ai, array(
'attendance_id' => $early_attend->attendance_id)
);
} else {
$nai['extra'] = $extras;
$nai['absent_hour'] = $absentHour;
$nai['shift_hour'] = $total_shift_hour;
$this->general_model->db->update('attendance', $nai, array(
'attendance_id' => $early_attend->attendance_id)
);
}
} else {
$dayofweek = date('w', @$checkin);
if (($dayofweek == @$eshift->weekend) || ($this->employee_model->check_holiday($checkin))) {
if ($ai['checkin'] != '' && $ai['checkout'] != '') {
$kstart = date_create(date('Y-m-d H:i:s', @$checkin));
$kend = date_create(date('Y-m-d H:i:s', @$checkout));
$diff = date_diff($kstart, $kend);
if ($diff->h >= 3) {
$extras = 1;
} else if ($diff->h > 1.5) {
$extras = 0.5;
} else {
$extras = 0;
}
}
}
$ai['extra'] = $extras;
$ai['absent_hour'] = $absentHour;
$ai['shift_hour'] = $total_shift_hour;
$this->general_model->db->insert('attendance', $ai);
}
}
}
// last sync date add
$insert_data = array(
"last_sync_date" => $dateto,
"created_by" => $user_data['user_id'],
"company_id" => $user_data['company_id']
);
if ($check_exist->num_rows() > 0) {
$this->db->where("company_id", $user_data['company_id'])->update("bf_attendance_sync", $insert_data);
} else {
$this->db->insert("bf_attendance_sync", $insert_data);
}
}
}
echo "success";
exit;
} catch (Exception $exception) {
print_r($exception->getMessage());
exit;
}
}