Order.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. * @property int $id
  11. * @property string $sn 订单编号
  12. * @property int $user_id 购买者ID
  13. * @property int|null $goods_id 商品ID
  14. * @property int|null $coupon_id 优惠券ID
  15. * @property int $origin_amount 订单原始总价,单位分
  16. * @property int $amount 订单总价,单位分
  17. * @property \Illuminate\Support\Carbon|null $expired_at 过期时间
  18. * @property int $is_expire 是否已过期:0-未过期、1-已过期
  19. * @property int $pay_type 支付渠道:0-余额、1-支付宝、2-QQ、3-微信、4-虚拟货币、5-paypal
  20. * @property string $pay_way 支付方式:balance、f2fpay、codepay、payjs、bitpayx等
  21. * @property int $status 订单状态:-1-已关闭、0-待支付、1-已支付待确认、2-已完成
  22. * @property \Illuminate\Support\Carbon $created_at 创建时间
  23. * @property \Illuminate\Support\Carbon $updated_at 最后更新时间
  24. * @property-read \App\Models\Coupon|null $coupon
  25. * @property-read string $pay_type_icon
  26. * @property-read string $pay_type_label
  27. * @property-read string $pay_way_label
  28. * @property-read string $status_label
  29. * @property-read \App\Models\Goods|null $goods
  30. * @property-read \App\Models\Payment|null $payment
  31. * @property-read \App\Models\User $user
  32. * @method static \Illuminate\Database\Eloquent\Builder|Order active()
  33. * @method static \Illuminate\Database\Eloquent\Builder|Order activePackage()
  34. * @method static \Illuminate\Database\Eloquent\Builder|Order activePlan()
  35. * @method static \Illuminate\Database\Eloquent\Builder|Order newModelQuery()
  36. * @method static \Illuminate\Database\Eloquent\Builder|Order newQuery()
  37. * @method static \Illuminate\Database\Eloquent\Builder|Order query()
  38. * @method static \Illuminate\Database\Eloquent\Builder|Order recentUnPay()
  39. * @method static \Illuminate\Database\Eloquent\Builder|Order uid($uid = null)
  40. * @method static \Illuminate\Database\Eloquent\Builder|Order userActivePackage($uid = null)
  41. * @method static \Illuminate\Database\Eloquent\Builder|Order userActivePlan($uid = null)
  42. * @method static \Illuminate\Database\Eloquent\Builder|Order userPrepay($uid = null)
  43. * @method static \Illuminate\Database\Eloquent\Builder|Order whereAmount($value)
  44. * @method static \Illuminate\Database\Eloquent\Builder|Order whereCouponId($value)
  45. * @method static \Illuminate\Database\Eloquent\Builder|Order whereCreatedAt($value)
  46. * @method static \Illuminate\Database\Eloquent\Builder|Order whereExpiredAt($value)
  47. * @method static \Illuminate\Database\Eloquent\Builder|Order whereGoodsId($value)
  48. * @method static \Illuminate\Database\Eloquent\Builder|Order whereId($value)
  49. * @method static \Illuminate\Database\Eloquent\Builder|Order whereIsExpire($value)
  50. * @method static \Illuminate\Database\Eloquent\Builder|Order whereOriginAmount($value)
  51. * @method static \Illuminate\Database\Eloquent\Builder|Order wherePayType($value)
  52. * @method static \Illuminate\Database\Eloquent\Builder|Order wherePayWay($value)
  53. * @method static \Illuminate\Database\Eloquent\Builder|Order whereSn($value)
  54. * @method static \Illuminate\Database\Eloquent\Builder|Order whereStatus($value)
  55. * @method static \Illuminate\Database\Eloquent\Builder|Order whereUpdatedAt($value)
  56. * @method static \Illuminate\Database\Eloquent\Builder|Order whereUserId($value)
  57. * @mixin \Eloquent
  58. */
  59. class Order extends Model
  60. {
  61. protected $table = 'order';
  62. protected $dates = ['expired_at'];
  63. protected $guarded = [];
  64. public function user(): BelongsTo
  65. {
  66. return $this->belongsTo(User::class);
  67. }
  68. public function goods(): BelongsTo
  69. {
  70. return $this->belongsTo(Goods::class)->withTrashed();
  71. }
  72. public function coupon(): BelongsTo
  73. {
  74. return $this->belongsTo(Coupon::class)->withTrashed();
  75. }
  76. public function payment(): HasOne
  77. {
  78. return $this->hasOne(Payment::class);
  79. }
  80. public function scopeUid($query, $uid = null)
  81. {
  82. return $query->whereUserId($uid ?: Auth::id());
  83. }
  84. public function scopeRecentUnPay($query)
  85. {
  86. return $query->whereStatus(0)->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-15 minutes')));
  87. }
  88. public function scopeUserPrepay($query, $uid = null)
  89. {
  90. return $query->uid($uid)->whereStatus(3);
  91. }
  92. public function scopeActive($query)
  93. {
  94. return $query->whereIsExpire(0)->whereStatus(2);
  95. }
  96. public function scopeActivePlan($query)
  97. {
  98. return $query->active()->with('goods')->whereHas('goods', static function ($query) {
  99. $query->whereType(2);
  100. });
  101. }
  102. public function scopeActivePackage($query)
  103. {
  104. return $query->active()->with('goods')->whereHas('goods', static function ($query) {
  105. $query->whereType(1);
  106. });
  107. }
  108. public function scopeUserActivePlan($query, $uid = null)
  109. {
  110. return $query->uid($uid)->activePlan();
  111. }
  112. public function scopeUserActivePackage($query, $uid = null)
  113. {
  114. return $query->uid($uid)->activePackage();
  115. }
  116. public function close() // 关闭订单
  117. {
  118. return $this->update(['status' => -1]);
  119. }
  120. public function paid() // 完成订单
  121. {
  122. return $this->update(['status' => 1]);
  123. }
  124. public function complete() // 完成订单
  125. {
  126. return $this->update(['status' => 2]);
  127. }
  128. public function prepay() // 预支付订单
  129. {
  130. return $this->update(['status' => 3]);
  131. }
  132. // 订单状态
  133. public function getStatusLabelAttribute(): string
  134. {
  135. switch ($this->attributes['status']) {
  136. case -1:
  137. $status_label = '<span class="badge badge-default">'.trans('user.status.closed').'</span>';
  138. break;
  139. case 0:
  140. $status_label = '<span class="badge badge-danger">'.trans('user.status.waiting_payment').'</span>';
  141. break;
  142. case 1:
  143. $status_label = '<span class="badge badge-info">'.trans('user.status.waiting_confirm').'</span>';
  144. break;
  145. case 2:
  146. if ($this->attributes['goods_id'] === 0) {
  147. $status_label = '<span class="badge badge-default">'.trans('user.status.completed').'</span>';
  148. } elseif ($this->attributes['is_expire']) {
  149. $status_label = '<span class="badge badge-default">'.trans('user.status.expired').'</span>';
  150. } else {
  151. $status_label = '<span class="badge badge-success">'.trans('user.status.using').'</span>';
  152. }
  153. break;
  154. case 3:
  155. $status_label = '<span class="badge badge-info">'.trans('user.status.prepaid').'</span>';
  156. break;
  157. default:
  158. $status_label = trans('user.unknown');
  159. }
  160. return $status_label;
  161. }
  162. public function getOriginAmountAttribute($value)
  163. {
  164. return $value / 100;
  165. }
  166. public function setOriginAmountAttribute($value)
  167. {
  168. return $this->attributes['origin_amount'] = $value * 100;
  169. }
  170. public function getAmountAttribute($value)
  171. {
  172. return $value / 100;
  173. }
  174. public function setAmountAttribute($value)
  175. {
  176. return $this->attributes['amount'] = $value * 100;
  177. }
  178. // 支付渠道
  179. public function getPayTypeLabelAttribute(): string
  180. {
  181. switch ($this->attributes['pay_type']) {
  182. case 0:
  183. $pay_type_label = trans('common.payment.credit');
  184. break;
  185. case 1:
  186. $pay_type_label = trans('common.payment.alipay');
  187. break;
  188. case 2:
  189. $pay_type_label = '支付宝支付';
  190. break;
  191. case 3:
  192. $pay_type_label = trans('common.payment.wechat');
  193. break;
  194. case 4:
  195. $pay_type_label = trans('common.payment.crypto');
  196. break;
  197. case 5:
  198. $pay_type_label = 'PayPal';
  199. break;
  200. case 6:
  201. $pay_type_label = 'Stripe';
  202. break;
  203. case 7:
  204. trans('common.payment.manual');
  205. break;
  206. default:
  207. $pay_type_label = '';
  208. }
  209. return $pay_type_label;
  210. }
  211. // 支付图标
  212. public function getPayTypeIconAttribute(): string
  213. {
  214. // return '/assets/images/payment/'.config('common.payment.icon')[$this->attributes['pay_type']] ?? 'coin.png';
  215. $base_path = '/assets/images/payment/';
  216. switch ($this->attributes['pay_type']) {
  217. case 1:
  218. $pay_type_icon = $base_path.'alipay.png';
  219. break;
  220. case 2:
  221. $pay_type_icon = $base_path.'alipay-other.png';
  222. break;
  223. case 3:
  224. $pay_type_icon = $base_path.'wechat.png';
  225. break;
  226. case 5:
  227. $pay_type_icon = $base_path.'paypal.png';
  228. break;
  229. case 6:
  230. $pay_type_icon = $base_path.'stripe.png';
  231. break;
  232. default:
  233. $pay_type_icon = $base_path.'coin.png';
  234. }
  235. return $pay_type_icon;
  236. }
  237. // 支付方式
  238. public function getPayWayLabelAttribute(): string
  239. {
  240. //return config('common.payment.labels')[$this->attributes['pay_way']] ?? '未知';
  241. switch ($this->attributes['pay_way']) {
  242. case 'credit':
  243. $pay_way_label = '余额';
  244. break;
  245. case 'youzan':
  246. $pay_way_label = '有赞云';
  247. break;
  248. case 'f2fpay':
  249. $pay_way_label = '支付宝当面付';
  250. break;
  251. case 'codepay':
  252. $pay_way_label = '码支付';
  253. break;
  254. case 'payjs':
  255. $pay_way_label = 'PayJs';
  256. break;
  257. case 'bitpayx':
  258. $pay_way_label = '麻瓜宝';
  259. break;
  260. case 'paypal':
  261. $pay_way_label = 'PayPal';
  262. break;
  263. case 'stripe':
  264. $pay_way_label = 'Stripe';
  265. break;
  266. case 'paybeaver':
  267. $pay_way_label = '海狸支付';
  268. break;
  269. case 'zpay':
  270. $pay_way_label = 'zpay';
  271. break;
  272. case 'epay':
  273. $pay_way_label = '易支付';
  274. break;
  275. case 'theadpay':
  276. $pay_way_label = '平头哥';
  277. break;
  278. case 'epaytt':
  279. $pay_way_label = 'epaytt';
  280. break;
  281. case 'bypay':
  282. $pay_way_label = 'bypay';
  283. break;
  284. default:
  285. $pay_way_label = '未知';
  286. }
  287. return $pay_way_label;
  288. }
  289. }