104 lines
2.7 KiB
PHP
104 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class AfterSale extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'after_sales';
|
|
|
|
protected $fillable = [
|
|
'short_id',
|
|
'order_id',
|
|
'order_short_id',
|
|
'type',
|
|
'status',
|
|
'reason',
|
|
'description',
|
|
'refund_amount',
|
|
'return_express_company',
|
|
'return_express_no',
|
|
'reject_reason',
|
|
'processed_by',
|
|
'processed_at',
|
|
'created_by',
|
|
'completed_at',
|
|
];
|
|
|
|
protected $casts = [
|
|
'refund_amount' => 'decimal:2',
|
|
'processed_at' => 'datetime',
|
|
'completed_at' => 'datetime',
|
|
];
|
|
|
|
// 售后类型
|
|
const TYPE_REFUND_ONLY = 'refund_only'; // 仅退款
|
|
const TYPE_RETURN = 'return'; // 退货
|
|
const TYPE_EXCHANGE = 'exchange'; // 换货
|
|
|
|
// 售后状态
|
|
const STATUS_PENDING = 'pending'; // 待处理
|
|
const STATUS_PROCESSING = 'processing'; // 处理中
|
|
const STATUS_COMPLETED = 'completed'; // 已完成
|
|
const STATUS_REJECTED = 'rejected'; // 已拒绝
|
|
|
|
// 获取类型文本
|
|
public static function getTypeText(string $type): string
|
|
{
|
|
return match($type) {
|
|
self::TYPE_REFUND_ONLY => '仅退款',
|
|
self::TYPE_RETURN => '退货',
|
|
self::TYPE_EXCHANGE => '换货',
|
|
default => $type,
|
|
};
|
|
}
|
|
|
|
// 获取状态文本
|
|
public static function getStatusText(string $status): string
|
|
{
|
|
return match($status) {
|
|
self::STATUS_PENDING => '待处理',
|
|
self::STATUS_PROCESSING => '处理中',
|
|
self::STATUS_COMPLETED => '已完成',
|
|
self::STATUS_REJECTED => '已拒绝',
|
|
default => $status,
|
|
};
|
|
}
|
|
|
|
// 生成售后单号
|
|
public static function generateShortId(): string
|
|
{
|
|
return 'AS' . date('Ymd') . str_pad(random_int(1, 9999), 4, '0', STR_PAD_LEFT);
|
|
}
|
|
|
|
// 关联订单
|
|
public function order(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Order::class, 'order_id');
|
|
}
|
|
|
|
// 关联处理人
|
|
public function processor(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'processed_by');
|
|
}
|
|
|
|
// 关联申请人
|
|
public function creator(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
}
|
|
|
|
// 关联明细
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(AfterSaleItem::class, 'after_sale_id');
|
|
}
|
|
}
|