RefundController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. //退款退货
  3. namespace hd\controllers;
  4. use biz\shop\classes\ShopExtClass;
  5. use bizHd\order\classes\OrderClass;
  6. use bizHd\order\classes\OrderGoodsClass;
  7. use bizHd\order\classes\OrderItemClass;
  8. use bizHd\refund\classes\HdRefundClass;
  9. use bizHd\refund\classes\HdRefundGoodsClass;
  10. use bizHd\refund\classes\HdRefundItemClass;
  11. use bizHd\refund\services\HdRefundService;
  12. use common\components\noticeUtil;
  13. use Yii;
  14. use common\components\util;
  15. class RefundController extends BaseController
  16. {
  17. //列表
  18. public function actionList()
  19. {
  20. $get = Yii::$app->request->get();
  21. $where = [];
  22. $where['mainId'] = $this->mainId;
  23. $orderSn = $get['orderSn'] ?? '';
  24. if (!empty($orderSn)) {
  25. $where['orderSn'] = $orderSn;
  26. }
  27. $list = HdRefundClass::getOrderList($where);
  28. util::success($list);
  29. }
  30. //退款单详情 ssh 20220427
  31. public function actionDetail()
  32. {
  33. $id = Yii::$app->request->get('id', 0);
  34. $refund = HdRefundClass::getById($id, true);
  35. HdRefundClass::valid($refund, $this->mainId);
  36. $refundSn = $refund->refundSn ?? '';
  37. $itemList = HdRefundItemClass::getItemListBySn($refundSn);
  38. $goodsList = HdRefundGoodsClass::getGoodsListBySn($refundSn);
  39. util::success(['info' => $refund, 'itemList' => $itemList, 'goodsList' => $goodsList]);
  40. }
  41. //退款 ssh 20220427
  42. public function actionCreateOrder()
  43. {
  44. $shopAdmin = $this->shopAdmin;
  45. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  46. util::fail('超管才能操作退款');
  47. }
  48. $post = Yii::$app->request->post();
  49. $id = isset($post['id']) ? $post['id'] : 0;
  50. //避免网络延迟,暂时重复申请
  51. $cacheKey = 'hd_order_refund_' . $id;
  52. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  53. if (!empty($has)) {
  54. util::fail('请10秒之后再提交');
  55. }
  56. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 10, 'has']);
  57. $connection = Yii::$app->db;
  58. $transaction = $connection->beginTransaction();
  59. try {
  60. $order = OrderClass::getById($id, true);
  61. OrderClass::valid($order, $this->mainId);
  62. if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) {
  63. util::fail("待付款订单无法退款");
  64. }
  65. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  66. util::fail("取消订单不能发起退款");
  67. }
  68. if ($order->fromType == 4) {
  69. util::fail('请在美团APP发起退货退款');
  70. }
  71. if ($order->debt != 1 && $order->addTime < '2023-10-17 00:00:00') {
  72. util::fail('10月17日前订单无法申请退款');
  73. }
  74. $addTime = $order->addTime ?? '';
  75. $current = time();
  76. if ($order->debt != 1 && (strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  77. util::fail('历史订单,不能再发起退款');
  78. }
  79. if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
  80. util::fail('历史订单,不能发起退款哦');
  81. }
  82. //退款类型 1退货并退款 2 仅退款
  83. $refundType = $post['refundType'] ?? 1;
  84. if ($refundType == HdRefundClass::REFUND_TYPE_MONEY_GOOD) {
  85. //商品花材结构 property 0商品 1花材
  86. //[{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎',property:1}]
  87. $productJson = $post['product'] ?? '';
  88. if (empty($productJson)) {
  89. util::fail('请选择商品');
  90. }
  91. $productList = json_decode($productJson, true);
  92. if (!is_array($productList)) {
  93. util::fail('请选择商品哦');
  94. }
  95. $post['product'] = $productList;
  96. } else {
  97. //仅退款商品花材直接设置为空
  98. $post['product'] = [];
  99. }
  100. $post['price'] = $post['price'] ?? 0;
  101. if ($post['price'] <= 0) {
  102. util::fail("退款金额不能小于0");
  103. }
  104. if (bccomp($post['price'], $order->orderPrice, 2) == 1) {
  105. util::fail("退款金额超过订单金额");
  106. }
  107. $post['shopId'] = $this->shopId;
  108. $post['sjId'] = $this->sjId;
  109. $post['shopAdminId'] = $this->shopAdminId;
  110. $post['mainId'] = $this->mainId;
  111. $shopAdmin = $this->shopAdmin;
  112. $adminName = $shopAdmin['name'] ?? '';
  113. $post['shopAdminName'] = $adminName;
  114. HdRefundService::addRefund($post, $order);
  115. $transaction->commit();
  116. //制作单取消通知
  117. $shopId = $this->shopId ?? 0;
  118. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  119. ShopExtClass::orderCancelRemind($shopExt, $order);
  120. util::complete();
  121. } catch (\Exception $exception) {
  122. $transaction->rollBack();
  123. Yii::info("出错了:" . $exception->getMessage());
  124. util::fail('操作失败');
  125. }
  126. util::complete();
  127. }
  128. public function actionAllRefund()
  129. {
  130. $get = Yii::$app->request->get();
  131. $connection = Yii::$app->db;
  132. $transaction = $connection->beginTransaction();
  133. try {
  134. $id = $get['id'] ?? 0;
  135. $order = OrderClass::getById($id, true);
  136. OrderClass::valid($order, $this->mainId);
  137. $shopId = $this->shopId ?? 0;
  138. $sjId = $this->sjId ?? 0;
  139. $mainId = $this->mainId ?? 0;
  140. $post = [];
  141. $post['price'] = $order->actPrice ?? 0;
  142. $post['refundType'] = HdRefundClass::REFUND_TYPE_MONEY_GOOD;
  143. $post['id'] = $order->id ?? 0;
  144. $post['shopId'] = $shopId;
  145. $post['sjId'] = $sjId;
  146. $post['shopAdminId'] = 0;
  147. $post['mainId'] = $mainId;
  148. $staff = $this->shopAdmin;
  149. $post['shopAdminName'] = $staff->name ?? '';
  150. $orderSn = $order->orderSn ?? '';
  151. $orderGoods = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  152. $productData = [];
  153. if (!empty($orderGoods)) {
  154. foreach ($orderGoods as $goodsItem) {
  155. $productData[] = ['productId' => $goodsItem->goodsId, 'num' => $goodsItem->num, 'unitPrice' => $goodsItem->unitPrice, 'property' => 0,];
  156. }
  157. }
  158. $orderItem = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  159. if (!empty($orderItem)) {
  160. foreach ($orderItem as $singe) {
  161. $productData[] = ['productId' => $singe->itemId, 'num' => $singe->num, 'unitPrice' => $singe->unitPrice, 'property' => 1,];
  162. }
  163. }
  164. $post['product'] = $productData;
  165. HdRefundService::addRefund($post, $order);
  166. $transaction->commit();
  167. //制作单取消通知
  168. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  169. ShopExtClass::orderCancelRemind($shopExt, $order);
  170. util::complete();
  171. } catch (\Exception $e) {
  172. $transaction->rollBack();
  173. $msg = $e->getMessage();
  174. noticeUtil::push("取消报错了,错误信息:{$msg}", '15280215347');
  175. util::fail('退款失败');
  176. }
  177. }
  178. }