function buildReports($xml = "")
{
    global $RESPONSE, $STATS, $INTERNAL;
    if (empty($_POST[POST_INTERN_XMLCLIP_REPORTS_END_TIME])) {
        return;
    }
    if (empty($STATS->CurrentDay) || $INTERNAL[CALLER_SYSTEM_ID]->GetPermission(PERMISSION_REPORTS) == PERMISSION_NONE) {
        return;
    }
    if ($_POST[POST_INTERN_XMLCLIP_REPORTS_END_TIME] == XML_CLIP_NULL) {
        $_POST[POST_INTERN_XMLCLIP_REPORTS_END_TIME] = "0_0";
    }
    $parts = explode("_", $_POST[POST_INTERN_XMLCLIP_REPORTS_END_TIME]);
    if ($result = queryDB(true, "SELECT *,(SELECT MAX(`time`) FROM `" . DB_PREFIX . DATABASE_STATS_AGGS . "`) AS `maxtime`,(SELECT MAX(`mtime`) FROM `" . DB_PREFIX . DATABASE_STATS_AGGS . "` WHERE `maxtime`=`time`) AS `maxmtime` FROM `" . DB_PREFIX . DATABASE_STATS_AGGS . "` WHERE (`time` = " . DBManager::RealEscape($parts[0]) . " AND `mtime` > " . DBManager::RealEscape($parts[1]) . ") OR (`time` > " . DBManager::RealEscape($parts[0]) . ") ORDER BY `time` ASC,`mtime` ASC LIMIT 1")) {
        while ($row = DBManager::FetchArray($result)) {
            if ($row["month"] == 0) {
                $report = new StatisticYear($row["year"], 0, 0);
            } else {
                if ($row["day"] == 0) {
                    $report = new StatisticMonth($row["year"], $row["month"], 0);
                } else {
                    $report = new StatisticDay($row["year"], $row["month"], $row["day"]);
                }
            }
            $type = -1;
            $update = false;
            $value = "";
            if ($report->Type == STATISTIC_PERIOD_TYPE_DAY) {
                if ($_POST[POST_INTERN_PROCESS_UPDATE_REPORT_TYPE] == 1) {
                    if ($STATS->CurrentDay->CreateVisitorList) {
                        if (empty($row["aggregated"]) && (!@file_exists($report->GetFilename(true, true)) || $row["time"] < time() - StatisticProvider::$AutoUpdateTime)) {
                            $report->SaveVisitorListToFile();
                        }
                        if (@file_exists($report->GetFilename(true, true))) {
                            $value = getFile($report->GetFilename(true, true));
                        }
                    }
                    $type = 1;
                } else {
                    if ($_POST[POST_INTERN_PROCESS_UPDATE_REPORT_TYPE] == 0) {
                        if ($STATS->CurrentDay->CreateReport) {
                            if (empty($row["aggregated"]) && (!@file_exists($report->GetFilename(true, false)) || $row["time"] < time() - StatisticProvider::$AutoUpdateTime)) {
                                $update = true;
                                $report->SaveReportToFile();
                            } else {
                                if (@file_exists($report->GetFilename(true, false))) {
                                    $value = getFile($report->GetFilename(true, false));
                                }
                            }
                        }
                        $type = 0;
                    }
                }
            } else {
                if (empty($row["aggregated"]) && (!@file_exists($report->GetFilename(true, false)) || $row["time"] < time() - StatisticProvider::$AutoUpdateTime)) {
                    $report->SaveReportToFile();
                }
                if (@file_exists($report->GetFilename(true, false))) {
                    $value = getFile($report->GetFilename(true, false));
                }
                $type = $report->Type == STATISTIC_PERIOD_TYPE_MONTH ? 2 : 3;
            }
            if ($type > -1) {
                $convrate = $row["sessions"] > 0 ? round(100 * $row["conversions"] / $row["sessions"], StatisticProvider::$RoundPrecision) : 0;
                $chats = $chatsd = 0;
                $qmonth = $report->Type == STATISTIC_PERIOD_TYPE_YEAR ? "" : " AND `month`='" . DBManager::RealEscape($row["month"]) . "'";
                $qday = $report->Type != STATISTIC_PERIOD_TYPE_DAY ? "" : " AND `day`='" . DBManager::RealEscape($row["day"]) . "'";
                if ($results = queryDB(true, "SELECT (SUM(`amount`)-SUM(`multi`)) AS `samount` FROM `" . DB_PREFIX . DATABASE_STATS_AGGS_CHATS . "` WHERE `year`='" . DBManager::RealEscape($row["year"]) . "'" . $qmonth . $qday . "")) {
                    if (DBManager::GetRowCount($results) == 1) {
                        $rows = DBManager::FetchArray($results);
                        if (is_numeric($rows["samount"])) {
                            $chats = $rows["samount"];
                        }
                    }
                }
                $xml .= "<r cid=\"" . base64_encode(getId(3)) . "\" ragg=\"" . base64_encode(empty($row["aggregated"]) ? 0 : 1) . "\" rtype=\"" . base64_encode($type) . "\" convrate=\"" . base64_encode($convrate) . "\" chats=\"" . base64_encode($chats) . "\" update=\"" . base64_encode($update ? 1 : 0) . "\" visitors=\"" . base64_encode($row["sessions"]) . "\" time=\"" . base64_encode($row["time"]) . "\" mtime=\"" . base64_encode($row["mtime"]) . "\" year=\"" . base64_encode($row["year"]) . "\" month=\"" . base64_encode($row["month"]) . "\" day=\"" . base64_encode($row["day"]) . "\">" . base64_encode($value) . "</r>\r\n";
            }
            $xml .= "<ri maxtime=\"" . base64_encode($row["maxtime"]) . "\" maxmtime=\"" . base64_encode($row["maxmtime"]) . "\" />";
        }
    }
    $RESPONSE->SetStandardResponse(1, $xml);
}