OrderRefund.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\store\model;
  3. use app\common\model\OrderRefund as OrderRefundModel;
  4. use app\store\model\User as UserModel;
  5. use app\common\service\Message as MessageService;
  6. use app\common\service\order\Refund as RefundService;
  7. use app\common\enum\OrderType as OrderTypeEnum;
  8. /**
  9. * 售后单模型
  10. * Class OrderRefund
  11. * @package app\api\model
  12. */
  13. class OrderRefund extends OrderRefundModel
  14. {
  15. /**
  16. * 获取售后单列表
  17. * @param array $query
  18. * @return \think\Paginator
  19. * @throws \think\exception\DbException
  20. */
  21. public function getList($query = [])
  22. {
  23. // 查询条件:订单号
  24. if (isset($query['order_no']) && !empty($query['order_no'])) {
  25. $this->where('order.order_no', 'like', "%{$query['order_no']}%");
  26. }
  27. // 查询条件:起始日期
  28. if (isset($query['start_time']) && !empty($query['start_time'])) {
  29. $this->where('m.create_time', '>=', strtotime($query['start_time']));
  30. }
  31. // 查询条件:截止日期
  32. if (isset($query['end_time']) && !empty($query['end_time'])) {
  33. $this->where('m.create_time', '<', strtotime($query['end_time']) + 86400);
  34. }
  35. // 售后类型
  36. if (isset($query['type']) && $query['type'] > 0) {
  37. $this->where('m.type', '=', $query['type']);
  38. }
  39. // 处理状态
  40. if (isset($query['state']) && is_numeric($query['state'])) {
  41. $this->where('m.status', '=', $query['state']);
  42. }
  43. // 获取列表数据
  44. return $this->alias('m')
  45. ->field('m.*, order.order_no')
  46. ->with(['order_goods.image', 'orderMaster', 'user'])
  47. ->join('order', 'order.order_id = m.order_id')
  48. ->order(['m.create_time' => 'desc'])
  49. ->paginate(10, false, [
  50. 'query' => \request()->request()
  51. ]);
  52. }
  53. /**
  54. * 商家审核
  55. * @param $data
  56. * @return bool
  57. */
  58. public function audit($data)
  59. {
  60. if ($data['is_agree'] == 20 && empty($data['refuse_desc'])) {
  61. $this->error = '请输入拒绝原因';
  62. return false;
  63. }
  64. if ($data['is_agree'] == 10 && empty($data['address_id'])) {
  65. $this->error = '请选择退货地址';
  66. return false;
  67. }
  68. $this->transaction(function () use ($data) {
  69. // 拒绝申请, 标记售后单状态为已拒绝
  70. $data['is_agree'] == 20 && $data['status'] = 10;
  71. // 同意换货申请, 标记售后单状态为已完成
  72. $data['is_agree'] == 10 && $this['type']['value'] == 20 && $data['status'] = 20;
  73. // 更新退款单状态
  74. $this->allowField(true)->save($data);
  75. // 同意售后申请, 记录退货地址
  76. if ($data['is_agree'] == 10) {
  77. (new OrderRefundAddress)->add($this['order_refund_id'], $data['address_id']);
  78. }
  79. // 订单详情
  80. $order = Order::detail($this['order_id']);
  81. // 发送消息通知
  82. MessageService::send('order.refund', [
  83. 'refund' => $this, // 退款单信息
  84. 'order_no' => $order['order_no'], // 订单信息
  85. 'order_type' => OrderTypeEnum::MASTER, // 订单类型
  86. ]);
  87. });
  88. return true;
  89. }
  90. /**
  91. * 确认收货并退款
  92. * @param $data
  93. * @return bool
  94. * @throws \think\exception\DbException
  95. */
  96. public function receipt($data)
  97. {
  98. // 订单详情
  99. $order = Order::detail($this['order_id']);
  100. if ($data['refund_money'] > min($order['pay_price'], $this['order_goods']['total_pay_price'])) {
  101. $this->error = '退款金额不能大于商品实付款金额';
  102. return false;
  103. }
  104. $this->transaction(function () use ($order, $data) {
  105. // 更新售后单状态
  106. $this->allowField(true)->save([
  107. 'refund_money' => $data['refund_money'],
  108. 'is_receipt' => 1,
  109. 'status' => 20
  110. ]);
  111. // 消减用户的实际消费金额
  112. // 条件:判断订单是否已结算
  113. if ($order['is_settled'] == true) {
  114. (new UserModel)->setDecUserExpend($order['user_id'], $data['refund_money']);
  115. }
  116. // 执行原路退款
  117. (new RefundService)->execute($order, $data['refund_money']);
  118. // 发送消息通知
  119. MessageService::send('order.refund', [
  120. 'refund' => $this, // 退款单信息
  121. 'order_no' => $order['order_no'], // 订单信息
  122. 'order_type' => OrderTypeEnum::MASTER, // 订单类型
  123. ]);
  124. });
  125. return true;
  126. }
  127. }