LaborReport.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. namespace App;
  3. use Carbon\Carbon;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Traits\ModelTimeFormat;
  6. use Illuminate\Database\Eloquent\SoftDeletes;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\Gate;
  10. class LaborReport extends Model
  11. {
  12. use ModelTimeFormat;
  13. use SoftDeletes;
  14. protected $fillable=[
  15. 'id','enter_number','user_workgroup_id','user_id','name','mobile_phone','identity_number','labor_company_id',
  16. 'check_in_at','verify_at','group_user_id','check_out_at','online_duration','working_duration','created_at','updated_at',
  17. 'user_duty_check_id','relax_time','remark'
  18. ];
  19. protected $appends = [
  20. 'is_exportGroup','is_export', 'exit_at','enter_at','sequence', 'amountOfJoined','thisRecordOnlineTime','thisRecordWorkingTime','thisRoundRecordWorkingTime',
  21. 'totalOnlineTime','verifyPerson','userDutyCheckVerifyUserId','isAdult','round_check_in_at','round_check_out_at','has_group_verify_right',
  22. ];
  23. protected $tempFields = [
  24. 'temEnteringRecord',
  25. 'sequence',
  26. 'amountOfJoined',
  27. ];
  28. static function tokenOfBroadcastEnterAndLeave(){
  29. return md5('tokenOfListAll'.Carbon::now()->format('Y-m-d'));
  30. }
  31. public function userWorkgroup(){
  32. return $this->hasOne('App\UserWorkgroup','id','user_workgroup_id');
  33. }
  34. public function laborCompany(){
  35. return $this->hasOne('App\LaborCompany','id','labor_company_id');
  36. }
  37. public function user(){
  38. return $this->hasOne('App\User','id','group_user_id');
  39. }
  40. public function userDutyCheck(){
  41. return $this->hasOne('App\UserDutyCheck','id','user_duty_check_id');
  42. }
  43. public function laborReportStatus(){
  44. return $this->hasMany('App\LaborReportStatus','labor_report_id','id');
  45. }
  46. public function getRoundCheckInAtAttribute(){
  47. if (!$this['check_in_at'])return null;
  48. $round_check_in_at=Carbon::parse($this['check_in_at'])->format('i');
  49. if ($round_check_in_at>=0&&$round_check_in_at<=5) return Carbon::parse($this['check_in_at'])->clone()->setMinutes(00)->setSeconds(00)->format('Y-m-d H:i:s');
  50. if ($round_check_in_at>5&&$round_check_in_at<=35) return Carbon::parse($this['check_in_at'])->clone()->setMinutes(30)->setSeconds(00)->format('Y-m-d H:i:s');
  51. if ($round_check_in_at>35&&$round_check_in_at<=59) return Carbon::parse($this['check_in_at'])->clone()->addHour()->setMinutes(00)->setSeconds(00)->format('Y-m-d H:i:s');
  52. }
  53. public function getRoundCheckOutAtAttribute(){
  54. if (!$this['check_out_at'])return null;
  55. $round_check_out_at=Carbon::parse($this['check_out_at'])->format('i');
  56. if ($round_check_out_at>=0&&$round_check_out_at<25) return Carbon::parse($this['check_out_at'])->clone()->setMinutes(00)->setSeconds(00)->format('Y-m-d H:i:s');
  57. if ($round_check_out_at>=25&&$round_check_out_at<=55) return Carbon::parse($this['check_out_at'])->clone()->setMinutes(30)->setSeconds(00)->format('Y-m-d H:i:s');
  58. if ($round_check_out_at>55&&$round_check_out_at<=59) return Carbon::parse($this['check_out_at'])->clone()->addHour()->setMinutes(00)->setSeconds(00)->format('Y-m-d H:i:s');
  59. }
  60. //has_group_verify_right'
  61. public function getHasGroupVerifyRightAttribute(){
  62. if (!Gate::allows('人事管理-临时工报表')){return null; }
  63. if (Gate::allows('人事管理-临时工报表-管理全部组')){return true;}
  64. $user=Auth::user();
  65. $userWorkgroupIds=$user->getPermittingWorkgroupIds($allowAll=false);
  66. if (count($userWorkgroupIds)!=0) return in_array($this['user_workgroup_id'],$userWorkgroupIds);
  67. }
  68. public function getIsExportGroupAttribute(){
  69. return $this['check_out_at']? true:false;
  70. }
  71. public function getVerifyPersonAttribute(){
  72. if ($this['group_user_id']&&$this['group_user_id']==Auth::user()['id'])
  73. return Auth::user()['name'];
  74. }
  75. public function getUserDutyCheckVerifyUserIdAttribute(){
  76. return $this['userDutyCheck']['verify_user_id']?$this['userDutyCheck']['verify_user_id']:'';
  77. }
  78. //总在线时长
  79. public function getTotalOnlineTimeAttribute(){
  80. $laborReports=LaborReport::where('enter_number',$this['enter_number'])->get();
  81. $totalOnlineTime=$laborReports->reduce(function ($value,$laborReport){
  82. return ($value??0)+$laborReport['thisRecordOnlineTime'];
  83. });
  84. return $totalOnlineTime;
  85. }
  86. //总工作时长
  87. public function getTotalWorkingTimeAttribute(){
  88. $laborReports=LaborReport::where('enter_number',$this['enter_number'])->orderBy('id','asc')->get();
  89. $totalWorkingTime=$laborReports->reduce(function ($value,$laborReport){
  90. return ($value??0)+$laborReport['thisRecordWorkingTime'];
  91. });
  92. return $totalWorkingTime;
  93. }
  94. //本轮工作起始时间
  95. public function getThisRoundOnlineStartTimeAttribute(){
  96. $laborReports=LaborReport::where('enter_number',$this['enter_number'])->orderBy('id','asc')->get();
  97. foreach ($laborReports as $laborReport){
  98. if ($laborReport['enter_at']&&$laborReport['check_in_at'])
  99. return $laborReport['check_in_at'];
  100. }
  101. }
  102. //本轮工作结束时间
  103. public function getThisRoundOnlineEndTimeAttribute(){
  104. $laborReports=LaborReport::where('enter_number',$this['enter_number'])->get();
  105. foreach ($laborReports as $laborReport){
  106. if ($laborReport['exit_at']&&$laborReport['check_out_at'])
  107. return $laborReport['check_out_at'];
  108. }
  109. }
  110. //本次在线时长
  111. public function getThisRecordOnlineTimeAttribute(){
  112. if($this['enter_at']&&!$this['check_out_at']&&!$this['exit_at'])
  113. return round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['enter_at']))/3600,2);
  114. if ($this['enter_at']&&$this['check_out_at']&&!$this['exit_at'])
  115. return round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['enter_at']))/3600,2);
  116. if ($this['enter_at']&&$this['exit_at'])
  117. return round(Carbon::parse($this['exit_at'])->diffInSeconds(Carbon::parse($this['enter_at']))/3600,2);
  118. if ($this['check_in_at']&&!$this['enter_at']&&!$this['check_out_at']&&!$this['exit_at'])
  119. return round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['check_in_at']))/3600,2);
  120. if ($this['check_in_at']&&!$this['enter_at']&&$this['check_out_at']&&!$this['exit_at'])
  121. return round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['check_in_at']))/3600,2);
  122. if ($this['check_in_at']&&!$this['enter_at']&&$this['exit_at'])
  123. return round(Carbon::parse($this['exit_at'])->diffInSeconds(Carbon::parse($this['check_in_at']))/3600,2);
  124. }
  125. //本次工作时长
  126. public function getThisRecordWorkingTimeAttribute(){
  127. if ($this['check_in_at']&&!$this['check_out_at']&&!$this['relax_time']){
  128. $workingTime=round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['check_in_at']))/3600,2);
  129. $checkInTime=Carbon::parse($this['check_in_at']);
  130. $checkOutTime=Carbon::parse(Carbon::now())->format('H');
  131. return $this->minusLunchTime($checkInTime,$checkOutTime,$workingTime);
  132. }
  133. if ($this['check_in_at']&&$this['check_out_at']&&!$this['relax_time']){
  134. $workingTime=round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['check_in_at']))/3600,2);
  135. $checkInTime=Carbon::parse($this['check_in_at']);
  136. $checkOutTime=Carbon::parse($this['check_out_at'])->format('H');
  137. return $this->minusLunchTime($checkInTime,$checkOutTime,$workingTime);
  138. }
  139. if ($this['check_in_at']&&$this['check_out_at']&&$this['relax_time']){
  140. $workingTime=round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['check_in_at']))/3600,2);
  141. $checkInTime=Carbon::parse($this['check_in_at']);
  142. $checkOutTime=Carbon::parse($this['check_out_at'])->format('H');
  143. return $this->minusLunchTime($checkInTime,$checkOutTime,$workingTime)-$this['relax_time']/60;
  144. }
  145. }
  146. //本次取整工作时长
  147. public function getThisRoundRecordWorkingTimeAttribute(){
  148. if ($this['round_check_in_at']&&!$this['round_check_out_at']&&!$this['relax_time']){
  149. $workingTime=round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['round_check_in_at']))/3600,2);
  150. $checkInTime=Carbon::parse($this['round_check_in_at']);
  151. $checkOutTime=Carbon::parse(Carbon::now())->format('H');
  152. return $this->minusLunchTime($checkInTime,$checkOutTime,$workingTime);
  153. }
  154. if ($this['round_check_in_at']&&$this['round_check_out_at']&&!$this['relax_time']){
  155. $workingTime=round(Carbon::parse($this['round_check_out_at'])->diffInSeconds(Carbon::parse($this['round_check_in_at']))/3600,2);
  156. $checkInTime=Carbon::parse($this['round_check_in_at']);
  157. $checkOutTime=Carbon::parse($this['round_check_out_at'])->format('H');
  158. return $this->minusLunchTime($checkInTime,$checkOutTime,$workingTime);
  159. }
  160. if ($this['round_check_in_at']&&$this['round_check_out_at']&&$this['relax_time']){
  161. $workingTime=round(Carbon::parse($this['round_check_out_at'])->diffInSeconds(Carbon::parse($this['round_check_in_at']))/3600,2);
  162. $checkInTime=Carbon::parse($this['round_check_in_at']);
  163. $checkOutTime=Carbon::parse($this['round_check_out_at'])->format('H');
  164. return $this->minusLunchTime($checkInTime,$checkOutTime,$workingTime)-$this['relax_time']/60;
  165. }
  166. }
  167. //工作时长减午饭休息时间
  168. public function minusLunchTime($checkInTime,$checkOutTime,$hour){
  169. if ($checkInTime->lessThanOrEqualTo($checkInTime->clone()->setHours(11)->setMinutes(30))&&(int)$checkOutTime>=13){
  170. $hour=$hour-1;
  171. }
  172. return $hour;
  173. }
  174. //转场序号
  175. public function getSequenceAttribute()
  176. {
  177. if(Arr::has($this->tempFields,'sequence'))return $this->tempFields['sequence'];
  178. $laborReports=LaborReport::where('enter_number',$this['enter_number'])->orderBy('id','asc')->get();
  179. foreach($laborReports as $i=>$laborReport){
  180. if($laborReport['id']==$this['id']){
  181. $this->tempFields['sequence']=$i+1;
  182. return $this->tempFields['sequence'];
  183. }
  184. }
  185. }
  186. public function getAmountOfJoinedAttribute()
  187. {
  188. if(Arr::has($this->tempFields,'amountOfJoined'))return $this->tempFields['amountOfJoined'];
  189. $this->tempFields['amountOfJoined']=LaborReport::where('enter_number',$this['enter_number'])->orderBy('id','asc')->count();
  190. return $this->tempFields['amountOfJoined'];
  191. }
  192. //进场时间
  193. public function getEnterAtAttribute()
  194. {
  195. $laborReportStatus=LaborReportStatus::where('labor_report_id',$this['id'])->where('status','未审核')->orderBy('id','desc')->first();
  196. if (empty($laborReportStatus))return null;
  197. return Carbon::parse($laborReportStatus['created_at'])->format('Y-m-d H:i:s');
  198. }
  199. public function getEnteringRecordAttribute()
  200. {
  201. $laborReport=LaborReport::with(['laborReportStatus'=>function($query){
  202. return $query->whereIn('status',['已入场','未审核']);
  203. }])->where('user_id',$this['user_id'])->orderBy('id','desc')->first();
  204. if(empty($laborReport))return null;
  205. return $laborReport;
  206. }
  207. public function getExitAtAttribute()
  208. {
  209. $laborReportStatus=LaborReportStatus::where('labor_report_id',$this['id'])->where('status','已退场')->orderBy('id','desc')->first();
  210. if (empty($laborReportStatus))return null;
  211. return Carbon::parse($laborReportStatus['created_at'])->format('Y-m-d H:i:s');
  212. }
  213. //创建或获取进场编号
  214. public function makeOrGetEnteringNumber()
  215. {
  216. if($this['enteringRecord']&&$this['enteringRecord']['enter_number'])
  217. return $this['enteringRecord']['enter_number'];
  218. $enteringNumber=date("ymd").str_pad($this['id']>99999?$this['id']%99999:$this['id'],4,"0",STR_PAD_LEFT);
  219. $this['enter_number']=$enteringNumber;
  220. return $enteringNumber;
  221. }
  222. public function getIsExportAttribute(){
  223. $laborReportStatus=$this->laborReportStatus()->where('labor_report_id',$this['id'])->orderBy('id','desc')->first();
  224. return $laborReportStatus['status']=='已退场'?true:false;
  225. }
  226. //出场更新临时工报表信息
  227. static function exitAndChangeLaborReport($laborReport,$userDutyCheck){
  228. $laborReportStatus=new LaborReportStatus([
  229. 'labor_report_id'=>$laborReport['id'],
  230. 'status'=>'已退场',
  231. ]);
  232. $laborReportStatus->save();
  233. $check_in_at=$laborReport->check_in_at??null;
  234. $exit_at=$userDutyCheck->checked_at;
  235. $online_duration=round(Carbon::parse($exit_at)->diffInSeconds(Carbon::parse($check_in_at))/3600,2);
  236. $laborReport->user_duty_check_id=$userDutyCheck->id;
  237. if ($laborReport->enter_at){
  238. $enter_at=$laborReport['enter_at'];
  239. $laborReport->online_duration=round(Carbon::parse($exit_at)->diffInSeconds(Carbon::parse($enter_at))/3600,2);
  240. }else{
  241. $laborReport->online_duration=$online_duration;
  242. }
  243. $laborReport->update();
  244. $laborReport->is_export=$laborReport->getIsExportAttribute();
  245. return $laborReport;
  246. }
  247. /**
  248. * 是否成年
  249. * @param 身份证号
  250. * @return int 0 成年,1未成年
  251. */
  252. public function getIsAdultAttribute(){
  253. $flag = 0;
  254. $tyear=intval(substr($this['identity_number'],6,4));
  255. $tmonth=intval(substr($this['identity_number'],10,2));
  256. $tday=intval(substr($this['identity_number'],12,2));
  257. if($tyear>date("Y")||$tyear<(date("Y")-100)){
  258. $flag=0;
  259. }elseif($tmonth<0||$tmonth>12){
  260. $flag=0;
  261. }elseif($tday<0||$tday>31){
  262. $flag=0;
  263. }else{
  264. $day_sum = self::full_year_day($tyear,$tmonth,16);
  265. if((time()-mktime(0,0,0,$tmonth,$tday,$tyear))>$day_sum*24*60*60){
  266. $flag=0;
  267. }else{
  268. $flag=1;
  269. }
  270. }
  271. return $flag;
  272. }
  273. /**
  274. * n周岁的天数
  275. * @param $tyear
  276. * @param $tmonth
  277. * @return int
  278. */
  279. public static function full_year_day($tyear,$tmonth,$type=16){
  280. $sum=365*$type;
  281. for($i=$tyear+1;$i<$tyear+$type;$i++)//考虑中间年份
  282. {
  283. if(self::is_leap_year($i))
  284. $sum++;
  285. }
  286. if(self::is_leap_year($tyear)&&$tmonth<=2)//考虑初末两年
  287. $sum++;
  288. if(self::is_leap_year($tyear+$type)&&$tmonth>=3){
  289. $sum++;
  290. }
  291. return $sum;
  292. }
  293. /**
  294. * @param $year
  295. * @return int 1是闰年,0不是闰年
  296. */
  297. public static function is_leap_year($year){
  298. if(($year%4==0&&$year%100!=0)||$year%400==0)
  299. return 1;
  300. else
  301. return 0;
  302. }
  303. }