| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066 |
- <?php
- //退款退货
- namespace ghs\controllers;
- use bizGhs\notify\classes\NotifyClass;
- use bizGhs\order\classes\CheckOrderClass;
- use bizGhs\order\classes\RefundOrderItemClass;
- use bizGhs\order\services\RefundOrderService;
- use bizGhs\order\classes\NextDayRefundClass;
- use bizGhs\shop\classes\ShopClass;
- use bizHd\cg\classes\CgRefundClass;
- use bizHd\cg\classes\CgRefundItemClass;
- use bizHd\notice\classes\NoticeClass;
- use bizHd\purchase\classes\PurchaseClass;
- use common\components\dateUtil;
- use common\components\dict;
- use common\components\imgUtil;
- use Yii;
- use bizGhs\order\classes\RefundOrderClass;
- use common\components\util;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\order\classes\OrderClass;
- use bizGhs\order\services\OrderService;
- use bizGhs\order\classes\StockWastageOrderClass;
- class RefundController extends BaseController
- {
- /**
- * 原单是否可走跨天售后(售后页预检)
- * GET: orderId
- */
- public function actionCheckNextDayEligible()
- {
- $orderId = Yii::$app->request->get('orderId', 0);
- $order = OrderClass::getById($orderId, true);
- if (empty($order) || intval($order->mainId) !== intval($this->mainId)) {
- util::fail('没有找到订单');
- }
- $check = NextDayRefundClass::checkEligible($order);
- $check['hasPayTime'] = NextDayRefundClass::hasValidPayTime($order) ? 1 : 0;
- $check['hasNextDay'] = bccomp((string)($order->nextDayTkPrice ?? '0'), '0', 2) > 0 ? 1 : 0;
- $check['nextDayTkPrice'] = $order->nextDayTkPrice ?? '0.00';
- $check['tkPrice'] = $order->tkPrice ?? '0.00';
- $check['actPrice'] = $order->actPrice ?? '0.00';
- $check['payWay'] = $order->payWay ?? 0;
- $check['onlinePay'] = $order->onlinePay ?? 0;
- $check['debtPrice'] = $order->debtPrice ?? '0.00';
- $check['remainDebtPrice'] = $order->remainDebtPrice ?? '0.00';
- $check['clearId'] = intval($order->clearId ?? 0);
- $check['payTime'] = $order->payTime ?? '';
- util::success($check);
- }
- //取消的订单退钱,特殊情况的操作:商家取消订单时,客户同时付款成功了,造成的流程冲突,需要退钱 ssh 20250429
- public function actionRefundMoney()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? '';
- $order = OrderClass::getById($id, true);
- if (empty($order)) {
- util::fail('没有找到订单');
- }
- if ($order->mainId != $this->mainId) {
- util::fail('不是你的订单');
- }
- $addTime = $order->addTime;
- $addTime = strtotime($addTime);
- $earTime = strtotime('2025-05-01 00:00:00');
- if ($addTime < $earTime) {
- util::fail('2025.5.1号前的订单,不能退款');
- }
- $shop = $this->shop;
- $purchaseId = $order->purchaseId ?? 0;
- $cg = PurchaseClass::getById($purchaseId, true);
- if (empty($cg)) {
- util::fail('没有找到订单');
- }
- $staff = $this->shopAdmin;
- if ($staff->mobile != '15280215347') {
- util::fail('请联系管理员操作');
- }
- $respond = CgRefundClass::onlyRefundMoney($shop, $cg);
- $status = $respond['status'];
- $msg = $respond['msg'];
- util::success(['status' => $status], $msg);
- }
- //修改金额 ssh 20240412
- public function actionModifyAmount()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $amount = $get['amount'] ?? 0;
- $refund = RefundOrderClass::getById($id, true);
- RefundOrderClass::valid($refund, $this->mainId);
- if (is_numeric($amount) == false) {
- util::fail('请填写金额');
- }
- if ($refund->status != 0) {
- util::fail('待审核订单才能修改');
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $refund->refundPrice = $amount;
- $refund->save();
- $cgRefundId = $refund->cgRefundId ?? 0;
- $cgRefund = CgRefundClass::getById($cgRefundId, true);
- if (empty($cgRefund)) {
- util::fail('没有找到退款信息');
- }
- $cgRefund->refundPrice = $amount;
- $cgRefund->save();
- $transaction->commit();
- util::complete();
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::info("操作失败原因:" . $e->getMessage());
- util::fail('操作失败');
- }
- }
- //修改申请原因 ssh 20240302
- public function actionModifyCause()
- {
- $staff = $this->shopAdmin;
- if (isset($staff->finance) == false || $staff->finance == 0) {
- util::fail('请有财务权限的人修改');
- }
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $cause = $get['cause'] ?? 0;
- $refund = RefundOrderClass::getById($id, true);
- RefundOrderClass::valid($refund, $this->mainId);
- $refund->cause = $cause;
- $refund->save();
- $orderSn = $refund->orderSn ?? '';
- RefundOrderItemClass::updateByCondition(['orderSn' => $orderSn], ['cause' => $cause]);
- $cgRefundId = $refund->cgRefundId ?? 0;
- $cgRefund = CgRefundClass::getById($cgRefundId, true);
- if (empty($cgRefund)) {
- util::fail('没有找到退款信息');
- }
- $cgRefund->cause = $cause;
- $cgRefund->save();
- $cgOrderSn = $cgRefund->orderSn ?? '';
- CgRefundItemClass::updateByCondition(['orderSn' => $cgOrderSn], ['cause' => $cause]);
- util::complete('修改成功');
- }
- //审核通过 ssh 20230811
- public function actionPass()
- {
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('无法操作');
- }
- //多处要同步修改ghs_refund ssh 20250331
- if (getenv('YII_ENV') == 'production') {
- //小向花卉售后权限
- if ($this->mainId == 23390) {
- if (!in_array($this->adminId, [23960,77951,4])) {
- util::fail('你不能售后哦');
- }
- //小向花卉售后必须备注
- $remark = $post['remark'] ?? '';
- if (empty($remark)) {
- util::fail('请填写备注007');
- }
- }
- //国恋只有指定人有退款权限
- if ($this->mainId == 7704) {
- if (in_array($this->shopAdminId, [133727, 133545]) == false) {
- util::fail('无法操作。。');
- }
- }
- //小齐出车指定人可以售后
- if ($this->mainId == 42680) {
- if (!in_array($this->adminId, [40144])) {
- util::fail('你不能售后哦。。');
- }
- }
- }
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $version = $get['version'] ?? 0;
- $confirm = $get['confirm'] ?? 0;
- $refund = RefundOrderClass::getById($id, true);
- RefundOrderClass::valid($refund, $this->mainId);
- $relateOrderSn = $refund->relateOrderSn ?? '';
- if (empty($relateOrderSn)) {
- util::fail('没有订单信息');
- }
- $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $relateOrderSn], null, '*', null, true);
- $has = false;
- if (!empty($refundList)) {
- foreach ($refundList as $single) {
- if ($single->status == 1) {
- $has = true;
- }
- }
- }
- if ($has == true) {
- if ($version == 0) {
- util::fail('请先升级系统');
- }
- if ($confirm == 0) {
- util::success(['error' => 'notFirstApply']);
- }
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- RefundOrderService::passRefund($refund);
- $transaction->commit();
- $staff = $this->shopAdmin;
- $staffId = $staff->id;
- $staffName = $staff->name ?? '';
- $refund->shopAdminId = $staffId;
- $refund->shopAdminName = $staffName;
- $refund->save();
- //审核结果通知
- $cgRefundId = $refund->cgRefundId ?? 0;
- $cgRefund = CgRefundClass::getById($cgRefundId, true);
- $cgShopId = $cgRefund->shopId ?? 0;
- $cgShop = ShopClass::getById($cgShopId, true);
- if (!empty($cgRefund) && !empty($cgShop)) {
- NoticeClass::afterSaleNotice($cgShop, $cgRefund);
- }
- util::complete('操作成功');
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::info("操作失败原因:" . $e->getMessage());
- util::fail('操作失败');
- }
- }
- //驳回 ssh 20230813
- public function actionReject()
- {
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('无法操作');
- }
- //多处要同步修改ghs_refund ssh 20250331
- if (getenv('YII_ENV') == 'production') {
- //小向花卉售后权限
- if ($this->mainId == 23390) {
- if (!in_array($this->adminId, [23960,77951,4])) {
- util::fail('你不能售后哦。。');
- }
- //小向花卉售后必须备注
- $remark = $post['remark'] ?? '';
- if (empty($remark)) {
- util::fail('请填写备注007');
- }
- }
- //国恋只有指定人有退款权限
- if ($this->mainId == 7704) {
- if (in_array($this->shopAdminId, [133727, 133545]) == false) {
- util::fail('无法操作。。');
- }
- }
- //小齐出车指定人可以售后
- if ($this->mainId == 42680) {
- if (!in_array($this->adminId, [40144])) {
- util::fail('你不能售后哦。。。');
- }
- }
- }
- $post = Yii::$app->request->post();
- $id = $post['id'] ?? 0;
- $rejectReason = $post['rejectReason'] ?? '';
- $refund = RefundOrderClass::getById($id, true);
- RefundOrderClass::valid($refund, $this->mainId);
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- RefundOrderClass::rejectRefund($refund, $rejectReason);
- $transaction->commit();
- $staff = $this->shopAdmin;
- $staffId = $staff->id;
- $staffName = $staff->name ?? '';
- $refund->shopAdminId = $staffId;
- $refund->shopAdminName = $staffName;
- $refund->save();
- //审核结果通知
- $cgRefundId = $refund->cgRefundId ?? 0;
- $cgRefund = CgRefundClass::getById($cgRefundId, true);
- $cgShopId = $cgRefund->shopId ?? 0;
- $cgShop = ShopClass::getById($cgShopId, true);
- if (!empty($cgRefund) && !empty($cgShop)) {
- NoticeClass::afterSaleNotice($cgShop, $cgRefund);
- }
- util::complete('操作成功');
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::info("操作失败原因:" . $e->getMessage());
- util::fail('操作失败');
- }
- }
- //售后之后减少库存 ssh 20250912
- public function actionReduceStock()
- {
- $shopAdmin = $this->shopAdmin;
- if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
- util::fail('管理员才能报损');
- }
- $post = Yii::$app->request->post();
- $refundItemId = $post['refundItemId'] ?? 0;
- $refundItem = RefundOrderItemClass::getById($refundItemId, true);
- if (empty($refundItem)) {
- util::fail('没有找到退货信息');
- }
- if ($refundItem->mainId != $this->mainId) {
- util::fail('不是您的退款信息');
- }
- $reduceProductId = $refundItem->productId ?? 0;
- $reduceNum = $post['reduceNum'] ?? 0;
- $productInfo = ProductClass::getById($reduceProductId, true);
- if (empty($productInfo)) {
- util::fail('没有找到花材');
- }
- if ($productInfo->mainId != $this->mainId) {
- util::fail('不是你的花材');
- }
- $unitType = $refundItem->xhUnitType ?? 0;
- if ($unitType == 1) {
- util::fail('小单位,无法减库存');
- }
- $refundNum = $refundItem->xhNum ?? 0;
- $hasReduceNum = $refundItem->xhReduceNum ?? 0;
- $remainNum = bcsub($refundNum, $hasReduceNum);
- if ($reduceNum > $remainNum) {
- util::fail('剩余可减数量 ' . $remainNum);
- }
- $stock = $productInfo->stock ?? 0;
- if ($reduceNum > $stock) {
- util::fail('库存不足了');
- }
- $orderSn = $refundItem->orderSn ?? '';
- $bigNum = bcsub($stock, $reduceNum);
- $refundItem->xhReduceNum = bcadd($refundItem->xhReduceNum, $reduceNum);
- $refundItem->save();
- $shop = $this->shop;
- $sjId = $shop->sjId ?? 0;
- $shopId = $shop->id ?? 0;
- $mainId = $shop->mainId ?? 0;
- $adminId = $this->adminId;
- $staff = $this->shopAdmin;
- $staffName = $staff->name ?? '';
- $params = [
- 'pdType' => '',
- 'itemInfo' => [['productId' => $reduceProductId, 'bigNum' => $bigNum, 'smallNum' => '']],
- 'remark' => '售后' . $orderSn . '直接减少库存',
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'adminId' => $adminId,
- 'mainId' => $mainId,
- 'staffName' => $staffName,
- ];
- CheckOrderClass::opOrder($params);
- util::complete('操作成功');
- }
- //退款之后报损 ssh 20230409
- public function actionRefundWaste()
- {
- $shopAdmin = $this->shopAdmin;
- if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
- util::fail('管理员才能报损');
- }
- $post = Yii::$app->request->post();
- $refundItemId = $post['refundItemId'] ?? 0;
- $refundItem = RefundOrderItemClass::getById($refundItemId, true);
- if (empty($refundItem)) {
- util::fail('没有找到退货信息');
- }
- if ($refundItem->mainId != $this->mainId) {
- util::fail('不是您的退款信息');
- }
- $unitType = $refundItem->xhUnitType ?? 0;
- if ($unitType == 1) {
- util::fail('小单位,无法报损');
- }
- $wasteProductId = $refundItem->productId ?? 0;
- $wasteNum = $post['wasteNum'] ?? 0;
- unset($post['refundItemId']);
- unset($post['wasteNum']);
- unset($post['wasteProductId']);
- $productInfo = ProductClass::getById($wasteProductId, true);
- if (empty($productInfo)) {
- util::fail('没有找到花材');
- }
- $ptItemId = $productInfo->itemId ?? 0;
- $ratio = $productInfo->ratio ?? 20;
- $refundNum = $refundItem->xhNum ?? 0;
- $hasWasteNum = $refundItem->xhWasteNum ?? 0;
- $remainNum = bcsub($refundNum, $hasWasteNum);
- if ($wasteNum > $remainNum) {
- util::fail('剩余可报损数量 ' . $remainNum);
- }
- $productList = [['productId' => $wasteProductId, 'bigNum' => $wasteNum, 'smallNum' => 0, 'classId' => 0, 'itemId' => $ptItemId, 'ratio' => $ratio]];
- $post['shopId'] = $this->shopId;
- $post['sjId'] = $this->sjId;
- $post['mainId'] = $this->mainId;
- $post['shopAdminId'] = $this->shopAdminId;
- $adminName = $shopAdmin['name'] ?? '';
- $post['shopAdminName'] = $adminName;
- //属于售后报损
- $post['saleType'] = 1;
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $refundItem->xhWasteNum = bcadd($refundItem->xhWasteNum, $wasteNum);
- $refundItem->save();
- $post['product'] = $productList;
- $return = StockWastageOrderClass::addOrder($post);
- $transaction->commit();
- util::success($return);
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::info("报损失败原因:" . $e->getMessage());
- util::fail('报损失败');
- }
- }
- /**
- * 销售售后记录列表:ghs /order/refund-list、小程序售后记录共用。
- * matchPayTime/fromAffect=1 时按原单 payTime + 售后 addTime,与跨天影响统计同口径。
- */
- public function actionList()
- {
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $this->mainId;
- $orderSn = $get['orderSn'] ?? '';
- if (!empty($orderSn)) {
- $where['relateOrderSn'] = $orderSn;
- }
- // 可选:只查无关联销售单
- $freeOnly = intval($get['freeOnly'] ?? 0);
- if ($freeOnly === 1) {
- $where['relateOrderSn'] = '';
- }
- // sameDay:-1全部 1当天售后 0跨天售后
- $sameDay = isset($get['sameDay']) ? intval($get['sameDay']) : -1;
- if ($sameDay === 0 || $sameDay === 1) {
- $where['sameDay'] = $sameDay;
- }
- $searchTime = $get['searchTime'] ?? '';
- if (!empty($searchTime)) {
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
- $where['orderTime'] = ['between', [$period['startTime'], $period['endTime']]];
- }
- $refundSearchTime = $get['refundSearchTime'] ?? '';
- // 工作台退款记录:未传时间时默认近30天
- $useDefaultLast30 = intval($get['useDefaultLast30'] ?? 0);
- if ($useDefaultLast30 === 1 && empty($refundSearchTime) && empty($searchTime)) {
- $refundSearchTime = 'last30Days';
- }
- // 申请日只用 refundStart/End,禁止回落到订单日 startTime/endTime;
- // 否则从跨天影响点进来会把「原单日」当成「售后创建日」,列表金额偏大
- if (!empty($refundSearchTime)) {
- $refundStartTime = $get['refundStartTime'] ?? '';
- $refundEndTime = $get['refundEndTime'] ?? '';
- $period = dateUtil::formatTime($refundSearchTime, $refundStartTime, $refundEndTime);
- $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
- }
- $customId = $get['customId'] ?? '';
- if (!empty($customId)) {
- $where['customId'] = $customId;
- }
- $shopAdminId = $get['shopAdminId'] ?? 0;
- if (!empty($shopAdminId)) {
- $where['shopAdminId'] = $shopAdminId;
- }
- $status = $get['status'] ?? -1;
- if ($status > -1) {
- $where['status'] = $status;
- }
- $export = $get['export'] ?? 0;
- if ($export == 1) {
- if (isset($where['orderTime']) == false && isset($where['addTime']) == false) {
- util::fail('请选时间');
- }
- ini_set('memory_limit', '2045M');
- set_time_limit(0);
- if (isset($where['orderTime'])) {
- $start = $where['orderTime'][1][0];
- $end = $where['orderTime'][1][1];
- $monthTime = bcmul(31, 86400);
- $startArea = strtotime($start);
- $endArea = strtotime($end);
- $diff = bcsub($endArea, $startArea);
- if ($diff > $monthTime) {
- util::fail('最长可导出一个月');
- }
- }
- if (isset($where['addTime'])) {
- $start = $where['addTime'][1][0];
- $end = $where['addTime'][1][1];
- $monthTime = bcmul(31, 86400);
- $startArea = strtotime($start);
- $endArea = strtotime($end);
- $diff = bcsub($endArea, $startArea);
- if ($diff > $monthTime) {
- util::fail('最长可导出一个月');
- }
- }
- }
- // 从跨天售后影响点进来:按原单 payTime + 售后 addTime,且必须挂上批发单,与统计同口径
- $matchPayTime = intval($get['matchPayTime'] ?? 0);
- if ($matchPayTime !== 1) {
- $matchPayTime = intval($get['fromAffect'] ?? 0);
- }
- $useAffectList = $matchPayTime === 1
- && !empty($searchTime)
- && isset($where['addTime'])
- && isset($where['orderTime']);
- if ($useAffectList) {
- $respond = NextDayRefundClass::getListByOrderPayTime(
- $this->mainId,
- ['startTime' => $where['addTime'][1][0], 'endTime' => $where['addTime'][1][1]],
- ['startTime' => $where['orderTime'][1][0], 'endTime' => $where['orderTime'][1][1]],
- [
- 'status' => $status,
- 'sameDay' => $sameDay,
- 'customId' => $customId,
- 'shopAdminId' => $shopAdminId,
- ]
- );
- } else {
- $respond = RefundOrderClass::getOrderList($where);
- }
- if ($export == 1) {
- RefundOrderClass::exportRefundData($respond, $this->mainId);
- }
- util::success($respond);
- }
- //主要根据花材找退款记录 ssh 20240229
- public function actionGetListByItem()
- {
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $this->mainId;
- $status = $get['status'] ?? -1;
- $cause = $get['cause'] ?? -1;
- if ($status > -1) {
- $where['status'] = $status;
- }
- $string = $get['ids'];
- $ids = json_decode($string, true);
- if (!empty($ids)) {
- $productIds = [];
- $itemList = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'classId' => ['in', $ids]], null, 'id', 'id');
- if (!empty($itemList)) {
- $productIds = array_keys($itemList);
- }
- if (empty($productIds)) {
- util::success(['list' => [], 'moreData' => 0, 'totalAmount' => 0, 'totalNum' => 0, 'totalPage' => 1]);
- }
- $where['productId'] = ['in', $productIds];
- }
- if ($cause > -1) {
- $where['cause'] = $cause;
- }
- $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $list = RefundOrderItemClass::getRefundItemList($where);
- util::success($list);
- }
- public function actionDetail()
- {
- $id = Yii::$app->request->get('id', 0);
- $refund = RefundOrderClass::getById($id);
- if (empty($refund)) {
- util::fail('没有找到退款信息');
- }
- if (isset($refund['mainId']) == false || $refund['mainId'] != $this->mainId) {
- util::fail('没有权限');
- }
- $imgString = $refund['imgList'] ?? '';
- $relateOrderSn = $refund['relateOrderSn'] ?? '';
- // 无原单退款:不强制关联销售单
- $order = null;
- if ($relateOrderSn !== '' && $relateOrderSn !== null) {
- $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
- }
- if (!empty($order)) {
- $refund['relateOrderId'] = $order->id;
- //补充原单信息,供前端判断该售后是否要走"转冲销单"(非当天单/欠款已结清),
- //以及转冲销单弹框里只读展示原单支付渠道。ssh 冲销单功能
- $refund['orderInfo'] = [
- 'id' => $order->id,
- 'payTime' => $order->payTime,
- 'debtPrice' => $order->debtPrice,
- 'remainDebtPrice' => $order->remainDebtPrice,
- 'actPrice' => $order->actPrice,
- 'forwardPrice' => $order->forwardPrice ?? 0,
- 'payWay' => $order->payWay,
- ];
- $refund['isFreeRefund'] = 0;
- } else {
- $refund['relateOrderId'] = 0;
- $refund['orderInfo'] = null;
- $refund['isFreeRefund'] = 1;
- }
- if (!empty($imgString)) {
- $imgArr = json_decode($imgString, true);
- $smallImgList = [];
- $bigImgList = [];
- if (!empty($imgArr)) {
- foreach ($imgArr as $image) {
- $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
- $smallImgList[] = $smallImg;
- $bigImgList[] = $bigImg;
- }
- }
- $refund['imgList'] = $imgArr;
- $refund['smallImgList'] = $smallImgList;
- $refund['bigImgList'] = $bigImgList;
- }
- $orderSn = $refund['orderSn'] ?? '';
- $itemList = RefundOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
- $staff = $this->shopAdmin;
- $staffId = $this->shopAdminId;
- $notify = NotifyClass::getByCondition(['staffId' => $staffId, 'type' => 0, 'targetId' => $id], true);
- if (!empty($notify)) {
- if ($notify->read == 0) {
- $notify->read = 1;
- $notify->save();
- $notifyNum = $staff->notifyNum;
- if ($notifyNum > 0) {
- $notifyNum--;
- $staff->notifyNum = $notifyNum;
- $staff->save();
- }
- }
- }
- $itemRefundOption = dict::getDict('itemRefundOption');
- util::success(['info' => $refund, 'itemList' => $itemList, 'itemRefundOption' => $itemRefundOption]);
- }
- //退款 lqh 2021.6.25
- public function actionCreateOrder()
- {
- $post = Yii::$app->request->post();
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
- util::fail('超管才能操作退款');
- }
- //多处要同步修改ghs_refund ssh 20250331
- if (getenv('YII_ENV') == 'production') {
- //小向花卉售后权限
- if ($this->mainId == 23390) {
- if (!in_array($this->adminId, [23960,77951,4])) {
- util::fail('你不能售后哈。。');
- }
- //小向花卉售后必须备注
- $remark = $post['remark'] ?? '';
- if (empty($remark)) {
- util::fail('请填写备注007');
- }
- }
- //国恋只有指定人有退款权限
- if ($this->mainId == 7704) {
- if (in_array($this->shopAdminId, [133727, 133545]) == false) {
- util::fail('无法操作。。');
- }
- }
- //小齐出车指定人可以售后
- if ($this->mainId == 42680) {
- if (!in_array($this->adminId, [40144])) {
- util::fail('你不能售后哦。。');
- }
- }
- }
- $id = isset($post['id']) ? $post['id'] : 0;
- $refundType = $post['refundType'] ?? 1;
- //避免网络延迟,暂时重复申请
- $cacheKey = 'ghs_order_refund_' . $id;
- $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
- if (!empty($has)) {
- util::fail('请5秒之后再提交');
- }
- Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
- try {
- $respond = util::runWithDbConcurrencyRetry(function () use ($post, $id, $refundType) {
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
- //花材列表结构
- // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
- $productJson = $post['product'] ?? '';
- if (empty($productJson)) {
- util::fail('请选择花材');
- }
- $productList = json_decode($productJson, true);
- if (!is_array($productList)) {
- util::fail('请选择花材哦');
- }
- $post['product'] = $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;
- $shopAdmin = $this->shopAdmin;
- $adminName = $shopAdmin['name'] ?? '';
- $post['shopAdminName'] = $adminName;
- $respond = OrderService::refund($id, $post);
- $refundId = $respond->id;
- $refundInfo = RefundOrderClass::getById($refundId, true);
- RefundOrderService::passRefund($refundInfo);
- $transaction->commit();
- // 跨天售后:返回海报/结果页所需字段
- $refundInfo = RefundOrderClass::getById($refundId, true);
- $order = OrderClass::getById($id, true);
- $custom = null;
- if (!empty($order)) {
- $custom = \bizGhs\custom\classes\CustomClass::getById(intval($order->customId ?? 0), true);
- }
- return [
- 'id' => $refundId,
- 'orderSn' => $refundInfo->orderSn ?? '',
- 'sameDay' => intval($refundInfo->sameDay ?? 1),
- 'onlinePay' => intval($refundInfo->onlinePay ?? 0),
- 'payWay' => intval($refundInfo->payWay ?? 0),
- 'hasReturn' => intval($refundInfo->hasReturn ?? 0),
- 'couldReturn' => intval($refundInfo->couldReturn ?? 0),
- 'returnBalance' => intval($refundInfo->returnBalance ?? 0),
- 'refundPrice' => $refundInfo->refundPrice ?? 0,
- 'customId' => intval($order->customId ?? 0),
- 'customName' => $custom->name ?? ($order->customName ?? ''),
- 'customBalance' => bcadd((string)($custom->balance ?? '0'), '0', 2),
- 'orderId' => intval($id),
- 'nextDayTkPrice' => $order->nextDayTkPrice ?? '0.00',
- 'needFundAction' => NextDayRefundClass::needFundAction($refundInfo),
- ];
- } catch (\Exception $exception) {
- if ($transaction->isActive) {
- $transaction->rollBack();
- }
- throw $exception;
- }
- });
- util::success($respond);
- } catch (\Exception $exception) {
- Yii::info("退款出错了,报错信息:" . $exception->getMessage());
- if (util::isDbConcurrencyException($exception)) {
- util::fail('系统繁忙中,请稍后再试');
- } else {
- // 透出业务失败原因,便于跨天售后排错
- util::fail($exception->getMessage() ?: '操作失败');
- }
- }
- }
- /**
- * 无原单退款(工作台「退款」)
- * POST: customId, price, product(json), payWay, refundType/returnWay, remark
- * 立即写售后单 sameDay=0 并通过;资金落地留给成功页确认。
- */
- public function actionCreateFreeRefund()
- {
- $post = Yii::$app->request->post();
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
- util::fail('超管才能操作退款');
- }
- // 与 create-order 生产环境售后权限保持一致
- if (getenv('YII_ENV') == 'production') {
- if ($this->mainId == 23390) {
- if (!in_array($this->adminId, [23960, 77951, 4])) {
- util::fail('你不能售后哈。。');
- }
- $remark = $post['remark'] ?? '';
- if (empty($remark)) {
- util::fail('请填写备注007');
- }
- }
- if ($this->mainId == 7704) {
- if (in_array($this->shopAdminId, [133727, 133545]) == false) {
- util::fail('无法操作。。');
- }
- }
- if ($this->mainId == 42680) {
- if (!in_array($this->adminId, [40144])) {
- util::fail('你不能售后哦。。');
- }
- }
- }
- $customId = intval($post['customId'] ?? 0);
- $cacheKey = 'ghs_free_refund_' . $this->mainId . '_' . $customId;
- $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
- if (!empty($has)) {
- util::fail('请5秒之后再提交');
- }
- Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
- try {
- $respond = util::runWithDbConcurrencyRetry(function () use ($post) {
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- // returnWay 优先:1库存退回要花材;0库存不退可只记金额
- $returnWay = intval($post['returnWay'] ?? -1);
- if ($returnWay === 1) {
- $refundType = RefundOrderClass::REFUND_TYPE_MONEY_GOOD;
- } elseif ($returnWay === 0) {
- $refundType = RefundOrderClass::REFUND_TYPE_MONEY;
- } else {
- $refundType = intval($post['refundType'] ?? RefundOrderClass::REFUND_TYPE_MONEY_GOOD);
- }
- $post['refundType'] = $refundType;
- if ($refundType == RefundOrderClass::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;
- $shopAdmin = $this->shopAdmin;
- $post['shopAdminName'] = $shopAdmin['name'] ?? '';
- $result = RefundOrderService::createFreeRefund($post);
- $transaction->commit();
- return $result;
- } catch (\Exception $exception) {
- if ($transaction->isActive) {
- $transaction->rollBack();
- }
- throw $exception;
- }
- });
- util::success($respond);
- } catch (\Exception $exception) {
- Yii::info('无原单退款出错:' . $exception->getMessage());
- if (util::isDbConcurrencyException($exception)) {
- util::fail('系统繁忙中,请稍后再试');
- }
- util::fail($exception->getMessage() ?: '操作失败');
- }
- }
- /**
- * 确认已线下原路退回(现金/银行卡等人工确认)
- * POST: id=售后单 id
- */
- public function actionMarkHasReturn()
- {
- $post = Yii::$app->request->post();
- $id = intval($post['id'] ?? 0);
- $refund = RefundOrderClass::getById($id, true);
- if (empty($refund)) {
- util::fail('没有找到退款信息');
- }
- if (intval($refund->mainId) !== intval($this->mainId)) {
- util::fail('没有权限');
- }
- try {
- $respond = util::runWithDbConcurrencyRetry(function () use ($refund) {
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $refund = NextDayRefundClass::markHasReturn($refund);
- $transaction->commit();
- return [
- 'id' => intval($refund->id ?? 0),
- 'hasReturn' => intval($refund->hasReturn ?? 0),
- 'couldReturn' => intval($refund->couldReturn ?? 0),
- 'returnBalance' => intval($refund->returnBalance ?? 0),
- 'payWay' => intval($refund->payWay ?? 0),
- 'onlinePay' => intval($refund->onlinePay ?? 0),
- 'refundPrice' => $refund->refundPrice ?? 0,
- 'customId' => intval($refund->customId ?? 0),
- 'customName' => $refund->customName ?? '',
- 'orderSn' => $refund->orderSn ?? '',
- ];
- } catch (\Exception $exception) {
- if ($transaction->isActive) {
- $transaction->rollBack();
- }
- throw $exception;
- }
- });
- util::success($respond);
- } catch (\Exception $exception) {
- util::fail($exception->getMessage() ?: '操作失败');
- }
- }
- /**
- * 不想线下转账:返充到客户余额
- * POST: id=售后单 id
- */
- public function actionReturnBalance()
- {
- $post = Yii::$app->request->post();
- $id = intval($post['id'] ?? 0);
- $refund = RefundOrderClass::getById($id, true);
- if (empty($refund)) {
- util::fail('没有找到退款信息');
- }
- if (intval($refund->mainId) !== intval($this->mainId)) {
- util::fail('没有权限');
- }
- try {
- $respond = util::runWithDbConcurrencyRetry(function () use ($refund) {
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $order = null;
- $relateOrderSn = trim((string)($refund->relateOrderSn ?? ''));
- if ($relateOrderSn !== '') {
- $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
- }
- $result = NextDayRefundClass::applyReturnBalance($refund, $order);
- $refund = $result['refund'];
- $transaction->commit();
- return [
- 'id' => intval($refund->id ?? 0),
- 'hasReturn' => intval($refund->hasReturn ?? 0),
- 'couldReturn' => intval($refund->couldReturn ?? 0),
- 'returnBalance' => intval($refund->returnBalance ?? 0),
- 'payWay' => intval($refund->payWay ?? 0),
- 'onlinePay' => intval($refund->onlinePay ?? 0),
- 'refundPrice' => $refund->refundPrice ?? 0,
- 'customId' => intval($refund->customId ?? 0),
- 'customName' => $refund->customName ?? '',
- 'customBalance' => $result['customBalance'],
- 'orderSn' => $refund->orderSn ?? '',
- ];
- } catch (\Exception $exception) {
- if ($transaction->isActive) {
- $transaction->rollBack();
- }
- throw $exception;
- }
- });
- util::success($respond);
- } catch (\Exception $exception) {
- util::fail($exception->getMessage() ?: '操作失败');
- }
- }
- //修改售后原因 ssh 20250623
- public function actionChangeRefundOption()
- {
- $post = Yii::$app->request->post();
- $id = $post['id'] ?? 0;
- $sonId = $post['sonId'] ?? 0;
- $optionId = $post['optionId'] ?? 0;
- $refund = RefundOrderClass::getById($id, true);
- if (empty($refund)) {
- util::fail('没有找到退款信息');
- }
- if ($refund->mainId != $this->mainId) {
- util::fail('没有权限');
- }
- $orderSn = $refund->orderSn;
- $son = RefundOrderItemClass::getById($sonId, true);
- if (empty($son)) {
- util::fail('没有找到花材信息');
- }
- if ($son->orderSn != $orderSn) {
- util::fail('退款信息有问题');
- }
- $map = dict::getDict('itemRefundOption');
- $info = $map[$optionId] ?? [];
- $name = $info['name'] ?? '未选';
- $son->refundOptionId = $optionId;
- $son->refundOptionName = $name;
- $son->save();
- util::complete('修改成功');
- }
- }
|