RefundController.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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. //补充原单信息,供前端判断该售后是否要走"转冲销单"(非当天单/欠款已结清),
  548. //以及转冲销单弹框里只读展示原单支付渠道。ssh 冲销单功能
  549. $refund['orderInfo'] = [
  550. 'id' => $order->id,
  551. 'payTime' => $order->payTime,
  552. 'debtPrice' => $order->debtPrice,
  553. 'remainDebtPrice' => $order->remainDebtPrice,
  554. 'actPrice' => $order->actPrice,
  555. 'forwardPrice' => $order->forwardPrice,
  556. 'payWay' => $order->payWay,
  557. ];
  558. if (!empty($imgString)) {
  559. $imgArr = json_decode($imgString, true);
  560. $smallImgList = [];
  561. $bigImgList = [];
  562. if (!empty($imgArr)) {
  563. foreach ($imgArr as $image) {
  564. $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  565. $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  566. $smallImgList[] = $smallImg;
  567. $bigImgList[] = $bigImg;
  568. }
  569. }
  570. $refund['imgList'] = $imgArr;
  571. $refund['smallImgList'] = $smallImgList;
  572. $refund['bigImgList'] = $bigImgList;
  573. }
  574. $orderSn = $refund['orderSn'] ?? '';
  575. $itemList = RefundOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  576. $staff = $this->shopAdmin;
  577. $staffId = $this->shopAdminId;
  578. $notify = NotifyClass::getByCondition(['staffId' => $staffId, 'type' => 0, 'targetId' => $id], true);
  579. if (!empty($notify)) {
  580. if ($notify->read == 0) {
  581. $notify->read = 1;
  582. $notify->save();
  583. $notifyNum = $staff->notifyNum;
  584. if ($notifyNum > 0) {
  585. $notifyNum--;
  586. $staff->notifyNum = $notifyNum;
  587. $staff->save();
  588. }
  589. }
  590. }
  591. $itemRefundOption = dict::getDict('itemRefundOption');
  592. util::success(['info' => $refund, 'itemList' => $itemList, 'itemRefundOption' => $itemRefundOption]);
  593. }
  594. //退款 lqh 2021.6.25
  595. public function actionCreateOrder()
  596. {
  597. $post = Yii::$app->request->post();
  598. $shopAdmin = $this->shopAdmin;
  599. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  600. util::fail('超管才能操作退款');
  601. }
  602. //多处要同步修改ghs_refund ssh 20250331
  603. if (getenv('YII_ENV') == 'production') {
  604. //小向花卉售后权限
  605. if ($this->mainId == 23390) {
  606. if (!in_array($this->adminId, [23960,77951,4])) {
  607. util::fail('你不能售后哈。。');
  608. }
  609. //小向花卉售后必须备注
  610. $remark = $post['remark'] ?? '';
  611. if (empty($remark)) {
  612. util::fail('请填写备注007');
  613. }
  614. }
  615. //国恋只有指定人有退款权限
  616. if ($this->mainId == 7704) {
  617. if (in_array($this->shopAdminId, [133727, 133545]) == false) {
  618. util::fail('无法操作。。');
  619. }
  620. }
  621. //小齐出车指定人可以售后
  622. if ($this->mainId == 42680) {
  623. if (!in_array($this->adminId, [40144])) {
  624. util::fail('你不能售后哦。。');
  625. }
  626. }
  627. }
  628. $id = isset($post['id']) ? $post['id'] : 0;
  629. $refundType = $post['refundType'] ?? 1;
  630. //避免网络延迟,暂时重复申请
  631. $cacheKey = 'ghs_order_refund_' . $id;
  632. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  633. if (!empty($has)) {
  634. util::fail('请5秒之后再提交');
  635. }
  636. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  637. try {
  638. $respond = util::runWithDbConcurrencyRetry(function () use ($post, $id, $refundType) {
  639. $connection = Yii::$app->db;
  640. $transaction = $connection->beginTransaction();
  641. try {
  642. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  643. //花材列表结构
  644. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
  645. $productJson = $post['product'] ?? '';
  646. if (empty($productJson)) {
  647. util::fail('请选择花材');
  648. }
  649. $productList = json_decode($productJson, true);
  650. if (!is_array($productList)) {
  651. util::fail('请选择花材哦');
  652. }
  653. $post['product'] = $productList;
  654. } else {
  655. //仅退款花材直接设置为空
  656. $post['product'] = [];
  657. }
  658. $post['price'] = $post['price'] ?? 0;
  659. if ($post['price'] <= 0) {
  660. util::fail("退款金额不能小于0");
  661. }
  662. $post['shopId'] = $this->shopId;
  663. $post['sjId'] = $this->sjId;
  664. $post['shopAdminId'] = $this->shopAdminId;
  665. $post['mainId'] = $this->mainId;
  666. $shopAdmin = $this->shopAdmin;
  667. $adminName = $shopAdmin['name'] ?? '';
  668. $post['shopAdminName'] = $adminName;
  669. $respond = OrderService::refund($id, $post);
  670. $refundId = $respond->id;
  671. $refundInfo = RefundOrderClass::getById($refundId, true);
  672. RefundOrderService::passRefund($refundInfo);
  673. $transaction->commit();
  674. // 隔天冲销:返回海报/结果页所需字段
  675. $refundInfo = RefundOrderClass::getById($refundId, true);
  676. $order = OrderClass::getById($id, true);
  677. $custom = null;
  678. if (!empty($order)) {
  679. $custom = \bizGhs\custom\classes\CustomClass::getById(intval($order->customId ?? 0), true);
  680. }
  681. return [
  682. 'id' => $refundId,
  683. 'orderSn' => $refundInfo->orderSn ?? '',
  684. 'sameDay' => intval($refundInfo->sameDay ?? 1),
  685. 'fundType' => intval($refundInfo->fundType ?? 0),
  686. 'refundPrice' => $refundInfo->refundPrice ?? 0,
  687. 'customId' => intval($order->customId ?? 0),
  688. 'customName' => $custom->name ?? ($order->customName ?? ''),
  689. 'customBalance' => bcadd((string)($custom->balance ?? '0'), '0', 2),
  690. 'orderId' => intval($id),
  691. 'nextDayTkPrice' => $order->nextDayTkPrice ?? '0.00',
  692. ];
  693. } catch (\Exception $exception) {
  694. if ($transaction->isActive) {
  695. $transaction->rollBack();
  696. }
  697. throw $exception;
  698. }
  699. });
  700. util::success($respond);
  701. } catch (\Exception $exception) {
  702. Yii::info("退款出错了,报错信息:" . $exception->getMessage());
  703. if (util::isDbConcurrencyException($exception)) {
  704. util::fail('系统繁忙中,请稍后再试');
  705. } else {
  706. // 透出业务失败原因,便于隔天冲销排错
  707. util::fail($exception->getMessage() ?: '操作失败');
  708. }
  709. }
  710. }
  711. /**
  712. * 无原单退款(工作台「退款」)
  713. * POST: customId, price, product(json), fundType, refundType, remark
  714. * 立即写售后单 sameDay=0 并通过;无关联销售单/采购单。
  715. */
  716. public function actionCreateFreeRefund()
  717. {
  718. $post = Yii::$app->request->post();
  719. $shopAdmin = $this->shopAdmin;
  720. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  721. util::fail('超管才能操作退款');
  722. }
  723. // 与 create-order 生产环境售后权限保持一致
  724. if (getenv('YII_ENV') == 'production') {
  725. if ($this->mainId == 23390) {
  726. if (!in_array($this->adminId, [23960, 77951, 4])) {
  727. util::fail('你不能售后哈。。');
  728. }
  729. $remark = $post['remark'] ?? '';
  730. if (empty($remark)) {
  731. util::fail('请填写备注007');
  732. }
  733. }
  734. if ($this->mainId == 7704) {
  735. if (in_array($this->shopAdminId, [133727, 133545]) == false) {
  736. util::fail('无法操作。。');
  737. }
  738. }
  739. if ($this->mainId == 42680) {
  740. if (!in_array($this->adminId, [40144])) {
  741. util::fail('你不能售后哦。。');
  742. }
  743. }
  744. }
  745. $customId = intval($post['customId'] ?? 0);
  746. $cacheKey = 'ghs_free_refund_' . $this->mainId . '_' . $customId;
  747. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  748. if (!empty($has)) {
  749. util::fail('请5秒之后再提交');
  750. }
  751. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  752. try {
  753. $respond = util::runWithDbConcurrencyRetry(function () use ($post) {
  754. $connection = Yii::$app->db;
  755. $transaction = $connection->beginTransaction();
  756. try {
  757. $refundType = intval($post['refundType'] ?? RefundOrderClass::REFUND_TYPE_MONEY_GOOD);
  758. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  759. $productJson = $post['product'] ?? '';
  760. if (empty($productJson)) {
  761. util::fail('请选择花材');
  762. }
  763. $productList = is_array($productJson) ? $productJson : json_decode($productJson, true);
  764. if (!is_array($productList) || empty($productList)) {
  765. util::fail('请选择花材哦');
  766. }
  767. $post['product'] = $productList;
  768. } else {
  769. $post['product'] = [];
  770. }
  771. $post['price'] = $post['price'] ?? 0;
  772. if ($post['price'] <= 0) {
  773. util::fail('退款金额不能小于0');
  774. }
  775. $post['shopId'] = $this->shopId;
  776. $post['sjId'] = $this->sjId;
  777. $post['shopAdminId'] = $this->shopAdminId;
  778. $post['mainId'] = $this->mainId;
  779. $shopAdmin = $this->shopAdmin;
  780. $post['shopAdminName'] = $shopAdmin['name'] ?? '';
  781. $result = RefundOrderService::createFreeRefund($post);
  782. $transaction->commit();
  783. return $result;
  784. } catch (\Exception $exception) {
  785. if ($transaction->isActive) {
  786. $transaction->rollBack();
  787. }
  788. throw $exception;
  789. }
  790. });
  791. util::success($respond);
  792. } catch (\Exception $exception) {
  793. Yii::info('无原单退款出错:' . $exception->getMessage());
  794. if (util::isDbConcurrencyException($exception)) {
  795. util::fail('系统繁忙中,请稍后再试');
  796. }
  797. util::fail($exception->getMessage() ?: '操作失败');
  798. }
  799. }
  800. //修改售后原因 ssh 20250623
  801. public function actionChangeRefundOption()
  802. {
  803. $post = Yii::$app->request->post();
  804. $id = $post['id'] ?? 0;
  805. $sonId = $post['sonId'] ?? 0;
  806. $optionId = $post['optionId'] ?? 0;
  807. $refund = RefundOrderClass::getById($id, true);
  808. if (empty($refund)) {
  809. util::fail('没有找到退款信息');
  810. }
  811. if ($refund->mainId != $this->mainId) {
  812. util::fail('没有权限');
  813. }
  814. $orderSn = $refund->orderSn;
  815. $son = RefundOrderItemClass::getById($sonId, true);
  816. if (empty($son)) {
  817. util::fail('没有找到花材信息');
  818. }
  819. if ($son->orderSn != $orderSn) {
  820. util::fail('退款信息有问题');
  821. }
  822. $map = dict::getDict('itemRefundOption');
  823. $info = $map[$optionId] ?? [];
  824. $name = $info['name'] ?? '未选';
  825. $son->refundOptionId = $optionId;
  826. $son->refundOptionName = $name;
  827. $son->save();
  828. util::complete('修改成功');
  829. }
  830. }