RefundController.php 19 KB

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