Order.php 5.6 KB

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