RefundController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. */
  71. public function actionCreateOrder()
  72. {
  73. $post = Yii::$app->request->post();
  74. $id = (int)($post['id'] ?? 0);
  75. util::checkRepeatCommit('mall_refund_' . $id, 5);
  76. $order = HdOrderClass::getById($id, true);
  77. if (empty($order)) {
  78. util::fail('没有找到订单');
  79. }
  80. $this->assertOrderOwner($order);
  81. if ((int)$order->payStatus !== 1) {
  82. util::fail('订单未付款,无法申请售后');
  83. }
  84. if ((int)$order->status === HdOrderClass::ORDER_STATUS_UN_PAY) {
  85. util::fail('待付款订单无法申请售后');
  86. }
  87. if ((int)$order->status === HdOrderClass::ORDER_STATUS_CANCEL) {
  88. util::fail('已取消订单无法申请售后');
  89. }
  90. // 前端 ORDER_STATUS 含已退款=6,全额退完后可能落此状态
  91. if ((int)$order->status === 6) {
  92. util::fail('已退款订单无法申请售后');
  93. }
  94. if ((int)($order->forward ?? 0) === 1) {
  95. util::fail('售后收入单无法申请售后');
  96. }
  97. if (MallRefundApplyClass::hasPendingByOrderId($id)) {
  98. util::fail('已有待审核的售后申请,请勿重复提交');
  99. }
  100. // 分销关联订单:完成后超过 settleDays 不可再申请
  101. DistributionOrderClass::assertRefundNotExpired(
  102. $id,
  103. (int)$order->shopId,
  104. (int)($order->mainId ?? $this->mainId)
  105. );
  106. $connection = Yii::$app->db;
  107. $transaction = $connection->beginTransaction();
  108. try {
  109. $extra = [
  110. 'userId' => (int)$this->userId,
  111. 'hdId' => (int)$this->hdId,
  112. 'customId' => (int)$this->customId,
  113. 'shopId' => (int)$order->shopId,
  114. 'mainId' => (int)($order->mainId ?? $this->mainId),
  115. ];
  116. $apply = MallRefundApplyService::createApply($post, $order, $extra);
  117. $transaction->commit();
  118. util::success([
  119. 'id' => (int)$apply->id,
  120. 'status' => (int)$apply->status,
  121. 'statusText' => MallRefundApplyClass::statusText($apply->status),
  122. ], '提交成功,请等待商家审核');
  123. } catch (\Exception $e) {
  124. $transaction->rollBack();
  125. Yii::info('商城售后申请失败:' . $e->getMessage());
  126. // util::fail 已输出时不会走到这里;其它异常统一提示
  127. if (strpos($e->getMessage(), '操作失败') === false) {
  128. util::fail($e->getMessage() ?: '申请失败');
  129. }
  130. util::fail('申请失败');
  131. }
  132. }
  133. /**
  134. * 顾客售后申请列表
  135. */
  136. public function actionList()
  137. {
  138. $where = ['customId' => (int)$this->customId];
  139. $status = Yii::$app->request->get('status', '');
  140. if ($status !== '' && $status !== null) {
  141. $where['status'] = (int)$status;
  142. }
  143. $list = MallRefundApplyClass::getCustomApplyList($where);
  144. if (!empty($list['list'])) {
  145. foreach ($list['list'] as &$row) {
  146. $row['statusText'] = MallRefundApplyClass::statusText($row['status'] ?? 0);
  147. }
  148. unset($row);
  149. }
  150. util::success($list);
  151. }
  152. /**
  153. * 顾客售后申请详情
  154. */
  155. public function actionDetail()
  156. {
  157. $id = (int)Yii::$app->request->get('id', 0);
  158. $apply = MallRefundApplyClass::getById($id, true);
  159. MallRefundApplyClass::validByCustom($apply, (int)$this->customId);
  160. $data = $apply->toArray();
  161. $data['statusText'] = MallRefundApplyClass::statusText($apply->status);
  162. if (!empty($data['product'])) {
  163. $data['productList'] = json_decode($data['product'], true) ?: [];
  164. }
  165. util::success($data);
  166. }
  167. /**
  168. * 顾客撤销待审核申请
  169. */
  170. public function actionCancelOrder()
  171. {
  172. $post = Yii::$app->request->post();
  173. $id = (int)($post['id'] ?? 0);
  174. $apply = MallRefundApplyClass::getById($id, true);
  175. MallRefundApplyClass::validByCustom($apply, (int)$this->customId);
  176. $connection = Yii::$app->db;
  177. $transaction = $connection->beginTransaction();
  178. try {
  179. MallRefundApplyService::cancel($apply);
  180. $transaction->commit();
  181. util::complete('已撤销申请');
  182. } catch (\Exception $e) {
  183. $transaction->rollBack();
  184. Yii::info('撤销售后申请失败:' . $e->getMessage());
  185. util::fail($e->getMessage() ?: '撤销失败');
  186. }
  187. }
  188. /**
  189. * 校验订单归属当前登录顾客
  190. * @param array|object $order
  191. */
  192. protected function assertOrderOwner($order)
  193. {
  194. $customId = is_object($order) ? (int)($order->customId ?? 0) : (int)($order['customId'] ?? 0);
  195. $userId = is_object($order) ? (int)($order->userId ?? 0) : (int)($order['userId'] ?? 0);
  196. if ($customId > 0 && (int)$this->customId > 0 && $customId === (int)$this->customId) {
  197. return;
  198. }
  199. // 兼容早期仅绑 userId 的订单
  200. if ($userId > 0 && $userId === (int)$this->userId) {
  201. return;
  202. }
  203. util::fail('不是你的订单,无法操作');
  204. }
  205. /**
  206. * 是否允许新开售后申请
  207. * @param array $order
  208. * @param array|null $apply
  209. * @return int 1可申请 0不可
  210. */
  211. protected function canApplyRefund($order, $apply)
  212. {
  213. if ((int)($order['payStatus'] ?? 0) !== 1) {
  214. return 0;
  215. }
  216. $status = (int)($order['status'] ?? 0);
  217. if (in_array($status, [HdOrderClass::ORDER_STATUS_UN_PAY, HdOrderClass::ORDER_STATUS_CANCEL, 6], true)) {
  218. return 0;
  219. }
  220. if ((int)($order['forward'] ?? 0) === 1) {
  221. return 0;
  222. }
  223. if (!empty($apply) && (int)($apply['status'] ?? -1) === MallRefundApplyClass::STATUS_PENDING) {
  224. return 0;
  225. }
  226. if (DistributionOrderClass::isDistOrderSettled((int)($order['id'] ?? 0))) {
  227. return 0;
  228. }
  229. $could = bcsub((string)($order['orderPrice'] ?? 0), (string)($order['tkPrice'] ?? 0), 2);
  230. if (bccomp($could, '0', 2) <= 0) {
  231. return 0;
  232. }
  233. return 1;
  234. }
  235. }