LaborReport.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. <<<<<<< HEAD
  97. if ($this['group_user_id']&&$this['group_user_id']==Auth::user()['id'])
  98. return Auth::user()['name'];
  99. =======
  100. $user=Auth::user();
  101. if ($this['group_user_id']&&$user){
  102. if ($this['group_user_id']==$user['id']){
  103. return $user['name'];
  104. }
  105. }
  106. >>>>>>> b0347ca84d8eeedd9c1ac88b1c55b77c6a0613e8
  107. }
  108. public function getUserDutyCheckVerifyUserIdAttribute(){
  109. return $this['userDutyCheck']['verify_user_id']?$this['userDutyCheck']['verify_user_id']:'';
  110. }
  111. //总工作时长
  112. public function getTotalWorkingTimeAttribute(){
  113. if(self::$withoutAppends) {
  114. $totalWorkingTime = $this->laborReports->reduce(function ($value, $laborReport) {
  115. return ($value ?? 0) + $laborReport['thisRecordWorkingTime'];
  116. });
  117. return $totalWorkingTime;
  118. }
  119. }
  120. //本轮工作起始时间
  121. public function getThisRoundOnlineStartTimeAttribute(){
  122. if(self::$withoutAppends) {
  123. foreach ($this->laborReports as $laborReport) {
  124. if ($laborReport['enter_at'] && $laborReport['check_in_at'])
  125. return $laborReport['check_in_at'];
  126. }
  127. }
  128. }
  129. //本轮工作结束时间
  130. public function getThisRoundOnlineEndTimeAttribute(){
  131. if(self::$withoutAppends) {
  132. foreach ($this->laborReports as $laborReport) {
  133. if ($laborReport['exit_at'] && $laborReport['check_out_at'])
  134. return $laborReport['check_out_at'];
  135. }
  136. }
  137. }
  138. //本次在线时长
  139. public function getThisRecordOnlineTimeAttribute(){
  140. if ($this['enter_at'] && !$this['check_out_at'] && !$this['exit_at'])
  141. return round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['enter_at'])) / 3600, 2);
  142. if ($this['enter_at'] && $this['check_out_at'] && !$this['exit_at'])
  143. return round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['enter_at'])) / 3600, 2);
  144. if ($this['enter_at'] && $this['exit_at'])
  145. return round(Carbon::parse($this['exit_at'])->diffInSeconds(Carbon::parse($this['enter_at'])) / 3600, 2);
  146. if ($this['check_in_at'] && !$this['enter_at'] && !$this['check_out_at'] && !$this['exit_at'])
  147. return round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['check_in_at'])) / 3600, 2);
  148. if ($this['check_in_at'] && !$this['enter_at'] && $this['check_out_at'] && !$this['exit_at'])
  149. return round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['check_in_at'])) / 3600, 2);
  150. if ($this['check_in_at'] && !$this['enter_at'] && $this['exit_at'])
  151. return round(Carbon::parse($this['exit_at'])->diffInSeconds(Carbon::parse($this['check_in_at'])) / 3600, 2);
  152. }
  153. //本次工作时长
  154. public function getThisRecordWorkingTimeAttribute(){
  155. if ($this['check_in_at']&&!$this['check_out_at']&&!$this['relax_time']) {
  156. $workingTime = round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['check_in_at'])) / 3600, 2);
  157. $checkInTime = Carbon::parse($this['check_in_at']);
  158. $checkOutTime = Carbon::parse(Carbon::now());
  159. return $this->minusLunchTime($checkInTime, $checkOutTime, $workingTime);
  160. }
  161. if ($this['check_in_at']&&$this['check_out_at']&&!$this['relax_time']){
  162. $workingTime=round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['check_in_at']))/3600,2);
  163. $checkInTime=Carbon::parse($this['check_in_at']);
  164. $checkOutTime=Carbon::parse($this['check_out_at']);
  165. return $this->minusLunchTime($checkInTime,$checkOutTime,$workingTime);
  166. }
  167. if ($this['check_in_at']&&$this['check_out_at']&&$this['relax_time']){
  168. $workingTime=round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['check_in_at']))/3600,2);
  169. $checkInTime=Carbon::parse($this['check_in_at']);
  170. $checkOutTime=Carbon::parse($this['check_out_at']);
  171. return $this->minusLunchTime($checkInTime,$checkOutTime,$workingTime)-$this['relax_time']/60;
  172. }
  173. }
  174. //本次取整工作时长
  175. public function getThisRoundRecordWorkingTimeAttribute(){
  176. if ($this['round_check_in_at'] && !$this['round_check_out_at'] && !$this['relax_time']) {
  177. $workingTime = round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['round_check_in_at'])) / 3600, 2);
  178. $checkInTime = Carbon::parse($this['round_check_in_at']);
  179. $checkOutTime = Carbon::parse(Carbon::now());
  180. return $this->minusLunchTime($checkInTime, $checkOutTime, $workingTime);
  181. }
  182. if ($this['round_check_in_at'] && $this['round_check_out_at'] && !$this['relax_time']) {
  183. $workingTime = round(Carbon::parse($this['round_check_out_at'])->diffInSeconds(Carbon::parse($this['round_check_in_at'])) / 3600, 2);
  184. $checkInTime = Carbon::parse($this['round_check_in_at']);
  185. $checkOutTime = Carbon::parse($this['round_check_out_at']);
  186. return $this->minusLunchTime($checkInTime, $checkOutTime, $workingTime);
  187. }
  188. if ($this['round_check_in_at'] && $this['round_check_out_at'] && $this['relax_time']) {
  189. $workingTime = round(Carbon::parse($this['round_check_out_at'])->diffInSeconds(Carbon::parse($this['round_check_in_at'])) / 3600, 2);
  190. $checkInTime = Carbon::parse($this['round_check_in_at']);
  191. $checkOutTime = Carbon::parse($this['round_check_out_at']);
  192. return $this->minusLunchTime($checkInTime, $checkOutTime, $workingTime) - $this['relax_time'] / 60;
  193. }
  194. }
  195. //工作时长默认休息时间
  196. public function minusLunchTime($checkInTime,$checkOutTime,$hour){
  197. //白班:第一餐 默认减去1小时
  198. if ($checkInTime->lessThanOrEqualTo($checkInTime->clone()->setHours(11)->setMinutes(30))//进组时间小于等于进组当天11:30
  199. &&Carbon::parse($checkOutTime)->gte(Carbon::parse($checkInTime->clone()->setHour(13)->setMinute(00)->setSecond(00)))){//退组时间大于进组当天的13:00
  200. $hour=$hour-1;
  201. }
  202. //夜班:第一餐 默认减去半小时
  203. if ($checkInTime->lessThanOrEqualTo($checkInTime->clone()->setHours(23)->setMinutes(59))//进组时间小于等于当天23:59
  204. &&Carbon::parse($checkOutTime)->gte(Carbon::parse($checkInTime->clone()->subDays(-1)->setHour(1)))){//退组时间大于等于进组后一天的1:00
  205. $hour=$hour-0.5;
  206. }
  207. return $hour;
  208. }
  209. private function checkExitStatus()
  210. {
  211. $status = $this->laborReportStatus->sortByDesc('id')->first();
  212. $this->is_export = ($status['status'] == '已退场') ? true :false;
  213. if ($status['status'] == '已退场')$this->exit_at=Carbon::parse($status['created_at'])->format('Y-m-d H:i:s');
  214. }
  215. private function checkEnterStatus(){
  216. $status = $this->laborReportStatus->sortBy('id')->first();
  217. if ($status['status'] == '未审核')$this->enter_at=Carbon::parse($status['created_at'])->format('Y-m-d H:i:s');
  218. }
  219. private function checkAmountOfJoined(){
  220. if ($this->amountOfJoined!=count($this->laborReports))$this->amountOfJoined=count($this->laborReports);
  221. }
  222. //转组序号
  223. private function checkSequence(){
  224. if (!$this->sequence) foreach ($this->laborReports as $i => $laborReport) {
  225. if ($laborReport['id'] == $this['id']) {
  226. $this->sequence = $i + 1;
  227. }
  228. }
  229. }
  230. //总在线时长
  231. private function checkTotalOnlineTime(){
  232. if (!$this->totalOnlineTime) $this->totalOnlineTime = $this->laborReports->reduce(function ($value, $laborReport) {
  233. return ($value ?? 0) + $laborReport['thisRecordOnlineTime'];
  234. });
  235. }
  236. public static function processing($laborReports)
  237. {
  238. foreach ($laborReports as $laborReport){
  239. /** @var LaborReport $laborReport */
  240. $laborReport->checkEnterStatus(); //校验进场并赋值
  241. $laborReport->checkAmountOfJoined();
  242. $laborReport->checkSequence();
  243. $laborReport->checkTotalOnlineTime();
  244. $laborReport->checkExitStatus(); //校验出场并赋值
  245. }
  246. }
  247. //创建或获取进场编号
  248. public function makeOrGetEnteringNumber(): string
  249. {
  250. if($this['enteringRecord']&&$this['enteringRecord']['enter_number'])
  251. return $this['enteringRecord']['enter_number'];
  252. $enteringNumber=date("ymd").str_pad($this['id']>99999?$this['id']%99999:$this['id'],4,"0",STR_PAD_LEFT);
  253. $this['enter_number']=$enteringNumber;
  254. return $enteringNumber;
  255. }
  256. //出场更新临时工报表信息
  257. static function exitAndChangeLaborReport($laborReport,$userDutyCheck){
  258. $laborReportStatus=new LaborReportStatus([
  259. 'labor_report_id'=>$laborReport['id'],
  260. 'status'=>'已退场',
  261. ]);
  262. $laborReportStatus->save();
  263. $check_in_at=$laborReport->check_in_at??null;
  264. $exit_at=$userDutyCheck->checked_at;
  265. $online_duration=round(Carbon::parse($exit_at)->diffInSeconds(Carbon::parse($check_in_at))/3600,2);
  266. $laborReport->user_duty_check_id=$userDutyCheck->id;
  267. if ($laborReport->enter_at){
  268. $enter_at=$laborReport['enter_at'];
  269. $laborReport->online_duration=round(Carbon::parse($exit_at)->diffInSeconds(Carbon::parse($enter_at))/3600,2);
  270. }else{
  271. $laborReport->online_duration=$online_duration;
  272. }
  273. $laborReport->update();
  274. $laborReport->is_export=true;
  275. return $laborReport;
  276. }
  277. /**
  278. * 是否成年
  279. * @param 身份证号
  280. * @return int 0 成年,1未成年
  281. */
  282. public function getIsAdultAttribute(){
  283. $flag = 0;
  284. $tyear=intval(substr($this['identity_number'],6,4));
  285. $tmonth=intval(substr($this['identity_number'],10,2));
  286. $tday=intval(substr($this['identity_number'],12,2));
  287. if($tyear>date("Y")||$tyear<(date("Y")-100)){
  288. $flag=0;
  289. }elseif($tmonth<0||$tmonth>12){
  290. $flag=0;
  291. }elseif($tday<0||$tday>31){
  292. $flag=0;
  293. }else{
  294. $day_sum = self::full_year_day($tyear,$tmonth,16);
  295. if((time()-mktime(0,0,0,$tmonth,$tday,$tyear))>$day_sum*24*60*60){
  296. $flag=0;
  297. }else{
  298. $flag=1;
  299. }
  300. }
  301. return $flag;
  302. }
  303. /**
  304. * n周岁的天数
  305. * @param $tyear
  306. * @param $tmonth
  307. * @return int
  308. */
  309. public static function full_year_day($tyear,$tmonth,$type=16){
  310. $sum=365*$type;
  311. for($i=$tyear+1;$i<$tyear+$type;$i++)//考虑中间年份
  312. {
  313. if(self::is_leap_year($i))
  314. $sum++;
  315. }
  316. if(self::is_leap_year($tyear)&&$tmonth<=2)//考虑初末两年
  317. $sum++;
  318. if(self::is_leap_year($tyear+$type)&&$tmonth>=3){
  319. $sum++;
  320. }
  321. return $sum;
  322. }
  323. /**
  324. * @param $year
  325. * @return int 1是闰年,0不是闰年
  326. */
  327. public static function is_leap_year($year){
  328. if(($year%4==0&&$year%100!=0)||$year%400==0)
  329. return 1;
  330. else
  331. return 0;
  332. }
  333. }