RefundController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. //退款退货
  3. namespace ghs\controllers;
  4. use bizGhs\notify\classes\NotifyClass;
  5. use bizGhs\order\classes\RefundOrderItemClass;
  6. use bizGhs\order\services\RefundOrderService;
  7. use bizGhs\shop\classes\ShopClass;
  8. use bizHd\cg\classes\CgRefundClass;
  9. use bizHd\cg\classes\CgRefundItemClass;
  10. use bizHd\notice\classes\NoticeClass;
  11. use bizHd\purchase\classes\PurchaseClearClass;
  12. use common\components\dateUtil;
  13. use common\components\imgUtil;
  14. use common\components\stringUtil;
  15. use Yii;
  16. use bizGhs\order\classes\RefundOrderClass;
  17. use common\components\util;
  18. use bizGhs\product\classes\ProductClass;
  19. use bizGhs\order\classes\OrderClass;
  20. use bizGhs\order\services\OrderService;
  21. use bizGhs\order\classes\StockWastageOrderClass;
  22. class RefundController extends BaseController
  23. {
  24. //修改金额 ssh 20240412
  25. public function actionModifyAmount()
  26. {
  27. $get = Yii::$app->request->get();
  28. $id = $get['id'] ?? 0;
  29. $amount = $get['amount'] ?? 0;
  30. $refund = RefundOrderClass::getById($id, true);
  31. RefundOrderClass::valid($refund, $this->mainId);
  32. if (is_numeric($amount) == false) {
  33. util::fail('请填写金额');
  34. }
  35. if ($refund->status != 0) {
  36. util::fail('待审核订单才能修改');
  37. }
  38. $connection = Yii::$app->db;
  39. $transaction = $connection->beginTransaction();
  40. try {
  41. $refund->refundPrice = $amount;
  42. $refund->save();
  43. $cgRefundId = $refund->cgRefundId ?? 0;
  44. $cgRefund = CgRefundClass::getById($cgRefundId, true);
  45. if (empty($cgRefund)) {
  46. util::fail('没有找到退款信息');
  47. }
  48. $cgRefund->refundPrice = $amount;
  49. $cgRefund->save();
  50. $transaction->commit();
  51. util::complete();
  52. } catch (\Exception $e) {
  53. $transaction->rollBack();
  54. Yii::info("操作失败原因:" . $e->getMessage());
  55. util::fail('操作失败');
  56. }
  57. }
  58. //修改申请原因 ssh 20240302
  59. public function actionModifyCause()
  60. {
  61. $staff = $this->shopAdmin;
  62. if (isset($staff->finance) == false || $staff->finance == 0) {
  63. util::fail('请有财务权限的人修改');
  64. }
  65. $get = Yii::$app->request->get();
  66. $id = $get['id'] ?? 0;
  67. $cause = $get['cause'] ?? 0;
  68. $refund = RefundOrderClass::getById($id, true);
  69. RefundOrderClass::valid($refund, $this->mainId);
  70. $refund->cause = $cause;
  71. $refund->save();
  72. $orderSn = $refund->orderSn ?? '';
  73. RefundOrderItemClass::updateByCondition(['orderSn' => $orderSn], ['cause' => $cause]);
  74. $cgRefundId = $refund->cgRefundId ?? 0;
  75. $cgRefund = CgRefundClass::getById($cgRefundId, true);
  76. if (empty($cgRefund)) {
  77. util::fail('没有找到退款信息');
  78. }
  79. $cgRefund->cause = $cause;
  80. $cgRefund->save();
  81. $cgOrderSn = $cgRefund->orderSn ?? '';
  82. CgRefundItemClass::updateByCondition(['orderSn' => $cgOrderSn], ['cause' => $cause]);
  83. util::complete('修改成功');
  84. }
  85. //审核通过 ssh 20230811
  86. public function actionPass()
  87. {
  88. $shopAdmin = $this->shopAdmin;
  89. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  90. util::fail('无法操作');
  91. }
  92. if (getenv('YII_ENV') == 'production') {
  93. //小向花卉开单售后控制
  94. if ($this->mainId == 23390) {
  95. if (!in_array($this->adminId, [24043, 24585, 24586, 24654, 24115, 23960])) {
  96. util::fail('你不能售后哦。。');
  97. }
  98. }
  99. } else {
  100. if ($this->mainId == 644) {
  101. // if (!in_array($this->adminId, [919])) {
  102. // util::fail('你不能售后哈');
  103. // }
  104. }
  105. }
  106. $get = Yii::$app->request->get();
  107. $id = $get['id'] ?? 0;
  108. $version = $get['version'] ?? 0;
  109. $confirm = $get['confirm'] ?? 0;
  110. $refund = RefundOrderClass::getById($id, true);
  111. RefundOrderClass::valid($refund, $this->mainId);
  112. $relateOrderSn = $refund->relateOrderSn ?? '';
  113. if (empty($relateOrderSn)) {
  114. util::fail('没有订单信息');
  115. }
  116. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $relateOrderSn], null, '*', null, true);
  117. $has = false;
  118. if (!empty($refundList)) {
  119. foreach ($refundList as $single) {
  120. if ($single->status == 1) {
  121. $has = true;
  122. }
  123. }
  124. }
  125. if ($has == true) {
  126. if ($version == 0) {
  127. util::fail('请先升级系统');
  128. }
  129. if ($confirm == 0) {
  130. util::success(['error' => 'notFirstApply']);
  131. }
  132. }
  133. $connection = Yii::$app->db;//事务处理
  134. $transaction = $connection->beginTransaction();
  135. try {
  136. RefundOrderService::passRefund($refund);
  137. $transaction->commit();
  138. $staff = $this->shopAdmin;
  139. $staffId = $staff->id;
  140. $staffName = $staff->name ?? '';
  141. $refund->shopAdminId = $staffId;
  142. $refund->shopAdminName = $staffName;
  143. $refund->save();
  144. //审核结果通知
  145. $cgRefundId = $refund->cgRefundId ?? 0;
  146. $cgRefund = CgRefundClass::getById($cgRefundId, true);
  147. $cgShopId = $cgRefund->shopId ?? 0;
  148. $cgShop = ShopClass::getById($cgShopId, true);
  149. if (!empty($cgRefund) && !empty($cgShop)) {
  150. NoticeClass::afterSaleNotice($cgShop, $cgRefund);
  151. }
  152. util::complete('操作成功');
  153. } catch (\Exception $e) {
  154. $transaction->rollBack();
  155. Yii::info("操作失败原因:" . $e->getMessage());
  156. util::fail('操作失败');
  157. }
  158. }
  159. //驳回 ssh 20230813
  160. public function actionReject()
  161. {
  162. $shopAdmin = $this->shopAdmin;
  163. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  164. util::fail('无法操作');
  165. }
  166. if (getenv('YII_ENV') == 'production') {
  167. //小向花卉开单售后控制
  168. if ($this->mainId == 23390) {
  169. if (!in_array($this->adminId, [24043, 24585, 24586, 24654, 24115, 23960])) {
  170. util::fail('你不能售后哦。。');
  171. }
  172. }
  173. } else {
  174. if ($this->mainId == 644) {
  175. // if (!in_array($this->adminId, [919])) {
  176. // util::fail('你不能售后哈');
  177. // }
  178. }
  179. }
  180. $post = Yii::$app->request->post();
  181. $id = $post['id'] ?? 0;
  182. $rejectReason = $post['rejectReason'] ?? '';
  183. $refund = RefundOrderClass::getById($id, true);
  184. RefundOrderClass::valid($refund, $this->mainId);
  185. $connection = Yii::$app->db;//事务处理
  186. $transaction = $connection->beginTransaction();
  187. try {
  188. RefundOrderClass::rejectRefund($refund, $rejectReason);
  189. $transaction->commit();
  190. $staff = $this->shopAdmin;
  191. $staffId = $staff->id;
  192. $staffName = $staff->name ?? '';
  193. $refund->shopAdminId = $staffId;
  194. $refund->shopAdminName = $staffName;
  195. $refund->save();
  196. //审核结果通知
  197. $cgRefundId = $refund->cgRefundId ?? 0;
  198. $cgRefund = CgRefundClass::getById($cgRefundId, true);
  199. $cgShopId = $cgRefund->shopId ?? 0;
  200. $cgShop = ShopClass::getById($cgShopId, true);
  201. if (!empty($cgRefund) && !empty($cgShop)) {
  202. NoticeClass::afterSaleNotice($cgShop, $cgRefund);
  203. }
  204. util::complete('操作成功');
  205. } catch (\Exception $e) {
  206. $transaction->rollBack();
  207. Yii::info("操作失败原因:" . $e->getMessage());
  208. util::fail('操作失败');
  209. }
  210. }
  211. //退款之后报损 ssh 20230409
  212. public function actionRefundWaste()
  213. {
  214. $shopAdmin = $this->shopAdmin;
  215. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  216. util::fail('超管才能报损');
  217. }
  218. $post = Yii::$app->request->post();
  219. $refundItemId = $post['refundItemId'] ?? 0;
  220. $wasteProductId = $post['wasteProductId'] ?? 0;
  221. $wasteNum = $post['wasteNum'] ?? 0;
  222. unset($post['refundId']);
  223. unset($post['wasteNum']);
  224. unset($post['wasteProductId']);
  225. $productInfo = ProductClass::getById($wasteProductId, true);
  226. if (empty($productInfo)) {
  227. util::fail('没有找到花材');
  228. }
  229. $ptItemId = $productInfo->itemId ?? 0;
  230. $ratio = $productInfo->ratio ?? 20;
  231. $refundItem = RefundOrderItemClass::getById($refundItemId, true);
  232. if (empty($refundItem)) {
  233. util::fail('没有找到退货信息');
  234. }
  235. if ($refundItem->mainId != $this->mainId) {
  236. util::fail('不是您的退款信息');
  237. }
  238. $unitType = $refundItem->xhUnitType ?? 0;
  239. if ($unitType == 1) {
  240. util::fail('无法报损');
  241. }
  242. $refundNum = $refundItem->xhNum ?? 0;
  243. $hasWasteNum = $refundItem->xhWasteNum ?? 0;
  244. $remainNum = bcsub($refundNum, $hasWasteNum);
  245. if ($wasteNum > $remainNum) {
  246. util::fail('剩余可报损数量 ' . $remainNum);
  247. }
  248. $productList = [['productId' => $wasteProductId, 'bigNum' => $wasteNum, 'smallNum' => 0, 'classId' => 0, 'itemId' => $ptItemId, 'ratio' => $ratio]];
  249. $post['shopId'] = $this->shopId;
  250. $post['sjId'] = $this->sjId;
  251. $post['mainId'] = $this->mainId;
  252. $post['shopAdminId'] = $this->shopAdminId;
  253. $shopAdmin = $this->shopAdmin;
  254. $adminName = $shopAdmin['name'] ?? '';
  255. $post['shopAdminName'] = $adminName;
  256. $connection = Yii::$app->db;//事务处理
  257. $transaction = $connection->beginTransaction();
  258. try {
  259. $refundItem->xhWasteNum = bcadd($refundItem->xhWasteNum, $wasteNum);
  260. $refundItem->save();
  261. $post['product'] = $productList;
  262. $return = StockWastageOrderClass::addOrder($post);
  263. $transaction->commit();
  264. util::success($return);
  265. } catch (\Exception $e) {
  266. $transaction->rollBack();
  267. Yii::info("报损失败原因:" . $e->getMessage());
  268. util::fail('报损失败');
  269. }
  270. }
  271. //列表
  272. public function actionList()
  273. {
  274. $get = Yii::$app->request->get();
  275. $where = [];
  276. $where['mainId'] = $this->mainId;
  277. $orderSn = $get['orderSn'] ?? '';
  278. if (!empty($orderSn)) {
  279. $where['relateOrderSn'] = $orderSn;
  280. }
  281. $searchTime = $get['searchTime'] ?? '';
  282. if (!empty($searchTime)) {
  283. $startTime = $get['startTime'] ?? '';
  284. $endTime = $get['endTime'] ?? '';
  285. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  286. $where['orderTime'] = ['between', [$period['startTime'], $period['endTime']]];
  287. }
  288. $refundSearchTime = $get['refundSearchTime'] ?? '';
  289. if (!empty($refundSearchTime)) {
  290. $startTime = $get['refundStartTime'] ?? '';
  291. $endTime = $get['refundEndTime'] ?? '';
  292. $period = dateUtil::formatTime($refundSearchTime, $startTime, $endTime);
  293. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  294. }
  295. $customName = $get['customName'] ?? '';
  296. if (!empty($customName)) {
  297. if (stringUtil::isLetter($customName)) {
  298. $customName = strtolower($customName);
  299. $where['customNamePy'] = ['like', $customName];
  300. } else {
  301. $where['customName'] = ['like', $customName];
  302. }
  303. }
  304. $shopAdminId = $get['shopAdminId'] ?? 0;
  305. if (!empty($shopAdminId)) {
  306. $where['shopAdminId'] = $shopAdminId;
  307. }
  308. $list = RefundOrderClass::getOrderList($where);
  309. util::success($list);
  310. }
  311. //主要根据花材找退款记录 ssh 20240229
  312. public function actionGetListByItem()
  313. {
  314. $get = Yii::$app->request->get();
  315. $where = [];
  316. $where['mainId'] = $this->mainId;
  317. $status = $get['status'] ?? -1;
  318. $cause = $get['cause'] ?? -1;
  319. if ($status > -1) {
  320. $where['status'] = $status;
  321. }
  322. $string = $get['ids'];
  323. $ids = json_decode($string, true);
  324. if (!empty($ids)) {
  325. $productIds = [];
  326. $itemList = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'classId' => ['in', $ids]], null, 'id', 'id');
  327. if (!empty($itemList)) {
  328. $productIds = array_keys($itemList);
  329. }
  330. if (empty($productIds)) {
  331. util::success(['list' => [], 'moreData' => 0, 'totalAmount' => 0, 'totalNum' => 0, 'totalPage' => 1]);
  332. }
  333. $where['productId'] = ['in', $productIds];
  334. }
  335. if ($cause > -1) {
  336. $where['cause'] = $cause;
  337. }
  338. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  339. $startTime = $get['startTime'] ?? '';
  340. $endTime = $get['endTime'] ?? '';
  341. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  342. $start = $period['startTime'];
  343. $end = $period['endTime'];
  344. $currentStartDate = date('Y-m-d', strtotime($start));
  345. $currentEndDate = date('Y-m-d', strtotime($end));
  346. $currentStartTime = $currentStartDate . ' 00:00:00';
  347. $currentEndTime = $currentEndDate . ' 23:59:59';
  348. $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
  349. $list = RefundOrderItemClass::getRefundItemList($where);
  350. util::success($list);
  351. }
  352. public function actionDetail()
  353. {
  354. $id = Yii::$app->request->get('id', 0);
  355. $refund = RefundOrderClass::getById($id);
  356. if (empty($refund)) {
  357. util::fail('没有找到退款信息');
  358. }
  359. if (isset($refund['mainId']) == false || $refund['mainId'] != $this->mainId) {
  360. util::fail('没有权限');
  361. }
  362. $imgString = $refund['imgList'] ?? '';
  363. $relateOrderSn = $refund['relateOrderSn'] ?? '';
  364. $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  365. if (empty($order)) {
  366. util::fail('数据出错了');
  367. }
  368. $refund['relateOrderId'] = $order->id;
  369. if (!empty($imgString)) {
  370. $imgArr = json_decode($imgString, true);
  371. $smallImgList = [];
  372. $bigImgList = [];
  373. if (!empty($imgArr)) {
  374. foreach ($imgArr as $image) {
  375. $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  376. $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  377. $smallImgList[] = $smallImg;
  378. $bigImgList[] = $bigImg;
  379. }
  380. }
  381. $refund['imgList'] = $imgArr;
  382. $refund['smallImgList'] = $smallImgList;
  383. $refund['bigImgList'] = $bigImgList;
  384. }
  385. $orderSn = $refund['orderSn'] ?? '';
  386. $itemList = RefundOrderItemClass::getOrderItemDetail($orderSn);
  387. $staff = $this->shopAdmin;
  388. $staffId = $this->shopAdminId;
  389. $notify = NotifyClass::getByCondition(['staffId' => $staffId, 'type' => 0, 'targetId' => $id], true);
  390. if (!empty($notify)) {
  391. if ($notify->read == 0) {
  392. $notify->read = 1;
  393. $notify->save();
  394. $notifyNum = $staff->notifyNum;
  395. if ($notifyNum > 0) {
  396. $notifyNum--;
  397. $staff->notifyNum = $notifyNum;
  398. $staff->save();
  399. }
  400. }
  401. }
  402. util::success(['info' => $refund, 'itemList' => $itemList]);
  403. }
  404. //退款 lqh 2021.6.25
  405. public function actionCreateOrder()
  406. {
  407. $shopAdmin = $this->shopAdmin;
  408. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  409. util::fail('超管才能操作退款');
  410. }
  411. //国恋只有指定人有退款权限
  412. if ($this->mainId == 7704) {
  413. if (in_array($this->shopAdminId, [133727, 133545]) == false) {
  414. util::fail('无法操作。。');
  415. }
  416. }
  417. if (getenv('YII_ENV') == 'production') {
  418. //小向花卉开单售后控制
  419. if ($this->mainId == 23390) {
  420. if (!in_array($this->adminId, [24043, 24585, 24586, 24654, 24115, 23960])) {
  421. util::fail('你不能售后哦。。');
  422. }
  423. }
  424. } else {
  425. if ($this->mainId == 644) {
  426. // if (!in_array($this->adminId, [919])) {
  427. // util::fail('你不能售后哈');
  428. // }
  429. }
  430. }
  431. $post = Yii::$app->request->post();
  432. $id = isset($post['id']) ? $post['id'] : 0;
  433. $refundType = $post['refundType'] ?? 1;
  434. //避免网络延迟,暂时重复申请
  435. $cacheKey = 'ghs_order_refund_' . $id;
  436. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  437. if (!empty($has)) {
  438. util::fail('请5秒之后再提交');
  439. }
  440. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  441. $connection = Yii::$app->db;
  442. $transaction = $connection->beginTransaction();
  443. try {
  444. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  445. //花材列表结构
  446. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
  447. $productJson = $post['product'] ?? '';
  448. if (empty($productJson)) {
  449. util::fail('请选择花材');
  450. }
  451. $productList = json_decode($productJson, true);
  452. if (!is_array($productList)) {
  453. util::fail('请选择花材哦');
  454. }
  455. $post['product'] = $productList;
  456. } else {
  457. //仅退款花材直接设置为空
  458. $post['product'] = [];
  459. }
  460. $post['price'] = $post['price'] ?? 0;
  461. if ($post['price'] <= 0) {
  462. util::fail("退款金额不能小于0");
  463. }
  464. $post['shopId'] = $this->shopId;
  465. $post['sjId'] = $this->sjId;
  466. $post['shopAdminId'] = $this->shopAdminId;
  467. $post['mainId'] = $this->mainId;
  468. $shopAdmin = $this->shopAdmin;
  469. $adminName = $shopAdmin['name'] ?? '';
  470. $post['shopAdminName'] = $adminName;
  471. $respond = OrderService::refund($id, $post);
  472. $refundId = $respond->id;
  473. $refundInfo = RefundOrderClass::getById($refundId, true);
  474. RefundOrderService::passRefund($refundInfo);
  475. $transaction->commit();
  476. util::success($respond);
  477. } catch (\Exception $exception) {
  478. $transaction->rollBack();
  479. Yii::info("退款出错了,报错信息:" . $exception->getMessage());
  480. util::fail('操作失败');
  481. }
  482. }
  483. }