RefundController.php 12 KB

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