122 lines
2.6 KiB
PHP
122 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Mall\Models;
|
|
|
|
use App\Models\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
use Modules\Mall\Models\Traits\BelongsToOrder;
|
|
|
|
class OrderItem extends Model
|
|
{
|
|
|
|
use BelongsToOrder;
|
|
|
|
protected $table = 'mall_order_items';
|
|
|
|
const UPDATED_AT = null;
|
|
|
|
public $casts = [
|
|
'source' => 'json',
|
|
];
|
|
|
|
/**
|
|
* Notes: 关联商品
|
|
* @Author: 玄尘
|
|
* @Date : 2021/8/11 9:26
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
|
|
*/
|
|
public function item(): morphTo
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
/**
|
|
* Notes: 关联退款单
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/19 9:30
|
|
*/
|
|
public function refund_items(): HasMany
|
|
{
|
|
return $this->hasMany(RefundItem::class, 'order_item_id');
|
|
}
|
|
|
|
/**
|
|
* Notes: 商品状态
|
|
* @Author: 玄尘
|
|
* @Date : 2021/6/17 9:07
|
|
*/
|
|
public function getRefundStatusTextAttribute()
|
|
{
|
|
if ($this->isRefund()) {
|
|
$refund_item = $this->refund_items()->latest()->first();
|
|
if ($refund_item) {
|
|
return $refund_item->refund->status_test;
|
|
} else {
|
|
return '未知状态';
|
|
}
|
|
} else {
|
|
return '正常';
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Notes: 是否申请退款
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/21 11:15
|
|
*/
|
|
public function isRefund(): int
|
|
{
|
|
return $this->refund_items()->count() > 0;
|
|
}
|
|
|
|
/**
|
|
* Notes: 是否可以退款/货
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/19 9:32
|
|
*/
|
|
public function canRefund(): bool
|
|
{
|
|
$can = true;
|
|
|
|
//是否已申请退款/货
|
|
if ($this->isRefund()) {
|
|
//最后一个申请
|
|
$refundItem = $this->refund_items()->latest()->first();
|
|
if ($refundItem && $refundItem->refund->state != Refund::REFUND_REFUSE) {
|
|
$can = false;
|
|
}
|
|
}
|
|
|
|
$is_post_sale = $this->source['is_post_sale'] ?? 0;
|
|
|
|
return $is_post_sale && $can && $this->order->can('refund');
|
|
}
|
|
|
|
/**
|
|
* Notes: 获取总额
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/20 13:13
|
|
* @return float
|
|
*/
|
|
public function getTotalAttribute(): float
|
|
{
|
|
return sprintf("%.2f", $this->price * $this->qty);
|
|
}
|
|
|
|
/**
|
|
* Notes: 获取水晶总数
|
|
* @Author: 玄尘
|
|
* @Date : 2021/5/21 11:26
|
|
*/
|
|
public function getTotalScoreAttribute(): string
|
|
{
|
|
$score = $this->source['score'] ?? 0;
|
|
|
|
return sprintf("%.2f", $score * $this->qty);
|
|
|
|
}
|
|
|
|
}
|