RefundController.php 8.6 KB

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