JsonArray::class, ]; protected $attributes = [ 'points' => 0, ]; protected $fillable = [ 'user_id', 'order_id', 'order_product_id', 'num', 'amount', 'type', 'state', 'description', 'images', 'remarks', 'tracking_number', 'sales_value', 'points', ]; public static $stateText = [ self::STATE_APPLY=>'待补充资料', self::STATE_VERIFY=>'待处理', self::STATE_AGREE=>'待确认', self::STATE_SHIPPING=>'待验收', self::STATE_FINANCE=>'待审核', self::STATE_FINISH=>'已完成', self::STATE_CANCEL=>'已取消', ]; /** * 获取售后订单状态进度 * * @return int */ public function getAfterSaleStateAttribute(): int { $afterSaleState = 0; switch ($this->state) { case 5: $afterSaleState = 4; break; case 6: $afterSaleState = 5; break; default: $afterSaleState = $this->state; break; } return $afterSaleState; } /** * 获取格式化售后金额 * * @return void */ public function getAmountFormatAttribute() { return trim_trailing_zeros(bcdiv($this->attributes['amount'], 100, 2)); } /** * 仅查询处理中的售后订单 * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeProcessing($query) { return $query->whereIn('state', [ static::STATE_APPLY, static::STATE_VERIFY, static::STATE_AGREE, static::STATE_SHIPPING, static::STATE_FINANCE, ]); } public function user() { return $this->belongsTo(User::class, 'user_id'); } public function order() { return $this->belongsTo(Order::class, 'order_id'); } public function orderProduct() { return $this->belongsTo(OrderProduct::class, 'order_product_id'); } public function logs() { return $this->hasMany(AfterSaleLog::class, 'after_sale_id'); } public function tags() { return $this->belongsToMany(Tag::class, 'taggables', 'taggable_id', 'tag_id')->wherePivot('taggable_type', self::class)->withTimestamps(); } /** * 确认此售后单是否是换货 * * @return bool */ public function isChange(): bool { return $this->type === static::TYPE_CHANGE; } /** * 确认此售后单是否已取消 * * @return bool */ public function isCancelled(): bool { return $this->state === static::STATE_CANCEL; } /** * 确认此售后单是否已完成 * * @return bool */ public function isFinished(): bool { return $this->state === static::STATE_FINISH; } /** * 设置申请售后时的录入操作日志 * * @param array $attributes * @param self|null $inviter * @return self */ public static function create(array $attributes = [], ?self $inviter = null): self { $afterSale = static::query()->create($attributes); $afterSale->createApplyLog(); return $afterSale; } /** * 申请日志 * * @return void */ protected function createApplyLog() { $this->logs()->create([ 'after_sale_id' => $this->id, 'name' => '售后申请', 'desc' => '申请理由:'.$this->description, 'images'=> $this->images, ]); } /** * 完成售后订单日志 * * @return void */ protected function createFinishLog() { $this->logs()->create([ 'after_sale_id' => $this->id, 'name' => '售后完成', 'desc' => '售后已结束', ]); } /** * {@inheritdoc} */ protected static function booted() { parent::saving(function ($afterSale) { // 如果自动创建sn if ($afterSale->sn === null) { $afterSale->sn = self::createSn(); } }); parent::saved(function ($afterSale) { //如果取消订单 if ($afterSale->state == self::STATE_CANCEL) { $afterSale->logs()->create([ 'after_sale_id' => $afterSale->id, 'name' => '取消申请', 'desc' => $afterSale->remarks, ]); $afterSale->orderProduct->update([ 'after_sale_state'=>0, ]); } //如果完成订单 if ($afterSale->state == self::STATE_FINISH) { $afterSale->createFinishLog(); $afterSale->orderProduct->update([ 'after_sale_state'=>0, ]); } }); } /** * 创建售后单号 * * @return void */ protected static function createSn() { $rand = strtoupper(Str::random(6)); return 'AS'.date('ymdHis').$rand; } }