RefundController.php 18 KB

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