RefundController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. //退款退货
  3. namespace hd\controllers;
  4. use biz\shop\classes\ShopExtClass;
  5. use bizHd\custom\classes\CustomClass;
  6. use bizHd\custom\classes\HdClass;
  7. use bizHd\distribution\classes\DistributionCommissionClass;
  8. use bizHd\hb\classes\HbClass;
  9. use bizHd\member\classes\MemberLevelClass;
  10. use bizHd\order\classes\OrderClass;
  11. use bizHd\refund\classes\HdNextDayRefundClass;
  12. use bizHd\refund\classes\HdRefundClass;
  13. use bizHd\refund\classes\HdRefundGoodsClass;
  14. use bizHd\refund\classes\HdRefundItemClass;
  15. use bizHd\refund\classes\MallRefundApplyClass;
  16. use bizHd\refund\services\HdRefundService;
  17. use bizHd\refund\services\MallRefundApplyService;
  18. use Yii;
  19. use common\components\util;
  20. class RefundController extends BaseController
  21. {
  22. //取消的订单退钱,特殊情况的操作:商家取消订单时,客户同时付款成功了,造成的流程冲突,需要退钱 ssh 20250429
  23. public function actionRefundAmount()
  24. {
  25. $get = Yii::$app->request->get();
  26. $id = $get['id'] ?? '';
  27. $order = OrderClass::getById($id, true);
  28. if (empty($order)) {
  29. util::fail('没有找到订单');
  30. }
  31. if ($order->mainId != $this->mainId) {
  32. util::fail('不是你的订单');
  33. }
  34. $staff = $this->shopAdmin;
  35. if ($staff->mobile != '15280215347') {
  36. util::fail('请联系管理员操作');
  37. }
  38. $shop = $this->shop;
  39. $respond = HdRefundClass::onlyRefundAmount($shop, $order);
  40. $status = $respond['status'];
  41. $msg = $respond['msg'];
  42. util::success(['status' => $status], $msg);
  43. }
  44. //列表(含无原单 orderId=0;sameDay 筛选)
  45. public function actionList()
  46. {
  47. $get = Yii::$app->request->get();
  48. $where = [];
  49. $where['mainId'] = $this->mainId;
  50. $orderSn = $get['orderSn'] ?? '';
  51. if (!empty($orderSn)) {
  52. $where['orderSn'] = $orderSn;
  53. }
  54. $customId = intval($get['customId'] ?? 0);
  55. if ($customId > 0) {
  56. $where['customId'] = $customId;
  57. }
  58. // sameDay:-1全部 1当天 0隔天/无原单
  59. $sameDay = isset($get['sameDay']) ? intval($get['sameDay']) : -1;
  60. if ($sameDay === 0 || $sameDay === 1) {
  61. $where['sameDay'] = $sameDay;
  62. }
  63. // freeOnly=1 仅无原单
  64. if (intval($get['freeOnly'] ?? 0) === 1) {
  65. $where['orderId'] = 0;
  66. }
  67. $list = HdRefundClass::getOrderList($where);
  68. util::success($list);
  69. }
  70. //退款单详情 ssh 20220427
  71. public function actionDetail()
  72. {
  73. $id = Yii::$app->request->get('id', 0);
  74. $refund = HdRefundClass::getById($id, true);
  75. HdRefundClass::valid($refund, $this->mainId);
  76. $refundSn = $refund->refundSn ?? '';
  77. $itemList = HdRefundItemClass::getItemListBySn($refundSn);
  78. $goodsList = HdRefundGoodsClass::getGoodsListBySn($refundSn);
  79. util::success(['info' => $refund, 'itemList' => $itemList, 'goodsList' => $goodsList]);
  80. }
  81. //退款 ssh 20220427
  82. public function actionCreateOrder()
  83. {
  84. $shopAdmin = $this->shopAdmin;
  85. if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) {
  86. util::fail('超管才能操作退款');
  87. }
  88. $post = Yii::$app->request->post();
  89. $id = isset($post['id']) ? $post['id'] : 0;
  90. $version = $post['version'] ?? 1;
  91. if ($version == 1) {
  92. util::fail('请升级小程序');
  93. }
  94. $refundType = $post['refundType'] ?? 1;//1退货并退款2只退款
  95. //避免重复提交
  96. util::checkRepeatCommit($id, 10);
  97. $connection = Yii::$app->db;
  98. $transaction = $connection->beginTransaction();
  99. try {
  100. $order = OrderClass::getById($id, true);
  101. OrderClass::valid($order, $this->mainId);
  102. if ($order->payStatus == 0) {
  103. util::fail('订单没有付款');
  104. }
  105. if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) {
  106. util::fail("待付款订单无法退款");
  107. }
  108. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  109. util::fail("取消订单不能发起退款");
  110. }
  111. if ($order->fromType == 4) {
  112. util::fail('请在美团APP发起退货退款');
  113. }
  114. if ($order->debt != 1 && $order->addTime < '2023-10-17 00:00:00') {
  115. util::fail('2023年10月17日前订单无法申请退款');
  116. }
  117. if ($order->status != 4) {
  118. //util::fail('订单完成了才能售后');
  119. }
  120. $addTime = $order->addTime ?? '';
  121. $current = time();
  122. if ($order->debt != 1 && (strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  123. util::fail('历史订单,不能再发起退款');
  124. }
  125. if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
  126. util::fail('历史订单,不能发起退款哦');
  127. }
  128. if ($refundType == 1) {
  129. //商品花材结构 property 0花束 1花材
  130. //[{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎',property:1}]
  131. $productJson = $post['product'] ?? '';
  132. if (empty($productJson)) {
  133. util::fail('请选择商品');
  134. }
  135. $productList = json_decode($productJson, true);
  136. if (!is_array($productList)) {
  137. util::fail('请选择商品哦');
  138. }
  139. $post['product'] = $productList;
  140. }
  141. $post['price'] = $post['price'] ?? 0;
  142. if ($post['price'] <= 0) {
  143. util::fail("退款金额不能小于0");
  144. }
  145. if (bccomp($post['price'], $order->orderPrice, 2) == 1) {
  146. util::fail("退款金额超过订单金额");
  147. }
  148. $post['shopId'] = $this->shopId;
  149. $post['sjId'] = $this->sjId;
  150. $post['shopAdminId'] = $this->shopAdminId;
  151. $post['mainId'] = $this->mainId;
  152. $shopAdmin = $this->shopAdmin;
  153. $adminName = $shopAdmin['name'] ?? '';
  154. $post['shopAdminName'] = $adminName;
  155. $main = $this->main;
  156. // 非付款日 / 已结清等:强制隔天售后写入(sameDay=0)
  157. if (HdNextDayRefundClass::mustUseNextDay($order, $main)) {
  158. $post['sameDay'] = HdRefundClass::SAME_DAY_NO;
  159. } else {
  160. $ret = OrderClass::ifShPay($order, $main, $post['price']);
  161. if (($ret['shAddPay'] ?? 0) == 1) {
  162. $post['sameDay'] = HdRefundClass::SAME_DAY_NO;
  163. }
  164. }
  165. $refund = HdRefundService::addRefund($post, $order);
  166. $sameDay = intval($refund->sameDay ?? HdRefundClass::SAME_DAY_YES);
  167. // 当天售后:累计消费在此回退;隔天已在 applyHdAmountAndFund 处理
  168. if ($sameDay === HdRefundClass::SAME_DAY_YES) {
  169. $custom = CustomClass::getLockById($order->customId);
  170. if (!empty($custom)) {
  171. $custom->buyAmount = bcsub($custom->buyAmount, $post['price'], 2);
  172. $customName = $custom->name ?? $order->customName ?? '';
  173. $orderSnText = $order->orderSn ?? '';
  174. $shopAdminName = $this->shopAdminName;
  175. $eventPrefix = $customName;
  176. if ($eventPrefix === '' && $orderSnText !== '') {
  177. $eventPrefix = '客户';
  178. }
  179. $custom->eventRemark = $eventPrefix . '售后' . $orderSnText;
  180. $custom->relateId = $order->id;
  181. $custom->relateType = 1;
  182. $custom->staffId = $this->shopAdminId;
  183. $custom->staffName = $shopAdminName;
  184. $custom->mainId = $this->mainId;
  185. $custom->save();
  186. $hd = HdClass::getLockById($custom->hdId);
  187. if (!empty($hd)) {
  188. $hd->expendAmount = bcsub($hd->expendAmount, $post['price'], 2);
  189. $hd->save();
  190. }
  191. }
  192. }
  193. //红包退款
  194. $refundHb = $post['refundHb'] ?? 0;
  195. $hbId = $order->hbId;
  196. if ($refundHb == 1 && $hbId > 0) {
  197. $updatedOrder = OrderClass::getById($id, true, 'id, actPrice, hbId, hbAmount');
  198. if ($updatedOrder->actPrice <= 0.00) {
  199. hbClass::hbBack($updatedOrder);
  200. }
  201. }
  202. $transaction->commit();
  203. DistributionCommissionClass::tryCalcDistributionAfterPay((int)$id);
  204. $shopId = $this->shopId ?? 0;
  205. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  206. ShopExtClass::orderCancelRemind($shopExt, $order);
  207. $refund = HdRefundClass::getById($refund->id, true);
  208. $payload = HdNextDayRefundClass::buildFundResultPayload($refund);
  209. // 需选手选资金落地时返回详情,前端跳转 forwardResult
  210. if (!empty($payload['needFundAction'])) {
  211. util::success($payload);
  212. }
  213. util::success($payload);
  214. } catch (\Exception $exception) {
  215. $transaction->rollBack();
  216. Yii::info("出错了:" . $exception->getMessage());
  217. util::fail($exception->getMessage() ?: '操作失败');
  218. }
  219. }
  220. /**
  221. * 无原单退款(工作台「退款」)
  222. */
  223. public function actionCreateFreeRefund()
  224. {
  225. $shopAdmin = $this->shopAdmin;
  226. if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) {
  227. util::fail('超管才能操作退款');
  228. }
  229. $post = Yii::$app->request->post();
  230. $customId = intval($post['customId'] ?? 0);
  231. util::checkRepeatCommit('hd_free_refund_' . $this->mainId . '_' . $customId, 5);
  232. $connection = Yii::$app->db;
  233. $transaction = $connection->beginTransaction();
  234. try {
  235. $returnWay = intval($post['returnWay'] ?? -1);
  236. if ($returnWay === 1) {
  237. $refundType = HdRefundClass::REFUND_TYPE_MONEY_GOOD;
  238. } elseif ($returnWay === 0) {
  239. $refundType = HdRefundClass::REFUND_TYPE_MONEY;
  240. } else {
  241. $refundType = intval($post['refundType'] ?? HdRefundClass::REFUND_TYPE_MONEY_GOOD);
  242. }
  243. $post['refundType'] = $refundType;
  244. if ($refundType == HdRefundClass::REFUND_TYPE_MONEY_GOOD) {
  245. $productJson = $post['product'] ?? '';
  246. if (empty($productJson)) {
  247. util::fail('请选择花材');
  248. }
  249. $productList = is_array($productJson) ? $productJson : json_decode($productJson, true);
  250. if (!is_array($productList) || empty($productList)) {
  251. util::fail('请选择花材哦');
  252. }
  253. $post['product'] = $productList;
  254. } else {
  255. $productJson = $post['product'] ?? '';
  256. if (!empty($productJson)) {
  257. $productList = is_array($productJson) ? $productJson : json_decode($productJson, true);
  258. $post['product'] = is_array($productList) ? $productList : [];
  259. } else {
  260. $post['product'] = [];
  261. }
  262. }
  263. $post['price'] = $post['price'] ?? 0;
  264. if ($post['price'] <= 0) {
  265. util::fail('退款金额不能小于0');
  266. }
  267. $post['shopId'] = $this->shopId;
  268. $post['sjId'] = $this->sjId;
  269. $post['shopAdminId'] = $this->shopAdminId;
  270. $post['mainId'] = $this->mainId;
  271. $post['shopAdminName'] = $shopAdmin['name'] ?? '';
  272. $result = HdRefundService::createFreeRefund($post);
  273. $transaction->commit();
  274. util::success($result);
  275. } catch (\Exception $exception) {
  276. if ($transaction->isActive) {
  277. $transaction->rollBack();
  278. }
  279. Yii::info('无原单退款出错:' . $exception->getMessage());
  280. util::fail($exception->getMessage() ?: '操作失败');
  281. }
  282. }
  283. /**
  284. * 确认已线下原路退回
  285. */
  286. public function actionMarkHasReturn()
  287. {
  288. $post = Yii::$app->request->post();
  289. $id = intval($post['id'] ?? 0);
  290. $refund = HdRefundClass::getById($id, true);
  291. HdRefundClass::valid($refund, $this->mainId);
  292. $connection = Yii::$app->db;
  293. $transaction = $connection->beginTransaction();
  294. try {
  295. $refund = HdNextDayRefundClass::markHasReturn($refund);
  296. $transaction->commit();
  297. util::success(HdNextDayRefundClass::buildFundResultPayload($refund));
  298. } catch (\Exception $exception) {
  299. if ($transaction->isActive) {
  300. $transaction->rollBack();
  301. }
  302. util::fail($exception->getMessage() ?: '操作失败');
  303. }
  304. }
  305. /**
  306. * 不想线下转账:返充到客户余额
  307. */
  308. public function actionReturnBalance()
  309. {
  310. $post = Yii::$app->request->post();
  311. $id = intval($post['id'] ?? 0);
  312. $refund = HdRefundClass::getById($id, true);
  313. HdRefundClass::valid($refund, $this->mainId);
  314. $connection = Yii::$app->db;
  315. $transaction = $connection->beginTransaction();
  316. try {
  317. $order = null;
  318. $orderId = intval($refund->orderId ?? 0);
  319. if ($orderId > 0) {
  320. $order = OrderClass::getById($orderId, true);
  321. }
  322. $ret = HdNextDayRefundClass::applyReturnBalance($refund, $order);
  323. $transaction->commit();
  324. $payload = HdNextDayRefundClass::buildFundResultPayload($ret['refund']);
  325. $payload['customBalance'] = $ret['customBalance'];
  326. util::success($payload);
  327. } catch (\Exception $exception) {
  328. if ($transaction->isActive) {
  329. $transaction->rollBack();
  330. }
  331. util::fail($exception->getMessage() ?: '操作失败');
  332. }
  333. }
  334. public function actionAllRefund()
  335. {
  336. util::fail('功能开发中');
  337. }
  338. /**
  339. * 商城售后申请列表(商家端)
  340. */
  341. public function actionMallApplyList()
  342. {
  343. $get = Yii::$app->request->get();
  344. $where = ['mainId' => $this->mainId, 'shopId' => $this->shopId];
  345. $status = $get['status'] ?? '';
  346. if ($status !== '' && $status !== null && $status !== 'all') {
  347. $where['status'] = (int)$status;
  348. }
  349. $orderSn = trim((string)($get['orderSn'] ?? ''));
  350. if ($orderSn !== '') {
  351. $where['orderSn'] = $orderSn;
  352. }
  353. $list = MallRefundApplyClass::getApplyList($where);
  354. if (!empty($list['list'])) {
  355. foreach ($list['list'] as &$row) {
  356. $row['statusText'] = MallRefundApplyClass::statusText($row['status'] ?? 0);
  357. }
  358. unset($row);
  359. }
  360. util::success($list);
  361. }
  362. /**
  363. * 商城售后申请详情(商家端)
  364. */
  365. public function actionMallApplyDetail()
  366. {
  367. $id = (int)Yii::$app->request->get('id', 0);
  368. $apply = MallRefundApplyClass::getById($id, true);
  369. MallRefundApplyClass::valid($apply, $this->mainId);
  370. $data = $apply->toArray();
  371. $data['statusText'] = MallRefundApplyClass::statusText($apply->status);
  372. $data['productList'] = [];
  373. if (!empty($data['product'])) {
  374. $data['productList'] = json_decode($data['product'], true) ?: [];
  375. }
  376. $order = OrderClass::getById((int)$apply->orderId);
  377. $data['orderInfo'] = $order ?: [];
  378. util::success($data);
  379. }
  380. /**
  381. * 商城售后申请审核通过:执行真实退款
  382. */
  383. public function actionMallApplyPass()
  384. {
  385. $shopAdmin = $this->shopAdmin;
  386. if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) {
  387. util::fail('超管才能审核售后');
  388. }
  389. $post = Yii::$app->request->post();
  390. $id = (int)($post['id'] ?? 0);
  391. util::checkRepeatCommit('mall_apply_pass_' . $id, 10);
  392. $apply = MallRefundApplyClass::getById($id, true);
  393. MallRefundApplyClass::valid($apply, $this->mainId);
  394. $connection = Yii::$app->db;
  395. $transaction = $connection->beginTransaction();
  396. try {
  397. $order = OrderClass::getById((int)$apply->orderId, true);
  398. OrderClass::valid($order, $this->mainId);
  399. $admin = [
  400. 'id' => (int)$this->shopAdminId,
  401. 'name' => $this->shopAdminName ?? ($shopAdmin['name'] ?? ''),
  402. 'shopId' => (int)$this->shopId,
  403. 'sjId' => (int)$this->sjId,
  404. 'mainId' => (int)$this->mainId,
  405. ];
  406. MallRefundApplyService::approve($apply, $order, $admin);
  407. $orderId = (int)$apply->orderId;
  408. $transaction->commit();
  409. // TODO 事务提交后再重算分佣(与即时售后 create-order 一致,避免嵌套事务)
  410. DistributionCommissionClass::tryCalcDistributionAfterPay($orderId);
  411. util::complete('审核通过,退款已处理');
  412. } catch (\Exception $e) {
  413. $transaction->rollBack();
  414. Yii::info('商城售后审核通过失败:' . $e->getMessage());
  415. util::fail($e->getMessage() ?: '审核失败');
  416. }
  417. }
  418. /**
  419. * 商城售后申请驳回
  420. */
  421. public function actionMallApplyReject()
  422. {
  423. $shopAdmin = $this->shopAdmin;
  424. if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) {
  425. util::fail('超管才能审核售后');
  426. }
  427. $post = Yii::$app->request->post();
  428. $id = (int)($post['id'] ?? 0);
  429. $reason = $post['rejectReason'] ?? ($post['reason'] ?? '');
  430. $apply = MallRefundApplyClass::getById($id, true);
  431. MallRefundApplyClass::valid($apply, $this->mainId);
  432. $connection = Yii::$app->db;
  433. $transaction = $connection->beginTransaction();
  434. try {
  435. $admin = [
  436. 'id' => (int)$this->shopAdminId,
  437. 'name' => $this->shopAdminName ?? ($shopAdmin['name'] ?? ''),
  438. ];
  439. MallRefundApplyService::reject($apply, $reason, $admin);
  440. $transaction->commit();
  441. util::complete('已驳回');
  442. } catch (\Exception $e) {
  443. $transaction->rollBack();
  444. Yii::info('商城售后驳回失败:' . $e->getMessage());
  445. util::fail($e->getMessage() ?: '驳回失败');
  446. }
  447. }
  448. }