RefundController.php 30 KB

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