Order.php 5.2 KB

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