DistributionReportClass.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * 用途:门店分销报表统计(xhDistributionOrder / xhDistributionFlow / xhDistributionUser)
  4. * 谁用:hdApp 应用-门店设置-分销报表
  5. */
  6. namespace bizHd\distribution\classes;
  7. use common\components\dateUtil;
  8. use Yii;
  9. use yii\db\Query;
  10. class DistributionReportClass
  11. {
  12. /**
  13. * 门店分销报表汇总
  14. * @param int $shopId
  15. * @return array
  16. */
  17. public static function getReportStat($shopId)
  18. {
  19. $shopId = (int)$shopId;
  20. $get = Yii::$app->request->get();
  21. $searchTime = isset($get['searchTime']) ? trim($get['searchTime']) : 'today';
  22. $startTime = isset($get['startTime']) ? trim($get['startTime']) : '';
  23. $endTime = isset($get['endTime']) ? trim($get['endTime']) : '';
  24. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  25. $timeBetween = null;
  26. if (!empty($period['startTime']) && !empty($period['endTime'])) {
  27. $timeBetween = [$period['startTime'], $period['endTime']];
  28. }
  29. $orderQuery = (new Query())
  30. ->from('xhDistributionOrder')
  31. ->where(['shopId' => $shopId]);
  32. if ($timeBetween) {
  33. $orderQuery->andWhere(['between', 'orderTime', $timeBetween[0], $timeBetween[1]]);
  34. }
  35. $orderCount = (int)(clone $orderQuery)->count('*', Yii::$app->db);
  36. $commissionAmount = (float)(clone $orderQuery)->sum('commissionAmount');
  37. $distUserCount = (int)(clone $orderQuery)
  38. ->select('distId')
  39. ->distinct()
  40. ->count('distId', Yii::$app->db);
  41. $inviteQuery = (new Query())
  42. ->from('xhDistributionUser')
  43. ->where(['shopId' => $shopId])
  44. ->andWhere(['>', 'inviterId', 0]);
  45. if ($timeBetween) {
  46. $inviteQuery->andWhere(['between', 'inviteTime', $timeBetween[0], $timeBetween[1]]);
  47. }
  48. $inviteCount = (int)$inviteQuery->count('*', Yii::$app->db);
  49. $pendingQuery = (new Query())
  50. ->from('xhDistributionOrder')
  51. ->where(['shopId' => $shopId, 'settleStatus' => 0]);
  52. if ($timeBetween) {
  53. $pendingQuery->andWhere(['between', 'orderTime', $timeBetween[0], $timeBetween[1]]);
  54. }
  55. $pendingAmount = (float)$pendingQuery->sum('commissionAmount');
  56. $settledQuery = (new Query())
  57. ->from('xhDistributionOrder')
  58. ->where(['shopId' => $shopId, 'settleStatus' => 1]);
  59. if ($timeBetween) {
  60. $settledQuery->andWhere(['between', 'settleTime', $timeBetween[0], $timeBetween[1]]);
  61. }
  62. $settledAmount = (float)$settledQuery->sum('commissionAmount');
  63. $depositQuery = (new Query())
  64. ->from('xhDistributionFlow')
  65. ->where(['shopId' => $shopId, 'flowType' => 2, 'flowStatus' => 3]);
  66. if ($timeBetween) {
  67. $depositQuery->andWhere(['between', 'flowTime', $timeBetween[0], $timeBetween[1]]);
  68. }
  69. $depositAmount = (float)$depositQuery->sum('amount');
  70. return [
  71. 'orderCount' => $orderCount,
  72. 'commissionAmount' => round($commissionAmount, 2),
  73. 'distUserCount' => $distUserCount,
  74. 'inviteCount' => $inviteCount,
  75. 'pendingAmount' => round($pendingAmount, 2),
  76. 'settledAmount' => round($settledAmount, 2),
  77. 'depositAmount' => round($depositAmount, 2),
  78. ];
  79. }
  80. }