RefundController.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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. // 工作台「退款记录」:只查无关联销售单的退款
  428. $freeOnly = intval($get['freeOnly'] ?? 0);
  429. if ($freeOnly === 1) {
  430. $where['relateOrderSn'] = '';
  431. }
  432. $searchTime = $get['searchTime'] ?? '';
  433. if (!empty($searchTime)) {
  434. $startTime = $get['startTime'] ?? '';
  435. $endTime = $get['endTime'] ?? '';
  436. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  437. $where['orderTime'] = ['between', [$period['startTime'], $period['endTime']]];
  438. }
  439. $refundSearchTime = $get['refundSearchTime'] ?? '';
  440. // 退款记录默认近30天(按售后单创建时间)
  441. if ($freeOnly === 1 && empty($refundSearchTime) && empty($searchTime)) {
  442. $refundSearchTime = 'last30Days';
  443. }
  444. if (!empty($refundSearchTime)) {
  445. $startTime = $get['refundStartTime'] ?? ($get['startTime'] ?? '');
  446. $endTime = $get['refundEndTime'] ?? ($get['endTime'] ?? '');
  447. $period = dateUtil::formatTime($refundSearchTime, $startTime, $endTime);
  448. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  449. }
  450. $customId = $get['customId'] ?? '';
  451. if (!empty($customId)) {
  452. $where['customId'] = $customId;
  453. }
  454. $shopAdminId = $get['shopAdminId'] ?? 0;
  455. if (!empty($shopAdminId)) {
  456. $where['shopAdminId'] = $shopAdminId;
  457. }
  458. $status = $get['status'] ?? -1;
  459. if ($status > -1) {
  460. $where['status'] = $status;
  461. }
  462. $export = $get['export'] ?? 0;
  463. if ($export == 1) {
  464. if (isset($where['orderTime']) == false && isset($where['addTime']) == false) {
  465. util::fail('请选时间');
  466. }
  467. ini_set('memory_limit', '2045M');
  468. set_time_limit(0);
  469. if (isset($where['orderTime'])) {
  470. $start = $where['orderTime'][1][0];
  471. $end = $where['orderTime'][1][1];
  472. $monthTime = bcmul(31, 86400);
  473. $startArea = strtotime($start);
  474. $endArea = strtotime($end);
  475. $diff = bcsub($endArea, $startArea);
  476. if ($diff > $monthTime) {
  477. util::fail('最长可导出一个月');
  478. }
  479. }
  480. if (isset($where['addTime'])) {
  481. $start = $where['addTime'][1][0];
  482. $end = $where['addTime'][1][1];
  483. $monthTime = bcmul(31, 86400);
  484. $startArea = strtotime($start);
  485. $endArea = strtotime($end);
  486. $diff = bcsub($endArea, $startArea);
  487. if ($diff > $monthTime) {
  488. util::fail('最长可导出一个月');
  489. }
  490. }
  491. }
  492. $respond = RefundOrderClass::getOrderList($where);
  493. if ($export == 1) {
  494. RefundOrderClass::exportRefundData($respond, $this->mainId);
  495. }
  496. util::success($respond);
  497. }
  498. //主要根据花材找退款记录 ssh 20240229
  499. public function actionGetListByItem()
  500. {
  501. $get = Yii::$app->request->get();
  502. $where = [];
  503. $where['mainId'] = $this->mainId;
  504. $status = $get['status'] ?? -1;
  505. $cause = $get['cause'] ?? -1;
  506. if ($status > -1) {
  507. $where['status'] = $status;
  508. }
  509. $string = $get['ids'];
  510. $ids = json_decode($string, true);
  511. if (!empty($ids)) {
  512. $productIds = [];
  513. $itemList = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'classId' => ['in', $ids]], null, 'id', 'id');
  514. if (!empty($itemList)) {
  515. $productIds = array_keys($itemList);
  516. }
  517. if (empty($productIds)) {
  518. util::success(['list' => [], 'moreData' => 0, 'totalAmount' => 0, 'totalNum' => 0, 'totalPage' => 1]);
  519. }
  520. $where['productId'] = ['in', $productIds];
  521. }
  522. if ($cause > -1) {
  523. $where['cause'] = $cause;
  524. }
  525. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  526. $startTime = $get['startTime'] ?? '';
  527. $endTime = $get['endTime'] ?? '';
  528. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  529. $start = $period['startTime'];
  530. $end = $period['endTime'];
  531. $currentStartDate = date('Y-m-d', strtotime($start));
  532. $currentEndDate = date('Y-m-d', strtotime($end));
  533. $currentStartTime = $currentStartDate . ' 00:00:00';
  534. $currentEndTime = $currentEndDate . ' 23:59:59';
  535. $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
  536. $list = RefundOrderItemClass::getRefundItemList($where);
  537. util::success($list);
  538. }
  539. public function actionDetail()
  540. {
  541. $id = Yii::$app->request->get('id', 0);
  542. $refund = RefundOrderClass::getById($id);
  543. if (empty($refund)) {
  544. util::fail('没有找到退款信息');
  545. }
  546. if (isset($refund['mainId']) == false || $refund['mainId'] != $this->mainId) {
  547. util::fail('没有权限');
  548. }
  549. $imgString = $refund['imgList'] ?? '';
  550. $relateOrderSn = $refund['relateOrderSn'] ?? '';
  551. // 无原单退款:不强制关联销售单
  552. $order = null;
  553. if ($relateOrderSn !== '' && $relateOrderSn !== null) {
  554. $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  555. }
  556. if (!empty($order)) {
  557. $refund['relateOrderId'] = $order->id;
  558. //补充原单信息,供前端判断该售后是否要走"转冲销单"(非当天单/欠款已结清),
  559. //以及转冲销单弹框里只读展示原单支付渠道。ssh 冲销单功能
  560. $refund['orderInfo'] = [
  561. 'id' => $order->id,
  562. 'payTime' => $order->payTime,
  563. 'debtPrice' => $order->debtPrice,
  564. 'remainDebtPrice' => $order->remainDebtPrice,
  565. 'actPrice' => $order->actPrice,
  566. 'forwardPrice' => $order->forwardPrice ?? 0,
  567. 'payWay' => $order->payWay,
  568. ];
  569. $refund['isFreeRefund'] = 0;
  570. } else {
  571. $refund['relateOrderId'] = 0;
  572. $refund['orderInfo'] = null;
  573. $refund['isFreeRefund'] = 1;
  574. }
  575. if (!empty($imgString)) {
  576. $imgArr = json_decode($imgString, true);
  577. $smallImgList = [];
  578. $bigImgList = [];
  579. if (!empty($imgArr)) {
  580. foreach ($imgArr as $image) {
  581. $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  582. $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  583. $smallImgList[] = $smallImg;
  584. $bigImgList[] = $bigImg;
  585. }
  586. }
  587. $refund['imgList'] = $imgArr;
  588. $refund['smallImgList'] = $smallImgList;
  589. $refund['bigImgList'] = $bigImgList;
  590. }
  591. $orderSn = $refund['orderSn'] ?? '';
  592. $itemList = RefundOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  593. $staff = $this->shopAdmin;
  594. $staffId = $this->shopAdminId;
  595. $notify = NotifyClass::getByCondition(['staffId' => $staffId, 'type' => 0, 'targetId' => $id], true);
  596. if (!empty($notify)) {
  597. if ($notify->read == 0) {
  598. $notify->read = 1;
  599. $notify->save();
  600. $notifyNum = $staff->notifyNum;
  601. if ($notifyNum > 0) {
  602. $notifyNum--;
  603. $staff->notifyNum = $notifyNum;
  604. $staff->save();
  605. }
  606. }
  607. }
  608. $itemRefundOption = dict::getDict('itemRefundOption');
  609. util::success(['info' => $refund, 'itemList' => $itemList, 'itemRefundOption' => $itemRefundOption]);
  610. }
  611. //退款 lqh 2021.6.25
  612. public function actionCreateOrder()
  613. {
  614. $post = Yii::$app->request->post();
  615. $shopAdmin = $this->shopAdmin;
  616. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  617. util::fail('超管才能操作退款');
  618. }
  619. //多处要同步修改ghs_refund ssh 20250331
  620. if (getenv('YII_ENV') == 'production') {
  621. //小向花卉售后权限
  622. if ($this->mainId == 23390) {
  623. if (!in_array($this->adminId, [23960,77951,4])) {
  624. util::fail('你不能售后哈。。');
  625. }
  626. //小向花卉售后必须备注
  627. $remark = $post['remark'] ?? '';
  628. if (empty($remark)) {
  629. util::fail('请填写备注007');
  630. }
  631. }
  632. //国恋只有指定人有退款权限
  633. if ($this->mainId == 7704) {
  634. if (in_array($this->shopAdminId, [133727, 133545]) == false) {
  635. util::fail('无法操作。。');
  636. }
  637. }
  638. //小齐出车指定人可以售后
  639. if ($this->mainId == 42680) {
  640. if (!in_array($this->adminId, [40144])) {
  641. util::fail('你不能售后哦。。');
  642. }
  643. }
  644. }
  645. $id = isset($post['id']) ? $post['id'] : 0;
  646. $refundType = $post['refundType'] ?? 1;
  647. //避免网络延迟,暂时重复申请
  648. $cacheKey = 'ghs_order_refund_' . $id;
  649. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  650. if (!empty($has)) {
  651. util::fail('请5秒之后再提交');
  652. }
  653. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  654. try {
  655. $respond = util::runWithDbConcurrencyRetry(function () use ($post, $id, $refundType) {
  656. $connection = Yii::$app->db;
  657. $transaction = $connection->beginTransaction();
  658. try {
  659. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  660. //花材列表结构
  661. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
  662. $productJson = $post['product'] ?? '';
  663. if (empty($productJson)) {
  664. util::fail('请选择花材');
  665. }
  666. $productList = json_decode($productJson, true);
  667. if (!is_array($productList)) {
  668. util::fail('请选择花材哦');
  669. }
  670. $post['product'] = $productList;
  671. } else {
  672. //仅退款花材直接设置为空
  673. $post['product'] = [];
  674. }
  675. $post['price'] = $post['price'] ?? 0;
  676. if ($post['price'] <= 0) {
  677. util::fail("退款金额不能小于0");
  678. }
  679. $post['shopId'] = $this->shopId;
  680. $post['sjId'] = $this->sjId;
  681. $post['shopAdminId'] = $this->shopAdminId;
  682. $post['mainId'] = $this->mainId;
  683. $shopAdmin = $this->shopAdmin;
  684. $adminName = $shopAdmin['name'] ?? '';
  685. $post['shopAdminName'] = $adminName;
  686. $respond = OrderService::refund($id, $post);
  687. $refundId = $respond->id;
  688. $refundInfo = RefundOrderClass::getById($refundId, true);
  689. RefundOrderService::passRefund($refundInfo);
  690. $transaction->commit();
  691. // 隔天冲销:返回海报/结果页所需字段
  692. $refundInfo = RefundOrderClass::getById($refundId, true);
  693. $order = OrderClass::getById($id, true);
  694. $custom = null;
  695. if (!empty($order)) {
  696. $custom = \bizGhs\custom\classes\CustomClass::getById(intval($order->customId ?? 0), true);
  697. }
  698. return [
  699. 'id' => $refundId,
  700. 'orderSn' => $refundInfo->orderSn ?? '',
  701. 'sameDay' => intval($refundInfo->sameDay ?? 1),
  702. 'fundType' => intval($refundInfo->fundType ?? 0),
  703. 'refundPrice' => $refundInfo->refundPrice ?? 0,
  704. 'customId' => intval($order->customId ?? 0),
  705. 'customName' => $custom->name ?? ($order->customName ?? ''),
  706. 'customBalance' => bcadd((string)($custom->balance ?? '0'), '0', 2),
  707. 'orderId' => intval($id),
  708. 'nextDayTkPrice' => $order->nextDayTkPrice ?? '0.00',
  709. ];
  710. } catch (\Exception $exception) {
  711. if ($transaction->isActive) {
  712. $transaction->rollBack();
  713. }
  714. throw $exception;
  715. }
  716. });
  717. util::success($respond);
  718. } catch (\Exception $exception) {
  719. Yii::info("退款出错了,报错信息:" . $exception->getMessage());
  720. if (util::isDbConcurrencyException($exception)) {
  721. util::fail('系统繁忙中,请稍后再试');
  722. } else {
  723. // 透出业务失败原因,便于隔天冲销排错
  724. util::fail($exception->getMessage() ?: '操作失败');
  725. }
  726. }
  727. }
  728. /**
  729. * 无原单退款(工作台「退款」)
  730. * POST: customId, price, product(json), fundType, refundType, remark
  731. * 立即写售后单 sameDay=0 并通过;无关联销售单/采购单。
  732. */
  733. public function actionCreateFreeRefund()
  734. {
  735. $post = Yii::$app->request->post();
  736. $shopAdmin = $this->shopAdmin;
  737. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  738. util::fail('超管才能操作退款');
  739. }
  740. // 与 create-order 生产环境售后权限保持一致
  741. if (getenv('YII_ENV') == 'production') {
  742. if ($this->mainId == 23390) {
  743. if (!in_array($this->adminId, [23960, 77951, 4])) {
  744. util::fail('你不能售后哈。。');
  745. }
  746. $remark = $post['remark'] ?? '';
  747. if (empty($remark)) {
  748. util::fail('请填写备注007');
  749. }
  750. }
  751. if ($this->mainId == 7704) {
  752. if (in_array($this->shopAdminId, [133727, 133545]) == false) {
  753. util::fail('无法操作。。');
  754. }
  755. }
  756. if ($this->mainId == 42680) {
  757. if (!in_array($this->adminId, [40144])) {
  758. util::fail('你不能售后哦。。');
  759. }
  760. }
  761. }
  762. $customId = intval($post['customId'] ?? 0);
  763. $cacheKey = 'ghs_free_refund_' . $this->mainId . '_' . $customId;
  764. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  765. if (!empty($has)) {
  766. util::fail('请5秒之后再提交');
  767. }
  768. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  769. try {
  770. $respond = util::runWithDbConcurrencyRetry(function () use ($post) {
  771. $connection = Yii::$app->db;
  772. $transaction = $connection->beginTransaction();
  773. try {
  774. // returnWay 优先:1库存退回要花材;0库存不退可只记金额
  775. $returnWay = intval($post['returnWay'] ?? -1);
  776. if ($returnWay === 1) {
  777. $refundType = RefundOrderClass::REFUND_TYPE_MONEY_GOOD;
  778. } elseif ($returnWay === 0) {
  779. $refundType = RefundOrderClass::REFUND_TYPE_MONEY;
  780. } else {
  781. $refundType = intval($post['refundType'] ?? RefundOrderClass::REFUND_TYPE_MONEY_GOOD);
  782. }
  783. $post['refundType'] = $refundType;
  784. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  785. $productJson = $post['product'] ?? '';
  786. if (empty($productJson)) {
  787. util::fail('请选择花材');
  788. }
  789. $productList = is_array($productJson) ? $productJson : json_decode($productJson, true);
  790. if (!is_array($productList) || empty($productList)) {
  791. util::fail('请选择花材哦');
  792. }
  793. $post['product'] = $productList;
  794. } else {
  795. // 库存不退:金额已够,明细可选保留供对账
  796. $productJson = $post['product'] ?? '';
  797. if (!empty($productJson)) {
  798. $productList = is_array($productJson) ? $productJson : json_decode($productJson, true);
  799. $post['product'] = is_array($productList) ? $productList : [];
  800. } else {
  801. $post['product'] = [];
  802. }
  803. }
  804. $post['price'] = $post['price'] ?? 0;
  805. if ($post['price'] <= 0) {
  806. util::fail('退款金额不能小于0');
  807. }
  808. $post['shopId'] = $this->shopId;
  809. $post['sjId'] = $this->sjId;
  810. $post['shopAdminId'] = $this->shopAdminId;
  811. $post['mainId'] = $this->mainId;
  812. $shopAdmin = $this->shopAdmin;
  813. $post['shopAdminName'] = $shopAdmin['name'] ?? '';
  814. $result = RefundOrderService::createFreeRefund($post);
  815. $transaction->commit();
  816. return $result;
  817. } catch (\Exception $exception) {
  818. if ($transaction->isActive) {
  819. $transaction->rollBack();
  820. }
  821. throw $exception;
  822. }
  823. });
  824. util::success($respond);
  825. } catch (\Exception $exception) {
  826. Yii::info('无原单退款出错:' . $exception->getMessage());
  827. if (util::isDbConcurrencyException($exception)) {
  828. util::fail('系统繁忙中,请稍后再试');
  829. }
  830. util::fail($exception->getMessage() ?: '操作失败');
  831. }
  832. }
  833. //修改售后原因 ssh 20250623
  834. public function actionChangeRefundOption()
  835. {
  836. $post = Yii::$app->request->post();
  837. $id = $post['id'] ?? 0;
  838. $sonId = $post['sonId'] ?? 0;
  839. $optionId = $post['optionId'] ?? 0;
  840. $refund = RefundOrderClass::getById($id, true);
  841. if (empty($refund)) {
  842. util::fail('没有找到退款信息');
  843. }
  844. if ($refund->mainId != $this->mainId) {
  845. util::fail('没有权限');
  846. }
  847. $orderSn = $refund->orderSn;
  848. $son = RefundOrderItemClass::getById($sonId, true);
  849. if (empty($son)) {
  850. util::fail('没有找到花材信息');
  851. }
  852. if ($son->orderSn != $orderSn) {
  853. util::fail('退款信息有问题');
  854. }
  855. $map = dict::getDict('itemRefundOption');
  856. $info = $map[$optionId] ?? [];
  857. $name = $info['name'] ?? '未选';
  858. $son->refundOptionId = $optionId;
  859. $son->refundOptionName = $name;
  860. $son->save();
  861. util::complete('修改成功');
  862. }
  863. }