RefundApply.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\frontend\modules\refund\models;
  3. use app\common\models\refund\RefundGoodsLog;
  4. use app\frontend\models\Order;
  5. /**
  6. * Created by PhpStorm.
  7. * Author: 芸众商城 www.yunzshop.com
  8. * Date: 2017/4/12
  9. * Time: 下午9:53
  10. */
  11. class RefundApply extends \app\common\models\refund\RefundApply
  12. {
  13. protected $appends = [
  14. 'refund_type_name', 'status_name', 'is_refunded', 'is_refunding', 'is_refund_fail', 'plugin_id',
  15. 'receive_status_name', 'refund_way_type_name','button_models',
  16. ];
  17. public function scopeFrontendSearch($query, $search = [])
  18. {
  19. $model = $query;
  20. if ($search['sn']) {
  21. $tag = substr($search['sn'], 0, 2);
  22. if ('RN' == strtoupper($tag)) {
  23. $model->where('yz_order_refund.refund_sn', $search['sn']);
  24. } else {
  25. $order_id = Order::where('order_sn', $search['sn'])->value('id');
  26. $model->where('yz_order_refund.order_id', $order_id);
  27. }
  28. }
  29. if ($search['order_goods_id']) {
  30. $refundId = RefundGoodsLog::withTrashed()->where('order_goods_id', $search['order_goods_id'])->pluck('refund_id')->unique()->toArray();
  31. $model->whereIn('yz_order_refund.id', $refundId);
  32. }
  33. if ($search['refund_id']) {
  34. $model->where('yz_order_refund.id', $search['refund_id']);
  35. }
  36. $model->with([
  37. 'order' => self::orderBuilder(), 'refundOrderGoods',
  38. ]);
  39. $model->orderBy('yz_order_refund.id', 'desc');
  40. return $model;
  41. }
  42. /**
  43. * 前端获取退款按钮 todo 转移到前端的model
  44. * @return array
  45. */
  46. public function getButtonModelsAttribute()
  47. {
  48. $result = [];
  49. if ($this->status == self::WAIT_CHECK) {
  50. $result[] = [
  51. 'name' => '修改申请',
  52. 'api' => 'refund.edit.index',
  53. 'value' => 1
  54. ];
  55. $result[] = [
  56. 'name' => '取消申请',
  57. 'api' => 'refund.operation.cancel',
  58. 'value' => 4
  59. ];
  60. }
  61. if ($this->status == self::WAIT_RETURN_GOODS) {
  62. if (!($this->order->plugin_id == 40)) {
  63. $result[] = [
  64. 'name' => '填写快递',
  65. 'api' => 'refund.operation.send',
  66. 'value' => 2
  67. ];
  68. }
  69. }
  70. if ($this->status == self::WAIT_RECEIVE_RESEND_GOODS) {
  71. $result[] = [
  72. 'name' => '确认收货',
  73. 'api' => 'refund.operation.complete',
  74. 'value' => 3
  75. ];
  76. }
  77. if ($this->refund_type == self::REFUND_TYPE_EXCHANGE_GOODS && $this->hasManyResendExpress->isNotEmpty()) {
  78. $result[] = [
  79. 'name' => '查看物流',
  80. 'api' => 'refund.express.resend-list',
  81. 'value' => 6
  82. ];
  83. }
  84. return $result;
  85. }
  86. public function scopeDetail($query)
  87. {
  88. return $query->with([
  89. 'order',
  90. 'returnExpress',
  91. 'hasManyResendExpress',
  92. 'refundOrderGoods',
  93. ]);
  94. }
  95. protected static function boot()
  96. {
  97. parent::boot();
  98. self::addGlobalScope(function ($query) {
  99. return $query->where('uid', \YunShop::app()->getMemberId());
  100. });
  101. }
  102. public function scopeDefaults($query)
  103. {
  104. return $query->with([
  105. 'order' => self::orderBuilder(),
  106. 'refundOrderGoods',
  107. ])->orderBy('id', 'desc');
  108. }
  109. private static function orderBuilder()
  110. {
  111. return function ($order) {
  112. return $order->select(['id', 'order_sn', 'plugin_id', 'status','refund_id']);
  113. };
  114. }
  115. public function hasOneCstoreOrder()
  116. {
  117. return $this->hasOne(\Yunshop\CouponStore\models\StoreOrder::class, 'order_id', 'order_id');
  118. }
  119. }