0, 'status' => self::STATUS_PENDING, ]; /** * @var array */ protected $casts = [ 'completed_at' => 'datetime', 'status' => 'int', ]; /** * @var array */ protected $fillable = [ 'user_id', 'sn', 'user_coupon_id', 'coupon_discount_amount', 'vip_discount_amount', 'reduced_amount', 'shipping_fee', 'products_total_amount', 'total_amount', 'note', 'remark', 'pay_sn', 'pay_way', 'pay_at', 'consignee_name', 'consignee_telephone', 'consignee_zone', 'consignee_address', 'shipping_state', 'status', 'completed_at', ]; /** * 下单人 * * @return void */ public function user() { return $this->belongsTo(User::class, 'user_id'); } /** * 使用的优惠券 * * @return void */ public function userCoupon() { return $this->hasOne(UserCoupon::class, 'id', 'user_coupon_id'); } /** * 仅查询支付过期的订单 */ public function scopeExpired() { return $this->where('status', static::STATUS_PENDING) ->where('created_at', '<=', now()->subSeconds(config('settings.order_payment_expires_at'))); } /** * 属于此订单的商品 */ public function products() { return $this->hasMany(OrderProduct::class); } /** * 此订单是否待付款 * * @return boolean */ public function isPending(): bool { return $this->status === static::STATUS_PENDING; } /** * 确认此订单是否是待发货 * * @return bool */ public function isPaid(): bool { return $this->status === static::STATUS_PAID && $this->shipping_state === static::SHIPPING_STATE_PENDING; } /** * 确认此订单是否是发货中 * * @return bool */ public function isShipping(): bool { return $this->status === static::STATUS_PAID && $this->shipping_state === static::SHIPPING_STATE_PROCESSING; } /** * 确认此订单是否是已发货 * * @return bool */ public function isShipped(): bool { return $this->status === static::STATUS_PAID && $this->shipping_state === static::SHIPPING_STATE_PROCESSED; } /** * 确认此订单是否是已完成 * * @return bool */ public function isCompleted(): bool { return $this->status === static::STATUS_COMPLETED; } /** * 确认此订单是否是已取消 * * @return bool */ public function isCancelled(): bool { return $this->status === static::STATUS_CANCELLED; } /** * 获取订单券优惠金额 * * @return string */ public function getCouponDiscountAmountFormatAttribute() { return Numeric::trimTrailingZero(bcdiv($this->attributes['coupon_discount_amount'], 100, 2)); } /** * 获取订单会员折扣金额 * * @return string */ public function getVipDiscountAmountFormatAttribute() { return Numeric::trimTrailingZero(bcdiv($this->attributes['vip_discount_amount'], 100, 2)); } /** * 获取订单减免金额 * * @return string */ public function getReducedAmountFormatAttribute() { return Numeric::trimTrailingZero(bcdiv($this->attributes['reduced_amount'], 100, 2)); } /** * 获取订单邮费 * * @return string */ public function getShippingFeeFormatAttribute() { return Numeric::trimTrailingZero(bcdiv($this->attributes['shipping_fee'], 100, 2)); } /** * 获取订单支付金额 * * @return string */ public function getTotalAmountFormatAttribute() { return Numeric::trimTrailingZero(bcdiv($this->attributes['total_amount'], 100, 2)); } /** * 获取订单商品总额 * * @return string */ public function getProductsTotalAmountFormatAttribute() { return Numeric::trimTrailingZero(bcdiv($this->attributes['products_total_amount'], 100, 2)); } /** * 待支付订单过期时间 * * @return int */ public function getExpiresAtAttribute() { if (! $this->isPending()) { return 0; } $seconds = now()->diffInSeconds( $this->created_at->addSeconds(config('settings.order_payment_expires_at')), false ); return $seconds > 0 ? $seconds : 0; } /** * 获取订单状态 * * @return int */ public function getOrderStatusAttribute(): int { // 待付款 if ($this->isPending()) { return OrderStatus::PENDING; } // 已付款(待发货) if ($this->isPaid()) { return OrderStatus::PAID; } // 发货中 if ($this->isShipping()) { return OrderStatus::SHIPPING; } // 已发货 if ($this->isShipped()) { return OrderStatus::SHIPPED; } // 已完成 if ($this->isCompleted()) { return OrderStatus::COMPLETED; } // 已取消 if ($this->isCancelled()) { return OrderStatus::CANCELLED; } // 其它 return OrderStatus::UNKNOWN; } }