Order.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. // 订单状态
  68. public function getStatusLabelAttribute(): string
  69. {
  70. switch ($this->attributes['status']) {
  71. case -1:
  72. $status_label = trans('home.invoice_status_closed');
  73. break;
  74. case 1:
  75. $status_label = trans('home.invoice_status_wait_confirm');
  76. break;
  77. case 2:
  78. $status_label = trans('home.invoice_status_payment_confirm');
  79. break;
  80. case 0:
  81. $status_label = trans('home.invoice_status_wait_payment');
  82. break;
  83. default:
  84. $status_label = 'Unknown';
  85. }
  86. return $status_label;
  87. }
  88. public function getOriginAmountAttribute($value)
  89. {
  90. return $value / 100;
  91. }
  92. public function setOriginAmountAttribute($value)
  93. {
  94. return $this->attributes['origin_amount'] = $value * 100;
  95. }
  96. public function getAmountAttribute($value)
  97. {
  98. return $value / 100;
  99. }
  100. public function setAmountAttribute($value)
  101. {
  102. return $this->attributes['amount'] = $value * 100;
  103. }
  104. // 支付渠道
  105. public function getPayTypeLabelAttribute(): string
  106. {
  107. switch ($this->attributes['pay_type']) {
  108. case 0:
  109. $pay_type_label = '余额';
  110. break;
  111. case 1:
  112. $pay_type_label = '支付宝';
  113. break;
  114. case 2:
  115. $pay_type_label = 'QQ';
  116. break;
  117. case 3:
  118. $pay_type_label = '微信';
  119. break;
  120. case 4:
  121. $pay_type_label = '虚拟货币';
  122. break;
  123. case 5:
  124. $pay_type_label = 'PayPal';
  125. break;
  126. case 6:
  127. $pay_type_label = 'Stripe';
  128. break;
  129. default:
  130. $pay_type_label = '';
  131. }
  132. return $pay_type_label;
  133. }
  134. // 支付图标
  135. public function getPayTypeIconAttribute(): string
  136. {
  137. $base_path = '/assets/images/payment/';
  138. switch ($this->attributes['pay_type']) {
  139. case 1:
  140. $pay_type_icon = $base_path.'alipay.png';
  141. break;
  142. case 2:
  143. $pay_type_icon = $base_path.'qq.png';
  144. break;
  145. case 3:
  146. $pay_type_icon = $base_path.'wechat.png';
  147. break;
  148. case 5:
  149. $pay_type_icon = $base_path.'paypal.png';
  150. break;
  151. case 6:
  152. $pay_type_icon = $base_path.'stripe.png';
  153. break;
  154. case 0:
  155. case 4:
  156. default:
  157. $pay_type_icon = $base_path.'coin.png';
  158. }
  159. return $pay_type_icon;
  160. }
  161. // 支付方式
  162. public function getPayWayLabelAttribute(): string
  163. {
  164. switch ($this->attributes['pay_way']) {
  165. case 'credit':
  166. $pay_way_label = '余额';
  167. break;
  168. case 'youzan':
  169. $pay_way_label = '有赞云';
  170. break;
  171. case 'f2fpay':
  172. $pay_way_label = '支付宝当面付';
  173. break;
  174. case 'codepay':
  175. $pay_way_label = '码支付';
  176. break;
  177. case 'payjs':
  178. $pay_way_label = 'PayJs';
  179. break;
  180. case 'bitpayx':
  181. $pay_way_label = '麻瓜宝';
  182. break;
  183. case 'paypal':
  184. $pay_way_label = 'PayPal';
  185. break;
  186. case 'stripe':
  187. $pay_way_label = 'Stripe';
  188. break;
  189. default:
  190. $pay_way_label = '未知';
  191. }
  192. return $pay_way_label;
  193. }
  194. }