123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- <?php
- namespace App\Models;
- use Auth;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- /**
- * 订单.
- *
- * @property int $id
- * @property string $sn 订单编号
- * @property int $user_id 购买者ID
- * @property int|null $goods_id 商品ID
- * @property int|null $coupon_id 优惠券ID
- * @property int $origin_amount 订单原始总价,单位分
- * @property int $amount 订单总价,单位分
- * @property \Illuminate\Support\Carbon|null $expired_at 过期时间
- * @property int $is_expire 是否已过期:0-未过期、1-已过期
- * @property int $pay_type 支付渠道:0-余额、1-支付宝、2-QQ、3-微信、4-虚拟货币、5-paypal
- * @property string $pay_way 支付方式:balance、f2fpay、codepay、payjs、bitpayx等
- * @property int $status 订单状态:-1-已关闭、0-待支付、1-已支付待确认、2-已完成
- * @property \Illuminate\Support\Carbon $created_at 创建时间
- * @property \Illuminate\Support\Carbon $updated_at 最后更新时间
- * @property-read \App\Models\Coupon|null $coupon
- * @property-read string $pay_type_icon
- * @property-read string $pay_type_label
- * @property-read string $pay_way_label
- * @property-read string $status_label
- * @property-read \App\Models\Goods|null $goods
- * @property-read \App\Models\Payment|null $payment
- * @property-read \App\Models\User $user
- * @method static \Illuminate\Database\Eloquent\Builder|Order active()
- * @method static \Illuminate\Database\Eloquent\Builder|Order activePackage()
- * @method static \Illuminate\Database\Eloquent\Builder|Order activePlan()
- * @method static \Illuminate\Database\Eloquent\Builder|Order newModelQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|Order newQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|Order query()
- * @method static \Illuminate\Database\Eloquent\Builder|Order recentUnPay()
- * @method static \Illuminate\Database\Eloquent\Builder|Order uid($uid = null)
- * @method static \Illuminate\Database\Eloquent\Builder|Order userActivePackage($uid = null)
- * @method static \Illuminate\Database\Eloquent\Builder|Order userActivePlan($uid = null)
- * @method static \Illuminate\Database\Eloquent\Builder|Order userPrepay($uid = null)
- * @method static \Illuminate\Database\Eloquent\Builder|Order whereAmount($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Order whereCouponId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Order whereCreatedAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Order whereExpiredAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Order whereGoodsId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Order whereId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Order whereIsExpire($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Order whereOriginAmount($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Order wherePayType($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Order wherePayWay($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Order whereSn($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Order whereStatus($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Order whereUpdatedAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Order whereUserId($value)
- * @mixin \Eloquent
- */
- class Order extends Model
- {
- protected $table = 'order';
- protected $dates = ['expired_at'];
- protected $guarded = [];
- public function user(): BelongsTo
- {
- return $this->belongsTo(User::class);
- }
- public function goods(): BelongsTo
- {
- return $this->belongsTo(Goods::class)->withTrashed();
- }
- public function coupon(): BelongsTo
- {
- return $this->belongsTo(Coupon::class)->withTrashed();
- }
- public function payment(): HasOne
- {
- return $this->hasOne(Payment::class);
- }
- public function scopeUid($query, $uid = null)
- {
- return $query->whereUserId($uid ?: Auth::id());
- }
- public function scopeRecentUnPay($query)
- {
- return $query->whereStatus(0)->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-15 minutes')));
- }
- public function scopeUserPrepay($query, $uid = null)
- {
- return $query->uid($uid)->whereStatus(3);
- }
- public function scopeActive($query)
- {
- return $query->whereIsExpire(0)->whereStatus(2);
- }
- public function scopeActivePlan($query)
- {
- return $query->active()->with('goods')->whereHas('goods', static function ($query) {
- $query->whereType(2);
- });
- }
- public function scopeActivePackage($query)
- {
- return $query->active()->with('goods')->whereHas('goods', static function ($query) {
- $query->whereType(1);
- });
- }
- public function scopeUserActivePlan($query, $uid = null)
- {
- return $query->uid($uid)->activePlan();
- }
- public function scopeUserActivePackage($query, $uid = null)
- {
- return $query->uid($uid)->activePackage();
- }
- public function close() // 关闭订单
- {
- return $this->update(['status' => -1]);
- }
- public function paid() // 完成订单
- {
- return $this->update(['status' => 1]);
- }
- public function complete() // 完成订单
- {
- return $this->update(['status' => 2]);
- }
- public function prepay() // 预支付订单
- {
- return $this->update(['status' => 3]);
- }
- // 订单状态
- public function getStatusLabelAttribute(): string
- {
- switch ($this->attributes['status']) {
- case -1:
- $status_label = '<span class="badge badge-default">'.trans('user.status.closed').'</span>';
- break;
- case 0:
- $status_label = '<span class="badge badge-danger">'.trans('user.status.waiting_payment').'</span>';
- break;
- case 1:
- $status_label = '<span class="badge badge-info">'.trans('user.status.waiting_confirm').'</span>';
- break;
- case 2:
- if ($this->attributes['goods_id'] === 0) {
- $status_label = '<span class="badge badge-default">'.trans('user.status.completed').'</span>';
- } elseif ($this->attributes['is_expire']) {
- $status_label = '<span class="badge badge-default">'.trans('user.status.expired').'</span>';
- } else {
- $status_label = '<span class="badge badge-success">'.trans('user.status.using').'</span>';
- }
- break;
- case 3:
- $status_label = '<span class="badge badge-info">'.trans('user.status.prepaid').'</span>';
- break;
- default:
- $status_label = trans('user.unknown');
- }
- return $status_label;
- }
- public function getOriginAmountAttribute($value)
- {
- return $value / 100;
- }
- public function setOriginAmountAttribute($value)
- {
- return $this->attributes['origin_amount'] = $value * 100;
- }
- public function getAmountAttribute($value)
- {
- return $value / 100;
- }
- public function setAmountAttribute($value)
- {
- return $this->attributes['amount'] = $value * 100;
- }
- // 支付渠道
- public function getPayTypeLabelAttribute(): string
- {
- switch ($this->attributes['pay_type']) {
- case 0:
- $pay_type_label = trans('common.payment.credit');
- break;
- case 1:
- $pay_type_label = trans('common.payment.alipay');
- break;
- case 2:
- $pay_type_label = '支付宝支付';
- break;
- case 3:
- $pay_type_label = trans('common.payment.wechat');
- break;
- case 4:
- $pay_type_label = trans('common.payment.crypto');
- break;
- case 5:
- $pay_type_label = 'PayPal';
- break;
- case 6:
- $pay_type_label = 'Stripe';
- break;
- case 7:
- trans('common.payment.manual');
- break;
- default:
- $pay_type_label = '';
- }
- return $pay_type_label;
- }
- // 支付图标
- public function getPayTypeIconAttribute(): string
- {
- // return '/assets/images/payment/'.config('common.payment.icon')[$this->attributes['pay_type']] ?? 'coin.png';
- $base_path = '/assets/images/payment/';
- switch ($this->attributes['pay_type']) {
- case 1:
- $pay_type_icon = $base_path.'alipay.png';
- break;
- case 2:
- $pay_type_icon = $base_path.'alipay-other.png';
- break;
- case 3:
- $pay_type_icon = $base_path.'wechat.png';
- break;
- case 5:
- $pay_type_icon = $base_path.'paypal.png';
- break;
- case 6:
- $pay_type_icon = $base_path.'stripe.png';
- break;
- default:
- $pay_type_icon = $base_path.'coin.png';
- }
- return $pay_type_icon;
- }
- // 支付方式
- public function getPayWayLabelAttribute(): string
- {
- //return config('common.payment.labels')[$this->attributes['pay_way']] ?? '未知';
- switch ($this->attributes['pay_way']) {
- case 'credit':
- $pay_way_label = '余额';
- break;
- case 'youzan':
- $pay_way_label = '有赞云';
- break;
- case 'f2fpay':
- $pay_way_label = '支付宝当面付';
- break;
- case 'codepay':
- $pay_way_label = '码支付';
- break;
- case 'payjs':
- $pay_way_label = 'PayJs';
- break;
- case 'bitpayx':
- $pay_way_label = '麻瓜宝';
- break;
- case 'paypal':
- $pay_way_label = 'PayPal';
- break;
- case 'stripe':
- $pay_way_label = 'Stripe';
- break;
- case 'paybeaver':
- $pay_way_label = '海狸支付';
- break;
- case 'zpay':
- $pay_way_label = 'zpay';
- break;
- case 'epay':
- $pay_way_label = '易支付';
- break;
- case 'theadpay':
- $pay_way_label = '平头哥';
- break;
- case 'epaytt':
- $pay_way_label = 'epaytt';
- break;
- case 'bypay':
- $pay_way_label = 'bypay';
- break;
- default:
- $pay_way_label = '未知';
- }
- return $pay_way_label;
- }
- }
|