RefundController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. $relateOrderSn = $refund['relateOrderSn'] ?? '';
  168. $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  169. if (empty($order)) {
  170. util::fail('数据出错了');
  171. }
  172. $refund['relateOrderId'] = $order->id;
  173. if (!empty($imgString)) {
  174. $imgArr = json_decode($imgString, true);
  175. $smallImgList = [];
  176. $bigImgList = [];
  177. if (!empty($imgArr)) {
  178. foreach ($imgArr as $image) {
  179. $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  180. $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  181. $smallImgList[] = $smallImg;
  182. $bigImgList[] = $bigImg;
  183. }
  184. }
  185. $refund['imgList'] = $imgArr;
  186. $refund['smallImgList'] = $smallImgList;
  187. $refund['bigImgList'] = $bigImgList;
  188. }
  189. $orderSn = $refund['orderSn'] ?? '';
  190. $itemList = RefundOrderItemClass::getOrderItemDetail($orderSn);
  191. util::success(['info' => $refund, 'itemList' => $itemList]);
  192. }
  193. //退款 lqh 2021.6.25
  194. public function actionCreateOrder()
  195. {
  196. $shopAdmin = $this->shopAdmin;
  197. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  198. util::fail('超管才能操作退款');
  199. }
  200. //国恋只有指定人有退款权限
  201. if ($this->mainId == 7704) {
  202. if (in_array($this->shopAdminId, [133727, 133545]) == false) {
  203. util::fail('没有权限操作');
  204. }
  205. }
  206. $connection = Yii::$app->db;
  207. $transaction = $connection->beginTransaction();
  208. try {
  209. $post = Yii::$app->request->post();
  210. $id = isset($post['id']) ? $post['id'] : 0;
  211. $refundType = $post['refundType'] ?? 1;
  212. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  213. //花材列表结构
  214. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
  215. $productJson = $post['product'] ?? '';
  216. if (empty($productJson)) {
  217. util::fail('请选择花材');
  218. }
  219. $productList = json_decode($productJson, true);
  220. if (!is_array($productList)) {
  221. util::fail('请选择花材哦');
  222. }
  223. $post['product'] = $productList;
  224. } else {
  225. //仅退款花材直接设置为空
  226. $post['product'] = [];
  227. }
  228. $post['price'] = $post['price'] ?? 0;
  229. if ($post['price'] <= 0) {
  230. util::fail("退款金额不能小于0");
  231. }
  232. $post['shopId'] = $this->shopId;
  233. $post['sjId'] = $this->sjId;
  234. $post['shopAdminId'] = $this->shopAdminId;
  235. $post['mainId'] = $this->mainId;
  236. $shopAdmin = $this->shopAdmin;
  237. $adminName = $shopAdmin['name'] ?? '';
  238. $post['shopAdminName'] = $adminName;
  239. $respond = OrderService::refund($id, $post);
  240. $refundId = $respond->id;
  241. $refundInfo = RefundOrderClass::getById($refundId, true);
  242. RefundOrderService::passRefund($refundInfo);
  243. $transaction->commit();
  244. util::success($respond);
  245. } catch (\Exception $exception) {
  246. $transaction->rollBack();
  247. Yii::info("退款出错了,报错信息:" . $exception->getMessage());
  248. util::fail('操作失败');
  249. }
  250. }
  251. }