Order.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Models;
  3. use Auth;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. use Illuminate\Database\Eloquent\Relations\HasOne;
  7. /**
  8. * 订单.
  9. */
  10. class Order extends Model
  11. {
  12. protected $table = 'order';
  13. protected $dates = ['expired_at'];
  14. protected $guarded = [];
  15. public function user(): BelongsTo
  16. {
  17. return $this->belongsTo(User::class);
  18. }
  19. public function goods(): BelongsTo
  20. {
  21. return $this->belongsTo(Goods::class)->withTrashed();
  22. }
  23. public function coupon(): BelongsTo
  24. {
  25. return $this->belongsTo(Coupon::class)->withTrashed();
  26. }
  27. public function payment(): HasOne
  28. {
  29. return $this->hasOne(Payment::class);
  30. }
  31. public function scopeUid($query, $uid = null)
  32. {
  33. return $query->whereUserId($uid ?: Auth::id());
  34. }
  35. public function scopeRecentUnPay($query)
  36. {
  37. return $query->whereStatus(0)->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-'.config('tasks.close.order').' minutes')));
  38. }
  39. public function scopeUserPrepay($query, $uid = null)
  40. {
  41. return $query->uid($uid)->whereStatus(3);
  42. }
  43. public function scopeActive($query)
  44. {
  45. return $query->whereIsExpire(0)->whereStatus(2);
  46. }
  47. public function scopeActivePlan($query)
  48. {
  49. return $query->active()->with('goods')->whereHas('goods', static function ($query) {
  50. $query->whereType(2);
  51. });
  52. }
  53. public function scopeActivePackage($query)
  54. {
  55. return $query->active()->with('goods')->whereHas('goods', static function ($query) {
  56. $query->whereType(1);
  57. });
  58. }
  59. public function scopeUserActivePlan($query, $uid = null)
  60. {
  61. return $query->uid($uid)->activePlan();
  62. }
  63. public function scopeUserActivePackage($query, $uid = null)
  64. {
  65. return $query->uid($uid)->activePackage();
  66. }
  67. public function close() // 关闭订单
  68. {
  69. return $this->update(['status' => -1]);
  70. }
  71. public function paid() // 完成订单
  72. {
  73. return $this->update(['status' => 1]);
  74. }
  75. public function complete() // 完成订单
  76. {
  77. return $this->update(['status' => 2]);
  78. }
  79. public function prepay() // 预支付订单
  80. {
  81. return $this->update(['status' => 3]);
  82. }
  83. // 订单状态
  84. public function getStatusLabelAttribute(): string
  85. {
  86. switch ($this->attributes['status']) {
  87. case -1:
  88. $status_label = '<span class="badge badge-default">'.trans('user.status.closed').'</span>';
  89. break;
  90. case 0:
  91. $status_label = '<span class="badge badge-danger">'.trans('user.status.waiting_payment').'</span>';
  92. break;
  93. case 1:
  94. $status_label = '<span class="badge badge-info">'.trans('user.status.waiting_confirm').'</span>';
  95. break;
  96. case 2:
  97. if ($this->attributes['goods_id'] === 0) {
  98. $status_label = '<span class="badge badge-default">'.trans('user.status.completed').'</span>';
  99. } elseif ($this->attributes['is_expire']) {
  100. $status_label = '<span class="badge badge-default">'.trans('user.status.expired').'</span>';
  101. } else {
  102. $status_label = '<span class="badge badge-success">'.trans('user.status.using').'</span>';
  103. }
  104. break;
  105. case 3:
  106. $status_label = '<span class="badge badge-info">'.trans('user.status.prepaid').'</span>';
  107. break;
  108. default:
  109. $status_label = trans('user.unknown');
  110. }
  111. return $status_label;
  112. }
  113. public function getOriginAmountAttribute($value)
  114. {
  115. return $value / 100;
  116. }
  117. public function setOriginAmountAttribute($value)
  118. {
  119. return $this->attributes['origin_amount'] = $value * 100;
  120. }
  121. public function getAmountAttribute($value)
  122. {
  123. return $value / 100;
  124. }
  125. public function setAmountAttribute($value)
  126. {
  127. return $this->attributes['amount'] = $value * 100;
  128. }
  129. // 支付渠道
  130. public function getPayTypeLabelAttribute(): string
  131. {
  132. return [
  133. 0 => trans('common.payment.credit'),
  134. 1 => trans('common.payment.alipay'),
  135. 2 => 'QQ',
  136. 3 => trans('common.payment.wechat'),
  137. 4 => trans('common.payment.crypto'),
  138. 5 => 'PayPal',
  139. 6 => 'Stripe',
  140. ][$this->attributes['pay_type']] ?? '';
  141. }
  142. // 支付图标
  143. public function getPayTypeIconAttribute(): string
  144. {
  145. return '/assets/images/payment/'.config('common.payment.icon')[$this->attributes['pay_type']] ?? 'coin.png';
  146. }
  147. // 支付方式
  148. public function getPayWayLabelAttribute(): string
  149. {
  150. return config('common.payment.labels')[$this->attributes['pay_way']] ?? '未知';
  151. }
  152. }