RefundController.php 19 KB

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