RefundController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. if (DistributionOrderClass::isDistOrderSettled($id)) {
  41. $refundDeadlineTip = '该订单分销佣金已结算,无法申请售后';
  42. } else {
  43. $distOrder = DistributionOrderClass::getByCondition(['orderId' => $id]);
  44. if (!empty($distOrder)) {
  45. $finishTime = $distOrder['finishTime'] ?? '';
  46. if (!empty($finishTime) && $finishTime !== '0000-00-00 00:00:00') {
  47. $rule = \bizHd\distribution\classes\DistributionRuleClass::getRule(
  48. (int)($order['shopId'] ?? 0),
  49. (int)($order['mainId'] ?? $this->mainId)
  50. );
  51. $settleDays = max(0, (int)($rule['settleDays'] ?? 0));
  52. $deadline = date('Y-m-d H:i', strtotime($finishTime) + $settleDays * 86400);
  53. $refundDeadlineTip = "分销订单完成后{$settleDays}天内可申请售后,截止:{$deadline}";
  54. }
  55. }
  56. }
  57. util::success([
  58. 'orderInfo' => $order,
  59. 'goodsInfoList' => $order['goodsInfoList'] ?? [],
  60. 'itemInfoList' => $order['itemList'] ?? [],
  61. 'couldRefundPrice' => $couldRefundPrice,
  62. 'apply' => $apply,
  63. 'refundDeadlineTip' => $refundDeadlineTip,
  64. 'canApply' => $this->canApplyRefund($order, $apply),
  65. ]);
  66. }
  67. /**
  68. * 顾客提交售后申请(仅落待审核记录)
  69. */
  70. public function actionCreateOrder()
  71. {
  72. $post = Yii::$app->request->post();
  73. $id = (int)($post['id'] ?? 0);
  74. util::checkRepeatCommit('mall_refund_' . $id, 5);
  75. $order = HdOrderClass::getById($id, true);
  76. if (empty($order)) {
  77. util::fail('没有找到订单');
  78. }
  79. $this->assertOrderOwner($order);
  80. if ((int)$order->payStatus !== 1) {
  81. util::fail('订单未付款,无法申请售后');
  82. }
  83. if ((int)$order->status === HdOrderClass::ORDER_STATUS_UN_PAY) {
  84. util::fail('待付款订单无法申请售后');
  85. }
  86. if ((int)$order->status === HdOrderClass::ORDER_STATUS_CANCEL) {
  87. util::fail('已取消订单无法申请售后');
  88. }
  89. // 前端 ORDER_STATUS 含已退款=6,全额退完后可能落此状态
  90. if ((int)$order->status === 6) {
  91. util::fail('已退款订单无法申请售后');
  92. }
  93. if ((int)($order->forward ?? 0) === 1) {
  94. util::fail('售后收入单无法申请售后');
  95. }
  96. if (MallRefundApplyClass::hasPendingByOrderId($id)) {
  97. util::fail('已有待审核的售后申请,请勿重复提交');
  98. }
  99. // 分销关联订单:完成后超过 settleDays 不可再申请
  100. DistributionOrderClass::assertRefundNotExpired(
  101. $id,
  102. (int)$order->shopId,
  103. (int)($order->mainId ?? $this->mainId)
  104. );
  105. $connection = Yii::$app->db;
  106. $transaction = $connection->beginTransaction();
  107. try {
  108. $extra = [
  109. 'userId' => (int)$this->userId,
  110. 'hdId' => (int)$this->hdId,
  111. 'customId' => (int)$this->customId,
  112. 'shopId' => (int)$order->shopId,
  113. 'mainId' => (int)($order->mainId ?? $this->mainId),
  114. ];
  115. $apply = MallRefundApplyService::createApply($post, $order, $extra);
  116. $transaction->commit();
  117. util::success([
  118. 'id' => (int)$apply->id,
  119. 'status' => (int)$apply->status,
  120. 'statusText' => MallRefundApplyClass::statusText($apply->status),
  121. ], '提交成功,请等待商家审核');
  122. } catch (\Exception $e) {
  123. $transaction->rollBack();
  124. Yii::info('商城售后申请失败:' . $e->getMessage());
  125. // util::fail 已输出时不会走到这里;其它异常统一提示
  126. if (strpos($e->getMessage(), '操作失败') === false) {
  127. util::fail($e->getMessage() ?: '申请失败');
  128. }
  129. util::fail('申请失败');
  130. }
  131. }
  132. /**
  133. * 顾客售后申请列表(mallApp 退款售后 Tab)
  134. * status:all 全部;0/pending 处理中;1/refunded 已退款;closed 已关闭(驳回+取消)
  135. */
  136. public function actionList()
  137. {
  138. $tab = Yii::$app->request->get('status', 'all');
  139. if ($tab === '' || $tab === null) {
  140. $tab = 'all';
  141. }
  142. $list = MallRefundApplyClass::getCustomApplyList((int)$this->userId, (string)$tab);
  143. util::success($list);
  144. }
  145. /**
  146. * 顾客售后申请详情(列表「查看详情」)
  147. */
  148. public function actionDetail()
  149. {
  150. $id = (int)Yii::$app->request->get('id', 0);
  151. $apply = MallRefundApplyClass::getById($id, true);
  152. MallRefundApplyClass::validByCustom($apply, (int)$this->customId, (int)$this->userId);
  153. $data = MallRefundApplyClass::formatCustomDetail($apply->toArray());
  154. util::success($data);
  155. }
  156. /**
  157. * 顾客撤销待审核申请
  158. */
  159. public function actionCancelOrder()
  160. {
  161. $post = Yii::$app->request->post();
  162. $id = (int)($post['id'] ?? 0);
  163. $apply = MallRefundApplyClass::getById($id, true);
  164. MallRefundApplyClass::validByCustom($apply, (int)$this->customId, (int)$this->userId);
  165. $connection = Yii::$app->db;
  166. $transaction = $connection->beginTransaction();
  167. try {
  168. MallRefundApplyService::cancel($apply);
  169. $transaction->commit();
  170. util::complete('已撤销申请');
  171. } catch (\Exception $e) {
  172. $transaction->rollBack();
  173. Yii::info('撤销售后申请失败:' . $e->getMessage());
  174. util::fail($e->getMessage() ?: '撤销失败');
  175. }
  176. }
  177. /**
  178. * 校验订单归属当前登录顾客
  179. * @param array|object $order
  180. */
  181. protected function assertOrderOwner($order)
  182. {
  183. $customId = is_object($order) ? (int)($order->customId ?? 0) : (int)($order['customId'] ?? 0);
  184. $userId = is_object($order) ? (int)($order->userId ?? 0) : (int)($order['userId'] ?? 0);
  185. if ($customId > 0 && (int)$this->customId > 0 && $customId === (int)$this->customId) {
  186. return;
  187. }
  188. // 兼容早期仅绑 userId 的订单
  189. if ($userId > 0 && $userId === (int)$this->userId) {
  190. return;
  191. }
  192. util::fail('不是你的订单,无法操作');
  193. }
  194. /**
  195. * 是否允许新开售后申请
  196. * @param array $order
  197. * @param array|null $apply
  198. * @return int 1可申请 0不可
  199. */
  200. protected function canApplyRefund($order, $apply)
  201. {
  202. if ((int)($order['payStatus'] ?? 0) !== 1) {
  203. return 0;
  204. }
  205. $status = (int)($order['status'] ?? 0);
  206. if (in_array($status, [HdOrderClass::ORDER_STATUS_UN_PAY, HdOrderClass::ORDER_STATUS_CANCEL, 6], true)) {
  207. return 0;
  208. }
  209. if ((int)($order['forward'] ?? 0) === 1) {
  210. return 0;
  211. }
  212. if (!empty($apply) && (int)($apply['status'] ?? -1) === MallRefundApplyClass::STATUS_PENDING) {
  213. return 0;
  214. }
  215. if (DistributionOrderClass::isDistOrderSettled((int)($order['id'] ?? 0))) {
  216. return 0;
  217. }
  218. $could = bcsub((string)($order['orderPrice'] ?? 0), (string)($order['tkPrice'] ?? 0), 2);
  219. if (bccomp($could, '0', 2) <= 0) {
  220. return 0;
  221. }
  222. return 1;
  223. }
  224. }