| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 | 
							- <?php
 
- namespace app\common\util;
 
- use app\api\controller\BaseAuthorized;
 
- use PHPExcel_IOFactory;
 
- use think\facade\Db;
 
- class PHPExcel{
 
-     public static function import($path,$login_staff)
 
-     {
 
-         $LeaveModel = new \app\common\model\Leave();
 
-         $result = [
 
-             'code'=>999,
 
-             'msg'=>'错误'
 
-         ];
 
-         
 
-         $path = public_path().'storage/'.$path;
 
-         $PHPExcel = new \PHPExcel();
 
-         
 
-         $PHPReader = new \PHPExcel_Reader_Excel2007();
 
-         if (!$PHPReader->canRead($path)) {
 
-             $PHPReader = new \PHPExcel_Reader_Excel5();
 
-             if (!$PHPReader->canRead($path)) {
 
-                 $result['msg'] = '请上传excel文件';
 
-                 return $result;
 
-             }
 
-         }
 
-         
 
-         $PHPExcel = $PHPReader->load($path);
 
-         
 
-         $sheet = $PHPExcel->getSheet(0);
 
-         
 
-         $allColumn = $sheet->getHighestColumn();
 
-         
 
-         $allRow = $sheet->getHighestRow();
 
-         
 
-         
 
-         $month = [];
 
-         $month2 = [];
 
-         $i = 0;
 
-         
 
-         for ($currentRow = 4; $currentRow <= $allRow; $currentRow++){
 
-             $number = $PHPExcel->getActiveSheet()->getCell("A" . $currentRow)->getValue();
 
-             if(!$number){
 
-                 break;
 
-             }
 
-             $array = [
 
-                 'month'=>$PHPExcel->getActiveSheet()->getCell("B" . $currentRow)->getValue(),
 
-                 'phone'=>$PHPExcel->getActiveSheet()->getCell("C" . $currentRow)->getValue(),
 
-                 'days'=>$PHPExcel->getActiveSheet()->getCell("E" . $currentRow)->getValue(),
 
-                 'real_days'=>$PHPExcel->getActiveSheet()->getCell("F" . $currentRow)->getValue(),
 
-                 'late'=>$PHPExcel->getActiveSheet()->getCell("G" . $currentRow)->getValue(),
 
-                 'leave'=>$PHPExcel->getActiveSheet()->getCell("H" . $currentRow)->getValue(),
 
-                 'absenteeism'=>$PHPExcel->getActiveSheet()->getCell("I" . $currentRow)->getValue(),
 
-                 'late_deduct'=>$PHPExcel->getActiveSheet()->getCell("J" . $currentRow)->getValue(),
 
-                 'leave_deduct'=>$PHPExcel->getActiveSheet()->getCell("K" . $currentRow)->getValue(),
 
-                 'absenteeism_deduct'=>$PHPExcel->getActiveSheet()->getCell("L" . $currentRow)->getValue(),
 
-                 'other_deduct'=>$PHPExcel->getActiveSheet()->getCell("M" . $currentRow)->getValue(),
 
-                 'sum_deduct'=>$PHPExcel->getActiveSheet()->getCell("N" . $currentRow)->getValue(),
 
-                 'other_reward'=>$PHPExcel->getActiveSheet()->getCell("O" . $currentRow)->getValue(),
 
-                 'remark'=>$PHPExcel->getActiveSheet()->getCell("P" . $currentRow)->getValue(),
 
-                 'create_time'=>date('Y-m-d'),
 
-                 'valid'=>1,
 
-                 'enterprise_id'=>$login_staff->enterprise_id,
 
-             ];
 
-             $month[$i][$array['month']] = $array['phone'];
 
-             $month2[$i][$array['phone']] = $array['month'];
 
-             $i++;
 
-             $user = \app\common\model\User::where('phone',$array['phone'])->find();
 
-             if (!$user){
 
-                 $msg = '导入数据错误,请检查报表格式或者手机号数据·1';
 
-                 $result['msg'] = $msg;
 
-                 return $result;
 
-             }
 
-             $staff = \app\common\model\Staff::where(['user_id'=>$user->id,'enterprise_id'=>$login_staff->enterprise_id])->find();
 
-             if (!$staff){
 
-                 $msg = '导入数据错误,请检查报表格式或者手机号数据·2';
 
-                 $result['msg'] = $msg;
 
-                 return $result;
 
-             }
 
-             if ($array['days']>31){
 
-                 $msg = $staff->name.'应出勤天数不得超过31天';
 
-                 $result['msg'] = $msg;
 
-                 return $result;
 
-             }
 
-             if ($array['late_deduct']+$array['leave_deduct']+$array['absenteeism_deduct']+$array['other_deduct']!=$array['sum_deduct']){
 
-                 $msg = $staff->name.'扣款合计不正确';
 
-                 $result['msg'] = $msg;
 
-                 return $result;
 
-             }
 
-         }
 
-         if (count(self::get_repeat_data($month))){
 
-             $msg = '导入数据重复';
 
-             $result['msg'] = $msg;
 
-             return $result;
 
-         }
 
-         for ($currentRow = 4; $currentRow <= $allRow; $currentRow++) {
 
-             
 
-             $number = $PHPExcel->getActiveSheet()->getCell("A" . $currentRow)->getValue();
 
-             if(!$number){
 
-                 $msg = '导入成功';
 
-                 $result['code'] = 0;
 
-                 $result['msg'] = $msg;
 
-                 break;
 
-             }
 
-             $data = [
 
-                 'month'=>$PHPExcel->getActiveSheet()->getCell("B" . $currentRow)->getValue(),
 
-                 'phone'=>$PHPExcel->getActiveSheet()->getCell("C" . $currentRow)->getValue(),
 
-                 'days'=>$PHPExcel->getActiveSheet()->getCell("E" . $currentRow)->getValue(),
 
-                 'real_days'=>$PHPExcel->getActiveSheet()->getCell("F" . $currentRow)->getValue(),
 
-                 'late'=>$PHPExcel->getActiveSheet()->getCell("G" . $currentRow)->getValue(),
 
-                 'leave'=>$PHPExcel->getActiveSheet()->getCell("H" . $currentRow)->getValue(),
 
-                 'absenteeism'=>$PHPExcel->getActiveSheet()->getCell("I" . $currentRow)->getValue(),
 
-                 'late_deduct'=>$PHPExcel->getActiveSheet()->getCell("J" . $currentRow)->getValue(),
 
-                 'leave_deduct'=>$PHPExcel->getActiveSheet()->getCell("K" . $currentRow)->getValue(),
 
-                 'absenteeism_deduct'=>$PHPExcel->getActiveSheet()->getCell("L" . $currentRow)->getValue(),
 
-                 'other_deduct'=>$PHPExcel->getActiveSheet()->getCell("M" . $currentRow)->getValue(),
 
-                 'sum_deduct'=>$PHPExcel->getActiveSheet()->getCell("N" . $currentRow)->getValue(),
 
-                 'other_reward'=>$PHPExcel->getActiveSheet()->getCell("O" . $currentRow)->getValue(),
 
-                 'remark'=>$PHPExcel->getActiveSheet()->getCell("P" . $currentRow)->getValue(),
 
-                 'create_time'=>date('Y-m-d'),
 
-                 'valid'=>1,
 
-                 'enterprise_id'=>$login_staff->enterprise_id,
 
-             ];
 
-             $user = \app\common\model\User::where('phone',$data['phone'])->find();
 
-             $staff = \app\common\model\Staff::where(['user_id'=>$user->id,'enterprise_id'=>$login_staff->enterprise_id])->find();
 
-             $data['staff_id'] = $staff->id;
 
-             $data['name'] = $staff->name;
 
-             $where['enterprise_id']=$login_staff->enterprise_id;
 
-             $where['month']=$data['month'];
 
-             $where['staff_id']=$data['staff_id'];
 
-             $LeaveModel->where($where)->delete();
 
-             
 
-             $LeaveModel = new \app\common\model\Leave();
 
-             $LeaveModel->save($data);
 
-         }
 
-         return $result;
 
-     }
 
-     
 
-     public static function get_repeat_data($array){
 
-         
 
-         
 
-         $count = count($array);
 
-         $repeat_data = [];
 
-         
 
-         for($i=0;$i<$count;$i++){
 
-             
 
-             for($j=$i+1;$j<$count;$j++){
 
-                 
 
-                 if($array[$i] == $array[$j]){
 
-                     $repeat_data[$i]=$array[$i];
 
-                     $repeat_data[$j]=$array[$j];
 
-                 }
 
-             }
 
-         }
 
-         return $repeat_data;
 
-     }
 
- }
 
 
  |