model = new AttendanceStatisc(); } /** * 考勤统计 */ public function index() { $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { //0:全部 1:我的 2:下属的 $type = input('type',0); list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $wheres=[]; switch($type){ case 1: $staff = Staff::info(); $wheres['staff_id'] = $staff->id; break; case 2: $wheres['staff_id'] = array('in',Staff::getLowerStaffId()); break; default: $wheres['staff_id'] = array('in',Staff::getMyStaffIds()); break; } //halt($where); $statisc = AttendanceStatisc::where($wheres)->where($where)->select(); $data = []; foreach ($statisc as $v) { $data[$v['staff_id']][$v['time']][] = [ 'clock_in' => $v['clock_in'],//上班打卡 'leaver_time' => $v['leaver_time'], 'clock_out' => $v['clock_out'], 'late_time' => $v['late_time'],//迟到时间 'start_time' => $v['start_time'], 'end_time' => $v['end_time'], ]; } $staffs = Staff::where(['id' =>$wheres['staff_id']])->column('name', 'id'); $result=[]; foreach ($data as $staff_id => $d) { $leave = 0;//早退 $leave_time = 0;//早退时间 $late = 0;//迟到 $late_time = 0;//迟到时间 $work = 0;//旷工 $work_time = 0;//旷工时间 $card = 0;//缺卡 $normal = 0;//正常 $error = 0;//异常 $overtime=0;//加班 foreach ($d as $day => $time) { $is_normal = 1;//正常 $is_error = 0;//异常 foreach ($time as $t) { //if (empty($t['clock_in']) && !empty($t['clock_out'])) {//缺卡 // $card += 1; //} //if (empty($t['clock_out']) && !empty($t['clock_in'])) {//缺卡 // $card += 1; //} if (empty($t['clock_in']) && empty($t['clock_out'])) { //$work += 1; //$end_time = strtotime(date('Y-m-d ') . $v['end_time']); //$start_time = strtotime(date('Y-m-d ') . $v['start_time']); //$end_time = $end_time > $start_time ? $end_time : $end_time + 86400; ////旷工时长 //$wtime = intval(($end_time - $start_time) / 60); //$work_time += $wtime; } if ($t['leaver_time'] > 0) {//早退 $leave += 1; $leave_time += $t['leaver_time']; } if ($t['late_time'] > 0) {//迟到 $late += 1; $late_time += $t['late_time']; } if (empty($t['clock_in']) || empty($t['clock_out']) || $t['late_time'] != 0 || $t['leaver_time'] != 0) {//不正常卡 //$is_normal = 0; //$is_error = 1; } //工作日加班 if ($t['clock_out'] && $leave_time == 0) { $clock_out = strtotime($t['clock_out']); $date = date('Y-m-d ', $clock_out); $end_time = $date . $t['end_time']; $end_time = strtotime($end_time); if ($end_time < $clock_out) { $end_time = $end_time + 86400; } $overtime += intval(($end_time - $clock_out) / 60); } } if ($is_normal == 1) { $normal += 1; } if ($is_error == 1) { $error += 1; } } //外勤 $month=date('Y-m',strtotime($day)); $other = Attendance::where(['type' => 1,'staff_id'=>$staff_id, 'time' => ['like', "{$month}%"]])->count(); $result[] = [ 'staff'=>$staffs[$staff_id], 'month'=>$month, 'leave' => $leave, 'leave_time' => $leave_time, 'late' => $late, 'late_time' => $late_time, 'work' => $work, 'work_time' => $work_time, 'card' => $card, 'normal' => $normal, 'error' => $error, 'other' => $other, 'overtime' => $overtime]; } $result = array("total" => count($result), "rows" => $result); return json($result); } return $this->view->fetch(); } //导出统计 public function export() { $param = $this->request->param(); $data = $this->aaa($param['time']); $title = [ '月份', '姓名', '部门', '岗位', '正常天数', '迟到天数', '缺卡天数', '外勤天数' ]; $file = export_excel($title, $data, '考勤统计'); if ($file['filePath']) { $this->success('导出成功', '', $file); } $this->error('导出失败'); } public function aaa($time){ $statisc = AttendanceStatisc::where('time','LIKE','%'.$time.'%')->select(); $data = []; foreach ($statisc as $v) { $data[$v['staff_id']][$v['time']][] = [ 'clock_in' => $v['clock_in'],//上班打卡 'leaver_time' => $v['leaver_time'], 'clock_out' => $v['clock_out'], 'late_time' => $v['late_time'],//迟到时间 'start_time' => $v['start_time'], 'end_time' => $v['end_time'], ]; } $wheres=[]; $wheres['staff_id'] = array('in',Staff::getMyStaffIds()); $staffs = Staff::where(['id' =>$wheres['staff_id']])->column('name', 'id'); $result=[]; foreach ($data as $staff_id => $d) { $leave = 0;//早退 $leave_time = 0;//早退时间 $late = 0;//迟到 $late_time = 0;//迟到时间 $work = 0;//旷工 $work_time = 0;//旷工时间 $card = 0;//缺卡 $normal = 0;//正常 $error = 0;//异常 $overtime=0;//加班 foreach ($d as $day => $time) { $is_normal = 1;//正常 $is_error = 0;//异常 foreach ($time as $t) { if (empty($t['clock_in'])) {//缺卡 $res = Attendance::where(['type' => 1,'staff_id'=>$staff_id, 'time' => ['like', "$day%"]])->select(); if(!$res){ $card += 1; } } if (empty($t['clock_in'])) { $work += 1; $end_time = strtotime(date('Y-m-d ') . $v['end_time']); $start_time = strtotime(date('Y-m-d ') . $v['start_time']); $end_time = $end_time > $start_time ? $end_time : $end_time + 86400; //旷工时长 $wtime = intval(($end_time - $start_time) / 60); $work_time += $wtime; } if ($t['leaver_time'] > 0) {//早退 $leave += 1; $leave_time += $t['leaver_time']; } if ($t['late_time'] > 0) {//迟到 $late += 1; $late_time += $t['late_time']; } if (empty($t['clock_in']) || $t['late_time'] != 0 || $t['leaver_time'] != 0) {//不正常卡 $is_normal = 0; $is_error = 1; } //工作日加班 if ($t['clock_out'] && $leave_time == 0) { $clock_out = strtotime($t['clock_out']); $date = date('Y-m-d ', $clock_out); $end_time = $date . $t['end_time']; $end_time = strtotime($end_time); if ($end_time < $clock_out) { $end_time = $end_time + 86400; } $overtime += intval(($end_time - $clock_out) / 60); } } if ($is_normal == 1) { $normal += 1; } if ($is_error == 1) { $error += 1; } } //外勤 $month=date('Y-m',strtotime($day)); $other = Attendance::where(['type' => 1,'staff_id'=>$staff_id, 'time' => ['like', "{$month}%"]])->group('DATE(time)')->count(); if(isset($staffs[$staff_id])){ $staff_data = Staff::where(['id' =>$staff_id])->find(); $group_ids = $staff_data['group_ids']; $group_name =implode(',',AuthGroup::where("find_in_set(id, '$group_ids')")->column('name')); $result[] = [ 'month'=>$month, //月份 'staff'=>$staffs[$staff_id], //姓名 'group_name'=>$group_name, 'post'=>$staff_data['post'], 'normal' => $normal,//正常 'late' => $late, //迟到 'card' => $card,//缺卡 'other' => $other, //外勤 ]; } } return $result; } //导出详情打卡记录 public function exportDetaile() { $param = $this->request->param(); $time = $param['time']; $statisc = AttendanceStatisc::where('time','LIKE','%'.$time.'%')->where(function ($query)use($param){ if(isset($param['staff_id']) && !empty($param['staff_id'])){ $query->where('staff_id',$param['staff_id']); } })->select(); $staffs = Staff::where('id' ,'in',Staff::getMyStaffIds())->column('name', 'id'); $data = []; foreach ($statisc as &$item){ $staff_data = Staff::where(['id' =>$item['staff_id']])->find(); $group_ids = $staff_data['group_ids']; $group_name =implode(',',AuthGroup::where("find_in_set(id, '$group_ids')")->column('name')); $dktime = $item['clock_in']; if(!empty($dktime)){ $satus = '已打卡'; $type = '上班打卡'; }else{ $day = $item['time']; $res = Attendance::where(['type' => 1,'staff_id'=>$item['staff_id'], 'time' => ['like', "$day%"]])->order('time asc')->find(); if($res){ $dktime = $res['time']; $satus = '已打卡'; $type = '外勤打卡'; }else{ $dktime = ''; $satus = '未打卡'; $type = ''; } } if(isset($staffs[$item['staff_id']])){ $staffs_name = $staffs[$item['staff_id']]; }else{ $staffs_name = ''; } $data[]=[ 'staff'=>$staffs_name, //姓名 'group_name'=>$group_name, 'post'=>$staff_data['post'], 'time'=>$item['time'], 'dktime'=>$dktime, 'satus'=>$satus, 'type'=>$type, ]; } $title = [ '姓名', '部门', '岗位', '时间', '打卡时间', '是否打卡', '类型' ]; $file = export_excel($title, $data, '考勤详细'); if ($file['filePath']) { $this->success('导出成功', '', $file); } $this->error('导出失败'); } }