RefundController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. //属于售后报损
  257. $post['saleType'] = 1;
  258. $connection = Yii::$app->db;
  259. $transaction = $connection->beginTransaction();
  260. try {
  261. $refundItem->xhWasteNum = bcadd($refundItem->xhWasteNum, $wasteNum);
  262. $refundItem->save();
  263. $post['product'] = $productList;
  264. $return = StockWastageOrderClass::addOrder($post);
  265. $transaction->commit();
  266. util::success($return);
  267. } catch (\Exception $e) {
  268. $transaction->rollBack();
  269. Yii::info("报损失败原因:" . $e->getMessage());
  270. util::fail('报损失败');
  271. }
  272. }
  273. //列表
  274. public function actionList()
  275. {
  276. $get = Yii::$app->request->get();
  277. $where = [];
  278. $where['mainId'] = $this->mainId;
  279. $orderSn = $get['orderSn'] ?? '';
  280. if (!empty($orderSn)) {
  281. $where['relateOrderSn'] = $orderSn;
  282. }
  283. $searchTime = $get['searchTime'] ?? '';
  284. if (!empty($searchTime)) {
  285. $startTime = $get['startTime'] ?? '';
  286. $endTime = $get['endTime'] ?? '';
  287. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  288. $where['orderTime'] = ['between', [$period['startTime'], $period['endTime']]];
  289. }
  290. $refundSearchTime = $get['refundSearchTime'] ?? '';
  291. if (!empty($refundSearchTime)) {
  292. $startTime = $get['refundStartTime'] ?? '';
  293. $endTime = $get['refundEndTime'] ?? '';
  294. $period = dateUtil::formatTime($refundSearchTime, $startTime, $endTime);
  295. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  296. }
  297. $customId = $get['customId'] ?? '';
  298. if (!empty($customId)) {
  299. $where['customId'] = $customId;
  300. }
  301. $shopAdminId = $get['shopAdminId'] ?? 0;
  302. if (!empty($shopAdminId)) {
  303. $where['shopAdminId'] = $shopAdminId;
  304. }
  305. $status = $get['status'] ?? -1;
  306. if ($status > -1) {
  307. $where['status'] = $status;
  308. }
  309. $export = $get['export'] ?? 0;
  310. if ($export == 1) {
  311. if (isset($where['orderTime']) == false && isset($where['addTime']) == false) {
  312. util::fail('请选时间');
  313. }
  314. ini_set('memory_limit', '2045M');
  315. set_time_limit(0);
  316. if (isset($where['orderTime'])) {
  317. $start = $where['orderTime'][1][0];
  318. $end = $where['orderTime'][1][1];
  319. $monthTime = bcmul(31, 86400);
  320. $startArea = strtotime($start);
  321. $endArea = strtotime($end);
  322. $diff = bcsub($endArea, $startArea);
  323. if ($diff > $monthTime) {
  324. util::fail('最长可导出一个月');
  325. }
  326. }
  327. if (isset($where['addTime'])) {
  328. $start = $where['addTime'][1][0];
  329. $end = $where['addTime'][1][1];
  330. $monthTime = bcmul(31, 86400);
  331. $startArea = strtotime($start);
  332. $endArea = strtotime($end);
  333. $diff = bcsub($endArea, $startArea);
  334. if ($diff > $monthTime) {
  335. util::fail('最长可导出一个月');
  336. }
  337. }
  338. }
  339. $respond = RefundOrderClass::getOrderList($where);
  340. if ($export == 1) {
  341. RefundOrderClass::exportRefundData($respond, $this->mainId);
  342. }
  343. util::success($respond);
  344. }
  345. //主要根据花材找退款记录 ssh 20240229
  346. public function actionGetListByItem()
  347. {
  348. $get = Yii::$app->request->get();
  349. $where = [];
  350. $where['mainId'] = $this->mainId;
  351. $status = $get['status'] ?? -1;
  352. $cause = $get['cause'] ?? -1;
  353. if ($status > -1) {
  354. $where['status'] = $status;
  355. }
  356. $string = $get['ids'];
  357. $ids = json_decode($string, true);
  358. if (!empty($ids)) {
  359. $productIds = [];
  360. $itemList = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'classId' => ['in', $ids]], null, 'id', 'id');
  361. if (!empty($itemList)) {
  362. $productIds = array_keys($itemList);
  363. }
  364. if (empty($productIds)) {
  365. util::success(['list' => [], 'moreData' => 0, 'totalAmount' => 0, 'totalNum' => 0, 'totalPage' => 1]);
  366. }
  367. $where['productId'] = ['in', $productIds];
  368. }
  369. if ($cause > -1) {
  370. $where['cause'] = $cause;
  371. }
  372. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  373. $startTime = $get['startTime'] ?? '';
  374. $endTime = $get['endTime'] ?? '';
  375. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  376. $start = $period['startTime'];
  377. $end = $period['endTime'];
  378. $currentStartDate = date('Y-m-d', strtotime($start));
  379. $currentEndDate = date('Y-m-d', strtotime($end));
  380. $currentStartTime = $currentStartDate . ' 00:00:00';
  381. $currentEndTime = $currentEndDate . ' 23:59:59';
  382. $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
  383. $list = RefundOrderItemClass::getRefundItemList($where);
  384. util::success($list);
  385. }
  386. public function actionDetail()
  387. {
  388. $id = Yii::$app->request->get('id', 0);
  389. $refund = RefundOrderClass::getById($id);
  390. if (empty($refund)) {
  391. util::fail('没有找到退款信息');
  392. }
  393. if (isset($refund['mainId']) == false || $refund['mainId'] != $this->mainId) {
  394. util::fail('没有权限');
  395. }
  396. $imgString = $refund['imgList'] ?? '';
  397. $relateOrderSn = $refund['relateOrderSn'] ?? '';
  398. $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  399. if (empty($order)) {
  400. util::fail('数据出错了');
  401. }
  402. $refund['relateOrderId'] = $order->id;
  403. if (!empty($imgString)) {
  404. $imgArr = json_decode($imgString, true);
  405. $smallImgList = [];
  406. $bigImgList = [];
  407. if (!empty($imgArr)) {
  408. foreach ($imgArr as $image) {
  409. $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  410. $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  411. $smallImgList[] = $smallImg;
  412. $bigImgList[] = $bigImg;
  413. }
  414. }
  415. $refund['imgList'] = $imgArr;
  416. $refund['smallImgList'] = $smallImgList;
  417. $refund['bigImgList'] = $bigImgList;
  418. }
  419. $orderSn = $refund['orderSn'] ?? '';
  420. $itemList = RefundOrderItemClass::getOrderItemDetail($orderSn);
  421. $staff = $this->shopAdmin;
  422. $staffId = $this->shopAdminId;
  423. $notify = NotifyClass::getByCondition(['staffId' => $staffId, 'type' => 0, 'targetId' => $id], true);
  424. if (!empty($notify)) {
  425. if ($notify->read == 0) {
  426. $notify->read = 1;
  427. $notify->save();
  428. $notifyNum = $staff->notifyNum;
  429. if ($notifyNum > 0) {
  430. $notifyNum--;
  431. $staff->notifyNum = $notifyNum;
  432. $staff->save();
  433. }
  434. }
  435. }
  436. util::success(['info' => $refund, 'itemList' => $itemList]);
  437. }
  438. //退款 lqh 2021.6.25
  439. public function actionCreateOrder()
  440. {
  441. $post = Yii::$app->request->post();
  442. $shopAdmin = $this->shopAdmin;
  443. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  444. util::fail('超管才能操作退款');
  445. }
  446. //国恋只有指定人有退款权限
  447. if ($this->mainId == 7704) {
  448. if (in_array($this->shopAdminId, [133727, 133545]) == false) {
  449. util::fail('无法操作。。');
  450. }
  451. }
  452. if (getenv('YII_ENV') == 'production') {
  453. //小向花卉开单售后控制
  454. if ($this->mainId == 23390) {
  455. if (!in_array($this->adminId, [24043, 24585, 24586, 24654, 24115, 23960])) {
  456. util::fail('你不能售后哦。。');
  457. }
  458. //小向花卉售后必须备注
  459. $remark = $post['remark'] ?? '';
  460. if (empty($remark)) {
  461. util::fail('请填写备注007');
  462. }
  463. }
  464. }
  465. $id = isset($post['id']) ? $post['id'] : 0;
  466. $refundType = $post['refundType'] ?? 1;
  467. //避免网络延迟,暂时重复申请
  468. $cacheKey = 'ghs_order_refund_' . $id;
  469. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  470. if (!empty($has)) {
  471. util::fail('请5秒之后再提交');
  472. }
  473. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  474. $connection = Yii::$app->db;
  475. $transaction = $connection->beginTransaction();
  476. try {
  477. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  478. //花材列表结构
  479. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
  480. $productJson = $post['product'] ?? '';
  481. if (empty($productJson)) {
  482. util::fail('请选择花材');
  483. }
  484. $productList = json_decode($productJson, true);
  485. if (!is_array($productList)) {
  486. util::fail('请选择花材哦');
  487. }
  488. $post['product'] = $productList;
  489. } else {
  490. //仅退款花材直接设置为空
  491. $post['product'] = [];
  492. }
  493. $post['price'] = $post['price'] ?? 0;
  494. if ($post['price'] <= 0) {
  495. util::fail("退款金额不能小于0");
  496. }
  497. $post['shopId'] = $this->shopId;
  498. $post['sjId'] = $this->sjId;
  499. $post['shopAdminId'] = $this->shopAdminId;
  500. $post['mainId'] = $this->mainId;
  501. $shopAdmin = $this->shopAdmin;
  502. $adminName = $shopAdmin['name'] ?? '';
  503. $post['shopAdminName'] = $adminName;
  504. $respond = OrderService::refund($id, $post);
  505. $refundId = $respond->id;
  506. $refundInfo = RefundOrderClass::getById($refundId, true);
  507. RefundOrderService::passRefund($refundInfo);
  508. $transaction->commit();
  509. util::success($respond);
  510. } catch (\Exception $exception) {
  511. $transaction->rollBack();
  512. Yii::info("退款出错了,报错信息:" . $exception->getMessage());
  513. util::fail('操作失败');
  514. }
  515. }
  516. }