RefundController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <?php
  2. //退款退货
  3. namespace ghs\controllers;
  4. use bizGhs\notify\classes\NotifyClass;
  5. use bizGhs\order\classes\CheckOrderClass;
  6. use bizGhs\order\classes\RefundOrderItemClass;
  7. use bizGhs\order\services\RefundOrderService;
  8. use bizGhs\shop\classes\ShopClass;
  9. use bizHd\cg\classes\CgRefundClass;
  10. use bizHd\cg\classes\CgRefundItemClass;
  11. use bizHd\notice\classes\NoticeClass;
  12. use bizHd\purchase\classes\PurchaseClass;
  13. use common\components\dateUtil;
  14. use common\components\dict;
  15. use common\components\imgUtil;
  16. use Yii;
  17. use bizGhs\order\classes\RefundOrderClass;
  18. use common\components\util;
  19. use bizGhs\product\classes\ProductClass;
  20. use bizGhs\order\classes\OrderClass;
  21. use bizGhs\order\services\OrderService;
  22. use bizGhs\order\classes\StockWastageOrderClass;
  23. class RefundController extends BaseController
  24. {
  25. //取消的订单退钱,特殊情况的操作:商家取消订单时,客户同时付款成功了,造成的流程冲突,需要退钱 ssh 20250429
  26. public function actionRefundMoney()
  27. {
  28. $get = Yii::$app->request->get();
  29. $id = $get['id'] ?? '';
  30. $order = OrderClass::getById($id, true);
  31. if (empty($order)) {
  32. util::fail('没有找到订单');
  33. }
  34. if ($order->mainId != $this->mainId) {
  35. util::fail('不是你的订单');
  36. }
  37. $addTime = $order->addTime;
  38. $addTime = strtotime($addTime);
  39. $earTime = strtotime('2025-05-01 00:00:00');
  40. if ($addTime < $earTime) {
  41. util::fail('2025.5.1号前的订单,不能退款');
  42. }
  43. $shop = $this->shop;
  44. $purchaseId = $order->purchaseId ?? 0;
  45. $cg = PurchaseClass::getById($purchaseId, true);
  46. if (empty($cg)) {
  47. util::fail('没有找到订单');
  48. }
  49. $staff = $this->shopAdmin;
  50. if ($staff->mobile != '15280215347') {
  51. util::fail('请联系管理员操作');
  52. }
  53. $respond = CgRefundClass::onlyRefundMoney($shop, $cg);
  54. $status = $respond['status'];
  55. $msg = $respond['msg'];
  56. util::success(['status' => $status], $msg);
  57. }
  58. //修改金额 ssh 20240412
  59. public function actionModifyAmount()
  60. {
  61. $get = Yii::$app->request->get();
  62. $id = $get['id'] ?? 0;
  63. $amount = $get['amount'] ?? 0;
  64. $refund = RefundOrderClass::getById($id, true);
  65. RefundOrderClass::valid($refund, $this->mainId);
  66. if (is_numeric($amount) == false) {
  67. util::fail('请填写金额');
  68. }
  69. if ($refund->status != 0) {
  70. util::fail('待审核订单才能修改');
  71. }
  72. $connection = Yii::$app->db;
  73. $transaction = $connection->beginTransaction();
  74. try {
  75. $refund->refundPrice = $amount;
  76. $refund->save();
  77. $cgRefundId = $refund->cgRefundId ?? 0;
  78. $cgRefund = CgRefundClass::getById($cgRefundId, true);
  79. if (empty($cgRefund)) {
  80. util::fail('没有找到退款信息');
  81. }
  82. $cgRefund->refundPrice = $amount;
  83. $cgRefund->save();
  84. $transaction->commit();
  85. util::complete();
  86. } catch (\Exception $e) {
  87. $transaction->rollBack();
  88. Yii::info("操作失败原因:" . $e->getMessage());
  89. util::fail('操作失败');
  90. }
  91. }
  92. //修改申请原因 ssh 20240302
  93. public function actionModifyCause()
  94. {
  95. $staff = $this->shopAdmin;
  96. if (isset($staff->finance) == false || $staff->finance == 0) {
  97. util::fail('请有财务权限的人修改');
  98. }
  99. $get = Yii::$app->request->get();
  100. $id = $get['id'] ?? 0;
  101. $cause = $get['cause'] ?? 0;
  102. $refund = RefundOrderClass::getById($id, true);
  103. RefundOrderClass::valid($refund, $this->mainId);
  104. $refund->cause = $cause;
  105. $refund->save();
  106. $orderSn = $refund->orderSn ?? '';
  107. RefundOrderItemClass::updateByCondition(['orderSn' => $orderSn], ['cause' => $cause]);
  108. $cgRefundId = $refund->cgRefundId ?? 0;
  109. $cgRefund = CgRefundClass::getById($cgRefundId, true);
  110. if (empty($cgRefund)) {
  111. util::fail('没有找到退款信息');
  112. }
  113. $cgRefund->cause = $cause;
  114. $cgRefund->save();
  115. $cgOrderSn = $cgRefund->orderSn ?? '';
  116. CgRefundItemClass::updateByCondition(['orderSn' => $cgOrderSn], ['cause' => $cause]);
  117. util::complete('修改成功');
  118. }
  119. //审核通过 ssh 20230811
  120. public function actionPass()
  121. {
  122. $shopAdmin = $this->shopAdmin;
  123. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  124. util::fail('无法操作');
  125. }
  126. //多处要同步修改ghs_refund ssh 20250331
  127. if (getenv('YII_ENV') == 'production') {
  128. //小向花卉售后权限
  129. if ($this->mainId == 23390) {
  130. if (!in_array($this->adminId, [23960,77951,4])) {
  131. util::fail('你不能售后哦');
  132. }
  133. //小向花卉售后必须备注
  134. $remark = $post['remark'] ?? '';
  135. if (empty($remark)) {
  136. util::fail('请填写备注007');
  137. }
  138. }
  139. //国恋只有指定人有退款权限
  140. if ($this->mainId == 7704) {
  141. if (in_array($this->shopAdminId, [133727, 133545]) == false) {
  142. util::fail('无法操作。。');
  143. }
  144. }
  145. //小齐出车指定人可以售后
  146. if ($this->mainId == 42680) {
  147. if (!in_array($this->adminId, [40144])) {
  148. util::fail('你不能售后哦。。');
  149. }
  150. }
  151. }
  152. $get = Yii::$app->request->get();
  153. $id = $get['id'] ?? 0;
  154. $version = $get['version'] ?? 0;
  155. $confirm = $get['confirm'] ?? 0;
  156. $refund = RefundOrderClass::getById($id, true);
  157. RefundOrderClass::valid($refund, $this->mainId);
  158. $relateOrderSn = $refund->relateOrderSn ?? '';
  159. if (empty($relateOrderSn)) {
  160. util::fail('没有订单信息');
  161. }
  162. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $relateOrderSn], null, '*', null, true);
  163. $has = false;
  164. if (!empty($refundList)) {
  165. foreach ($refundList as $single) {
  166. if ($single->status == 1) {
  167. $has = true;
  168. }
  169. }
  170. }
  171. if ($has == true) {
  172. if ($version == 0) {
  173. util::fail('请先升级系统');
  174. }
  175. if ($confirm == 0) {
  176. util::success(['error' => 'notFirstApply']);
  177. }
  178. }
  179. $connection = Yii::$app->db;
  180. $transaction = $connection->beginTransaction();
  181. try {
  182. RefundOrderService::passRefund($refund);
  183. $transaction->commit();
  184. $staff = $this->shopAdmin;
  185. $staffId = $staff->id;
  186. $staffName = $staff->name ?? '';
  187. $refund->shopAdminId = $staffId;
  188. $refund->shopAdminName = $staffName;
  189. $refund->save();
  190. //审核结果通知
  191. $cgRefundId = $refund->cgRefundId ?? 0;
  192. $cgRefund = CgRefundClass::getById($cgRefundId, true);
  193. $cgShopId = $cgRefund->shopId ?? 0;
  194. $cgShop = ShopClass::getById($cgShopId, true);
  195. if (!empty($cgRefund) && !empty($cgShop)) {
  196. NoticeClass::afterSaleNotice($cgShop, $cgRefund);
  197. }
  198. util::complete('操作成功');
  199. } catch (\Exception $e) {
  200. $transaction->rollBack();
  201. Yii::info("操作失败原因:" . $e->getMessage());
  202. util::fail('操作失败');
  203. }
  204. }
  205. //驳回 ssh 20230813
  206. public function actionReject()
  207. {
  208. $shopAdmin = $this->shopAdmin;
  209. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  210. util::fail('无法操作');
  211. }
  212. //多处要同步修改ghs_refund ssh 20250331
  213. if (getenv('YII_ENV') == 'production') {
  214. //小向花卉售后权限
  215. if ($this->mainId == 23390) {
  216. if (!in_array($this->adminId, [23960,77951,4])) {
  217. util::fail('你不能售后哦。。');
  218. }
  219. //小向花卉售后必须备注
  220. $remark = $post['remark'] ?? '';
  221. if (empty($remark)) {
  222. util::fail('请填写备注007');
  223. }
  224. }
  225. //国恋只有指定人有退款权限
  226. if ($this->mainId == 7704) {
  227. if (in_array($this->shopAdminId, [133727, 133545]) == false) {
  228. util::fail('无法操作。。');
  229. }
  230. }
  231. //小齐出车指定人可以售后
  232. if ($this->mainId == 42680) {
  233. if (!in_array($this->adminId, [40144])) {
  234. util::fail('你不能售后哦。。。');
  235. }
  236. }
  237. }
  238. $post = Yii::$app->request->post();
  239. $id = $post['id'] ?? 0;
  240. $rejectReason = $post['rejectReason'] ?? '';
  241. $refund = RefundOrderClass::getById($id, true);
  242. RefundOrderClass::valid($refund, $this->mainId);
  243. $connection = Yii::$app->db;
  244. $transaction = $connection->beginTransaction();
  245. try {
  246. RefundOrderClass::rejectRefund($refund, $rejectReason);
  247. $transaction->commit();
  248. $staff = $this->shopAdmin;
  249. $staffId = $staff->id;
  250. $staffName = $staff->name ?? '';
  251. $refund->shopAdminId = $staffId;
  252. $refund->shopAdminName = $staffName;
  253. $refund->save();
  254. //审核结果通知
  255. $cgRefundId = $refund->cgRefundId ?? 0;
  256. $cgRefund = CgRefundClass::getById($cgRefundId, true);
  257. $cgShopId = $cgRefund->shopId ?? 0;
  258. $cgShop = ShopClass::getById($cgShopId, true);
  259. if (!empty($cgRefund) && !empty($cgShop)) {
  260. NoticeClass::afterSaleNotice($cgShop, $cgRefund);
  261. }
  262. util::complete('操作成功');
  263. } catch (\Exception $e) {
  264. $transaction->rollBack();
  265. Yii::info("操作失败原因:" . $e->getMessage());
  266. util::fail('操作失败');
  267. }
  268. }
  269. //售后之后减少库存 ssh 20250912
  270. public function actionReduceStock()
  271. {
  272. $shopAdmin = $this->shopAdmin;
  273. if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
  274. util::fail('管理员才能报损');
  275. }
  276. $post = Yii::$app->request->post();
  277. $refundItemId = $post['refundItemId'] ?? 0;
  278. $refundItem = RefundOrderItemClass::getById($refundItemId, true);
  279. if (empty($refundItem)) {
  280. util::fail('没有找到退货信息');
  281. }
  282. if ($refundItem->mainId != $this->mainId) {
  283. util::fail('不是您的退款信息');
  284. }
  285. $reduceProductId = $refundItem->productId ?? 0;
  286. $reduceNum = $post['reduceNum'] ?? 0;
  287. $productInfo = ProductClass::getById($reduceProductId, true);
  288. if (empty($productInfo)) {
  289. util::fail('没有找到花材');
  290. }
  291. if ($productInfo->mainId != $this->mainId) {
  292. util::fail('不是你的花材');
  293. }
  294. $unitType = $refundItem->xhUnitType ?? 0;
  295. if ($unitType == 1) {
  296. util::fail('小单位,无法减库存');
  297. }
  298. $refundNum = $refundItem->xhNum ?? 0;
  299. $hasReduceNum = $refundItem->xhReduceNum ?? 0;
  300. $remainNum = bcsub($refundNum, $hasReduceNum);
  301. if ($reduceNum > $remainNum) {
  302. util::fail('剩余可减数量 ' . $remainNum);
  303. }
  304. $stock = $productInfo->stock ?? 0;
  305. if ($reduceNum > $stock) {
  306. util::fail('库存不足了');
  307. }
  308. $orderSn = $refundItem->orderSn ?? '';
  309. $bigNum = bcsub($stock, $reduceNum);
  310. $refundItem->xhReduceNum = bcadd($refundItem->xhReduceNum, $reduceNum);
  311. $refundItem->save();
  312. $shop = $this->shop;
  313. $sjId = $shop->sjId ?? 0;
  314. $shopId = $shop->id ?? 0;
  315. $mainId = $shop->mainId ?? 0;
  316. $adminId = $this->adminId;
  317. $staff = $this->shopAdmin;
  318. $staffName = $staff->name ?? '';
  319. $params = [
  320. 'pdType' => '',
  321. 'itemInfo' => [['productId' => $reduceProductId, 'bigNum' => $bigNum, 'smallNum' => '']],
  322. 'remark' => '售后' . $orderSn . '直接减少库存',
  323. 'sjId' => $sjId,
  324. 'shopId' => $shopId,
  325. 'adminId' => $adminId,
  326. 'mainId' => $mainId,
  327. 'staffName' => $staffName,
  328. ];
  329. CheckOrderClass::opOrder($params);
  330. util::complete('操作成功');
  331. }
  332. //退款之后报损 ssh 20230409
  333. public function actionRefundWaste()
  334. {
  335. $shopAdmin = $this->shopAdmin;
  336. if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
  337. util::fail('管理员才能报损');
  338. }
  339. $post = Yii::$app->request->post();
  340. $refundItemId = $post['refundItemId'] ?? 0;
  341. $refundItem = RefundOrderItemClass::getById($refundItemId, true);
  342. if (empty($refundItem)) {
  343. util::fail('没有找到退货信息');
  344. }
  345. if ($refundItem->mainId != $this->mainId) {
  346. util::fail('不是您的退款信息');
  347. }
  348. $unitType = $refundItem->xhUnitType ?? 0;
  349. if ($unitType == 1) {
  350. util::fail('小单位,无法报损');
  351. }
  352. $wasteProductId = $refundItem->productId ?? 0;
  353. $wasteNum = $post['wasteNum'] ?? 0;
  354. unset($post['refundItemId']);
  355. unset($post['wasteNum']);
  356. unset($post['wasteProductId']);
  357. $productInfo = ProductClass::getById($wasteProductId, true);
  358. if (empty($productInfo)) {
  359. util::fail('没有找到花材');
  360. }
  361. $ptItemId = $productInfo->itemId ?? 0;
  362. $ratio = $productInfo->ratio ?? 20;
  363. $refundNum = $refundItem->xhNum ?? 0;
  364. $hasWasteNum = $refundItem->xhWasteNum ?? 0;
  365. $remainNum = bcsub($refundNum, $hasWasteNum);
  366. if ($wasteNum > $remainNum) {
  367. util::fail('剩余可报损数量 ' . $remainNum);
  368. }
  369. $productList = [['productId' => $wasteProductId, 'bigNum' => $wasteNum, 'smallNum' => 0, 'classId' => 0, 'itemId' => $ptItemId, 'ratio' => $ratio]];
  370. $post['shopId'] = $this->shopId;
  371. $post['sjId'] = $this->sjId;
  372. $post['mainId'] = $this->mainId;
  373. $post['shopAdminId'] = $this->shopAdminId;
  374. $adminName = $shopAdmin['name'] ?? '';
  375. $post['shopAdminName'] = $adminName;
  376. //属于售后报损
  377. $post['saleType'] = 1;
  378. $connection = Yii::$app->db;
  379. $transaction = $connection->beginTransaction();
  380. try {
  381. $refundItem->xhWasteNum = bcadd($refundItem->xhWasteNum, $wasteNum);
  382. $refundItem->save();
  383. $post['product'] = $productList;
  384. $return = StockWastageOrderClass::addOrder($post);
  385. $transaction->commit();
  386. util::success($return);
  387. } catch (\Exception $e) {
  388. $transaction->rollBack();
  389. Yii::info("报损失败原因:" . $e->getMessage());
  390. util::fail('报损失败');
  391. }
  392. }
  393. //列表
  394. public function actionList()
  395. {
  396. $get = Yii::$app->request->get();
  397. $where = [];
  398. $where['mainId'] = $this->mainId;
  399. $orderSn = $get['orderSn'] ?? '';
  400. if (!empty($orderSn)) {
  401. $where['relateOrderSn'] = $orderSn;
  402. }
  403. $searchTime = $get['searchTime'] ?? '';
  404. if (!empty($searchTime)) {
  405. $startTime = $get['startTime'] ?? '';
  406. $endTime = $get['endTime'] ?? '';
  407. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  408. $where['orderTime'] = ['between', [$period['startTime'], $period['endTime']]];
  409. }
  410. $refundSearchTime = $get['refundSearchTime'] ?? '';
  411. if (!empty($refundSearchTime)) {
  412. $startTime = $get['refundStartTime'] ?? '';
  413. $endTime = $get['refundEndTime'] ?? '';
  414. $period = dateUtil::formatTime($refundSearchTime, $startTime, $endTime);
  415. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  416. }
  417. $customId = $get['customId'] ?? '';
  418. if (!empty($customId)) {
  419. $where['customId'] = $customId;
  420. }
  421. $shopAdminId = $get['shopAdminId'] ?? 0;
  422. if (!empty($shopAdminId)) {
  423. $where['shopAdminId'] = $shopAdminId;
  424. }
  425. $status = $get['status'] ?? -1;
  426. if ($status > -1) {
  427. $where['status'] = $status;
  428. }
  429. $export = $get['export'] ?? 0;
  430. if ($export == 1) {
  431. if (isset($where['orderTime']) == false && isset($where['addTime']) == false) {
  432. util::fail('请选时间');
  433. }
  434. ini_set('memory_limit', '2045M');
  435. set_time_limit(0);
  436. if (isset($where['orderTime'])) {
  437. $start = $where['orderTime'][1][0];
  438. $end = $where['orderTime'][1][1];
  439. $monthTime = bcmul(31, 86400);
  440. $startArea = strtotime($start);
  441. $endArea = strtotime($end);
  442. $diff = bcsub($endArea, $startArea);
  443. if ($diff > $monthTime) {
  444. util::fail('最长可导出一个月');
  445. }
  446. }
  447. if (isset($where['addTime'])) {
  448. $start = $where['addTime'][1][0];
  449. $end = $where['addTime'][1][1];
  450. $monthTime = bcmul(31, 86400);
  451. $startArea = strtotime($start);
  452. $endArea = strtotime($end);
  453. $diff = bcsub($endArea, $startArea);
  454. if ($diff > $monthTime) {
  455. util::fail('最长可导出一个月');
  456. }
  457. }
  458. }
  459. $respond = RefundOrderClass::getOrderList($where);
  460. if ($export == 1) {
  461. RefundOrderClass::exportRefundData($respond, $this->mainId);
  462. }
  463. util::success($respond);
  464. }
  465. //主要根据花材找退款记录 ssh 20240229
  466. public function actionGetListByItem()
  467. {
  468. $get = Yii::$app->request->get();
  469. $where = [];
  470. $where['mainId'] = $this->mainId;
  471. $status = $get['status'] ?? -1;
  472. $cause = $get['cause'] ?? -1;
  473. if ($status > -1) {
  474. $where['status'] = $status;
  475. }
  476. $string = $get['ids'];
  477. $ids = json_decode($string, true);
  478. if (!empty($ids)) {
  479. $productIds = [];
  480. $itemList = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'classId' => ['in', $ids]], null, 'id', 'id');
  481. if (!empty($itemList)) {
  482. $productIds = array_keys($itemList);
  483. }
  484. if (empty($productIds)) {
  485. util::success(['list' => [], 'moreData' => 0, 'totalAmount' => 0, 'totalNum' => 0, 'totalPage' => 1]);
  486. }
  487. $where['productId'] = ['in', $productIds];
  488. }
  489. if ($cause > -1) {
  490. $where['cause'] = $cause;
  491. }
  492. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  493. $startTime = $get['startTime'] ?? '';
  494. $endTime = $get['endTime'] ?? '';
  495. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  496. $start = $period['startTime'];
  497. $end = $period['endTime'];
  498. $currentStartDate = date('Y-m-d', strtotime($start));
  499. $currentEndDate = date('Y-m-d', strtotime($end));
  500. $currentStartTime = $currentStartDate . ' 00:00:00';
  501. $currentEndTime = $currentEndDate . ' 23:59:59';
  502. $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
  503. $list = RefundOrderItemClass::getRefundItemList($where);
  504. util::success($list);
  505. }
  506. public function actionDetail()
  507. {
  508. $id = Yii::$app->request->get('id', 0);
  509. $refund = RefundOrderClass::getById($id);
  510. if (empty($refund)) {
  511. util::fail('没有找到退款信息');
  512. }
  513. if (isset($refund['mainId']) == false || $refund['mainId'] != $this->mainId) {
  514. util::fail('没有权限');
  515. }
  516. $imgString = $refund['imgList'] ?? '';
  517. $relateOrderSn = $refund['relateOrderSn'] ?? '';
  518. $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  519. if (empty($order)) {
  520. util::fail('数据出错了');
  521. }
  522. $refund['relateOrderId'] = $order->id;
  523. if (!empty($imgString)) {
  524. $imgArr = json_decode($imgString, true);
  525. $smallImgList = [];
  526. $bigImgList = [];
  527. if (!empty($imgArr)) {
  528. foreach ($imgArr as $image) {
  529. $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  530. $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  531. $smallImgList[] = $smallImg;
  532. $bigImgList[] = $bigImg;
  533. }
  534. }
  535. $refund['imgList'] = $imgArr;
  536. $refund['smallImgList'] = $smallImgList;
  537. $refund['bigImgList'] = $bigImgList;
  538. }
  539. $orderSn = $refund['orderSn'] ?? '';
  540. $itemList = RefundOrderItemClass::getOrderItemDetail($orderSn);
  541. $staff = $this->shopAdmin;
  542. $staffId = $this->shopAdminId;
  543. $notify = NotifyClass::getByCondition(['staffId' => $staffId, 'type' => 0, 'targetId' => $id], true);
  544. if (!empty($notify)) {
  545. if ($notify->read == 0) {
  546. $notify->read = 1;
  547. $notify->save();
  548. $notifyNum = $staff->notifyNum;
  549. if ($notifyNum > 0) {
  550. $notifyNum--;
  551. $staff->notifyNum = $notifyNum;
  552. $staff->save();
  553. }
  554. }
  555. }
  556. $itemRefundOption = dict::getDict('itemRefundOption');
  557. util::success(['info' => $refund, 'itemList' => $itemList, 'itemRefundOption' => $itemRefundOption]);
  558. }
  559. //退款 lqh 2021.6.25
  560. public function actionCreateOrder()
  561. {
  562. $post = Yii::$app->request->post();
  563. $shopAdmin = $this->shopAdmin;
  564. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  565. util::fail('超管才能操作退款');
  566. }
  567. //多处要同步修改ghs_refund ssh 20250331
  568. if (getenv('YII_ENV') == 'production') {
  569. //小向花卉售后权限
  570. if ($this->mainId == 23390) {
  571. if (!in_array($this->adminId, [23960,77951,4])) {
  572. util::fail('你不能售后哈。。');
  573. }
  574. //小向花卉售后必须备注
  575. $remark = $post['remark'] ?? '';
  576. if (empty($remark)) {
  577. util::fail('请填写备注007');
  578. }
  579. }
  580. //国恋只有指定人有退款权限
  581. if ($this->mainId == 7704) {
  582. if (in_array($this->shopAdminId, [133727, 133545]) == false) {
  583. util::fail('无法操作。。');
  584. }
  585. }
  586. //小齐出车指定人可以售后
  587. if ($this->mainId == 42680) {
  588. if (!in_array($this->adminId, [40144])) {
  589. util::fail('你不能售后哦。。');
  590. }
  591. }
  592. }
  593. $id = isset($post['id']) ? $post['id'] : 0;
  594. $refundType = $post['refundType'] ?? 1;
  595. //避免网络延迟,暂时重复申请
  596. $cacheKey = 'ghs_order_refund_' . $id;
  597. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  598. if (!empty($has)) {
  599. util::fail('请5秒之后再提交');
  600. }
  601. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  602. try {
  603. $respond = util::runWithDbConcurrencyRetry(function () use ($post, $id, $refundType) {
  604. $connection = Yii::$app->db;
  605. $transaction = $connection->beginTransaction();
  606. try {
  607. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  608. //花材列表结构
  609. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
  610. $productJson = $post['product'] ?? '';
  611. if (empty($productJson)) {
  612. util::fail('请选择花材');
  613. }
  614. $productList = json_decode($productJson, true);
  615. if (!is_array($productList)) {
  616. util::fail('请选择花材哦');
  617. }
  618. $post['product'] = $productList;
  619. } else {
  620. //仅退款花材直接设置为空
  621. $post['product'] = [];
  622. }
  623. $post['price'] = $post['price'] ?? 0;
  624. if ($post['price'] <= 0) {
  625. util::fail("退款金额不能小于0");
  626. }
  627. $post['shopId'] = $this->shopId;
  628. $post['sjId'] = $this->sjId;
  629. $post['shopAdminId'] = $this->shopAdminId;
  630. $post['mainId'] = $this->mainId;
  631. $shopAdmin = $this->shopAdmin;
  632. $adminName = $shopAdmin['name'] ?? '';
  633. $post['shopAdminName'] = $adminName;
  634. $respond = OrderService::refund($id, $post);
  635. $refundId = $respond->id;
  636. $refundInfo = RefundOrderClass::getById($refundId, true);
  637. RefundOrderService::passRefund($refundInfo);
  638. $transaction->commit();
  639. return $respond;
  640. } catch (\Exception $exception) {
  641. if ($transaction->isActive) {
  642. $transaction->rollBack();
  643. }
  644. throw $exception;
  645. }
  646. });
  647. util::success($respond);
  648. } catch (\Exception $exception) {
  649. Yii::info("退款出错了,报错信息:" . $exception->getMessage());
  650. if (util::isDbConcurrencyException($exception)) {
  651. util::fail('系统繁忙中,请稍后再试');
  652. } else {
  653. util::fail('操作失败');
  654. }
  655. }
  656. }
  657. //修改售后原因 ssh 20250623
  658. public function actionChangeRefundOption()
  659. {
  660. $post = Yii::$app->request->post();
  661. $id = $post['id'] ?? 0;
  662. $sonId = $post['sonId'] ?? 0;
  663. $optionId = $post['optionId'] ?? 0;
  664. $refund = RefundOrderClass::getById($id, true);
  665. if (empty($refund)) {
  666. util::fail('没有找到退款信息');
  667. }
  668. if ($refund->mainId != $this->mainId) {
  669. util::fail('没有权限');
  670. }
  671. $orderSn = $refund->orderSn;
  672. $son = RefundOrderItemClass::getById($sonId, true);
  673. if (empty($son)) {
  674. util::fail('没有找到花材信息');
  675. }
  676. if ($son->orderSn != $orderSn) {
  677. util::fail('退款信息有问题');
  678. }
  679. $map = dict::getDict('itemRefundOption');
  680. $info = $map[$optionId] ?? [];
  681. $name = $info['name'] ?? '未选';
  682. $son->refundOptionId = $optionId;
  683. $son->refundOptionName = $name;
  684. $son->save();
  685. util::complete('修改成功');
  686. }
  687. }