6
0
Fork 0
jiqu-library-server/app/Models/AfterSale.php

241 lines
5.8 KiB
PHP

<?php
namespace App\Models;
use App\Casts\JsonArray;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class AfterSale extends Model
{
use HasFactory;
use HasDateTimeFormatter;
public const STATE_APPLY = 1; // 待补充资料
public const STATE_VERIFY = 2; // 待审核
public const STATE_AGREE = 3; // 待同意
public const STATE_SHIPPING = 4; // 待收货
public const STATE_FINANCE = 5; // 待打款
public const STATE_FINISH = 6; // 完成
public const STATE_CANCEL = 7; // 取消
public const TYPE_REFUND_AND_RETURN = 1; // 退款退货
public const TYPE_REFUND = 2; // 退款不退货
public const TYPE_CHANGE = 3; // 换货
public const TYPE_FILL = 4; // 漏发
protected $casts = [
'images' => JsonArray::class,
];
protected $fillable = [
'user_id',
'order_id',
'order_product_id',
'num',
'amount',
'type',
'state',
'description',
'images',
'remarks',
'tracking_number',
];
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;
}
/**
* 仅查询处理中的售后订单
*
* @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->status === static::STATE_CANCEL;
}
/**
* 设置申请售后时的录入操作日志
*
* @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 createCancelLog()
{
$this->logs()->create([
'after_sale_id' => $this->id,
'name' => '取消申请',
'desc' => '用户取消售后申请',
]);
}
/**
* 完成售后订单日志
*
* @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->createCancelLog();
$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;
}
}