LaborApply.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App;
  3. use App\Traits\ModelTimeFormat;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Traits\ModelLogChanging;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. class LaborApply extends Model
  8. {
  9. const CAN_CREATE_STATUS_OPEN = 1;
  10. const CAN_CREATE_STATUS_FORBID = 2;
  11. const CAN_CREATE_STATUS_TEMPORARY_OPEN = 3;
  12. use ModelLogChanging;
  13. use ModelTimeFormat;
  14. public $fillable = [
  15. 'status',//状态
  16. 'warehouse_id',//仓库
  17. 'user_workgroup_id',//仓库小组
  18. 'apply_user_id',//申请人
  19. 'man_num',//男工数量
  20. 'woman_num',//女工数量
  21. 'actual_num',//到岗人数
  22. 'remark',//用工要求
  23. ];
  24. protected $appends = ['arrive_rate'];
  25. static public $enums = [
  26. 'status' => [
  27. '' => 0,
  28. '创建' => 1,
  29. '指派成功' => 2,
  30. '劳务接单' => 3,
  31. '任务完结' => 4,
  32. ],
  33. ];
  34. function __construct(array $attributes = [])
  35. {
  36. foreach (self::$enums as &$enum) {
  37. $enum = $enum + array_flip($enum);
  38. }
  39. parent::__construct($attributes);
  40. }
  41. public function getStatusAttribute($value)
  42. {
  43. if (!$value) return '';
  44. return self::$enums['status'][$value];
  45. }
  46. public function setStatusAttribute($value)
  47. {
  48. if (!$value) return 0;
  49. $this->attributes['status'] = self::$enums['status'][$value];
  50. }
  51. public function warehouse(): BelongsTo
  52. {
  53. return $this->belongsTo(Warehouse::class);
  54. }
  55. public function userWorkGroup(): BelongsTo
  56. {
  57. return $this->belongsTo(UserWorkgroup::class, 'user_workgroup_id', 'id');
  58. }
  59. public function applyUser(): BelongsTo
  60. {
  61. return $this->belongsTo(User::class, 'apply_user_id', 'id');
  62. }
  63. public function getArriveRateAttribute()
  64. {
  65. if (0 === $this->actual_num) {
  66. return '0'.'%';
  67. }
  68. $apply_num = $this->man_num + $this->woman_num;
  69. $arrive_rate = $this->actual_num / $apply_num * 100;
  70. return number_format($arrive_rate, 1) . '%';
  71. }
  72. public function scopeFilter($query, $filters)
  73. {
  74. return $filters->apply($query);
  75. }
  76. }