RefundController.php 14 KB

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