| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <?php
- namespace App;
- use Carbon\Carbon;
- use Illuminate\Database\Eloquent\Model;
- use App\Traits\ModelTimeFormat;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Gate;
- use App\Traits\ModelLogChanging;
- use Tightenco\Collect\Support\Collection;
- class LaborReport extends Model
- {
- use ModelLogChanging;
- use ModelTimeFormat;
- use SoftDeletes;
- protected $fillable=[
- 'id','enter_number','user_workgroup_id','user_id','name','mobile_phone','identity_number','labor_company_id',
- 'check_in_at','verify_at','group_user_id','check_out_at','online_duration','working_duration','created_at','updated_at',
- 'user_duty_check_id','relax_time','remark'
- ];
- protected $appends = [
- 'is_exportGroup','thisRecordOnlineTime','thisRecordWorkingTime','thisRoundRecordWorkingTime',
- 'verifyPerson','userDutyCheckVerifyUserId','isAdult','round_check_in_at','round_check_out_at','has_group_verify_right',
- ];
- protected $tempFields = [
- 'temEnteringRecord',
- 'sequence',
- 'amountOfJoined',
- ];
- public static $withoutAppends = true;
- static function tokenOfBroadcastEnterAndLeave(){
- return md5('tokenOfListAll'.Carbon::now()->format('Y-m-d'));
- }
- public function userWorkgroup(): \Illuminate\Database\Eloquent\Relations\HasOne
- {
- return $this->hasOne('App\UserWorkgroup','id','user_workgroup_id');
- }
- public function laborCompany(): \Illuminate\Database\Eloquent\Relations\HasOne
- {
- return $this->hasOne('App\LaborCompany','id','labor_company_id');
- }
- public function user(): \Illuminate\Database\Eloquent\Relations\HasOne
- {
- return $this->hasOne('App\User','id','group_user_id');
- }
- public function userDutyCheck(): \Illuminate\Database\Eloquent\Relations\HasOne
- {
- return $this->hasOne('App\UserDutyCheck','id','user_duty_check_id');
- }
- public function laborReportStatus(): \Illuminate\Database\Eloquent\Relations\HasMany
- {
- return $this->hasMany('App\LaborReportStatus','labor_report_id','id');
- }
- public function laborReports(): \Illuminate\Database\Eloquent\Relations\HasMany
- {
- return $this->hasMany(get_class($this),'enter_number','enter_number');
- }
- //进组取整时间
- public function getRoundCheckInAtAttribute(){
- if (!$this['check_in_at'])return null;
- $round_check_in_at=Carbon::parse($this['check_in_at'])->format('i');
- 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');
- 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');
- 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');
- }
- //退组取整时间
- public function getRoundCheckOutAtAttribute(){
- if (!$this['check_out_at'])return null;
- $round_check_out_at=Carbon::parse($this['check_out_at'])->format('i');
- 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');
- 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');
- 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');
- }
- public function getHasGroupVerifyRightAttribute(){
- if (!Gate::allows('人事管理-临时工报表')){return null; }
- if (Gate::allows('人事管理-临时工报表-管理全部组')){return true;}
- $user=Auth::user();
- $userWorkgroupIds=$user->getPermittingWorkgroupIds($allowAll=false);
- if (count($userWorkgroupIds)!=0) return in_array($this['user_workgroup_id'],$userWorkgroupIds);
- }
- public function getEnteringRecordAttribute()
- {
- if(self::$withoutAppends) {
- $laborReport = LaborReport::with(['laborReportStatus' => function ($query) {
- return $query->whereIn('status', ['已入场', '未审核']);
- }])->where('user_id', $this['user_id'])->orderBy('id', 'desc')->first();
- if (empty($laborReport)) return null;
- return $laborReport;
- }
- }
- public function getIsExportGroupAttribute(){
- return $this['check_out_at']? true:false;
- }
- public function getVerifyPersonAttribute(){
- if ($this['group_user_id']&&$this['group_user_id']==Auth::user()['id'])
- return Auth::user()['name'];
- }
- public function getUserDutyCheckVerifyUserIdAttribute(){
- return $this['userDutyCheck']['verify_user_id']?$this['userDutyCheck']['verify_user_id']:'';
- }
- //总工作时长
- public function getTotalWorkingTimeAttribute(){
- if(self::$withoutAppends) {
- $totalWorkingTime = $this->laborReports->reduce(function ($value, $laborReport) {
- return ($value ?? 0) + $laborReport['thisRecordWorkingTime'];
- });
- return $totalWorkingTime;
- }
- }
- //本轮工作起始时间
- public function getThisRoundOnlineStartTimeAttribute(){
- if(self::$withoutAppends) {
- foreach ($this->laborReports as $laborReport) {
- if ($laborReport['enter_at'] && $laborReport['check_in_at'])
- return $laborReport['check_in_at'];
- }
- }
- }
- //本轮工作结束时间
- public function getThisRoundOnlineEndTimeAttribute(){
- if(self::$withoutAppends) {
- foreach ($this->laborReports as $laborReport) {
- if ($laborReport['exit_at'] && $laborReport['check_out_at'])
- return $laborReport['check_out_at'];
- }
- }
- }
- //本次在线时长
- public function getThisRecordOnlineTimeAttribute(){
- if ($this['enter_at'] && !$this['check_out_at'] && !$this['exit_at'])
- return round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['enter_at'])) / 3600, 2);
- if ($this['enter_at'] && $this['check_out_at'] && !$this['exit_at'])
- return round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['enter_at'])) / 3600, 2);
- if ($this['enter_at'] && $this['exit_at'])
- return round(Carbon::parse($this['exit_at'])->diffInSeconds(Carbon::parse($this['enter_at'])) / 3600, 2);
- if ($this['check_in_at'] && !$this['enter_at'] && !$this['check_out_at'] && !$this['exit_at'])
- return round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['check_in_at'])) / 3600, 2);
- if ($this['check_in_at'] && !$this['enter_at'] && $this['check_out_at'] && !$this['exit_at'])
- return round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['check_in_at'])) / 3600, 2);
- if ($this['check_in_at'] && !$this['enter_at'] && $this['exit_at'])
- return round(Carbon::parse($this['exit_at'])->diffInSeconds(Carbon::parse($this['check_in_at'])) / 3600, 2);
- }
- //本次工作时长
- public function getThisRecordWorkingTimeAttribute(){
- if ($this['check_in_at']&&!$this['check_out_at']&&!$this['relax_time']) {
- $workingTime = round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['check_in_at'])) / 3600, 2);
- $checkInTime = Carbon::parse($this['check_in_at']);
- $checkOutTime = Carbon::parse(Carbon::now());
- return $this->minusLunchTime($checkInTime, $checkOutTime, $workingTime);
- }
- if ($this['check_in_at']&&$this['check_out_at']&&!$this['relax_time']){
- $workingTime=round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['check_in_at']))/3600,2);
- $checkInTime=Carbon::parse($this['check_in_at']);
- $checkOutTime=Carbon::parse($this['check_out_at']);
- return $this->minusLunchTime($checkInTime,$checkOutTime,$workingTime);
- }
- if ($this['check_in_at']&&$this['check_out_at']&&$this['relax_time']){
- $workingTime=round(Carbon::parse($this['check_out_at'])->diffInSeconds(Carbon::parse($this['check_in_at']))/3600,2);
- $checkInTime=Carbon::parse($this['check_in_at']);
- $checkOutTime=Carbon::parse($this['check_out_at']);
- return $this->minusLunchTime($checkInTime,$checkOutTime,$workingTime)-$this['relax_time']/60;
- }
- }
- //本次取整工作时长
- public function getThisRoundRecordWorkingTimeAttribute(){
- if ($this['round_check_in_at'] && !$this['round_check_out_at'] && !$this['relax_time']) {
- $workingTime = round(Carbon::parse(Carbon::now())->diffInSeconds(Carbon::parse($this['round_check_in_at'])) / 3600, 2);
- $checkInTime = Carbon::parse($this['round_check_in_at']);
- $checkOutTime = Carbon::parse(Carbon::now());
- return $this->minusLunchTime($checkInTime, $checkOutTime, $workingTime);
- }
- if ($this['round_check_in_at'] && $this['round_check_out_at'] && !$this['relax_time']) {
- $workingTime = round(Carbon::parse($this['round_check_out_at'])->diffInSeconds(Carbon::parse($this['round_check_in_at'])) / 3600, 2);
- $checkInTime = Carbon::parse($this['round_check_in_at']);
- $checkOutTime = Carbon::parse($this['round_check_out_at']);
- return $this->minusLunchTime($checkInTime, $checkOutTime, $workingTime);
- }
- if ($this['round_check_in_at'] && $this['round_check_out_at'] && $this['relax_time']) {
- $workingTime = round(Carbon::parse($this['round_check_out_at'])->diffInSeconds(Carbon::parse($this['round_check_in_at'])) / 3600, 2);
- $checkInTime = Carbon::parse($this['round_check_in_at']);
- $checkOutTime = Carbon::parse($this['round_check_out_at']);
- return $this->minusLunchTime($checkInTime, $checkOutTime, $workingTime) - $this['relax_time'] / 60;
- }
- }
- //工作时长默认休息时间
- public function minusLunchTime($checkInTime,$checkOutTime,$hour){
- //白班:第一餐 默认减去1小时
- if ($checkInTime->lessThanOrEqualTo($checkInTime->clone()->setHours(11)->setMinutes(30))//进组时间小于等于进组当天11:30
- &&Carbon::parse($checkOutTime)->gte(Carbon::parse($checkInTime->clone()->setHour(13)->setMinute(00)->setSecond(00)))){//退组时间大于进组当天的13:00
- $hour=$hour-1;
- }
- //夜班:第一餐 默认减去半小时
- if ($checkInTime->lessThanOrEqualTo($checkInTime->clone()->setHours(23)->setMinutes(59))//进组时间小于等于当天23:59
- &&Carbon::parse($checkOutTime)->gte(Carbon::parse($checkInTime->clone()->subDays(-1)->setHour(1)))){//退组时间大于等于进组后一天的1:00
- $hour=$hour-0.5;
- }
- return $hour;
- }
- private function checkExitStatus()
- {
- $status = $this->laborReportStatus->sortByDesc('id')->first();
- $this->is_export = ($status['status'] == '已退场') ? true :false;
- if ($status['status'] == '已退场')$this->exit_at=Carbon::parse($status['created_at'])->format('Y-m-d H:i:s');
- }
- private function checkEnterStatus(){
- $status = $this->laborReportStatus->sortBy('id')->first();
- if ($status['status'] == '未审核')$this->enter_at=Carbon::parse($status['created_at'])->format('Y-m-d H:i:s');
- }
- private function checkAmountOfJoined(){
- if ($this->amountOfJoined!=count($this->laborReports))$this->amountOfJoined=count($this->laborReports);
- }
- //转组序号
- private function checkSequence(){
- if (!$this->sequence) foreach ($this->laborReports as $i => $laborReport) {
- if ($laborReport['id'] == $this['id']) {
- $this->sequence = $i + 1;
- }
- }
- }
- //总在线时长
- private function checkTotalOnlineTime(){
- if (!$this->totalOnlineTime) $this->totalOnlineTime = $this->laborReports->reduce(function ($value, $laborReport) {
- return ($value ?? 0) + $laborReport['thisRecordOnlineTime'];
- });
- }
- public static function processing($laborReports)
- {
- foreach ($laborReports as $laborReport){
- /** @var LaborReport $laborReport */
- $laborReport->checkEnterStatus(); //校验进场并赋值
- $laborReport->checkAmountOfJoined();
- $laborReport->checkSequence();
- $laborReport->checkTotalOnlineTime();
- $laborReport->checkExitStatus(); //校验出场并赋值
- }
- }
- //创建或获取进场编号
- public function makeOrGetEnteringNumber(): string
- {
- if($this['enteringRecord']&&$this['enteringRecord']['enter_number'])
- return $this['enteringRecord']['enter_number'];
- $enteringNumber=date("ymd").str_pad($this['id']>99999?$this['id']%99999:$this['id'],4,"0",STR_PAD_LEFT);
- $this['enter_number']=$enteringNumber;
- return $enteringNumber;
- }
- //出场更新临时工报表信息
- static function exitAndChangeLaborReport($laborReport,$userDutyCheck){
- $laborReportStatus=new LaborReportStatus([
- 'labor_report_id'=>$laborReport['id'],
- 'status'=>'已退场',
- ]);
- $laborReportStatus->save();
- $check_in_at=$laborReport->check_in_at??null;
- $exit_at=$userDutyCheck->checked_at;
- $online_duration=round(Carbon::parse($exit_at)->diffInSeconds(Carbon::parse($check_in_at))/3600,2);
- $laborReport->user_duty_check_id=$userDutyCheck->id;
- if ($laborReport->enter_at){
- $enter_at=$laborReport['enter_at'];
- $laborReport->online_duration=round(Carbon::parse($exit_at)->diffInSeconds(Carbon::parse($enter_at))/3600,2);
- }else{
- $laborReport->online_duration=$online_duration;
- }
- $laborReport->update();
- $laborReport->is_export=true;
- return $laborReport;
- }
- /**
- * 是否成年
- * @param 身份证号
- * @return int 0 成年,1未成年
- */
- public function getIsAdultAttribute(){
- $flag = 0;
- $tyear=intval(substr($this['identity_number'],6,4));
- $tmonth=intval(substr($this['identity_number'],10,2));
- $tday=intval(substr($this['identity_number'],12,2));
- if($tyear>date("Y")||$tyear<(date("Y")-100)){
- $flag=0;
- }elseif($tmonth<0||$tmonth>12){
- $flag=0;
- }elseif($tday<0||$tday>31){
- $flag=0;
- }else{
- $day_sum = self::full_year_day($tyear,$tmonth,16);
- if((time()-mktime(0,0,0,$tmonth,$tday,$tyear))>$day_sum*24*60*60){
- $flag=0;
- }else{
- $flag=1;
- }
- }
- return $flag;
- }
- /**
- * n周岁的天数
- * @param $tyear
- * @param $tmonth
- * @return int
- */
- public static function full_year_day($tyear,$tmonth,$type=16){
- $sum=365*$type;
- for($i=$tyear+1;$i<$tyear+$type;$i++)//考虑中间年份
- {
- if(self::is_leap_year($i))
- $sum++;
- }
- if(self::is_leap_year($tyear)&&$tmonth<=2)//考虑初末两年
- $sum++;
- if(self::is_leap_year($tyear+$type)&&$tmonth>=3){
- $sum++;
- }
- return $sum;
- }
- /**
- * @param $year
- * @return int 1是闰年,0不是闰年
- */
- public static function is_leap_year($year){
- if(($year%4==0&&$year%100!=0)||$year%400==0)
- return 1;
- else
- return 0;
- }
- }
|