AttendanceLeave.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Model;
  6. class AttendanceLeave extends Model
  7. {
  8. public function lists($page,$size,$name,$type,$orgId,$userId=0){
  9. $map[] = ['al.status','=',1];
  10. $map[] = ['al.org_id','=',$orgId];
  11. if($name){
  12. $map[] = ['ui.name','like','%'.$name.'%'];
  13. }
  14. if($type > 0){
  15. $map[] = ['al.leave_type','=',$type];
  16. }
  17. if($userId > 0){
  18. $map[] = ['al.user_id','=',$userId];
  19. }
  20. $lists = Db::name('attendance_leave')
  21. ->alias('al')
  22. ->join('user_info ui','ui.user_id = al.user_id')
  23. ->join('attendance_leave_type alt','alt.id = al.leave_type')
  24. ->where($map)
  25. ->field('al.*,ui.name as userName,alt.name as leaveName')
  26. ->page($page,$size)
  27. ->order('al.id desc')
  28. ->select();
  29. $lists = $lists?$lists:[];
  30. foreach ($lists as $k=>$v){
  31. $startday = date('Y-m-d',strtotime($v['start_time']));
  32. if($startday.' 12:00:00' > $v['start_time']){
  33. $lists[$k]['start_time'] = $startday.' 上午';
  34. } else {
  35. $lists[$k]['start_time'] = $startday.' 下午';
  36. }
  37. $endday = date('Y-m-d',strtotime($v['end_time']));
  38. if($endday.' 12:00:00' > $v['end_time']){
  39. $lists[$k]['end_time'] = $endday.' 上午';
  40. } else {
  41. $lists[$k]['end_time'] = $endday.' 下午';
  42. }
  43. }
  44. $total = Db::name('attendance_leave')
  45. ->alias('al')
  46. ->join('user_info ui','ui.user_id = al.user_id')
  47. ->join('attendance_leave_type alt','alt.id = al.leave_type')
  48. ->where($map)->count();
  49. $data = [
  50. 'total' => $total,
  51. 'list' => $lists?$lists:[]
  52. ];
  53. return $data;
  54. }
  55. public function leaveRecord($page,$size,$orgId,$uid,$startTime,$endTime){
  56. $map[] = ['al.status','=',1];
  57. $map[] = ['al.org_id','=',$orgId];
  58. if($startTime && $endTime){
  59. $map[] = ['ul.start_time','>=',$startTime.' 00:00:00'];
  60. $map[] = ['ul.end_time','<=',$endTime.' 23:59:59'];
  61. }
  62. if($uid > 0){
  63. $map[] = ['al.user_id','=',$uid];
  64. }
  65. $lists = Db::name('attendance_leave')
  66. ->alias('al')
  67. ->join('user_info ui','ui.user_id = al.user_id')
  68. ->join('attendance_leave_type alt','alt.id = al.leave_type')
  69. ->where($map)
  70. ->field('al.*,ui.name as userName,alt.name as leaveName')
  71. ->page($page,$size)
  72. ->order('al.id desc')
  73. ->select();
  74. $lists = $lists?$lists:[];
  75. foreach ($lists as $k=>$v){
  76. $startday = date('Y-m-d',strtotime($v['start_time']));
  77. if($startday.' 12:00:00' > $v['start_time']){
  78. $lists[$k]['start_time'] = $startday.' 上午';
  79. } else {
  80. $lists[$k]['start_time'] = $startday.' 下午';
  81. }
  82. $endday = date('Y-m-d',strtotime($v['end_time']));
  83. if($endday.' 12:00:00' > $v['end_time']){
  84. $lists[$k]['end_time'] = $endday.' 上午';
  85. } else {
  86. $lists[$k]['end_time'] = $endday.' 下午';
  87. }
  88. }
  89. $total = Db::name('attendance_leave')
  90. ->alias('al')
  91. ->join('user_info ui','ui.user_id = al.user_id')
  92. ->join('attendance_leave_type alt','alt.id = al.leave_type')
  93. ->where($map)->count();
  94. $data = [
  95. 'total' => $total,
  96. 'list' => $lists?$lists:[]
  97. ];
  98. return $data;
  99. }
  100. // 请假审批时生成请假记录
  101. public function advancedStartLeave($id,$orgId,$userId,$formJson){
  102. $formJson = json_decode($formJson,true);
  103. $start = '';
  104. $end = '';
  105. $isleavebj = '否'; // 默认不离京
  106. $lbjstart = '';
  107. $lbjend = '';
  108. $address = '';
  109. $type = 1; // 1=减去节假日 0=不减节假日
  110. $data = [
  111. 'org_id' => $orgId,
  112. 'user_id' => $userId,
  113. 'apply_id' => $id,
  114. 'create_time' => date('Y-m-d H:i:s'),
  115. 'status' => 0
  116. ];
  117. foreach ($formJson as $k=>$v){
  118. if($v['componentName'] == 'ddleavefield'){
  119. foreach ($v['components'] as $key=>$val){
  120. switch ($val['idx']){
  121. case '0':
  122. $leave = json_decode($val['values'],true);
  123. $data['leave_type'] = $leave['id'];
  124. break;
  125. case '1':
  126. $start = $val['values'];
  127. $starts = explode(' ',$start);
  128. if($starts[count($starts)-1] == '上午'){
  129. $data['start_time'] = $starts[0].' 00:00:00';
  130. }else{
  131. $data['start_time'] = $starts[0].' 12:00:00';
  132. }
  133. break;
  134. case '2':
  135. $end = $val['values'];
  136. $ends = explode(' ',$end);
  137. if($ends[count($ends)-1] == '上午'){
  138. $data['end_time'] = $ends[0].' 11:59:59';
  139. }else{
  140. $data['end_time'] = $ends[0].' 23:59:59';
  141. }
  142. break;
  143. // case '3':
  144. // $isleavebj = isset($val['values'])?$val['values']:'否';
  145. // break;
  146. // case '4':
  147. // $lbjstart = isset($val['values'])?$val['values']:'';
  148. // break;
  149. // case '5':
  150. // $lbjend = isset($val['values'])?$val['values']:'';
  151. // break;
  152. // case '6':
  153. // $address = isset($val['values'])?$val['values']:'';
  154. // break;
  155. }
  156. }
  157. break;
  158. }
  159. }
  160. if($data['leave_type'] == 6){// 产假,不减节假日
  161. $type = 0;
  162. }
  163. $data['days'] = calculate_leave($start,$end,$type);
  164. $data['cur_days'] = $data['days'];
  165. // if($isleavebj == '是'){
  166. // $ld = [
  167. // 'org_id' => $orgId,
  168. // 'user_id' => $userId,
  169. // 'start' => $lbjstart,
  170. // 'end' => $lbjend,
  171. // 'address' => $address,
  172. // 'type' => 1,
  173. // 'apply_id' => $id
  174. // ];
  175. // $res = model('LeaveBj')->add($ld);
  176. // if(!$res){
  177. // return false;
  178. // }
  179. // }
  180. if($data['leave_type'] == 3){
  181. // 扣除年假期余额
  182. $uret = Db::name('user_info')->where('user_id',$userId)->setDec('annual_leave',$data['days']);
  183. if(!$uret){
  184. return false;
  185. }
  186. }
  187. $ret = Db::name('attendance_leave')->insert($data);
  188. return $ret?true:false;
  189. }
  190. public function advancedEndLeave($id,$orgId,$userId,$formJson){
  191. $data = [
  192. 'update_time' => date('Y-m-d H:i:s'),
  193. 'status' => 1
  194. ];
  195. // 检查是否有离京记录,有就更新状态
  196. /*$info = Db::name('leave_bj')->where('apply_id',$id)->find();
  197. if($info){
  198. $res = Db::name('leave_bj')->where('id',$info['id'])->update($data);
  199. if(!$res){
  200. return false;
  201. }
  202. }*/
  203. $ret = Db::name('attendance_leave')->where('apply_id',$id)->update($data);
  204. return $ret?true:false;
  205. }
  206. public function advancedDisagreeLeave($applyId){
  207. $leave = Db::name('attendance_leave')->where('apply_id',$applyId)->find();
  208. $data = [
  209. 'update_time' => date('Y-m-d H:i:s'),
  210. 'status' => 2
  211. ];
  212. // 检查是否有离京记录,有就更新状态
  213. /*$info = Db::name('leave_bj')->where('apply_id',$applyId)->find();
  214. if($info){
  215. $res = Db::name('leave_bj')->where('id',$info['id'])->update($data);
  216. if(!$res){
  217. return false;
  218. }
  219. }*/
  220. if($leave['leave_type'] == 3){ // 年假
  221. // 返还用户年假假期余额
  222. $uret = Db::name('user_info')->where('user_id',$leave['user_id'])->setInc('annual_leave',$leave['days']);
  223. if(!$uret){
  224. return false;
  225. }
  226. }
  227. $ret = Db::name('attendance_leave')->where('apply_id',$applyId)->update($data);
  228. return $ret?true:false;
  229. }
  230. }