Statisc.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace app\admin\controller\qingdongams\attendance;
  3. use addons\qingdongams\model\AttendanceStatisc;
  4. use addons\qingdongams\model\Staff;
  5. use app\common\controller\Backend;
  6. use addons\qingdongams\model\Attendance;
  7. /**
  8. * 考勤统计
  9. */
  10. class Statisc extends Backend {
  11. public function _initialize() {
  12. parent::_initialize();
  13. $this->model = new AttendanceStatisc();
  14. }
  15. /**
  16. * 考勤统计
  17. */
  18. public function index() {
  19. $this->request->filter(['strip_tags']);
  20. if ($this->request->isAjax()) {
  21. //0:全部 1:我的 2:下属的
  22. $type = input('type',0);
  23. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  24. $wheres=[];
  25. switch($type){
  26. case 1:
  27. $staff = Staff::info();
  28. $wheres['staff_id'] = $staff->id;
  29. break;
  30. case 2:
  31. $wheres['staff_id'] = array('in',Staff::getLowerStaffId());
  32. break;
  33. default:
  34. $wheres['staff_id'] = array('in',Staff::getMyStaffIds());
  35. break;
  36. }
  37. $statisc = AttendanceStatisc::where($wheres)->where($where)->select();
  38. $data = [];
  39. foreach ($statisc as $v) {
  40. $data[$v['staff_id']][$v['time']][] = [
  41. 'clock_in' => $v['clock_in'],//上班打卡
  42. 'leaver_time' => $v['leaver_time'],
  43. 'clock_out' => $v['clock_out'],
  44. 'late_time' => $v['late_time'],//迟到时间
  45. 'start_time' => $v['start_time'],
  46. 'end_time' => $v['end_time'],
  47. ];
  48. }
  49. $staffs = Staff::where(['id' =>$wheres['staff_id'],'status'=>1])->column('name', 'id');
  50. $result=[];
  51. foreach ($data as $staff_id => $d) {
  52. $leave = 0;//早退
  53. $leave_time = 0;//早退时间
  54. $late = 0;//迟到
  55. $late_time = 0;//迟到时间
  56. $work = 0;//旷工
  57. $work_time = 0;//旷工时间
  58. $card = 0;//缺卡
  59. $normal = 0;//正常
  60. $error = 0;//异常
  61. $overtime=0;//加班
  62. foreach ($d as $day => $time) {
  63. $is_normal = 1;//正常
  64. $is_error = 0;//异常
  65. foreach ($time as $t) {
  66. if (empty($t['clock_in']) && !empty($t['clock_out'])) {//缺卡
  67. $card += 1;
  68. }
  69. if (empty($t['clock_out']) && !empty($t['clock_in'])) {//缺卡
  70. $card += 1;
  71. }
  72. if (empty($t['clock_in']) && empty($t['clock_out'])) {
  73. $work += 1;
  74. $end_time = strtotime(date('Y-m-d ') . $v['end_time']);
  75. $start_time = strtotime(date('Y-m-d ') . $v['start_time']);
  76. $end_time = $end_time > $start_time ? $end_time : $end_time + 86400;
  77. //旷工时长
  78. $wtime = intval(($end_time - $start_time) / 60);
  79. $work_time += $wtime;
  80. }
  81. if ($t['leaver_time'] > 0) {//早退
  82. $leave += 1;
  83. $leave_time += $t['leaver_time'];
  84. }
  85. if ($t['late_time'] > 0) {//迟到
  86. $late += 1;
  87. $late_time += $t['late_time'];
  88. }
  89. if (empty($t['clock_in']) || empty($t['clock_out'])
  90. || $t['late_time'] != 0 || $t['leaver_time'] != 0) {//不正常卡
  91. $is_normal = 0;
  92. $is_error = 1;
  93. }
  94. //工作日加班
  95. if ($t['clock_out'] && $leave_time == 0) {
  96. $clock_out = strtotime($t['clock_out']);
  97. $date = date('Y-m-d ', $clock_out);
  98. $end_time = $date . $t['end_time'];
  99. $end_time = strtotime($end_time);
  100. if ($end_time < $clock_out) {
  101. $end_time = $end_time + 86400;
  102. }
  103. $overtime += intval(($end_time - $clock_out) / 60);
  104. }
  105. }
  106. if ($is_normal == 1) {
  107. $normal += 1;
  108. }
  109. if ($is_error == 1) {
  110. $error += 1;
  111. }
  112. }
  113. //外勤
  114. $month=date('Y-m',strtotime($day));
  115. $other = Attendance::where(['type' => 1,'staff_id'=>$staff_id, 'time' => ['like', "{$month}%"]])->count();
  116. $result[] = [
  117. 'staff'=>$staffs[$staff_id],
  118. 'month'=>$month,
  119. 'leave' => $leave,
  120. 'leave_time' => $leave_time,
  121. 'late' => $late,
  122. 'late_time' => $late_time,
  123. 'work' => $work,
  124. 'work_time' => $work_time,
  125. 'card' => $card,
  126. 'normal' => $normal,
  127. 'error' => $error,
  128. 'other' => $other,
  129. 'overtime' => $overtime];
  130. }
  131. $result = array("total" => count($result), "rows" => $result);
  132. return json($result);
  133. }
  134. return $this->view->fetch();
  135. }
  136. }