Ver Fonte

跨天售后影响跟踪

shish há 10 horas atrás
pai
commit
945ad574bb

+ 33 - 5
app-ghs/controllers/RefundController.php

@@ -452,7 +452,10 @@ class RefundController extends BaseController
         }
     }
 
-    //列表
+    /**
+     * 销售售后记录列表:ghs /order/refund-list、小程序售后记录共用。
+     * matchPayTime/fromAffect=1 时按原单 payTime + 售后 addTime,与跨天影响统计同口径。
+     */
     public function actionList()
     {
         $get = Yii::$app->request->get();
@@ -486,10 +489,12 @@ class RefundController extends BaseController
         if ($useDefaultLast30 === 1 && empty($refundSearchTime) && empty($searchTime)) {
             $refundSearchTime = 'last30Days';
         }
+        // 申请日只用 refundStart/End,禁止回落到订单日 startTime/endTime;
+        // 否则从跨天影响点进来会把「原单日」当成「售后创建日」,列表金额偏大
         if (!empty($refundSearchTime)) {
-            $startTime = $get['refundStartTime'] ?? ($get['startTime'] ?? '');
-            $endTime = $get['refundEndTime'] ?? ($get['endTime'] ?? '');
-            $period = dateUtil::formatTime($refundSearchTime, $startTime, $endTime);
+            $refundStartTime = $get['refundStartTime'] ?? '';
+            $refundEndTime = $get['refundEndTime'] ?? '';
+            $period = dateUtil::formatTime($refundSearchTime, $refundStartTime, $refundEndTime);
             $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
         }
         $customId = $get['customId'] ?? '';
@@ -536,7 +541,30 @@ class RefundController extends BaseController
                 }
             }
         }
-        $respond = RefundOrderClass::getOrderList($where);
+        // 从跨天售后影响点进来:按原单 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);
         }

+ 80 - 0
biz-ghs/order/classes/NextDayRefundClass.php

@@ -972,6 +972,86 @@ class NextDayRefundClass
         ];
     }
 
+    /**
+     * 与 affectByOrderPayTime 同口径的售后明细:售后 addTime + 原单 payTime,必须挂上批发单。
+     * /refund/list?matchPayTime=1 用,避免列表按 xhRefund.orderTime 或漏申请日导致金额对不上。
+     *
+     * @param int $mainId
+     * @param array $refundPeriod addTime 起止(含时分秒)
+     * @param array $payPeriod 原单 payTime 起止
+     * @param array $extra status/sameDay/customId/shopAdminId,-1 或不传表示不限
+     * @return array{list:array,totalNum:int,totalPage:int,moreData:int,totalRefundPrice:string}
+     */
+    public static function getListByOrderPayTime($mainId, $refundPeriod, $payPeriod, $extra = [])
+    {
+        $addStart = $refundPeriod['startTime'] ?? '';
+        $addEnd = $refundPeriod['endTime'] ?? '';
+        $payStart = $payPeriod['startTime'] ?? '';
+        $payEnd = $payPeriod['endTime'] ?? '';
+        $params = [
+            ':mainId' => intval($mainId),
+            ':addStart' => $addStart,
+            ':addEnd' => $addEnd,
+            ':payStart' => $payStart,
+            ':payEnd' => $payEnd,
+        ];
+        $whereSql = "r.mainId = :mainId
+              AND r.addTime BETWEEN :addStart AND :addEnd
+              AND o.payTime BETWEEN :payStart AND :payEnd
+              AND o.payTime > '1970-01-01'";
+        // 状态/跨天与影响页一致时才收窄,列表改 tab 后按用户当前筛选项
+        $status = isset($extra['status']) ? intval($extra['status']) : -1;
+        if ($status > -1) {
+            $whereSql .= ' AND r.status = :status';
+            $params[':status'] = $status;
+        }
+        $sameDay = isset($extra['sameDay']) ? intval($extra['sameDay']) : -1;
+        if ($sameDay === 0 || $sameDay === 1) {
+            $whereSql .= ' AND r.sameDay = :sameDay';
+            $params[':sameDay'] = $sameDay;
+        }
+        $customId = intval($extra['customId'] ?? 0);
+        if ($customId > 0) {
+            $whereSql .= ' AND r.customId = :customId';
+            $params[':customId'] = $customId;
+        }
+        $shopAdminId = intval($extra['shopAdminId'] ?? 0);
+        if ($shopAdminId > 0) {
+            $whereSql .= ' AND r.shopAdminId = :shopAdminId';
+            $params[':shopAdminId'] = $shopAdminId;
+        }
+
+        $fromSql = "FROM xhRefund r
+            INNER JOIN xhGhsOrder o ON o.orderSn = r.relateOrderSn AND o.mainId = r.mainId
+            WHERE {$whereSql}";
+        $count = intval(Yii::$app->db->createCommand("SELECT COUNT(*) {$fromSql}", $params)->queryScalar());
+        $totalRefundPrice = bcadd(
+            (string)(Yii::$app->db->createCommand("SELECT COALESCE(SUM(r.refundPrice),0) {$fromSql}", $params)->queryScalar() ?: '0'),
+            '0',
+            2
+        );
+
+        $get = Yii::$app->request->get();
+        $page = max(1, intval($get['page'] ?? 1));
+        $pageSize = isset($get['pageSize']) && !empty($get['pageSize'])
+            ? max(1, intval($get['pageSize']))
+            : intval(Yii::$app->params['pageSize'] ?? 20);
+        $offset = ($page - 1) * $pageSize;
+        $totalPage = $pageSize > 0 ? (int)ceil($count / $pageSize) : 1;
+        $list = Yii::$app->db->createCommand(
+            "SELECT r.* {$fromSql} ORDER BY r.addTime DESC LIMIT {$offset}, {$pageSize}",
+            $params
+        )->queryAll();
+
+        return [
+            'list' => $list ?: [],
+            'totalNum' => $count,
+            'totalPage' => $totalPage,
+            'moreData' => $totalPage > $page ? 1 : 0,
+            'totalRefundPrice' => $totalRefundPrice,
+        ];
+    }
+
     /**
      * @param array $rows
      * @return array