| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- /**
- * 用途:门店分销报表统计(xhDistributionOrder / xhDistributionFlow / xhDistributionUser)
- * 谁用:hdApp 应用-门店设置-分销报表
- */
- namespace bizHd\distribution\classes;
- use common\components\dateUtil;
- use Yii;
- use yii\db\Query;
- class DistributionReportClass
- {
- /**
- * 门店分销报表汇总
- * @param int $shopId
- * @return array
- */
- public static function getReportStat($shopId)
- {
- $shopId = (int)$shopId;
- $get = Yii::$app->request->get();
- $searchTime = isset($get['searchTime']) ? trim($get['searchTime']) : 'today';
- $startTime = isset($get['startTime']) ? trim($get['startTime']) : '';
- $endTime = isset($get['endTime']) ? trim($get['endTime']) : '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
- $timeBetween = null;
- if (!empty($period['startTime']) && !empty($period['endTime'])) {
- $timeBetween = [$period['startTime'], $period['endTime']];
- }
- $orderQuery = (new Query())
- ->from('xhDistributionOrder')
- ->where(['shopId' => $shopId]);
- if ($timeBetween) {
- $orderQuery->andWhere(['between', 'orderTime', $timeBetween[0], $timeBetween[1]]);
- }
- $orderCount = (int)(clone $orderQuery)->count('*', Yii::$app->db);
- $commissionAmount = (float)(clone $orderQuery)->sum('commissionAmount');
- $distUserCount = (int)(clone $orderQuery)
- ->select('distId')
- ->distinct()
- ->count('distId', Yii::$app->db);
- $inviteQuery = (new Query())
- ->from('xhDistributionUser')
- ->where(['shopId' => $shopId])
- ->andWhere(['>', 'inviterId', 0]);
- if ($timeBetween) {
- $inviteQuery->andWhere(['between', 'inviteTime', $timeBetween[0], $timeBetween[1]]);
- }
- $inviteCount = (int)$inviteQuery->count('*', Yii::$app->db);
- $pendingQuery = (new Query())
- ->from('xhDistributionOrder')
- ->where(['shopId' => $shopId, 'settleStatus' => 0]);
- if ($timeBetween) {
- $pendingQuery->andWhere(['between', 'orderTime', $timeBetween[0], $timeBetween[1]]);
- }
- $pendingAmount = (float)$pendingQuery->sum('commissionAmount');
- $settledQuery = (new Query())
- ->from('xhDistributionOrder')
- ->where(['shopId' => $shopId, 'settleStatus' => 1]);
- if ($timeBetween) {
- $settledQuery->andWhere(['between', 'settleTime', $timeBetween[0], $timeBetween[1]]);
- }
- $settledAmount = (float)$settledQuery->sum('commissionAmount');
- $depositQuery = (new Query())
- ->from('xhDistributionFlow')
- ->where(['shopId' => $shopId, 'flowType' => 2, 'flowStatus' => 3]);
- if ($timeBetween) {
- $depositQuery->andWhere(['between', 'flowTime', $timeBetween[0], $timeBetween[1]]);
- }
- $depositAmount = (float)$depositQuery->sum('amount');
- return [
- 'orderCount' => $orderCount,
- 'commissionAmount' => round($commissionAmount, 2),
- 'distUserCount' => $distUserCount,
- 'inviteCount' => $inviteCount,
- 'pendingAmount' => round($pendingAmount, 2),
- 'settledAmount' => round($settledAmount, 2),
- 'depositAmount' => round($depositAmount, 2),
- ];
- }
- }
|