Order.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 = trans('home.invoice_status_closed');
  89. break;
  90. case 1:
  91. $status_label = trans('home.invoice_status_wait_confirm');
  92. break;
  93. case 2:
  94. $status_label = trans('home.invoice_status_payment_confirm');
  95. break;
  96. case 0:
  97. $status_label = trans('home.invoice_status_wait_payment');
  98. break;
  99. default:
  100. $status_label = 'Unknown';
  101. }
  102. return $status_label;
  103. }
  104. public function getOriginAmountAttribute($value)
  105. {
  106. return $value / 100;
  107. }
  108. public function setOriginAmountAttribute($value)
  109. {
  110. return $this->attributes['origin_amount'] = $value * 100;
  111. }
  112. public function getAmountAttribute($value)
  113. {
  114. return $value / 100;
  115. }
  116. public function setAmountAttribute($value)
  117. {
  118. return $this->attributes['amount'] = $value * 100;
  119. }
  120. // 支付渠道
  121. public function getPayTypeLabelAttribute(): string
  122. {
  123. switch ($this->attributes['pay_type']) {
  124. case 0:
  125. $pay_type_label = '余额';
  126. break;
  127. case 1:
  128. $pay_type_label = '支付宝';
  129. break;
  130. case 2:
  131. $pay_type_label = 'QQ';
  132. break;
  133. case 3:
  134. $pay_type_label = '微信';
  135. break;
  136. case 4:
  137. $pay_type_label = '虚拟货币';
  138. break;
  139. case 5:
  140. $pay_type_label = 'PayPal';
  141. break;
  142. case 6:
  143. $pay_type_label = 'Stripe';
  144. break;
  145. case 7:
  146. $pay_type_label = 'PayBeaver';
  147. break;
  148. default:
  149. $pay_type_label = '';
  150. }
  151. return $pay_type_label;
  152. }
  153. // 支付图标
  154. public function getPayTypeIconAttribute(): string
  155. {
  156. $base_path = '/assets/images/payment/';
  157. switch ($this->attributes['pay_type']) {
  158. case 1:
  159. $pay_type_icon = $base_path.'alipay.png';
  160. break;
  161. case 2:
  162. $pay_type_icon = $base_path.'qq.png';
  163. break;
  164. case 3:
  165. $pay_type_icon = $base_path.'wechat.png';
  166. break;
  167. case 5:
  168. $pay_type_icon = $base_path.'paypal.png';
  169. break;
  170. case 6:
  171. $pay_type_icon = $base_path.'stripe.png';
  172. break;
  173. case 7:
  174. $pay_type_icon = $base_path.'coin.png';
  175. break;
  176. case 0:
  177. case 4:
  178. default:
  179. $pay_type_icon = $base_path.'coin.png';
  180. }
  181. return $pay_type_icon;
  182. }
  183. // 支付方式
  184. public function getPayWayLabelAttribute(): string
  185. {
  186. switch ($this->attributes['pay_way']) {
  187. case 'credit':
  188. $pay_way_label = '余额';
  189. break;
  190. case 'youzan':
  191. $pay_way_label = '有赞云';
  192. break;
  193. case 'f2fpay':
  194. $pay_way_label = '支付宝当面付';
  195. break;
  196. case 'codepay':
  197. $pay_way_label = '码支付';
  198. break;
  199. case 'payjs':
  200. $pay_way_label = 'PayJs';
  201. break;
  202. case 'bitpayx':
  203. $pay_way_label = '麻瓜宝';
  204. break;
  205. case 'paypal':
  206. $pay_way_label = 'PayPal';
  207. break;
  208. case 'stripe':
  209. $pay_way_label = 'Stripe';
  210. break;
  211. case 'paybeaver':
  212. $pay_way_label = '海狸支付';
  213. break;
  214. default:
  215. $pay_way_label = '未知';
  216. }
  217. return $pay_way_label;
  218. }
  219. }