| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
- namespace App;
- use App\Services\NotificationService;
- use App\Traits\ModelTimeFormat;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Model;
- use App\Traits\ModelLogChanging;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Gate;
- class WorkOrder extends Model
- {
- use ModelTimeFormat;
- use SoftDeletes;
- // 工单 信息
- protected $fillable = [
- 'order_id', // 订单id
- 'owner_id', // 货主
- 'logistic_id', // 承运商
- 'creator_id', // 创建人
- 'reviewer_id', // 审核人
- 'last_handler_id', // 上一个处理人
- 'order_issue_type_id', // 问题件类型
- 'process_progress', // 处理进度 -----------------------------------
- // 拦截: 承运商->[已处理,已签收] 宝时审核->[拦截成功,拦截失败]
- // 信息更改: 承运商->[已处理,无法更改] 宝时审核->[更改成功,更改失败]
- // 快递异常: 承运商->[已处理,已拦截] 宝时审核->[丢件赔偿,签收成功]
- // 错漏发: 宝时处理->[已核实] 商家处理->[补发,不补发]
- // 破损: 承运商->[核实全部破损,核实部分破损,核实未破损] 宝时终审->[核实全部破损,核实部分破损,核实未破损]
- // 快递丢件: 待商家处理 待终审 完结
- // -----------------------------------
- 'type', // 快递异常填写:在途异常,签收未收到
- 'status', // 审核状态
- 'review_at', // 审核时间
- 'uniquely_tag', // 唯一标识
- 'remark', // 工单信息描述
- 'work_order_status', // 1 为创建状态 0 为处理状态
- ];
- static public $process_progress = [
- '已处理','已签收', // 拦截 承运商
- '拦截成功','拦截失败', // 宝时处理
- '已处理','无法更改', // 信息更改 承运商
- '更改成功','更改失败', // 宝时处理
- '已处理','已拦截', // 快递异常 承运商
- '丢件赔偿','签收成功', // 宝时处理
- '已核实', // 错漏发 宝时处理
- '补发','不补发', // 商家处理
- ''
- ];
- static public $enums = [
- 'status' => [
- '' => 0,
- '宝时处理' => 1,
- '货主处理' => 2,
- '承运商处理' => 3,
- '宝时终审' => 4,
- '完成' => 5,
- '待货主完结' => 6,
- ],
- 'last_status' => [
- '' => 0,
- '宝时处理' => 1,
- '货主处理' => 2,
- '承运商处理' => 3,
- '宝时终审' => 4,
- '完成' => 5,
- '待货主完结' => 6,
- ],
- ];
- 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 getLastStatusAttribute($value)
- {
- if (!$value) return '';
- return self::$enums['last_status'][$value];
- }
- public function setLastStatusAttribute($value)
- {
- if (!$value) return;
- if (is_numeric($value)) {
- $this->attributes['last_status'] = $value;
- } else {
- $this->attributes['last_status'] = self::$enums['last_status'][$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 lastHandler(): BelongsTo
- {
- return $this->belongsTo(User::class,'last_handler_id');
- }
- // 审核人
- public function reviewer(): BelongsTo
- {
- return $this->belongsTo(User::class, 'reviewer_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 details(): HasMany
- {
- return $this->hasMany(WorkOrderDetail::class);
- }
- public function scopeFilter($query, $filters)
- {
- return $filters->apply($query);
- }
- /** 默认 with 参数 */
- public function scopeDefaultWith($query)
- {
- $query->with($this->defaultWith());
- }
- public function defaultWith(): array
- {
- return ['owner', 'logistic', 'issueType', 'creator', 'lastHandler','details' => function ($query) {
- return $query->with('commodities.commodity', 'logs.creator', 'images.uploadFile','issueType');
- },
- '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($this->defaultWith());
- }
- 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"] . "建立了新工单<br/>" . $ownerName . ":" . $clientCode . "<br/>" . $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' => 5,'work_order_status' => 0]);
- }
- /**
- * 工单状态
- */
- public function changeStatus($status)
- {
- $this->status = $status;
- $this->update();
- }
- /**
- * 处理进度
- */
- public function changeProcessProgress($process_progress){
- $this->update(['process_progress' => $process_progress]);
- }
- /**
- * 工单状态 and 处理状态 and 处理人
- * @param $status
- * @param $process_progress
- * @param $last_status
- */
- public function change($status,$process_progress,$last_status){
- $user = Auth::user();
- $this->last_status = $last_status;
- $this->status = $status;
- $this->process_progress = $process_progress;
- $this->last_handler_id = $user['id'] ?? '';
- $this->update();
- }
- /**
- * 清除创建标记
- */
- public function clearWorkOrderStatus(){
- $this->update(['work_order_status' => 0]);
- }
- }
|