Order.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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)
  39. {
  40. return $query->whereStatus(0)->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-'.config('tasks.close.order').' minutes')));
  41. }
  42. public function scopeUserPrepay($query, $uid = null)
  43. {
  44. return $query->uid($uid)->whereStatus(3);
  45. }
  46. public function scopeActive($query)
  47. {
  48. return $query->whereIsExpire(0)->whereStatus(2);
  49. }
  50. public function scopeActivePlan($query)
  51. {
  52. return $query->active()->with('goods')->whereHas('goods', static function ($query) {
  53. $query->whereType(2);
  54. });
  55. }
  56. public function scopeActivePackage($query)
  57. {
  58. return $query->active()->with('goods')->whereHas('goods', static function ($query) {
  59. $query->whereType(1);
  60. });
  61. }
  62. public function scopeUserActivePlan($query, $uid = null)
  63. {
  64. return $query->uid($uid)->activePlan();
  65. }
  66. public function scopeUserActivePackage($query, $uid = null)
  67. {
  68. return $query->uid($uid)->activePackage();
  69. }
  70. public function close() // 关闭订单
  71. {
  72. return $this->update(['status' => -1]);
  73. }
  74. public function paid() // 完成订单
  75. {
  76. return $this->update(['status' => 1]);
  77. }
  78. public function complete() // 完成订单
  79. {
  80. return $this->update(['status' => 2]);
  81. }
  82. public function prepay() // 预支付订单
  83. {
  84. return $this->update(['status' => 3]);
  85. }
  86. // 订单状态
  87. public function getStatusLabelAttribute(): string
  88. {
  89. switch ($this->attributes['status']) {
  90. case -1:
  91. $status_label = '<span class="badge badge-default">'.trans('user.status.closed').'</span>';
  92. break;
  93. case 0:
  94. $status_label = '<span class="badge badge-danger">'.trans('user.status.waiting_payment').'</span>';
  95. break;
  96. case 1:
  97. $status_label = '<span class="badge badge-info">'.trans('user.status.waiting_confirm').'</span>';
  98. break;
  99. case 2:
  100. if ($this->attributes['goods_id'] === 0) {
  101. $status_label = '<span class="badge badge-default">'.trans('user.status.completed').'</span>';
  102. } elseif ($this->attributes['is_expire']) {
  103. $status_label = '<span class="badge badge-default">'.trans('user.status.expired').'</span>';
  104. } else {
  105. $status_label = '<span class="badge badge-success">'.trans('user.status.using').'</span>';
  106. }
  107. break;
  108. case 3:
  109. $status_label = '<span class="badge badge-info">'.trans('user.status.prepaid').'</span>';
  110. break;
  111. default:
  112. $status_label = trans('user.unknown');
  113. }
  114. return $status_label;
  115. }
  116. public function getOriginAmountAttribute($value)
  117. {
  118. return $value / 100;
  119. }
  120. public function setOriginAmountAttribute($value)
  121. {
  122. return $this->attributes['origin_amount'] = $value * 100;
  123. }
  124. public function getAmountAttribute($value)
  125. {
  126. return $value / 100;
  127. }
  128. public function setAmountAttribute($value)
  129. {
  130. return $this->attributes['amount'] = $value * 100;
  131. }
  132. // 支付渠道
  133. public function getPayTypeLabelAttribute(): string
  134. {
  135. return [
  136. 0 => trans('common.payment.credit'),
  137. 1 => trans('common.payment.alipay'),
  138. 2 => 'QQ',
  139. 3 => trans('common.payment.wechat'),
  140. 4 => trans('common.payment.crypto'),
  141. 5 => 'PayPal',
  142. 6 => 'Stripe',
  143. ][$this->attributes['pay_type']] ?? '';
  144. }
  145. // 支付图标
  146. public function getPayTypeIconAttribute(): string
  147. {
  148. return '/assets/images/payment/'.config('common.payment.icon')[$this->attributes['pay_type']] ?? 'coin.png';
  149. }
  150. // 支付方式
  151. public function getPayWayLabelAttribute(): string
  152. {
  153. return config('common.payment.labels')[$this->attributes['pay_way']] ?? '未知';
  154. }
  155. }