OrderRefund.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\api\model\order;
  3. use app\common\model\order\OrderRefund as OrderRefundModel;
  4. use app\common\model\order\Order as OrderModel;
  5. /**
  6. * 售后单模型
  7. */
  8. class OrderRefund extends OrderRefundModel
  9. {
  10. /**
  11. * 隐藏字段
  12. * @var array
  13. */
  14. protected $hidden = [
  15. 'app_id',
  16. 'update_time'
  17. ];
  18. /**
  19. * 追加字段
  20. * @var array
  21. */
  22. protected $append = [
  23. 'state_text', // 售后单状态文字描述
  24. ];
  25. /**
  26. * 售后单状态文字描述
  27. */
  28. public function getStateTextAttr($value, $data)
  29. {
  30. // 已完成
  31. if ($data['status'] == 20) {
  32. $text = [10 => '已同意退货并已退款', 20 => '已同意换货', '30' => '仅退款并已退款'];
  33. return $text[$data['type']];
  34. }
  35. // 已取消
  36. if ($data['status'] == 30) {
  37. return '已取消';
  38. }
  39. // 已拒绝
  40. if ($data['status'] == 10) {
  41. return $data['type'] == 10 ? '已拒绝退货退款' : '已拒绝换货';
  42. }
  43. // 进行中
  44. if ($data['status'] == 0) {
  45. if ($data['is_agree'] == 0) {
  46. return '等待审核中';
  47. }
  48. if ($data['type'] == 10) {
  49. return $data['is_user_send'] ? '已发货,待平台确认' : '已同意退货,请及时发货';
  50. }
  51. }
  52. return $value;
  53. }
  54. /**
  55. * 获取用户售后单列表
  56. */
  57. public function getList($user_id, $state = -1, $param)
  58. {
  59. $model = $this;
  60. $state > -1 && $model = $this->where('status', '=', $state);
  61. if(isset($params['shop_supplier_id'])&&$params['shop_supplier_id']){
  62. $model = $model->where('shop_supplier_id','=',$params['shop_supplier_id']);
  63. }
  64. return $model->with(['order_master', 'orderproduct.image'])
  65. ->where('user_id', '=', $user_id)
  66. ->order(['create_time' => 'desc'])
  67. ->paginate($param);
  68. }
  69. /**
  70. * 用户发货
  71. */
  72. public function delivery($data)
  73. {
  74. if (
  75. $this['type']['value'] != 10
  76. || $this['is_agree']['value'] != 10
  77. || $this['is_user_send'] != 0
  78. ) {
  79. $this->error = '当前售后单不合法,不允许该操作';
  80. return false;
  81. }
  82. if ($data['express_id'] <= 0) {
  83. $this->error = '请选择物流公司';
  84. return false;
  85. }
  86. if (empty($data['express_no'])) {
  87. $this->error = '请填写物流单号';
  88. return false;
  89. }
  90. return $this->save([
  91. 'is_user_send' => 1,
  92. 'send_time' => time(),
  93. 'express_id' => (int)$data['express_id'],
  94. 'express_no' => $data['express_no'],
  95. ]);
  96. }
  97. /**
  98. * 新增售后单记录
  99. */
  100. public function apply($user, $product, $data)
  101. {
  102. $this->startTrans();
  103. try {
  104. $shop_supplier_id = (new OrderModel())->where(['order_id'=>$product['order_id']])->value('
  105. shop_supplier_id');
  106. // 新增售后单记录
  107. $this->save([
  108. 'order_product_id' => $data['order_product_id'],
  109. 'order_id' => $product['order_id'],
  110. 'user_id' => $user['user_id'],
  111. 'type' => $data['type'],
  112. 'apply_desc' => $data['content'],
  113. 'is_agree' => 0,
  114. 'status' => 0,
  115. 'app_id' => self::$app_id,
  116. 'shop_supplier_id'=>$shop_supplier_id
  117. ]);
  118. // 记录凭证图片关系
  119. if (isset($data['images']) && !empty($data['images'])) {
  120. $this->saveImages($this['order_refund_id'], $data['images']);
  121. }
  122. $this->commit();
  123. return true;
  124. } catch (\Exception $e) {
  125. $this->error = $e->getMessage();
  126. $this->rollback();
  127. return false;
  128. }
  129. }
  130. //新增平台介入
  131. public function plateapply($order_refund_id){
  132. return $this->where(['order_refund_id'=>$order_refund_id])->update(['plate_status'=>10]);
  133. }
  134. /**
  135. * 记录售后单图片
  136. */
  137. private function saveImages($order_refund_id, $images)
  138. {
  139. $images_ids = [];
  140. foreach (json_decode($images, true) as $val) {
  141. $images_ids[] = $val['file_id'];
  142. }
  143. // 生成评价图片数据
  144. $data = [];
  145. foreach ($images_ids as $image_id) {
  146. $data[] = [
  147. 'order_refund_id' => $order_refund_id,
  148. 'image_id' => $image_id,
  149. 'app_id' => self::$app_id
  150. ];
  151. }
  152. return !empty($data) && (new OrderRefundImage)->saveAll($data);
  153. }
  154. }