[
'' => 0,
'宝时处理' => 1,
'货主处理' => 2,
'承运商处理' => 3,
'宝时终审' => 4,
'完成' => 5,
],
// 'work_order_status' => [
// '' => 0,
// '创建' => 1,
// '信息已填写' => 2,
// '快递已处理' => 3,
// '工单完成' => 4,
// ],
// 'grad' => [
// '' => 0,
// '一般' => 1,
// '重要' => 2,
// '紧急' => 3,
// '重要且紧急' => 4,
// ],
];
function __construct(array $attributes = [])
{
foreach (self::$enums as &$enum) {
$enum = $enum + array_flip($enum);
}
parent::__construct($attributes);
}
public function getStatusAttribute($value)
{
if (!$value) return '';
return self::$enums['status'][$value];
}
public function setStatusAttribute($value)
{
if (!$value) return;
if (is_numeric($value)) {
$this->attributes['status'] = $value;
} else {
$this->attributes['status'] = self::$enums['status'][$value];
}
}
// public function getWorkOrderStatusAttribute($value)
// {
// if (!$value) return '';
// return self::$enums['work_order_status'][$value];
// }
//
// public function setWorkOrderStatusAttribute($value)
// {
// if (!$value) return ;
// if (is_numeric($value)) {
// $this->attributes['work_order_status'] = $value;
// } else {
// $this->attributes['work_order_status'] = self::$enums['work_order_status'][$value];
// }
// }
//
// public function getGradAttribute($value)
// {
// if (!$value) return '';
// return self::$enums['grad'][$value];
// }
//
// public function setGradAttribute($value)
// {
// if (!$value) return ;
// if (is_numeric($value)) {
// $this->attributes['grad'] = $value;
// } else {
// $this->attributes['grad'] = self::$enums['grad'][$value];
// }
// }
// 关联订单
public function order(): BelongsTo
{
return $this->belongsTo(Order::class);
}
// 关联货主
public function owner(): BelongsTo
{
return $this->belongsTo(Owner::class, 'owner_id');
}
// 关联承运商
public function logistic(): BelongsTo
{
return $this->belongsTo(Logistic::class);
}
// 创建人
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'creator_id');
}
// 审核人
public function reviewer(): BelongsTo
{
return $this->belongsTo(User::class, 'reviewer_id');
}
// 工单类型
public function type(): BelongsTo
{
return $this->BelongsTo(WorkOrderType::class, 'work_order_type_id');
}
// 生成问题件类型
public function issueType(): BelongsTo
{
return $this->belongsTo(OrderIssueType::class, 'order_issue_type_id');
}
// 对应问题件
public function orderIssue(): BelongsTo
{
return $this->belongsTo(OrderIssue::class, 'order_id', 'order_id');
}
// 图片
public function images(): HasMany
{
return $this->hasMany(WorkOrderImage::class);
}
public function getPackageImagesAttribute()
{
return $this->images->where('type', 1);
}
public function getCommodityImagesAttribute()
{
return $this->images->where('type', 2);
}
public function getDealImagesAttribute()
{
return $this->images->where('type', 3);
}
public function getRefundImagesAttribute()
{
return $this->images->where('type', 4);
}
// 工单详情
public function details(): HasMany
{
return $this->hasMany(WorkOrderDetail::class);
}
// 工单商品信息
public function commodities(): HasMany
{
return $this->hasMany(WorkOrderCommodities::class);
}
// 处理日志
public function processLogs(): HasMany
{
return $this->hasMany(WorkOrderProcessLog::class);
}
public function baoShiLog()
{
return $this->processLogs->where('type', 1);
}
public function logisticLog()
{
return $this->processLogs->where('type', 2);
}
public function logs(): HasMany
{
return $this->hasMany(WorkOrderLog::class);
}
public function scopeFilter($query, $filters)
{
return $filters->apply($query);
}
/** 默认 with 参数 */
public function scopeDefaultWith($query)
{
$query->with(['type', 'owner', 'logistic', 'issueType', 'creator', 'details', 'commodities.commodity','logs.creator',
'processLogs.creator',
'images.uploadFile',
'reviewer',
'order.packages',
'orderIssue' => function ($query) {
/** @var Builder $query */
$query->with(['issueType', 'logs' => function ($query) {
if (Gate::denies('订单管理-问题件-客户不可见')) {
$query->with('user')->orderByDesc('created_at');
} else {
$query->with('user')->where('tag', '=', 0)->orderByDesc('created_at');
}
}]);
}]);
}
public function loadDefaultWith()
{
$this->loadMissing(['type', 'owner', 'logistic', 'issueType', 'creator', 'details', 'commodities.commodity','logs.creator',
'processLogs.creator',
'images.uploadFile',
'reviewer',
'order.packages',
'orderIssue' => function ($query) {
/** @var Builder $query */
$query->with(['issueType', 'logs' => function ($query) {
if (Gate::denies('订单管理-问题件-客户不可见')) {
$query->with('user')->orderByDesc('created_at');
} else {
$query->with('user')->where('tag', '=', 0)->orderByDesc('created_at');
}
}]);
}]);
}
public function notification()
{
$user = Auth::user();
$this->loadMissing('owner', 'order');
$remark = $this->remark;
$ownerName = $this->owner->name ?? '';
$clientCode = $this->order->client_code ?? '';
$msg = $user["name"] . "建立了新工单
" . $ownerName . ":" . $clientCode . "
" . $remark;
NotificationService::SingleRegister($msg, $clientCode, "订单管理-问题件");
}
public function saveWorkOrderDetail($params)
{
$param = (new WorkOrderDetail($params))->getAttributes();
$this->details()->create($param);
$this->loadMissing('details');
}
// 工单完结
public function end()
{
$this->update(['status' => 4]);
}
public function changeStatus($status)
{
$this->status = $status;
$this->update();
}
}