RefundController.php 12 KB

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