Order.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace App\Models;
  3. use App\Utils\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 $guarded = [];
  18. protected $casts = ['expired_at' => 'datetime'];
  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)->oldest();
  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(): bool
  75. { // 关闭订单
  76. return $this->update(['status' => -1]);
  77. }
  78. public function paid(): bool
  79. { // 支付需要确认的订单
  80. return $this->update(['status' => 1]);
  81. }
  82. public function complete(): bool
  83. { // 完成订单
  84. return $this->update(['status' => 2]);
  85. }
  86. public function prepay(): bool
  87. { // 预支付订单
  88. return $this->update(['status' => 3]);
  89. }
  90. public function expired(): bool
  91. { // 预支付订单
  92. return $this->update(['is_expire' => 1]);
  93. }
  94. public function getStatusLabelAttribute(): string
  95. { // 订单状态
  96. return $this->statusTags($this->attributes['status'], $this->attributes['is_expire']);
  97. }
  98. public function statusTags($status, $expire, $isHtml = true): string
  99. {
  100. switch ($status) {
  101. case -1:
  102. $label = trans('common.order.status.cancel');
  103. break;
  104. case 0:
  105. $tag = 1;
  106. $label = trans('common.payment.status.wait');
  107. break;
  108. case 1:
  109. $tag = 2;
  110. $label = trans('common.order.status.review');
  111. break;
  112. case 2:
  113. if ($this->attributes['goods_id'] === null) {
  114. $label = trans('common.order.status.complete');
  115. } elseif ($expire) {
  116. $label = trans('common.status.expire');
  117. } else {
  118. $tag = 3;
  119. $label = trans('common.order.status.ongoing');
  120. }
  121. break;
  122. case 3:
  123. $tag = 2;
  124. $label = trans('common.order.status.prepaid');
  125. break;
  126. default:
  127. $tag = 4;
  128. $label = trans('common.status.unknown');
  129. }
  130. if ($isHtml) {
  131. $label = '<span class="badge badge-'.['default', 'danger', 'info', 'success', 'warning'][$tag ?? 0].'">'.$label.'</span>';
  132. }
  133. return $label;
  134. }
  135. public function getOriginAmountAttribute($value)
  136. {
  137. return $value / 100;
  138. }
  139. public function setOriginAmountAttribute($value)
  140. {
  141. return $this->attributes['origin_amount'] = $value * 100;
  142. }
  143. public function getOriginAmountTagAttribute(): string
  144. {
  145. return Helpers::getPriceTag($this->origin_amount);
  146. }
  147. public function getAmountAttribute($value)
  148. {
  149. return $value / 100;
  150. }
  151. public function setAmountAttribute($value)
  152. {
  153. return $this->attributes['amount'] = $value * 100;
  154. }
  155. public function getAmountTagAttribute(): string
  156. {
  157. return Helpers::getPriceTag($this->amount);
  158. }
  159. // 支付渠道
  160. public function getPayTypeLabelAttribute(): string
  161. {
  162. return [
  163. 0 => trans('common.payment.credit'),
  164. 1 => trans('common.payment.alipay'),
  165. 2 => trans('common.payment.qq'),
  166. 3 => trans('common.payment.wechat'),
  167. 4 => trans('common.payment.crypto'),
  168. 5 => 'PayPal',
  169. 6 => 'Stripe',
  170. 7 => trans('common.payment.manual'),
  171. ][$this->attributes['pay_type']] ?? '';
  172. }
  173. // 支付图标
  174. public function getPayTypeIconAttribute(): string
  175. {
  176. return '/assets/images/payment/'.config('common.payment.icon')[$this->attributes['pay_type']] ?? 'coin.png';
  177. }
  178. // 支付方式
  179. public function getPayWayLabelAttribute(): string
  180. {
  181. return config('common.payment.labels')[$this->attributes['pay_way']] ?? '未知';
  182. }
  183. }