Blame view

application/admin/controller/antis/attendance/Statisc.php 13.5 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
  <?php

  

  namespace app\admin\controller\antis\attendance;

  

  use addons\antis\model\AttendanceStatisc;

  use addons\antis\model\Staff;

  use app\admin\model\AuthGroup;

  use app\common\controller\Backend;

  use addons\antis\model\Attendance;

  

  /**

   * 考勤统计

   */

  class Statisc extends Backend {

      public function _initialize() {

          parent::_initialize();

          $this->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('导出失败');

  

      }

  

  

  

  }