RefundController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. * 顾客售后申请列表
  134. */
  135. public function actionList()
  136. {
  137. $where = ['customId' => (int)$this->customId];
  138. $status = Yii::$app->request->get('status', '');
  139. if ($status !== '' && $status !== null) {
  140. $where['status'] = (int)$status;
  141. }
  142. $list = MallRefundApplyClass::getCustomApplyList($where);
  143. if (!empty($list['list'])) {
  144. foreach ($list['list'] as &$row) {
  145. $row['statusText'] = MallRefundApplyClass::statusText($row['status'] ?? 0);
  146. }
  147. unset($row);
  148. }
  149. util::success($list);
  150. }
  151. /**
  152. * 顾客售后申请详情
  153. */
  154. public function actionDetail()
  155. {
  156. $id = (int)Yii::$app->request->get('id', 0);
  157. $apply = MallRefundApplyClass::getById($id, true);
  158. MallRefundApplyClass::validByCustom($apply, (int)$this->customId);
  159. $data = $apply->toArray();
  160. $data['statusText'] = MallRefundApplyClass::statusText($apply->status);
  161. if (!empty($data['product'])) {
  162. $data['productList'] = json_decode($data['product'], true) ?: [];
  163. }
  164. util::success($data);
  165. }
  166. /**
  167. * 顾客撤销待审核申请
  168. */
  169. public function actionCancelOrder()
  170. {
  171. $post = Yii::$app->request->post();
  172. $id = (int)($post['id'] ?? 0);
  173. $apply = MallRefundApplyClass::getById($id, true);
  174. MallRefundApplyClass::validByCustom($apply, (int)$this->customId);
  175. $connection = Yii::$app->db;
  176. $transaction = $connection->beginTransaction();
  177. try {
  178. MallRefundApplyService::cancel($apply);
  179. $transaction->commit();
  180. util::complete('已撤销申请');
  181. } catch (\Exception $e) {
  182. $transaction->rollBack();
  183. Yii::info('撤销售后申请失败:' . $e->getMessage());
  184. util::fail($e->getMessage() ?: '撤销失败');
  185. }
  186. }
  187. /**
  188. * 校验订单归属当前登录顾客
  189. * @param array|object $order
  190. */
  191. protected function assertOrderOwner($order)
  192. {
  193. $customId = is_object($order) ? (int)($order->customId ?? 0) : (int)($order['customId'] ?? 0);
  194. $userId = is_object($order) ? (int)($order->userId ?? 0) : (int)($order['userId'] ?? 0);
  195. if ($customId > 0 && (int)$this->customId > 0 && $customId === (int)$this->customId) {
  196. return;
  197. }
  198. // 兼容早期仅绑 userId 的订单
  199. if ($userId > 0 && $userId === (int)$this->userId) {
  200. return;
  201. }
  202. util::fail('不是你的订单,无法操作');
  203. }
  204. /**
  205. * 是否允许新开售后申请
  206. * @param array $order
  207. * @param array|null $apply
  208. * @return int 1可申请 0不可
  209. */
  210. protected function canApplyRefund($order, $apply)
  211. {
  212. if ((int)($order['payStatus'] ?? 0) !== 1) {
  213. return 0;
  214. }
  215. $status = (int)($order['status'] ?? 0);
  216. if (in_array($status, [HdOrderClass::ORDER_STATUS_UN_PAY, HdOrderClass::ORDER_STATUS_CANCEL, 6], true)) {
  217. return 0;
  218. }
  219. if ((int)($order['forward'] ?? 0) === 1) {
  220. return 0;
  221. }
  222. if (!empty($apply) && (int)($apply['status'] ?? -1) === MallRefundApplyClass::STATUS_PENDING) {
  223. return 0;
  224. }
  225. if (DistributionOrderClass::isDistOrderSettled((int)($order['id'] ?? 0))) {
  226. return 0;
  227. }
  228. $could = bcsub((string)($order['orderPrice'] ?? 0), (string)($order['tkPrice'] ?? 0), 2);
  229. if (bccomp($could, '0', 2) <= 0) {
  230. return 0;
  231. }
  232. return 1;
  233. }
  234. }