LaborReport.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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','thisRecordOnlineTime','thisRecordWorkingTime','thisRoundRecordWorkingTime',
  24. '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. //进组取整时间
  60. public function getRoundCheckInAtAttribute(){
  61. if (!$this['check_in_at'])return null;
  62. $round_check_in_at=Carbon::parse($this['check_in_at'])->format('i');
  63. 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');
  64. 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');
  65. 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');
  66. }
  67. //退组取整时间
  68. public function getRoundCheckOutAtAttribute(){
  69. if (!$this['check_out_at'])return null;
  70. $round_check_out_at=Carbon::parse($this['check_out_at'])->format('i');
  71. 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');
  72. 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');
  73. 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');
  74. }
  75. public function getHasGroupVerifyRightAttribute(){
  76. if (!Gate::allows('人事管理-临时工报表')){return null; }
  77. if (Gate::allows('人事管理-临时工报表-管理全部组')){return true;}
  78. $user=Auth::user();
  79. $userWorkgroupIds=$user->getPermittingWorkgroupIds($allowAll=false);
  80. if (count($userWorkgroupIds)!=0) return in_array($this['user_workgroup_id'],$userWorkgroupIds);
  81. }
  82. public function getEnteringRecordAttribute()
  83. {
  84. if(self::$withoutAppends) {
  85. $laborReport = LaborReport::with(['laborReportStatus' => function ($query) {
  86. return $query->whereIn('status', ['已入场', '未审核']);
  87. }])->where('user_id', $this['user_id'])->orderBy('id', 'desc')->first();
  88. if (empty($laborReport)) return null;
  89. return $laborReport;
  90. }
  91. }
  92. public function getIsExportGroupAttribute(){
  93. return $this['check_out_at']? true:false;
  94. }
  95. public function getVerifyPersonAttribute(){
  96. if ($this['group_user_id']&&$this['group_user_id']==Auth::user()['id'])
  97. return Auth::user()['name'];
  98. }
  99. public function getUserDutyCheckVerifyUserIdAttribute(){
  100. return $this['userDutyCheck']['verify_user_id']?$this['userDutyCheck']['verify_user_id']:'';
  101. }
  102. //总工作时长
  103. public function getTotalWorkingTimeAttribute(){
  104. if(self::$withoutAppends) {
  105. $totalWorkingTime = $this->laborReports->reduce(function ($value, $laborReport) {
  106. return ($value ?? 0) + $laborReport['thisRecordWorkingTime'];
  107. });
  108. return $totalWorkingTime;
  109. }
  110. }
  111. //本轮工作起始时间
  112. public function getThisRoundOnlineStartTimeAttribute(){
  113. if(self::$withoutAppends) {
  114. foreach ($this->laborReports as $laborReport) {
  115. if ($laborReport['enter_at'] && $laborReport['check_in_at'])
  116. return $laborReport['check_in_at'];
  117. }
  118. }
  119. }
  120. //本轮工作结束时间
  121. public function getThisRoundOnlineEndTimeAttribute(){
  122. if(self::$withoutAppends) {
  123. foreach ($this->laborReports as $laborReport) {
  124. if ($laborReport['exit_at'] && $laborReport['check_out_at'])
  125. return $laborReport['check_out_at'];
  126. }
  127. }
  128. }
  129. //本次在线时长
  130. public function getThisRecordOnlineTimeAttribute(){
  131. if ($this['enter_at'] && !$this['check_out_at'] && !$this['exit_at'])
  132. return round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['enter_at'])) / 3600, 2);
  133. if ($this['enter_at'] && $this['check_out_at'] && !$this['exit_at'])
  134. return round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['enter_at'])) / 3600, 2);
  135. if ($this['enter_at'] && $this['exit_at'])
  136. return round(Carbon::parse($this['exit_at'])->diffInSeconds(Carbon::parse($this['enter_at'])) / 3600, 2);
  137. if ($this['check_in_at'] && !$this['enter_at'] && !$this['check_out_at'] && !$this['exit_at'])
  138. return round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['check_in_at'])) / 3600, 2);
  139. if ($this['check_in_at'] && !$this['enter_at'] && $this['check_out_at'] && !$this['exit_at'])
  140. return round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['check_in_at'])) / 3600, 2);
  141. if ($this['check_in_at'] && !$this['enter_at'] && $this['exit_at'])
  142. return round(Carbon::parse($this['exit_at'])->diffInSeconds(Carbon::parse($this['check_in_at'])) / 3600, 2);
  143. }
  144. //本次工作时长
  145. public function getThisRecordWorkingTimeAttribute(){
  146. if ($this['check_in_at']&&!$this['check_out_at']&&!$this['relax_time']) {
  147. $workingTime = round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['check_in_at'])) / 3600, 2);
  148. $checkInTime = Carbon::parse($this['check_in_at']);
  149. $checkOutTime = Carbon::parse(Carbon::now());
  150. return $this->minusLunchTime($checkInTime, $checkOutTime, $workingTime);
  151. }
  152. if ($this['check_in_at']&&$this['check_out_at']&&!$this['relax_time']){
  153. $workingTime=round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['check_in_at']))/3600,2);
  154. $checkInTime=Carbon::parse($this['check_in_at']);
  155. $checkOutTime=Carbon::parse($this['check_out_at']);
  156. return $this->minusLunchTime($checkInTime,$checkOutTime,$workingTime);
  157. }
  158. if ($this['check_in_at']&&$this['check_out_at']&&$this['relax_time']){
  159. $workingTime=round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['check_in_at']))/3600,2);
  160. $checkInTime=Carbon::parse($this['check_in_at']);
  161. $checkOutTime=Carbon::parse($this['check_out_at']);
  162. return $this->minusLunchTime($checkInTime,$checkOutTime,$workingTime)-$this['relax_time']/60;
  163. }
  164. }
  165. //本次取整工作时长
  166. public function getThisRoundRecordWorkingTimeAttribute(){
  167. if ($this['round_check_in_at'] && !$this['round_check_out_at'] && !$this['relax_time']) {
  168. $workingTime = round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['round_check_in_at'])) / 3600, 2);
  169. $checkInTime = Carbon::parse($this['round_check_in_at']);
  170. $checkOutTime = Carbon::parse(Carbon::now());
  171. return $this->minusLunchTime($checkInTime, $checkOutTime, $workingTime);
  172. }
  173. if ($this['round_check_in_at'] && $this['round_check_out_at'] && !$this['relax_time']) {
  174. $workingTime = round(Carbon::parse($this['round_check_out_at'])->diffInSeconds(Carbon::parse($this['round_check_in_at'])) / 3600, 2);
  175. $checkInTime = Carbon::parse($this['round_check_in_at']);
  176. $checkOutTime = Carbon::parse($this['round_check_out_at']);
  177. return $this->minusLunchTime($checkInTime, $checkOutTime, $workingTime);
  178. }
  179. if ($this['round_check_in_at'] && $this['round_check_out_at'] && $this['relax_time']) {
  180. $workingTime = round(Carbon::parse($this['round_check_out_at'])->diffInSeconds(Carbon::parse($this['round_check_in_at'])) / 3600, 2);
  181. $checkInTime = Carbon::parse($this['round_check_in_at']);
  182. $checkOutTime = Carbon::parse($this['round_check_out_at']);
  183. return $this->minusLunchTime($checkInTime, $checkOutTime, $workingTime) - $this['relax_time'] / 60;
  184. }
  185. }
  186. //工作时长默认休息时间
  187. public function minusLunchTime($checkInTime,$checkOutTime,$hour){
  188. //白班:第一餐 默认减去1小时
  189. if ($checkInTime->lessThanOrEqualTo($checkInTime->clone()->setHours(11)->setMinutes(30))//进组时间小于等于进组当天11:30
  190. &&Carbon::parse($checkOutTime)->gte(Carbon::parse($checkInTime->clone()->setHour(13)->setMinute(00)->setSecond(00)))){//退组时间大于进组当天的13:00
  191. $hour=$hour-1;
  192. }
  193. //夜班:第一餐 默认减去半小时
  194. if ($checkInTime->lessThanOrEqualTo($checkInTime->clone()->setHours(23)->setMinutes(59))//进组时间小于等于当天23:59
  195. &&Carbon::parse($checkOutTime)->gte(Carbon::parse($checkInTime->clone()->subDays(-1)->setHour(1)))){//退组时间大于等于进组后一天的1:00
  196. $hour=$hour-0.5;
  197. }
  198. return $hour;
  199. }
  200. private function checkExitStatus()
  201. {
  202. $status = $this->laborReportStatus->sortByDesc('id')->first();
  203. $this->is_export = ($status['status'] == '已退场') ? true :false;
  204. if ($status['status'] == '已退场')$this->exit_at=Carbon::parse($status['created_at'])->format('Y-m-d H:i:s');
  205. }
  206. private function checkEnterStatus(){
  207. $status = $this->laborReportStatus->sortBy('id')->first();
  208. if ($status['status'] == '未审核')$this->enter_at=Carbon::parse($status['created_at'])->format('Y-m-d H:i:s');
  209. }
  210. private function checkAmountOfJoined(){
  211. if ($this->amountOfJoined!=count($this->laborReports))$this->amountOfJoined=count($this->laborReports);
  212. }
  213. //转组序号
  214. private function checkSequence(){
  215. if (!$this->sequence) foreach ($this->laborReports as $i => $laborReport) {
  216. if ($laborReport['id'] == $this['id']) {
  217. $this->sequence = $i + 1;
  218. }
  219. }
  220. }
  221. //总在线时长
  222. private function checkTotalOnlineTime(){
  223. if (!$this->totalOnlineTime) $this->totalOnlineTime = $this->laborReports->reduce(function ($value, $laborReport) {
  224. return ($value ?? 0) + $laborReport['thisRecordOnlineTime'];
  225. });
  226. }
  227. public static function processing($laborReports)
  228. {
  229. foreach ($laborReports as $laborReport){
  230. /** @var LaborReport $laborReport */
  231. $laborReport->checkEnterStatus(); //校验进场并赋值
  232. $laborReport->checkAmountOfJoined();
  233. $laborReport->checkSequence();
  234. $laborReport->checkTotalOnlineTime();
  235. $laborReport->checkExitStatus(); //校验出场并赋值
  236. }
  237. }
  238. //创建或获取进场编号
  239. public function makeOrGetEnteringNumber(): string
  240. {
  241. if($this['enteringRecord']&&$this['enteringRecord']['enter_number'])
  242. return $this['enteringRecord']['enter_number'];
  243. $enteringNumber=date("ymd").str_pad($this['id']>99999?$this['id']%99999:$this['id'],4,"0",STR_PAD_LEFT);
  244. $this['enter_number']=$enteringNumber;
  245. return $enteringNumber;
  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=true;
  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. }