RefundController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * 用途:商城顾客售后申请接口
  4. * 谁用:mallApp 订单详情「申请售后」页
  5. * 解决:顾客提交/查看/撤销售后申请;真正退款由商家在 hdApp 审核通过后执行
  6. */
  7. namespace mall\controllers;
  8. use bizHd\distribution\classes\DistributionOrderClass;
  9. use bizHd\order\classes\OrderClass as HdOrderClass;
  10. use bizHd\refund\classes\MallRefundApplyClass;
  11. use bizHd\refund\services\MallRefundApplyService;
  12. use bizMall\order\classes\OrderClass;
  13. use common\components\util;
  14. use Yii;
  15. class RefundController extends BaseController
  16. {
  17. /**
  18. * 申请页初始化数据:订单商品 + 可退金额 + 已有申请进度
  19. */
  20. public function actionGetRefundData()
  21. {
  22. $id = (int)Yii::$app->request->get('id', 0);
  23. $order = OrderClass::getOrderById($id);
  24. if (empty($order)) {
  25. util::fail('没有找到订单');
  26. }
  27. $this->assertOrderOwner($order);
  28. $orderPrice = (float)($order['orderPrice'] ?? 0);
  29. $tkPrice = (float)($order['tkPrice'] ?? 0);
  30. $couldRefundPrice = round(max(0, $orderPrice - $tkPrice), 2);
  31. $apply = MallRefundApplyClass::getLatestByOrderId($id);
  32. if (!empty($apply) && is_array($apply)) {
  33. $apply['statusText'] = MallRefundApplyClass::statusText($apply['status'] ?? 0);
  34. if (!empty($apply['product']) && is_string($apply['product'])) {
  35. $apply['productList'] = json_decode($apply['product'], true) ?: [];
  36. }
  37. }
  38. // 分销售后截止提示(仅提示,真正拦截在提交时)
  39. $refundDeadlineTip = '';
  40. $distOrder = DistributionOrderClass::getByCondition(['orderId' => $id]);
  41. if (!empty($distOrder)) {
  42. $finishTime = $distOrder['finishTime'] ?? '';
  43. if (!empty($finishTime) && $finishTime !== '0000-00-00 00:00:00') {
  44. $rule = \bizHd\distribution\classes\DistributionRuleClass::getRule(
  45. (int)($order['shopId'] ?? 0),
  46. (int)($order['mainId'] ?? $this->mainId)
  47. );
  48. $settleDays = max(0, (int)($rule['settleDays'] ?? 0));
  49. $deadline = date('Y-m-d H:i', strtotime($finishTime) + $settleDays * 86400);
  50. $refundDeadlineTip = "分销订单完成后{$settleDays}天内可申请售后,截止:{$deadline}";
  51. }
  52. }
  53. util::success([
  54. 'orderInfo' => $order,
  55. 'goodsInfoList' => $order['goodsInfoList'] ?? [],
  56. 'itemInfoList' => $order['itemList'] ?? [],
  57. 'couldRefundPrice' => $couldRefundPrice,
  58. 'apply' => $apply,
  59. 'refundDeadlineTip' => $refundDeadlineTip,
  60. 'canApply' => $this->canApplyRefund($order, $apply),
  61. ]);
  62. }
  63. /**
  64. * 顾客提交售后申请(仅落待审核记录)
  65. */
  66. public function actionCreateOrder()
  67. {
  68. $post = Yii::$app->request->post();
  69. $id = (int)($post['id'] ?? 0);
  70. util::checkRepeatCommit('mall_refund_' . $id, 5);
  71. $order = HdOrderClass::getById($id, true);
  72. if (empty($order)) {
  73. util::fail('没有找到订单');
  74. }
  75. $this->assertOrderOwner($order);
  76. if ((int)$order->payStatus !== 1) {
  77. util::fail('订单未付款,无法申请售后');
  78. }
  79. if ((int)$order->status === HdOrderClass::ORDER_STATUS_UN_PAY) {
  80. util::fail('待付款订单无法申请售后');
  81. }
  82. if ((int)$order->status === HdOrderClass::ORDER_STATUS_CANCEL) {
  83. util::fail('已取消订单无法申请售后');
  84. }
  85. // 前端 ORDER_STATUS 含已退款=6,全额退完后可能落此状态
  86. if ((int)$order->status === 6) {
  87. util::fail('已退款订单无法申请售后');
  88. }
  89. if ((int)($order->forward ?? 0) === 1) {
  90. util::fail('售后收入单无法申请售后');
  91. }
  92. if (MallRefundApplyClass::hasPendingByOrderId($id)) {
  93. util::fail('已有待审核的售后申请,请勿重复提交');
  94. }
  95. // 分销关联订单:完成后超过 settleDays 不可再申请
  96. DistributionOrderClass::assertRefundNotExpired(
  97. $id,
  98. (int)$order->shopId,
  99. (int)($order->mainId ?? $this->mainId)
  100. );
  101. $connection = Yii::$app->db;
  102. $transaction = $connection->beginTransaction();
  103. try {
  104. $extra = [
  105. 'userId' => (int)$this->userId,
  106. 'hdId' => (int)$this->hdId,
  107. 'customId' => (int)$this->customId,
  108. 'shopId' => (int)$order->shopId,
  109. 'mainId' => (int)($order->mainId ?? $this->mainId),
  110. ];
  111. $apply = MallRefundApplyService::createApply($post, $order, $extra);
  112. $transaction->commit();
  113. util::success([
  114. 'id' => (int)$apply->id,
  115. 'status' => (int)$apply->status,
  116. 'statusText' => MallRefundApplyClass::statusText($apply->status),
  117. ], '提交成功,请等待商家审核');
  118. } catch (\Exception $e) {
  119. $transaction->rollBack();
  120. Yii::info('商城售后申请失败:' . $e->getMessage());
  121. // util::fail 已输出时不会走到这里;其它异常统一提示
  122. if (strpos($e->getMessage(), '操作失败') === false) {
  123. util::fail($e->getMessage() ?: '申请失败');
  124. }
  125. util::fail('申请失败');
  126. }
  127. }
  128. /**
  129. * 顾客售后申请列表
  130. */
  131. public function actionList()
  132. {
  133. $where = ['customId' => (int)$this->customId];
  134. $status = Yii::$app->request->get('status', '');
  135. if ($status !== '' && $status !== null) {
  136. $where['status'] = (int)$status;
  137. }
  138. $list = MallRefundApplyClass::getCustomApplyList($where);
  139. if (!empty($list['list'])) {
  140. foreach ($list['list'] as &$row) {
  141. $row['statusText'] = MallRefundApplyClass::statusText($row['status'] ?? 0);
  142. }
  143. unset($row);
  144. }
  145. util::success($list);
  146. }
  147. /**
  148. * 顾客售后申请详情
  149. */
  150. public function actionDetail()
  151. {
  152. $id = (int)Yii::$app->request->get('id', 0);
  153. $apply = MallRefundApplyClass::getById($id, true);
  154. MallRefundApplyClass::validByCustom($apply, (int)$this->customId);
  155. $data = $apply->toArray();
  156. $data['statusText'] = MallRefundApplyClass::statusText($apply->status);
  157. if (!empty($data['product'])) {
  158. $data['productList'] = json_decode($data['product'], true) ?: [];
  159. }
  160. util::success($data);
  161. }
  162. /**
  163. * 顾客撤销待审核申请
  164. */
  165. public function actionCancelOrder()
  166. {
  167. $post = Yii::$app->request->post();
  168. $id = (int)($post['id'] ?? 0);
  169. $apply = MallRefundApplyClass::getById($id, true);
  170. MallRefundApplyClass::validByCustom($apply, (int)$this->customId);
  171. $connection = Yii::$app->db;
  172. $transaction = $connection->beginTransaction();
  173. try {
  174. MallRefundApplyService::cancel($apply);
  175. $transaction->commit();
  176. util::complete('已撤销申请');
  177. } catch (\Exception $e) {
  178. $transaction->rollBack();
  179. Yii::info('撤销售后申请失败:' . $e->getMessage());
  180. util::fail($e->getMessage() ?: '撤销失败');
  181. }
  182. }
  183. /**
  184. * 校验订单归属当前登录顾客
  185. * @param array|object $order
  186. */
  187. protected function assertOrderOwner($order)
  188. {
  189. $customId = is_object($order) ? (int)($order->customId ?? 0) : (int)($order['customId'] ?? 0);
  190. $userId = is_object($order) ? (int)($order->userId ?? 0) : (int)($order['userId'] ?? 0);
  191. if ($customId > 0 && (int)$this->customId > 0 && $customId === (int)$this->customId) {
  192. return;
  193. }
  194. // 兼容早期仅绑 userId 的订单
  195. if ($userId > 0 && $userId === (int)$this->userId) {
  196. return;
  197. }
  198. util::fail('不是你的订单,无法操作');
  199. }
  200. /**
  201. * 是否允许新开售后申请
  202. * @param array $order
  203. * @param array|null $apply
  204. * @return int 1可申请 0不可
  205. */
  206. protected function canApplyRefund($order, $apply)
  207. {
  208. if ((int)($order['payStatus'] ?? 0) !== 1) {
  209. return 0;
  210. }
  211. $status = (int)($order['status'] ?? 0);
  212. if (in_array($status, [HdOrderClass::ORDER_STATUS_UN_PAY, HdOrderClass::ORDER_STATUS_CANCEL, 6], true)) {
  213. return 0;
  214. }
  215. if ((int)($order['forward'] ?? 0) === 1) {
  216. return 0;
  217. }
  218. if (!empty($apply) && (int)($apply['status'] ?? -1) === MallRefundApplyClass::STATUS_PENDING) {
  219. return 0;
  220. }
  221. $could = bcsub((string)($order['orderPrice'] ?? 0), (string)($order['tkPrice'] ?? 0), 2);
  222. if (bccomp($could, '0', 2) <= 0) {
  223. return 0;
  224. }
  225. return 1;
  226. }
  227. }