RefundController.php 11 KB

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