LaborReport.php 16 KB

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