LaborReport.php 17 KB

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