Order.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. /**
  8. * 订单.
  9. */
  10. class Order extends Model
  11. {
  12. protected $table = 'order';
  13. protected $dates = ['expired_at'];
  14. protected $guarded = [];
  15. public function user(): BelongsTo
  16. {
  17. return $this->belongsTo(User::class);
  18. }
  19. public function goods(): BelongsTo
  20. {
  21. return $this->belongsTo(Goods::class)->withTrashed();
  22. }
  23. public function coupon(): BelongsTo
  24. {
  25. return $this->belongsTo(Coupon::class)->withTrashed();
  26. }
  27. public function payment(): HasOne
  28. {
  29. return $this->hasOne(Payment::class);
  30. }
  31. public function scopeUid($query, $uid = null)
  32. {
  33. return $query->whereUserId($uid ?: Auth::id());
  34. }
  35. public function scopeRecentUnPay($query)
  36. {
  37. return $query->whereStatus(0)->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-15 minutes')));
  38. }
  39. public function scopeUserPrepay($query, $uid = null)
  40. {
  41. return $query->uid($uid)->whereStatus(3);
  42. }
  43. public function scopeActive($query)
  44. {
  45. return $query->whereIsExpire(0)->whereStatus(2);
  46. }
  47. public function scopeActivePlan($query)
  48. {
  49. return $query->active()->with('goods')->whereHas('goods', static function ($query) {
  50. $query->whereType(2);
  51. });
  52. }
  53. public function scopeActivePackage($query)
  54. {
  55. return $query->active()->with('goods')->whereHas('goods', static function ($query) {
  56. $query->whereType(1);
  57. });
  58. }
  59. public function scopeUserActivePlan($query, $uid = null)
  60. {
  61. return $query->uid($uid)->activePlan();
  62. }
  63. public function scopeUserActivePackage($query, $uid = null)
  64. {
  65. return $query->uid($uid)->activePackage();
  66. }
  67. public function close() // 关闭订单
  68. {
  69. return $this->update(['status' => -1]);
  70. }
  71. public function paid() // 完成订单
  72. {
  73. return $this->update(['status' => 1]);
  74. }
  75. public function complete() // 完成订单
  76. {
  77. return $this->update(['status' => 2]);
  78. }
  79. public function prepay() // 预支付订单
  80. {
  81. return $this->update(['status' => 3]);
  82. }
  83. // 订单状态
  84. public function getStatusLabelAttribute(): string
  85. {
  86. switch ($this->attributes['status']) {
  87. case -1:
  88. $status_label = '<span class="badge badge-default">'.trans('user.status.closed').'</span>';
  89. break;
  90. case 0:
  91. $status_label = '<span class="badge badge-danger">'.trans('user.status.waiting_payment').'</span>';
  92. break;
  93. case 1:
  94. $status_label = '<span class="badge badge-info">'.trans('user.status.waiting_confirm').'</span>';
  95. break;
  96. case 2:
  97. if ($this->attributes['goods_id'] === 0) {
  98. $status_label = '<span class="badge badge-default">'.trans('user.status.completed').'</span>';
  99. } elseif ($this->attributes['is_expire']) {
  100. $status_label = '<span class="badge badge-default">'.trans('user.status.expired').'</span>';
  101. } else {
  102. $status_label = '<span class="badge badge-success">'.trans('user.status.using').'</span>';
  103. }
  104. break;
  105. case 3:
  106. $status_label = '<span class="badge badge-info">'.trans('user.status.prepaid').'</span>';
  107. break;
  108. default:
  109. $status_label = trans('user.unknown');
  110. }
  111. return $status_label;
  112. }
  113. public function getOriginAmountAttribute($value)
  114. {
  115. return $value / 100;
  116. }
  117. public function setOriginAmountAttribute($value)
  118. {
  119. return $this->attributes['origin_amount'] = $value * 100;
  120. }
  121. public function getAmountAttribute($value)
  122. {
  123. return $value / 100;
  124. }
  125. public function setAmountAttribute($value)
  126. {
  127. return $this->attributes['amount'] = $value * 100;
  128. }
  129. // 支付渠道
  130. public function getPayTypeLabelAttribute(): string
  131. {
  132. switch ($this->attributes['pay_type']) {
  133. case 0:
  134. $pay_type_label = trans('common.payment.credit');
  135. break;
  136. case 1:
  137. $pay_type_label = trans('common.payment.alipay');
  138. break;
  139. case 2:
  140. $pay_type_label = 'QQ';
  141. break;
  142. case 3:
  143. $pay_type_label = trans('common.payment.wechat');
  144. break;
  145. case 4:
  146. $pay_type_label = trans('common.payment.crypto');
  147. break;
  148. case 5:
  149. $pay_type_label = 'PayPal';
  150. break;
  151. case 6:
  152. $pay_type_label = 'Stripe';
  153. break;
  154. default:
  155. $pay_type_label = '';
  156. }
  157. return $pay_type_label;
  158. }
  159. // 支付图标
  160. public function getPayTypeIconAttribute(): string
  161. {
  162. $base_path = '/assets/images/payment/';
  163. switch ($this->attributes['pay_type']) {
  164. case 1:
  165. $pay_type_icon = $base_path.'alipay.png';
  166. break;
  167. case 2:
  168. $pay_type_icon = $base_path.'qq.png';
  169. break;
  170. case 3:
  171. $pay_type_icon = $base_path.'wechat.png';
  172. break;
  173. case 5:
  174. $pay_type_icon = $base_path.'paypal.png';
  175. break;
  176. case 6:
  177. $pay_type_icon = $base_path.'stripe.png';
  178. break;
  179. default:
  180. $pay_type_icon = $base_path.'coin.png';
  181. }
  182. return $pay_type_icon;
  183. }
  184. // 支付方式
  185. public function getPayWayLabelAttribute(): string
  186. {
  187. switch ($this->attributes['pay_way']) {
  188. case 'credit':
  189. $pay_way_label = '余额';
  190. break;
  191. case 'youzan':
  192. $pay_way_label = '有赞云';
  193. break;
  194. case 'f2fpay':
  195. $pay_way_label = '支付宝当面付';
  196. break;
  197. case 'codepay':
  198. $pay_way_label = '码支付';
  199. break;
  200. case 'payjs':
  201. $pay_way_label = 'PayJs';
  202. break;
  203. case 'bitpayx':
  204. $pay_way_label = '麻瓜宝';
  205. break;
  206. case 'paypal':
  207. $pay_way_label = 'PayPal';
  208. break;
  209. case 'stripe':
  210. $pay_way_label = 'Stripe';
  211. break;
  212. case 'paybeaver':
  213. $pay_way_label = '海狸支付';
  214. break;
  215. default:
  216. $pay_way_label = '未知';
  217. }
  218. return $pay_way_label;
  219. }
  220. }