123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Model;
- class AttendanceLeave extends Model
- {
- public function lists($page,$size,$name,$type,$orgId,$userId=0){
- $map[] = ['al.status','=',1];
- $map[] = ['al.org_id','=',$orgId];
- if($name){
- $map[] = ['ui.name','like','%'.$name.'%'];
- }
- if($type > 0){
- $map[] = ['al.leave_type','=',$type];
- }
- if($userId > 0){
- $map[] = ['al.user_id','=',$userId];
- }
- $lists = Db::name('attendance_leave')
- ->alias('al')
- ->join('user_info ui','ui.user_id = al.user_id')
- ->join('attendance_leave_type alt','alt.id = al.leave_type')
- ->where($map)
- ->field('al.*,ui.name as userName,alt.name as leaveName')
- ->page($page,$size)
- ->order('al.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $startday = date('Y-m-d',strtotime($v['start_time']));
- if($startday.' 12:00:00' > $v['start_time']){
- $lists[$k]['start_time'] = $startday.' 上午';
- } else {
- $lists[$k]['start_time'] = $startday.' 下午';
- }
- $endday = date('Y-m-d',strtotime($v['end_time']));
- if($endday.' 12:00:00' > $v['end_time']){
- $lists[$k]['end_time'] = $endday.' 上午';
- } else {
- $lists[$k]['end_time'] = $endday.' 下午';
- }
- }
- $total = Db::name('attendance_leave')
- ->alias('al')
- ->join('user_info ui','ui.user_id = al.user_id')
- ->join('attendance_leave_type alt','alt.id = al.leave_type')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- public function leaveRecord($page,$size,$orgId,$uid,$startTime,$endTime){
- $map[] = ['al.status','=',1];
- $map[] = ['al.org_id','=',$orgId];
- if($startTime && $endTime){
- $map[] = ['ul.start_time','>=',$startTime.' 00:00:00'];
- $map[] = ['ul.end_time','<=',$endTime.' 23:59:59'];
- }
- if($uid > 0){
- $map[] = ['al.user_id','=',$uid];
- }
- $lists = Db::name('attendance_leave')
- ->alias('al')
- ->join('user_info ui','ui.user_id = al.user_id')
- ->join('attendance_leave_type alt','alt.id = al.leave_type')
- ->where($map)
- ->field('al.*,ui.name as userName,alt.name as leaveName')
- ->page($page,$size)
- ->order('al.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $startday = date('Y-m-d',strtotime($v['start_time']));
- if($startday.' 12:00:00' > $v['start_time']){
- $lists[$k]['start_time'] = $startday.' 上午';
- } else {
- $lists[$k]['start_time'] = $startday.' 下午';
- }
- $endday = date('Y-m-d',strtotime($v['end_time']));
- if($endday.' 12:00:00' > $v['end_time']){
- $lists[$k]['end_time'] = $endday.' 上午';
- } else {
- $lists[$k]['end_time'] = $endday.' 下午';
- }
- }
- $total = Db::name('attendance_leave')
- ->alias('al')
- ->join('user_info ui','ui.user_id = al.user_id')
- ->join('attendance_leave_type alt','alt.id = al.leave_type')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- // 请假审批时生成请假记录
- public function advancedStartLeave($id,$orgId,$userId,$formJson){
- $formJson = json_decode($formJson,true);
- $start = '';
- $end = '';
- $isleavebj = '否'; // 默认不离京
- $lbjstart = '';
- $lbjend = '';
- $address = '';
- $type = 1; // 1=减去节假日 0=不减节假日
- $data = [
- 'org_id' => $orgId,
- 'user_id' => $userId,
- 'apply_id' => $id,
- 'create_time' => date('Y-m-d H:i:s'),
- 'status' => 0
- ];
- foreach ($formJson as $k=>$v){
- if($v['componentName'] == 'ddleavefield'){
- foreach ($v['components'] as $key=>$val){
- switch ($val['idx']){
- case '0':
- $leave = json_decode($val['values'],true);
- $data['leave_type'] = $leave['id'];
- break;
- case '1':
- $start = $val['values'];
- $starts = explode(' ',$start);
- if($starts[count($starts)-1] == '上午'){
- $data['start_time'] = $starts[0].' 00:00:00';
- }else{
- $data['start_time'] = $starts[0].' 12:00:00';
- }
- break;
- case '2':
- $end = $val['values'];
- $ends = explode(' ',$end);
- if($ends[count($ends)-1] == '上午'){
- $data['end_time'] = $ends[0].' 11:59:59';
- }else{
- $data['end_time'] = $ends[0].' 23:59:59';
- }
- break;
- // case '3':
- // $isleavebj = isset($val['values'])?$val['values']:'否';
- // break;
- // case '4':
- // $lbjstart = isset($val['values'])?$val['values']:'';
- // break;
- // case '5':
- // $lbjend = isset($val['values'])?$val['values']:'';
- // break;
- // case '6':
- // $address = isset($val['values'])?$val['values']:'';
- // break;
- }
- }
- break;
- }
- }
- if($data['leave_type'] == 6){// 产假,不减节假日
- $type = 0;
- }
- $data['days'] = calculate_leave($start,$end,$type);
- $data['cur_days'] = $data['days'];
- // if($isleavebj == '是'){
- // $ld = [
- // 'org_id' => $orgId,
- // 'user_id' => $userId,
- // 'start' => $lbjstart,
- // 'end' => $lbjend,
- // 'address' => $address,
- // 'type' => 1,
- // 'apply_id' => $id
- // ];
- // $res = model('LeaveBj')->add($ld);
- // if(!$res){
- // return false;
- // }
- // }
- if($data['leave_type'] == 3){
- // 扣除年假期余额
- $uret = Db::name('user_info')->where('user_id',$userId)->setDec('annual_leave',$data['days']);
- if(!$uret){
- return false;
- }
- }
- $ret = Db::name('attendance_leave')->insert($data);
- return $ret?true:false;
- }
- public function advancedEndLeave($id,$orgId,$userId,$formJson){
- $data = [
- 'update_time' => date('Y-m-d H:i:s'),
- 'status' => 1
- ];
- // 检查是否有离京记录,有就更新状态
- /*$info = Db::name('leave_bj')->where('apply_id',$id)->find();
- if($info){
- $res = Db::name('leave_bj')->where('id',$info['id'])->update($data);
- if(!$res){
- return false;
- }
- }*/
- $ret = Db::name('attendance_leave')->where('apply_id',$id)->update($data);
- return $ret?true:false;
- }
- public function advancedDisagreeLeave($applyId){
- $leave = Db::name('attendance_leave')->where('apply_id',$applyId)->find();
- $data = [
- 'update_time' => date('Y-m-d H:i:s'),
- 'status' => 2
- ];
- // 检查是否有离京记录,有就更新状态
- /*$info = Db::name('leave_bj')->where('apply_id',$applyId)->find();
- if($info){
- $res = Db::name('leave_bj')->where('id',$info['id'])->update($data);
- if(!$res){
- return false;
- }
- }*/
- if($leave['leave_type'] == 3){ // 年假
- // 返还用户年假假期余额
- $uret = Db::name('user_info')->where('user_id',$leave['user_id'])->setInc('annual_leave',$leave['days']);
- if(!$uret){
- return false;
- }
- }
- $ret = Db::name('attendance_leave')->where('apply_id',$applyId)->update($data);
- return $ret?true:false;
- }
- }
|