OrderRefund.php 4.8 KB

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