RefundController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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\order\services\OrderService;
  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. //列表
  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. $list = HdRefundClass::getOrderList($where);
  55. util::success($list);
  56. }
  57. //退款单详情 ssh 20220427
  58. public function actionDetail()
  59. {
  60. $id = Yii::$app->request->get('id', 0);
  61. $refund = HdRefundClass::getById($id, true);
  62. HdRefundClass::valid($refund, $this->mainId);
  63. $refundSn = $refund->refundSn ?? '';
  64. $itemList = HdRefundItemClass::getItemListBySn($refundSn);
  65. $goodsList = HdRefundGoodsClass::getGoodsListBySn($refundSn);
  66. util::success(['info' => $refund, 'itemList' => $itemList, 'goodsList' => $goodsList]);
  67. }
  68. //退款 ssh 20220427
  69. public function actionCreateOrder()
  70. {
  71. $shopAdmin = $this->shopAdmin;
  72. if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) {
  73. util::fail('超管才能操作退款');
  74. }
  75. $post = Yii::$app->request->post();
  76. $id = isset($post['id']) ? $post['id'] : 0;
  77. $version = $post['version'] ?? 1;
  78. if ($version == 1) {
  79. util::fail('请升级小程序');
  80. }
  81. $refundType = $post['refundType'] ?? 1;//1退货并退款2只退款
  82. //避免重复提交
  83. util::checkRepeatCommit($id, 10);
  84. $connection = Yii::$app->db;
  85. $transaction = $connection->beginTransaction();
  86. try {
  87. $order = OrderClass::getById($id, true);
  88. OrderClass::valid($order, $this->mainId);
  89. if ($order->payStatus == 0) {
  90. util::fail('订单没有付款');
  91. }
  92. if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) {
  93. util::fail("待付款订单无法退款");
  94. }
  95. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  96. util::fail("取消订单不能发起退款");
  97. }
  98. if ($order->fromType == 4) {
  99. util::fail('请在美团APP发起退货退款');
  100. }
  101. if ($order->debt != 1 && $order->addTime < '2023-10-17 00:00:00') {
  102. util::fail('2023年10月17日前订单无法申请退款');
  103. }
  104. if ($order->status != 4) {
  105. //util::fail('订单完成了才能售后');
  106. }
  107. $addTime = $order->addTime ?? '';
  108. $current = time();
  109. if ($order->debt != 1 && (strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  110. util::fail('历史订单,不能再发起退款');
  111. }
  112. if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
  113. util::fail('历史订单,不能发起退款哦');
  114. }
  115. if ($order->forward == 1) {
  116. util::fail('负数订单不能发起售后');
  117. }
  118. if ($refundType == 1) {
  119. //商品花材结构 property 0花束 1花材
  120. //[{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎',property:1}]
  121. $productJson = $post['product'] ?? '';
  122. if (empty($productJson)) {
  123. util::fail('请选择商品');
  124. }
  125. $productList = json_decode($productJson, true);
  126. if (!is_array($productList)) {
  127. util::fail('请选择商品哦');
  128. }
  129. $post['product'] = $productList;
  130. }
  131. $post['price'] = $post['price'] ?? 0;
  132. if ($post['price'] <= 0) {
  133. util::fail("退款金额不能小于0");
  134. }
  135. if (bccomp($post['price'], $order->orderPrice, 2) == 1) {
  136. util::fail("退款金额超过订单金额");
  137. }
  138. $post['shopId'] = $this->shopId;
  139. $post['sjId'] = $this->sjId;
  140. $post['shopAdminId'] = $this->shopAdminId;
  141. $post['mainId'] = $this->mainId;
  142. $shopAdmin = $this->shopAdmin;
  143. $adminName = $shopAdmin['name'] ?? '';
  144. $post['shopAdminName'] = $adminName;
  145. $main = $this->main;
  146. //判断是否开负数订单
  147. $ret = OrderClass::ifShPay($order, $main, $post['price']);
  148. $shAddPay = $ret['shAddPay'] ?? 0;
  149. if ($shAddPay == 1) {
  150. $customId = $order->customId ?? 0;
  151. $custom = CustomClass::getById($customId, true);
  152. if (empty($custom)) {
  153. util::fail('没有客户哈!');
  154. }
  155. //线下付款
  156. $hasPay = 1;
  157. $shopId = $order->shopId;
  158. $staffId = $this->shopAdminId;
  159. $staffName = $this->shopAdminName;
  160. $forwardStock = $refundType == 1 ? 0 : 1;
  161. $refundPrice = $post['price'] ?? 0;
  162. $hasItem = false;
  163. $hasBouquet = false;
  164. // property 0花束 1花材
  165. $orderProductList = [];
  166. foreach ($productList as $thisInfo) {
  167. $num = $thisInfo['num'] ?? 0;
  168. $unitType = $thisInfo['unitType'] ?? 0;
  169. $property = $thisInfo['property'] ?? 0;
  170. $unitPrice = $thisInfo['unitPrice'] ?? 0;
  171. $productId = $thisInfo['productId'] ?? 0;
  172. $orderProductList[] = [
  173. 'productId' => $productId,
  174. 'unitPrice' => $unitPrice,
  175. 'remark' => '',
  176. 'property' => $property,
  177. 'unitType' => $unitType,
  178. 'num' => $num,
  179. ];
  180. if ($property == 0) {
  181. $hasBouquet = true;
  182. }
  183. if ($property == 1) {
  184. $hasItem = true;
  185. }
  186. }
  187. $orderType = 0;
  188. if ($hasBouquet) {
  189. $orderType = 1;
  190. }
  191. if ($hasItem) {
  192. $orderType = 2;
  193. }
  194. if ($hasItem && $hasBouquet) {
  195. $orderType = 3;
  196. }
  197. $sjId = $order->sjId;
  198. $mainId = $order->mainId;
  199. $deadline = time() + 8640000;
  200. $hdId = $order->hdId;
  201. $hdName = $order->hdName;
  202. $customNamePy = $order->customNamePy;
  203. $customName = $order->customName;
  204. $orderData = [
  205. 'hasPay' => 1,
  206. 'payWay' => 0,
  207. 'shopId' => $shopId,
  208. 'customId' => $customId,
  209. 'customName' => $customName,
  210. 'customNamePy' => $customNamePy,
  211. 'product' => $orderProductList,
  212. 'needPrint' => 2,
  213. 'getStaffId' => $staffId,
  214. 'getStaffName' => $staffName,
  215. 'shopAdminId' => $staffId,
  216. 'shopAdminName' => $staffName,
  217. 'dealPrice' => 1,
  218. 'forward' => 1,
  219. 'forwardStock' => $forwardStock,
  220. 'modifyPrice' => $refundPrice,
  221. 'orderType' => $orderType,
  222. 'sjId' => $sjId,
  223. 'mainId' => $mainId,
  224. 'deadline' => $deadline,
  225. 'staffId' => $staffId,
  226. 'staffName' => $staffName,
  227. 'flowerNum' => 0,
  228. 'fromType' => 1,
  229. 'hdId' => $hdId,
  230. 'hdName' => $hdName,
  231. 'sendType' => 1,
  232. 'sendSide' => 2,
  233. 'labourCost' => 0,
  234. ];
  235. //走负数订单,多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
  236. $returnOrder = OrderService::createHdOrder($orderData, $custom, $hasPay);
  237. $shop = $this->shop;
  238. $staff = $this->shopAdmin;
  239. //售后开付款单,库存要变化,关系要建立,多处需要同步修改 sh_pay_change ssh
  240. OrderClass::shPayChange($returnOrder, $shop, $staff, $order);
  241. $transaction->commit();
  242. // 负数售后单:原单金额未走 addRefund,仍尝试按原单重算分佣(无变化则内部忽略)
  243. DistributionCommissionClass::tryCalcDistributionAfterPay((int)$id);
  244. $orderId = $returnOrder->id;
  245. util::success(['forward' => 1, 'orderId' => $orderId]);
  246. } else {
  247. //走正常售后
  248. HdRefundService::addRefund($post, $order);
  249. }
  250. //售后之后,累计消费、积分、成长值回退 -- 表:xhCustom -> buyAmount、integral、growth,xhHd -> expendAmount
  251. $custom = CustomClass::getLockById($order->customId);
  252. if (!empty($custom)) {
  253. $custom->buyAmount = bcsub($custom->buyAmount, $post['price'], 2);
  254. $customName = $custom->name ?? $order->customName ?? '';
  255. $orderSnText = $order->orderSn ?? '';
  256. $shopAdminName = $this->shopAdminName;
  257. $eventPrefix = $customName;
  258. if ($eventPrefix === '' && $orderSnText !== '') {
  259. $eventPrefix = '客户';
  260. }
  261. $custom->eventRemark = $eventPrefix . '售后' . $orderSnText;
  262. $custom->relateId = $order->id;
  263. $custom->relateType = 1; //关联零售订单
  264. $custom->staffId = $this->shopAdminId;
  265. $custom->staffName = $shopAdminName;
  266. $custom->mainId = $this->mainId;
  267. $custom->save();
  268. $hd = HdClass::getLockById($custom->hdId);
  269. if (!empty($hd)) {
  270. $hd->expendAmount = bcsub($hd->expendAmount, $post['price'], 2);
  271. $hd->save();
  272. }
  273. }
  274. //红包退款
  275. $refundHb = $post['refundHb'] ?? 0;
  276. $hbId = $order->hbId;
  277. if ($refundHb == 1 && $hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
  278. $updatedOrder = OrderClass::getById($id, true, 'id, actPrice, hbId, hbAmount');
  279. if ($updatedOrder->actPrice <= 0.00) {
  280. hbClass::hbBack($updatedOrder);
  281. }
  282. }
  283. $transaction->commit();
  284. // 售后落库、红包回退等均已提交后再重算分佣(与 tkPrice/actPrice 一致)
  285. DistributionCommissionClass::tryCalcDistributionAfterPay((int)$id);
  286. //制作单取消通知
  287. $shopId = $this->shopId ?? 0;
  288. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  289. ShopExtClass::orderCancelRemind($shopExt, $order);
  290. util::complete();
  291. } catch (\Exception $exception) {
  292. $transaction->rollBack();
  293. Yii::info("出错了:" . $exception->getMessage());
  294. util::fail('操作失败');
  295. }
  296. util::complete();
  297. }
  298. public function actionAllRefund()
  299. {
  300. util::fail('功能开发中');
  301. }
  302. /**
  303. * 商城售后申请列表(商家端)
  304. */
  305. public function actionMallApplyList()
  306. {
  307. $get = Yii::$app->request->get();
  308. $where = ['mainId' => $this->mainId, 'shopId' => $this->shopId];
  309. $status = $get['status'] ?? '';
  310. if ($status !== '' && $status !== null && $status !== 'all') {
  311. $where['status'] = (int)$status;
  312. }
  313. $orderSn = trim((string)($get['orderSn'] ?? ''));
  314. if ($orderSn !== '') {
  315. $where['orderSn'] = $orderSn;
  316. }
  317. $list = MallRefundApplyClass::getApplyList($where);
  318. if (!empty($list['list'])) {
  319. foreach ($list['list'] as &$row) {
  320. $row['statusText'] = MallRefundApplyClass::statusText($row['status'] ?? 0);
  321. }
  322. unset($row);
  323. }
  324. util::success($list);
  325. }
  326. /**
  327. * 商城售后申请详情(商家端)
  328. */
  329. public function actionMallApplyDetail()
  330. {
  331. $id = (int)Yii::$app->request->get('id', 0);
  332. $apply = MallRefundApplyClass::getById($id, true);
  333. MallRefundApplyClass::valid($apply, $this->mainId);
  334. $data = $apply->toArray();
  335. $data['statusText'] = MallRefundApplyClass::statusText($apply->status);
  336. $data['productList'] = [];
  337. if (!empty($data['product'])) {
  338. $data['productList'] = json_decode($data['product'], true) ?: [];
  339. }
  340. $order = OrderClass::getById((int)$apply->orderId);
  341. $data['orderInfo'] = $order ?: [];
  342. util::success($data);
  343. }
  344. /**
  345. * 商城售后申请审核通过:执行真实退款
  346. */
  347. public function actionMallApplyPass()
  348. {
  349. $shopAdmin = $this->shopAdmin;
  350. if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) {
  351. util::fail('超管才能审核售后');
  352. }
  353. $post = Yii::$app->request->post();
  354. $id = (int)($post['id'] ?? 0);
  355. util::checkRepeatCommit('mall_apply_pass_' . $id, 10);
  356. $apply = MallRefundApplyClass::getById($id, true);
  357. MallRefundApplyClass::valid($apply, $this->mainId);
  358. $connection = Yii::$app->db;
  359. $transaction = $connection->beginTransaction();
  360. try {
  361. $order = OrderClass::getById((int)$apply->orderId, true);
  362. OrderClass::valid($order, $this->mainId);
  363. $admin = [
  364. 'id' => (int)$this->shopAdminId,
  365. 'name' => $this->shopAdminName ?? ($shopAdmin['name'] ?? ''),
  366. 'shopId' => (int)$this->shopId,
  367. 'sjId' => (int)$this->sjId,
  368. 'mainId' => (int)$this->mainId,
  369. ];
  370. MallRefundApplyService::approve($apply, $order, $admin);
  371. $orderId = (int)$apply->orderId;
  372. $transaction->commit();
  373. // TODO 事务提交后再重算分佣(与即时售后 create-order 一致,避免嵌套事务)
  374. DistributionCommissionClass::tryCalcDistributionAfterPay($orderId);
  375. util::complete('审核通过,退款已处理');
  376. } catch (\Exception $e) {
  377. $transaction->rollBack();
  378. Yii::info('商城售后审核通过失败:' . $e->getMessage());
  379. util::fail($e->getMessage() ?: '审核失败');
  380. }
  381. }
  382. /**
  383. * 商城售后申请驳回
  384. */
  385. public function actionMallApplyReject()
  386. {
  387. $shopAdmin = $this->shopAdmin;
  388. if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) {
  389. util::fail('超管才能审核售后');
  390. }
  391. $post = Yii::$app->request->post();
  392. $id = (int)($post['id'] ?? 0);
  393. $reason = $post['rejectReason'] ?? ($post['reason'] ?? '');
  394. $apply = MallRefundApplyClass::getById($id, true);
  395. MallRefundApplyClass::valid($apply, $this->mainId);
  396. $connection = Yii::$app->db;
  397. $transaction = $connection->beginTransaction();
  398. try {
  399. $admin = [
  400. 'id' => (int)$this->shopAdminId,
  401. 'name' => $this->shopAdminName ?? ($shopAdmin['name'] ?? ''),
  402. ];
  403. MallRefundApplyService::reject($apply, $reason, $admin);
  404. $transaction->commit();
  405. util::complete('已驳回');
  406. } catch (\Exception $e) {
  407. $transaction->rollBack();
  408. Yii::info('商城售后驳回失败:' . $e->getMessage());
  409. util::fail($e->getMessage() ?: '驳回失败');
  410. }
  411. }
  412. }