RefundController.php 12 KB

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