OrderRefund.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * @throws \think\exception\PDOException
  58. */
  59. public function audit($data)
  60. {
  61. if ($data['is_agree'] == 20 && empty($data['refuse_desc'])) {
  62. $this->error = '请输入拒绝原因';
  63. return false;
  64. }
  65. if ($data['is_agree'] == 10 && empty($data['address_id'])) {
  66. $this->error = '请选择退货地址';
  67. return false;
  68. }
  69. $this->startTrans();
  70. try {
  71. // 拒绝申请, 标记售后单状态为已拒绝
  72. $data['is_agree'] == 20 && $data['status'] = 10;
  73. // 同意换货申请, 标记售后单状态为已完成
  74. $data['is_agree'] == 10 && $this['type']['value'] == 20 && $data['status'] = 20;
  75. // 更新退款单状态
  76. $this->allowField(true)->save($data);
  77. // 同意售后申请, 记录退货地址
  78. if ($data['is_agree'] == 10) {
  79. $model = new OrderRefundAddress;
  80. $model->add($this['order_refund_id'], $data['address_id']);
  81. }
  82. // 订单详情
  83. $order = Order::detail($this['order_id']);
  84. // 发送模板消息
  85. (new MessageService)->refund(self::detail($this['order_refund_id']), $order['order_no'], OrderTypeEnum::MASTER);
  86. // 事务提交
  87. $this->commit();
  88. return true;
  89. } catch (\Exception $e) {
  90. $this->error = $e->getMessage();
  91. $this->rollback();
  92. return false;
  93. }
  94. }
  95. /**
  96. * 确认收货并退款
  97. * @param $data
  98. * @return bool
  99. * @throws \think\exception\DbException
  100. */
  101. public function receipt($data)
  102. {
  103. // 订单详情
  104. $order = Order::detail($this['order_id']);
  105. if ($data['refund_money'] > min($order['pay_price'], $this['order_goods']['total_pay_price'])) {
  106. $this->error = '退款金额不能大于商品实付款金额';
  107. return false;
  108. }
  109. $this->transaction(function () use ($order, $data) {
  110. // 更新售后单状态
  111. $this->allowField(true)->save([
  112. 'refund_money' => $data['refund_money'],
  113. 'is_receipt' => 1,
  114. 'status' => 20
  115. ]);
  116. // 消减用户的实际消费金额
  117. // 条件:判断订单是否已结算
  118. if ($order['is_settled'] == true) {
  119. (new UserModel)->setDecUserExpend($order['user_id'], $data['refund_money']);
  120. }
  121. // 执行原路退款
  122. (new RefundService)->execute($order, $data['refund_money']);
  123. // 发送模板消息
  124. (new MessageService)->refund(self::detail($this['order_refund_id']), $order['order_no'], OrderTypeEnum::MASTER);
  125. });
  126. return true;
  127. }
  128. }