LaborReport.php 16 KB

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