Order.php 5.5 KB

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