RefundController.php 18 KB

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