Exemple #1
0
 public function setClock($number, $employment_type, $from, $to, $remark)
 {
     $from_date = date('Y-m-d', strtotime($from));
     $to_date = date('Y-m-d', strtotime($to));
     $now = date('Y-m-d H:i:s');
     $user_session = new Zend_Session_Namespace('user');
     $user_id = $user_session->user_info['user_id'];
     $workday = new Hra_Model_Workday();
     $worktime = new Hra_Model_Worktime();
     $attendance = new Hra_Model_Attendance();
     $vacationWorkDayInfo = $workday->getWorkdayQtyByTimeRange($employment_type, $from, $to);
     $vacationWorkDayArr = $vacationWorkDayInfo['work_day'];
     // 根据时间范围覆盖的工作日范围记录对应工号员工的打卡时间
     for ($i = 0; $i < count($vacationWorkDayArr); $i++) {
         $d = $vacationWorkDayArr[$i];
         $worktimeRange = $worktime->getWroktimeRange($employment_type, $d);
         // 打卡时间
         $time_on = $d . ' ' . $worktimeRange['from'];
         $time_off = $d . ' ' . $worktimeRange['to'];
         if ($i == 0 && $d == $from_date) {
             // 当起始日期和覆盖的工作日范围第一天一致时,判断起始时间是否 > 当天设定的上班打卡时间,是的话记录上班打卡时间为起始时间,否则记录上班打卡时间为系统设定时间
             if (strtotime($from > strtotime($d . ' ' . $worktimeRange['from']))) {
                 $time_on = $from;
             }
         } else {
             if ($i == count($vacationWorkDayArr) - 1 && $d == $to_date) {
                 // 当截止日期和覆盖的工作日范围最后一天一致时,判断截止时间是否 < 当天设定的下班打卡时间,是的话记录下班打卡时间为截止时间,否则记录上班打卡时间为系统设定时间
                 if (strtotime($to < strtotime($d . ' ' . $worktimeRange['to']))) {
                     $time_off = $to;
                 }
             }
         }
         // 记录上班时间
         $data = array('number' => $number, 'time' => $time_on, 'type' => 1, 'remark' => $remark, 'create_user' => $user_id, 'create_time' => $now, 'update_user' => $user_id, 'update_time' => $now);
         $attendance->insert($data);
         // 记录下班时间
         $data = array('number' => $number, 'time' => $time_off, 'type' => 2, 'remark' => $remark, 'create_user' => $user_id, 'create_time' => $now, 'update_user' => $user_id, 'update_time' => $now);
         $attendance->insert($data);
     }
 }
 public function importAction()
 {
     $result = array('success' => true, 'info' => '导入成功');
     $request = $this->getRequest()->getParams();
     $type = isset($request['type']) ? $request['type'] : null;
     if (isset($_FILES['csv'])) {
         $now = date('Y-m-d H:i:s');
         $user_session = new Zend_Session_Namespace('user');
         $user_id = $user_session->user_info['user_id'];
         $file = $_FILES['csv'];
         $file_extension = strrchr($file['name'], ".");
         $h = new Application_Model_Helpers();
         $tmp_file_name = $h->getMicrotimeStr() . $file_extension;
         $savepath = "../temp/";
         $tmp_file_path = $savepath . $tmp_file_name;
         move_uploaded_file($file["tmp_name"], $tmp_file_path);
         if ($type == 'attendance') {
             $attendance = new Hra_Model_Attendance();
             // 出勤
             $file = fopen($tmp_file_path, "r");
             $insertIds = array();
             while (!feof($file)) {
                 $csv_data = fgetcsv($file);
                 $number = isset($csv_data[0]) && $csv_data[0] != '' ? $csv_data[0] : null;
                 $clock_in = isset($csv_data[2]) && $csv_data[2] != '' ? $csv_data[1] . ' ' . $csv_data[2] : null;
                 $clock_out = isset($csv_data[3]) && $csv_data[3] != '' ? $csv_data[1] . ' ' . $csv_data[3] : null;
                 if ($number && ($clock_in || $clock_out)) {
                     // 获取打卡日期:上班或下班打卡日期
                     $clock_date = $clock_in ? date('Y-m-d', strtotime($clock_in)) : date('Y-m-d', strtotime($clock_out));
                     if ($attendance->fetchAll("number = '" . $number . "' and clock_date = '" . $clock_date . "'")->count() == 0) {
                         $clock_hours = 0;
                         if ($clock_in != '' && $clock_out != '') {
                             $clock_hours = (strtotime($clock_out) - strtotime($clock_in)) / 3600;
                         }
                         $data = array('number' => $number, 'clock_date' => $clock_date, 'clock_in' => $clock_in, 'clock_out' => $clock_out, 'clock_hours' => $clock_hours, 'create_user' => $user_id, 'create_time' => $now, 'update_user' => $user_id, 'update_time' => $now);
                         try {
                             $insertId = $attendance->insert($data);
                             array_push($insertIds, $insertId);
                         } catch (Exception $e) {
                             $result['success'] = false;
                             $result['info'] = $e->getMessage();
                             echo Zend_Json::encode($result);
                             exit;
                         }
                     }
                 }
             }
             fclose($file);
             // 刷新打卡类别setAttendanceType($id)
             /* foreach ($insertIds as $id){
                    $this->setAttendanceType($id);
                } */
         }
     }
     echo Zend_Json::encode($result);
     exit;
 }