request->get(); $id = $get['id'] ?? ''; $order = OrderClass::getById($id, true); if (empty($order)) { util::fail('没有找到订单'); } if ($order->mainId != $this->mainId) { util::fail('不是你的订单'); } $staff = $this->shopAdmin; if ($staff->mobile != '15280215347') { util::fail('请联系管理员操作'); } $shop = $this->shop; $respond = HdRefundClass::onlyRefundAmount($shop, $order); $status = $respond['status']; $msg = $respond['msg']; util::success(['status' => $status], $msg); } //列表(含无原单 orderId=0;sameDay 筛选) public function actionList() { $get = Yii::$app->request->get(); $where = []; $where['mainId'] = $this->mainId; $orderSn = $get['orderSn'] ?? ''; if (!empty($orderSn)) { $where['orderSn'] = $orderSn; } $customId = intval($get['customId'] ?? 0); if ($customId > 0) { $where['customId'] = $customId; } // sameDay:-1全部 1当天售后 0隔天售后 $sameDay = isset($get['sameDay']) ? intval($get['sameDay']) : -1; if ($sameDay === 0 || $sameDay === 1) { $where['sameDay'] = $sameDay; } // freeOnly=1 仅无原单 if (intval($get['freeOnly'] ?? 0) === 1) { $where['orderId'] = 0; } $list = HdRefundClass::getOrderList($where); util::success($list); } //退款单详情 ssh 20220427 public function actionDetail() { $id = Yii::$app->request->get('id', 0); $refund = HdRefundClass::getById($id, true); HdRefundClass::valid($refund, $this->mainId); $refundSn = $refund->refundSn ?? ''; $itemList = HdRefundItemClass::getItemListBySn($refundSn); $goodsList = HdRefundGoodsClass::getGoodsListBySn($refundSn); util::success(['info' => $refund, 'itemList' => $itemList, 'goodsList' => $goodsList]); } //退款 ssh 20220427 public function actionCreateOrder() { $shopAdmin = $this->shopAdmin; if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) { util::fail('超管才能操作退款'); } $post = Yii::$app->request->post(); $id = isset($post['id']) ? $post['id'] : 0; $version = $post['version'] ?? 1; if ($version == 1) { util::fail('请升级小程序'); } $refundType = $post['refundType'] ?? 1;//1退货并退款2只退款 //避免重复提交 util::checkRepeatCommit($id, 10); $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); try { $order = OrderClass::getById($id, true); OrderClass::valid($order, $this->mainId); if ($order->payStatus == 0) { util::fail('订单没有付款'); } if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) { util::fail("待付款订单无法退款"); } if ($order->status == OrderClass::ORDER_STATUS_CANCEL) { util::fail("取消订单不能发起退款"); } if ($order->fromType == 4) { util::fail('请在美团APP发起退货退款'); } if ($order->debt != 1 && $order->addTime < '2023-10-17 00:00:00') { util::fail('2023年10月17日前订单无法申请退款'); } if ($order->status != 4) { //util::fail('订单完成了才能售后'); } $addTime = $order->addTime ?? ''; $current = time(); if ($order->debt != 1 && (strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) { util::fail('历史订单,不能再发起退款'); } if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) { util::fail('历史订单,不能发起退款哦'); } if ($refundType == 1) { //商品花材结构 property 0花束 1花材 //[{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎',property:1}] $productJson = $post['product'] ?? ''; if (empty($productJson)) { util::fail('请选择商品'); } $productList = json_decode($productJson, true); if (!is_array($productList)) { util::fail('请选择商品哦'); } $post['product'] = $productList; } $post['price'] = $post['price'] ?? 0; if ($post['price'] <= 0) { util::fail("退款金额不能小于0"); } if (bccomp($post['price'], $order->orderPrice, 2) == 1) { util::fail("退款金额超过订单金额"); } $post['shopId'] = $this->shopId; $post['sjId'] = $this->sjId; $post['shopAdminId'] = $this->shopAdminId; $post['mainId'] = $this->mainId; $shopAdmin = $this->shopAdmin; $adminName = $shopAdmin['name'] ?? ''; $post['shopAdminName'] = $adminName; $main = $this->main; // 非付款日 / 已结清等:强制隔天售后写入(sameDay=0) if (HdNextDayRefundClass::mustUseNextDay($order, $main)) { $post['sameDay'] = HdRefundClass::SAME_DAY_NO; } else { $ret = OrderClass::ifShPay($order, $main, $post['price']); if (($ret['shAddPay'] ?? 0) == 1) { $post['sameDay'] = HdRefundClass::SAME_DAY_NO; } } $refund = HdRefundService::addRefund($post, $order); $sameDay = intval($refund->sameDay ?? HdRefundClass::SAME_DAY_YES); // 当天售后:累计消费在此回退;隔天已在 applyHdAmountAndFund 处理 if ($sameDay === HdRefundClass::SAME_DAY_YES) { $custom = CustomClass::getLockById($order->customId); if (!empty($custom)) { $custom->buyAmount = bcsub($custom->buyAmount, $post['price'], 2); $customName = $custom->name ?? $order->customName ?? ''; $orderSnText = $order->orderSn ?? ''; $shopAdminName = $this->shopAdminName; $eventPrefix = $customName; if ($eventPrefix === '' && $orderSnText !== '') { $eventPrefix = '客户'; } $custom->eventRemark = $eventPrefix . '售后' . $orderSnText; $custom->relateId = $order->id; $custom->relateType = 1; $custom->staffId = $this->shopAdminId; $custom->staffName = $shopAdminName; $custom->mainId = $this->mainId; $custom->save(); $hd = HdClass::getLockById($custom->hdId); if (!empty($hd)) { $hd->expendAmount = bcsub($hd->expendAmount, $post['price'], 2); $hd->save(); } } } //红包退款 $refundHb = $post['refundHb'] ?? 0; $hbId = $order->hbId; if ($refundHb == 1 && $hbId > 0) { $updatedOrder = OrderClass::getById($id, true, 'id, actPrice, hbId, hbAmount'); if ($updatedOrder->actPrice <= 0.00) { hbClass::hbBack($updatedOrder); } } $transaction->commit(); DistributionCommissionClass::tryCalcDistributionAfterPay((int)$id); $shopId = $this->shopId ?? 0; $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true); ShopExtClass::orderCancelRemind($shopExt, $order); $refund = HdRefundClass::getById($refund->id, true); $payload = HdNextDayRefundClass::buildFundResultPayload($refund); // 需选手选资金落地时返回详情,前端跳转 forwardResult if (!empty($payload['needFundAction'])) { util::success($payload); } util::success($payload); } catch (\Exception $exception) { $transaction->rollBack(); Yii::info("出错了:" . $exception->getMessage()); util::fail($exception->getMessage() ?: '操作失败'); } } /** * 无原单退款(工作台「退款」) */ public function actionCreateFreeRefund() { $shopAdmin = $this->shopAdmin; if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) { util::fail('超管才能操作退款'); } $post = Yii::$app->request->post(); $customId = intval($post['customId'] ?? 0); util::checkRepeatCommit('hd_free_refund_' . $this->mainId . '_' . $customId, 5); $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); try { $returnWay = intval($post['returnWay'] ?? -1); if ($returnWay === 1) { $refundType = HdRefundClass::REFUND_TYPE_MONEY_GOOD; } elseif ($returnWay === 0) { $refundType = HdRefundClass::REFUND_TYPE_MONEY; } else { $refundType = intval($post['refundType'] ?? HdRefundClass::REFUND_TYPE_MONEY_GOOD); } $post['refundType'] = $refundType; if ($refundType == HdRefundClass::REFUND_TYPE_MONEY_GOOD) { $productJson = $post['product'] ?? ''; if (empty($productJson)) { util::fail('请选择花材'); } $productList = is_array($productJson) ? $productJson : json_decode($productJson, true); if (!is_array($productList) || empty($productList)) { util::fail('请选择花材哦'); } $post['product'] = $productList; } else { $productJson = $post['product'] ?? ''; if (!empty($productJson)) { $productList = is_array($productJson) ? $productJson : json_decode($productJson, true); $post['product'] = is_array($productList) ? $productList : []; } else { $post['product'] = []; } } $post['price'] = $post['price'] ?? 0; if ($post['price'] <= 0) { util::fail('退款金额不能小于0'); } $post['shopId'] = $this->shopId; $post['sjId'] = $this->sjId; $post['shopAdminId'] = $this->shopAdminId; $post['mainId'] = $this->mainId; $post['shopAdminName'] = $shopAdmin['name'] ?? ''; $result = HdRefundService::createFreeRefund($post); $transaction->commit(); util::success($result); } catch (\Exception $exception) { if ($transaction->isActive) { $transaction->rollBack(); } Yii::info('无原单退款出错:' . $exception->getMessage()); util::fail($exception->getMessage() ?: '操作失败'); } } /** * 确认已线下原路退回 */ public function actionMarkHasReturn() { $post = Yii::$app->request->post(); $id = intval($post['id'] ?? 0); $refund = HdRefundClass::getById($id, true); HdRefundClass::valid($refund, $this->mainId); $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); try { $refund = HdNextDayRefundClass::markHasReturn($refund); $transaction->commit(); util::success(HdNextDayRefundClass::buildFundResultPayload($refund)); } catch (\Exception $exception) { if ($transaction->isActive) { $transaction->rollBack(); } util::fail($exception->getMessage() ?: '操作失败'); } } /** * 不想线下转账:返充到客户余额 */ public function actionReturnBalance() { $post = Yii::$app->request->post(); $id = intval($post['id'] ?? 0); $refund = HdRefundClass::getById($id, true); HdRefundClass::valid($refund, $this->mainId); $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); try { $order = null; $orderId = intval($refund->orderId ?? 0); if ($orderId > 0) { $order = OrderClass::getById($orderId, true); } $ret = HdNextDayRefundClass::applyReturnBalance($refund, $order); $transaction->commit(); $payload = HdNextDayRefundClass::buildFundResultPayload($ret['refund']); $payload['customBalance'] = $ret['customBalance']; util::success($payload); } catch (\Exception $exception) { if ($transaction->isActive) { $transaction->rollBack(); } util::fail($exception->getMessage() ?: '操作失败'); } } public function actionAllRefund() { util::fail('功能开发中'); } /** * 商城售后申请列表(商家端) */ public function actionMallApplyList() { $get = Yii::$app->request->get(); $where = ['mainId' => $this->mainId, 'shopId' => $this->shopId]; $status = $get['status'] ?? ''; if ($status !== '' && $status !== null && $status !== 'all') { $where['status'] = (int)$status; } $orderSn = trim((string)($get['orderSn'] ?? '')); if ($orderSn !== '') { $where['orderSn'] = $orderSn; } $list = MallRefundApplyClass::getApplyList($where); if (!empty($list['list'])) { foreach ($list['list'] as &$row) { $row['statusText'] = MallRefundApplyClass::statusText($row['status'] ?? 0); } unset($row); } util::success($list); } /** * 商城售后申请详情(商家端) */ public function actionMallApplyDetail() { $id = (int)Yii::$app->request->get('id', 0); $apply = MallRefundApplyClass::getById($id, true); MallRefundApplyClass::valid($apply, $this->mainId); $data = $apply->toArray(); $data['statusText'] = MallRefundApplyClass::statusText($apply->status); $data['productList'] = []; if (!empty($data['product'])) { $data['productList'] = json_decode($data['product'], true) ?: []; } $order = OrderClass::getById((int)$apply->orderId); $data['orderInfo'] = $order ?: []; util::success($data); } /** * 商城售后申请审核通过:执行真实退款 */ public function actionMallApplyPass() { $shopAdmin = $this->shopAdmin; if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) { util::fail('超管才能审核售后'); } $post = Yii::$app->request->post(); $id = (int)($post['id'] ?? 0); util::checkRepeatCommit('mall_apply_pass_' . $id, 10); $apply = MallRefundApplyClass::getById($id, true); MallRefundApplyClass::valid($apply, $this->mainId); $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); try { $order = OrderClass::getById((int)$apply->orderId, true); OrderClass::valid($order, $this->mainId); $admin = [ 'id' => (int)$this->shopAdminId, 'name' => $this->shopAdminName ?? ($shopAdmin['name'] ?? ''), 'shopId' => (int)$this->shopId, 'sjId' => (int)$this->sjId, 'mainId' => (int)$this->mainId, ]; MallRefundApplyService::approve($apply, $order, $admin); $orderId = (int)$apply->orderId; $transaction->commit(); // TODO 事务提交后再重算分佣(与即时售后 create-order 一致,避免嵌套事务) DistributionCommissionClass::tryCalcDistributionAfterPay($orderId); util::complete('审核通过,退款已处理'); } catch (\Exception $e) { $transaction->rollBack(); Yii::info('商城售后审核通过失败:' . $e->getMessage()); util::fail($e->getMessage() ?: '审核失败'); } } /** * 商城售后申请驳回 */ public function actionMallApplyReject() { $shopAdmin = $this->shopAdmin; if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) { util::fail('超管才能审核售后'); } $post = Yii::$app->request->post(); $id = (int)($post['id'] ?? 0); $reason = $post['rejectReason'] ?? ($post['reason'] ?? ''); $apply = MallRefundApplyClass::getById($id, true); MallRefundApplyClass::valid($apply, $this->mainId); $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); try { $admin = [ 'id' => (int)$this->shopAdminId, 'name' => $this->shopAdminName ?? ($shopAdmin['name'] ?? ''), ]; MallRefundApplyService::reject($apply, $reason, $admin); $transaction->commit(); util::complete('已驳回'); } catch (\Exception $e) { $transaction->rollBack(); Yii::info('商城售后驳回失败:' . $e->getMessage()); util::fail($e->getMessage() ?: '驳回失败'); } } }