RefundController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. $addTime = $order->addTime ?? '';
  97. $current = time();
  98. if ($order->debt != 1 && (strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  99. util::fail('历史订单,不能再发起退款');
  100. }
  101. if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
  102. util::fail('历史订单,不能发起退款哦');
  103. }
  104. //退款类型 1退货并退款 2 仅退款
  105. $refundType = $post['refundType'] ?? 1;
  106. if ($refundType == HdRefundClass::REFUND_TYPE_MONEY_GOOD) {
  107. //商品花材结构 property 0商品 1花材
  108. //[{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎',property:1}]
  109. $productJson = $post['product'] ?? '';
  110. if (empty($productJson)) {
  111. util::fail('请选择商品');
  112. }
  113. $productList = json_decode($productJson, true);
  114. if (!is_array($productList)) {
  115. util::fail('请选择商品哦');
  116. }
  117. $post['product'] = $productList;
  118. } else {
  119. //仅退款商品花材直接设置为空
  120. $post['product'] = [];
  121. }
  122. $post['price'] = $post['price'] ?? 0;
  123. if ($post['price'] <= 0) {
  124. util::fail("退款金额不能小于0");
  125. }
  126. if (bccomp($post['price'], $order->orderPrice, 2) == 1) {
  127. util::fail("退款金额超过订单金额");
  128. }
  129. $post['shopId'] = $this->shopId;
  130. $post['sjId'] = $this->sjId;
  131. $post['shopAdminId'] = $this->shopAdminId;
  132. $post['mainId'] = $this->mainId;
  133. $shopAdmin = $this->shopAdmin;
  134. $adminName = $shopAdmin['name'] ?? '';
  135. $post['shopAdminName'] = $adminName;
  136. HdRefundService::addRefund($post, $order);
  137. $transaction->commit();
  138. //制作单取消通知
  139. $shopId = $this->shopId ?? 0;
  140. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  141. ShopExtClass::orderCancelRemind($shopExt, $order);
  142. util::complete();
  143. } catch (\Exception $exception) {
  144. $transaction->rollBack();
  145. Yii::info("出错了:" . $exception->getMessage());
  146. util::fail('操作失败');
  147. }
  148. util::complete();
  149. }
  150. public function actionAllRefund()
  151. {
  152. $get = Yii::$app->request->get();
  153. $connection = Yii::$app->db;
  154. $transaction = $connection->beginTransaction();
  155. try {
  156. $id = $get['id'] ?? 0;
  157. $order = OrderClass::getById($id, true);
  158. OrderClass::valid($order, $this->mainId);
  159. $shopId = $this->shopId ?? 0;
  160. $sjId = $this->sjId ?? 0;
  161. $mainId = $this->mainId ?? 0;
  162. $post = [];
  163. $post['price'] = $order->actPrice ?? 0;
  164. $post['refundType'] = HdRefundClass::REFUND_TYPE_MONEY_GOOD;
  165. $post['id'] = $order->id ?? 0;
  166. $post['shopId'] = $shopId;
  167. $post['sjId'] = $sjId;
  168. $post['shopAdminId'] = 0;
  169. $post['mainId'] = $mainId;
  170. $staff = $this->shopAdmin;
  171. $post['shopAdminName'] = $staff->name ?? '';
  172. $orderSn = $order->orderSn ?? '';
  173. $orderGoods = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  174. $productData = [];
  175. if (!empty($orderGoods)) {
  176. foreach ($orderGoods as $goodsItem) {
  177. $productData[] = ['productId' => $goodsItem->goodsId, 'num' => $goodsItem->num, 'unitPrice' => $goodsItem->unitPrice, 'property' => 0,];
  178. }
  179. }
  180. $orderItem = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  181. if (!empty($orderItem)) {
  182. foreach ($orderItem as $singe) {
  183. $unitType = $singe->unitType;
  184. $productData[] = ['productId' => $singe->itemId, 'num' => $singe->num, 'unitPrice' => $singe->unitPrice, 'property' => 1, 'unitType' => $unitType];
  185. }
  186. }
  187. $post['product'] = $productData;
  188. HdRefundService::addRefund($post, $order);
  189. $transaction->commit();
  190. //制作单取消通知
  191. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  192. ShopExtClass::orderCancelRemind($shopExt, $order);
  193. util::complete();
  194. } catch (\Exception $e) {
  195. $transaction->rollBack();
  196. $msg = $e->getMessage();
  197. noticeUtil::push("取消报错了,错误信息:{$msg}", '15280215347');
  198. util::fail('退款失败');
  199. }
  200. }
  201. }