generated from liutk/owl-admin-base
165 lines
4.4 KiB
PHP
165 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Admin\Filters\EmployeeFilter;
|
|
use App\Enums\EmployeeStatus;
|
|
use App\Traits\HasDateTimeFormatter;
|
|
use EloquentFilter\Filterable;
|
|
use Illuminate\Auth\Authenticatable;
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
use Slowlyo\OwlAdmin\Models\AdminUser;
|
|
use App\Enums\UserRole;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
/**
|
|
* 员工
|
|
*/
|
|
class Employee extends Model implements AuthenticatableContract
|
|
{
|
|
use Authenticatable, Filterable, HasApiTokens, HasDateTimeFormatter;
|
|
|
|
const JOB_KEY = 'job';
|
|
|
|
protected $fillable = ['store_id', 'name', 'avatar', 'phone', 'prize_images', 'skill_images', 'employee_status', 'admin_user_id', 'leave_at', 'join_at', 'remarks'];
|
|
|
|
protected $casts = [
|
|
'employee_status' => EmployeeStatus::class,
|
|
'prize_images' => 'json',
|
|
'skill_images' => 'json',
|
|
'leave_at' => 'datetime',
|
|
'join_at' => 'date:Y-m-d',
|
|
];
|
|
|
|
protected $attributes = [
|
|
'employee_status' => EmployeeStatus::Online,
|
|
];
|
|
|
|
protected $appends = ['employee_status_text', 'employee_status_color'];
|
|
|
|
public function modelFilter()
|
|
{
|
|
return EmployeeFilter::class;
|
|
}
|
|
|
|
public function scopeOnlyOnline(Builder $query)
|
|
{
|
|
$query->where('employee_status', EmployeeStatus::Online);
|
|
}
|
|
|
|
public function avatar(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn($value) => $value ? $value : url(config('admin.default_avatar')),
|
|
);
|
|
}
|
|
|
|
// 拥有的职位
|
|
public function jobs()
|
|
{
|
|
// $related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, $parentKey = null, $relatedKey = null, $relation = null
|
|
return $this->belongsToMany(Keyword::class, 'employee_jobs', 'employee_id', 'job_id', null, 'key');
|
|
}
|
|
|
|
// 关联登录账户
|
|
public function adminUser()
|
|
{
|
|
return $this->belongsTo(AdminUser::class, 'admin_user_id');
|
|
}
|
|
|
|
// 关联的门店(店员)
|
|
public function store()
|
|
{
|
|
return $this->belongsTo(Store::class, 'store_id');
|
|
}
|
|
|
|
// 打卡情况
|
|
public function signs()
|
|
{
|
|
return $this->hasMany(EmployeeSign::class, 'employee_id');
|
|
}
|
|
|
|
/**
|
|
* 店长佣金提成
|
|
*/
|
|
public function storeMasterCommissions()
|
|
{
|
|
return $this->hasMany(StoreMasterCommission::class, 'store_master_id');
|
|
}
|
|
|
|
/**
|
|
* 报销
|
|
*/
|
|
public function reimbursements()
|
|
{
|
|
return $this->hasMany(Reimbursement::class);
|
|
}
|
|
|
|
// 管理的门店(店长)
|
|
// public function masterStore()
|
|
// {
|
|
// return $this->belongsTo(Store::class, 'master_store_id');
|
|
// }
|
|
|
|
public function scopeEnable($q)
|
|
{
|
|
return $q->where('employee_status', EmployeeStatus::Online);
|
|
}
|
|
|
|
/**
|
|
* 确认当前员工是否已离职
|
|
*/
|
|
public function isResigned(): bool
|
|
{
|
|
return $this->employee_status === EmployeeStatus::Offline;
|
|
}
|
|
|
|
/**
|
|
* 确认当前员工是否是管理员
|
|
*/
|
|
public function isAdministrator(): bool
|
|
{
|
|
return $this->adminUser->isAdministrator();
|
|
}
|
|
|
|
/**
|
|
* 确认当前员工是否是店长
|
|
*/
|
|
public function isStoreMaster(): bool
|
|
{
|
|
return $this->store_id && $this->store?->master_id === $this->id;
|
|
}
|
|
|
|
/**
|
|
* 用户身份
|
|
* user: 普通员工, store: 店长, admin: 管理员, store_user: 店员
|
|
* @return array
|
|
*/
|
|
public function userRole(): array
|
|
{
|
|
return collect()
|
|
->when($this->isAdministrator(), fn ($collection) => $collection->push(UserRole::Admin))
|
|
->when($this->isStoreMaster(), fn ($collection) => $collection->push(UserRole::Store))
|
|
->when($this->store_id > 0, fn ($collection) => $collection->push(UserRole::StoreUser))
|
|
->whenEmpty(fn ($collection) => $collection->push(UserRole::User))
|
|
->all();
|
|
}
|
|
|
|
protected function employeeStatusText(): Attribute
|
|
{
|
|
return new Attribute(
|
|
get: fn () => $this->employee_status ? $this->employee_status->text() : '',
|
|
);
|
|
}
|
|
|
|
protected function employeeStatusColor(): Attribute
|
|
{
|
|
return new Attribute(
|
|
get: fn () => $this->employee_status ? $this->employee_status->color() : '',
|
|
);
|
|
}
|
|
}
|