RefundController.php 10 KB

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