LaborReport.php 16 KB

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