LaborReport.php 16 KB

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