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

172 lines
3.5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class OrderProduct extends Model
{
/**
* @var array
*/
protected $casts = [
'specs' => 'json',
'is_gift'=>'boolean',
];
/**
* @var array
*/
protected $fillable = [
'gift_for_sku_id',
'user_id',
'order_id',
'spu_id',
'sku_id',
'category_id',
'name',
'specs',
'cover',
'weight',
'sell_price',
'vip_price',
'sales_value',
'quantity',
'coupon_discount_amount',
'vip_discount_amount',
'reduced_amount',
'total_amount',
'after_sale_state',
'after_expire_at',
'remain_quantity',
'is_gift',
'activity_id',
];
public function packageProducts()
{
return $this->hasMany(OrderPackageProduct::class, 'order_product_id');
}
/**
* 此订单商品的售后单
*
*/
public function afterSales()
{
return $this->hasMany(AfterSale::class, 'order_product_id');
}
/**
* 关联的订单包裹
*
*/
public function packages()
{
return $this->belongsToMany(OrderPackage::class, 'order_package_products', 'order_product_id', 'order_package_id');
}
/**
* 此订单商品所属的SPU
*
*/
public function spu()
{
return $this->belongsTo(ProductSpu::class, 'spu_id');
}
/**
* 此订单商品所属的SKU
*
*/
public function sku()
{
return $this->belongsTo(ProductSku::class, 'sku_id');
}
/**
* 此订单商品所属的SKU
*
*/
public function order()
{
return $this->belongsTo(Order::class, 'order_id');
}
/**
* 确认此订单商品是否是赠品
*
* @return bool
*/
public function isGift()
{
return $this->gift_for_sku_id !== null || $this->is_gift == true;
}
/**
* 获取订单商品是否能发起售后
*
* @return bool
*/
public function getCanAfterSaleAttribute(): bool
{
$res = false;
if ($this->order->is_settlable) {
return false;
}
// 老判断,有过期时间,且未到过期时间,未发起过售后
// $oldJudge = !is_null($this->after_expire_at) && $this->after_expire_at > now() && $this->after_sale_state == 0;
//新判断, 有发货单,在售后时间范围内, 当前无售后;
if ($this->packages()->where('is_failed', false)->count() > 0) {
if ((is_null($this->after_expire_at) || $this->after_expire_at > now()) && $this->after_sale_state == 0) {
$res = true;
}
}
return $res;
}
/**
* 订单商品是否有售后
*
* @return boolean
*/
public function getHasAfterSaleAttribute(): bool
{
$res = false;
if ($this->afterSales()->count() > 0) {
$res = true;
}
return $res;
}
/**
* 获取订单商品销售价格
*
* @return string
*/
public function getSellPriceFormatAttribute()
{
return bcdiv($this->attributes['sell_price'], 100, 2);
}
/**
* 获取订单商品会员价格
*
* @return string
*/
public function getVipPriceFormatAttribute()
{
if (is_null($price = $this->attributes['vip_price'])) {
return '';
}
return bcdiv($price, 100, 2);
}
}