RefundController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. util::complete();
  30. }
  31. //修改申请原因 ssh 20240302
  32. public function actionModifyCause()
  33. {
  34. $staff = $this->shopAdmin;
  35. if (isset($staff->finance) == false || $staff->finance == 0) {
  36. util::fail('请有财务权限的人修改');
  37. }
  38. $get = Yii::$app->request->get();
  39. $id = $get['id'] ?? 0;
  40. $cause = $get['cause'] ?? 0;
  41. $refund = RefundOrderClass::getById($id, true);
  42. RefundOrderClass::valid($refund, $this->mainId);
  43. $refund->cause = $cause;
  44. $refund->save();
  45. $orderSn = $refund->orderSn ?? '';
  46. RefundOrderItemClass::updateByCondition(['orderSn' => $orderSn], ['cause' => $cause]);
  47. $cgRefundId = $refund->cgRefundId ?? 0;
  48. $cgRefund = CgRefundClass::getById($cgRefundId, true);
  49. if (empty($cgRefund)) {
  50. util::fail('没有找到退款信息');
  51. }
  52. $cgRefund->cause = $cause;
  53. $cgRefund->save();
  54. $cgOrderSn = $cgRefund->orderSn ?? '';
  55. CgRefundItemClass::updateByCondition(['orderSn' => $cgOrderSn], ['cause' => $cause]);
  56. util::complete('修改成功');
  57. }
  58. //审核通过 ssh 20230811
  59. public function actionPass()
  60. {
  61. $shopAdmin = $this->shopAdmin;
  62. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  63. util::fail('无法操作');
  64. }
  65. $get = Yii::$app->request->get();
  66. $id = $get['id'] ?? 0;
  67. $refund = RefundOrderClass::getById($id, true);
  68. RefundOrderClass::valid($refund, $this->mainId);
  69. $connection = Yii::$app->db;//事务处理
  70. $transaction = $connection->beginTransaction();
  71. try {
  72. RefundOrderService::passRefund($refund);
  73. $transaction->commit();
  74. //审核结果通知
  75. $cgRefundId = $refund->cgRefundId ?? 0;
  76. $cgRefund = CgRefundClass::getById($cgRefundId, true);
  77. $cgShopId = $cgRefund->shopId ?? 0;
  78. $cgShop = ShopClass::getById($cgShopId, true);
  79. if (!empty($cgRefund) && !empty($cgShop)) {
  80. NoticeClass::afterSaleNotice($cgShop, $cgRefund);
  81. }
  82. util::complete('操作成功');
  83. } catch (\Exception $e) {
  84. $transaction->rollBack();
  85. Yii::info("操作失败原因:" . $e->getMessage());
  86. util::fail('操作失败');
  87. }
  88. }
  89. //驳回 ssh 20230813
  90. public function actionReject()
  91. {
  92. $shopAdmin = $this->shopAdmin;
  93. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  94. util::fail('无法操作');
  95. }
  96. $post = Yii::$app->request->post();
  97. $id = $post['id'] ?? 0;
  98. $rejectReason = $post['rejectReason'] ?? '';
  99. $refund = RefundOrderClass::getById($id, true);
  100. RefundOrderClass::valid($refund, $this->mainId);
  101. $connection = Yii::$app->db;//事务处理
  102. $transaction = $connection->beginTransaction();
  103. try {
  104. RefundOrderClass::rejectRefund($refund, $rejectReason);
  105. $transaction->commit();
  106. //审核结果通知
  107. $cgRefundId = $refund->cgRefundId ?? 0;
  108. $cgRefund = CgRefundClass::getById($cgRefundId, true);
  109. $cgShopId = $cgRefund->shopId ?? 0;
  110. $cgShop = ShopClass::getById($cgShopId, true);
  111. if (!empty($cgRefund) && !empty($cgShop)) {
  112. NoticeClass::afterSaleNotice($cgShop, $cgRefund);
  113. }
  114. util::complete('操作成功');
  115. } catch (\Exception $e) {
  116. $transaction->rollBack();
  117. Yii::info("操作失败原因:" . $e->getMessage());
  118. util::fail('操作失败');
  119. }
  120. }
  121. //退款之后报损 ssh 20230409
  122. public function actionRefundWaste()
  123. {
  124. $shopAdmin = $this->shopAdmin;
  125. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  126. util::fail('超管才能报损');
  127. }
  128. $post = Yii::$app->request->post();
  129. $refundItemId = $post['refundItemId'] ?? 0;
  130. $wasteProductId = $post['wasteProductId'] ?? 0;
  131. $wasteNum = $post['wasteNum'] ?? 0;
  132. unset($post['refundId']);
  133. unset($post['wasteNum']);
  134. unset($post['wasteProductId']);
  135. $productInfo = ProductClass::getById($wasteProductId, true);
  136. if (empty($productInfo)) {
  137. util::fail('没有找到花材');
  138. }
  139. $ptItemId = $productInfo->itemId ?? 0;
  140. $ratio = $productInfo->ratio ?? 20;
  141. $refundItem = RefundOrderItemClass::getById($refundItemId, true);
  142. if (empty($refundItem)) {
  143. util::fail('没有找到退货信息');
  144. }
  145. if ($refundItem->mainId != $this->mainId) {
  146. util::fail('不是您的退款信息');
  147. }
  148. $unitType = $refundItem->xhUnitType ?? 0;
  149. if ($unitType == 1) {
  150. util::fail('无法报损');
  151. }
  152. $refundNum = $refundItem->xhNum ?? 0;
  153. $hasWasteNum = $refundItem->xhWasteNum ?? 0;
  154. $remainNum = bcsub($refundNum, $hasWasteNum);
  155. if ($wasteNum > $remainNum) {
  156. util::fail('剩余可报损数量 ' . $remainNum);
  157. }
  158. $productList = [['productId' => $wasteProductId, 'bigNum' => $wasteNum, 'smallNum' => 0, 'classId' => 0, 'itemId' => $ptItemId, 'ratio' => $ratio]];
  159. $post['shopId'] = $this->shopId;
  160. $post['sjId'] = $this->sjId;
  161. $post['mainId'] = $this->mainId;
  162. $post['shopAdminId'] = $this->shopAdminId;
  163. $shopAdmin = $this->shopAdmin;
  164. $adminName = $shopAdmin['name'] ?? '';
  165. $post['shopAdminName'] = $adminName;
  166. $connection = Yii::$app->db;//事务处理
  167. $transaction = $connection->beginTransaction();
  168. try {
  169. $refundItem->xhWasteNum = bcadd($refundItem->xhWasteNum, $wasteNum);
  170. $refundItem->save();
  171. $post['product'] = $productList;
  172. $return = StockWastageOrderClass::addOrder($post);
  173. $transaction->commit();
  174. util::success($return);
  175. } catch (\Exception $e) {
  176. $transaction->rollBack();
  177. Yii::info("报损失败原因:" . $e->getMessage());
  178. util::fail('报损失败');
  179. }
  180. }
  181. //列表
  182. public function actionList()
  183. {
  184. $get = Yii::$app->request->get();
  185. $where = [];
  186. $where['mainId'] = $this->mainId;
  187. $orderSn = $get['orderSn'] ?? '';
  188. if (!empty($orderSn)) {
  189. $where['relateOrderSn'] = $orderSn;
  190. }
  191. $list = RefundOrderClass::getOrderList($where);
  192. util::success($list);
  193. }
  194. //主要根据花材找退款记录 ssh 20240229
  195. public function actionGetListByItem()
  196. {
  197. $get = Yii::$app->request->get();
  198. $where = [];
  199. $where['mainId'] = $this->mainId;
  200. $status = $get['status'] ?? -1;
  201. $cause = $get['cause'] ?? -1;
  202. if ($status > -1) {
  203. $where['status'] = $status;
  204. }
  205. $string = $get['ids'];
  206. $ids = json_decode($string, true);
  207. if (!empty($ids)) {
  208. $productIds = [];
  209. $itemList = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'classId' => ['in', $ids]], null, 'id', 'id');
  210. if (!empty($itemList)) {
  211. $productIds = array_keys($itemList);
  212. }
  213. if (empty($productIds)) {
  214. util::success(['list' => [], 'moreData' => 0, 'totalAmount' => 0, 'totalNum' => 0, 'totalPage' => 1]);
  215. }
  216. $where['productId'] = ['in', $productIds];
  217. }
  218. if ($cause > -1) {
  219. $where['cause'] = $cause;
  220. }
  221. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  222. $startTime = $get['startTime'] ?? '';
  223. $endTime = $get['endTime'] ?? '';
  224. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  225. $start = $period['startTime'];
  226. $end = $period['endTime'];
  227. $currentStartDate = date('Y-m-d', strtotime($start));
  228. $currentEndDate = date('Y-m-d', strtotime($end));
  229. $currentStartTime = $currentStartDate . ' 00:00:00';
  230. $currentEndTime = $currentEndDate . ' 23:59:59';
  231. $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
  232. $list = RefundOrderItemClass::getRefundItemList($where);
  233. util::success($list);
  234. }
  235. public function actionDetail()
  236. {
  237. $id = Yii::$app->request->get('id', 0);
  238. $refund = RefundOrderClass::getById($id);
  239. if (empty($refund)) {
  240. util::fail('没有找到退款信息');
  241. }
  242. if (isset($refund['mainId']) == false || $refund['mainId'] != $this->mainId) {
  243. util::fail('没有权限');
  244. }
  245. $imgString = $refund['imgList'] ?? '';
  246. $relateOrderSn = $refund['relateOrderSn'] ?? '';
  247. $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  248. if (empty($order)) {
  249. util::fail('数据出错了');
  250. }
  251. $refund['relateOrderId'] = $order->id;
  252. if (!empty($imgString)) {
  253. $imgArr = json_decode($imgString, true);
  254. $smallImgList = [];
  255. $bigImgList = [];
  256. if (!empty($imgArr)) {
  257. foreach ($imgArr as $image) {
  258. $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  259. $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  260. $smallImgList[] = $smallImg;
  261. $bigImgList[] = $bigImg;
  262. }
  263. }
  264. $refund['imgList'] = $imgArr;
  265. $refund['smallImgList'] = $smallImgList;
  266. $refund['bigImgList'] = $bigImgList;
  267. }
  268. $orderSn = $refund['orderSn'] ?? '';
  269. $itemList = RefundOrderItemClass::getOrderItemDetail($orderSn);
  270. $staff = $this->shopAdmin;
  271. $staffId = $this->shopAdminId;
  272. $notify = NotifyClass::getByCondition(['staffId' => $staffId, 'type' => 0, 'targetId' => $id], true);
  273. if (!empty($notify)) {
  274. if ($notify->read == 0) {
  275. $notify->read = 1;
  276. $notify->save();
  277. $notifyNum = $staff->notifyNum;
  278. if ($notifyNum > 0) {
  279. $notifyNum--;
  280. $staff->notifyNum = $notifyNum;
  281. $staff->save();
  282. }
  283. }
  284. }
  285. util::success(['info' => $refund, 'itemList' => $itemList]);
  286. }
  287. //退款 lqh 2021.6.25
  288. public function actionCreateOrder()
  289. {
  290. $shopAdmin = $this->shopAdmin;
  291. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  292. util::fail('超管才能操作退款');
  293. }
  294. //国恋只有指定人有退款权限
  295. if ($this->mainId == 7704) {
  296. if (in_array($this->shopAdminId, [133727, 133545]) == false) {
  297. util::fail('无法操作');
  298. }
  299. }
  300. $post = Yii::$app->request->post();
  301. $id = isset($post['id']) ? $post['id'] : 0;
  302. $refundType = $post['refundType'] ?? 1;
  303. //避免网络延迟,暂时重复申请
  304. $cacheKey = 'ghs_order_refund_' . $id;
  305. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  306. if (!empty($has)) {
  307. util::fail('请10秒之后再提交');
  308. }
  309. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 10, 'has']);
  310. $connection = Yii::$app->db;
  311. $transaction = $connection->beginTransaction();
  312. try {
  313. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  314. //花材列表结构
  315. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
  316. $productJson = $post['product'] ?? '';
  317. if (empty($productJson)) {
  318. util::fail('请选择花材');
  319. }
  320. $productList = json_decode($productJson, true);
  321. if (!is_array($productList)) {
  322. util::fail('请选择花材哦');
  323. }
  324. $post['product'] = $productList;
  325. } else {
  326. //仅退款花材直接设置为空
  327. $post['product'] = [];
  328. }
  329. $post['price'] = $post['price'] ?? 0;
  330. if ($post['price'] <= 0) {
  331. util::fail("退款金额不能小于0");
  332. }
  333. $post['shopId'] = $this->shopId;
  334. $post['sjId'] = $this->sjId;
  335. $post['shopAdminId'] = $this->shopAdminId;
  336. $post['mainId'] = $this->mainId;
  337. $shopAdmin = $this->shopAdmin;
  338. $adminName = $shopAdmin['name'] ?? '';
  339. $post['shopAdminName'] = $adminName;
  340. $respond = OrderService::refund($id, $post);
  341. $refundId = $respond->id;
  342. $refundInfo = RefundOrderClass::getById($refundId, true);
  343. RefundOrderService::passRefund($refundInfo);
  344. $transaction->commit();
  345. util::success($respond);
  346. } catch (\Exception $exception) {
  347. $transaction->rollBack();
  348. Yii::info("退款出错了,报错信息:" . $exception->getMessage());
  349. util::fail('操作失败');
  350. }
  351. }
  352. }