Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.13% covered (warning)
83.13%
69 / 83
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReportRequestService
83.13% covered (warning)
83.13%
69 / 83
0.00% covered (danger)
0.00%
0 / 7
31.76
0.00% covered (danger)
0.00%
0 / 1
 getExchangeRequestData
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
 getExchangeRequestForDateRange
76.47% covered (warning)
76.47%
13 / 17
0.00% covered (danger)
0.00%
0 / 1
5.33
 getExchangeRequestForPeriod
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
2.04
 getRequestPeriod
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
2.26
 fetchAndCombineDataFromYears
95.45% covered (success)
95.45%
21 / 22
0.00% covered (danger)
0.00%
0 / 1
6
 createFilteredExchangeRequest
86.67% covered (warning)
86.67%
13 / 15
0.00% covered (danger)
0.00%
0 / 1
4.04
 prepareDownloadArgs
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
5.39
1<?php
2
3/**
4 * @package Zmsstatistic
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsstatistic\Service;
9
10use BO\Zmsentities\Day;
11use BO\Zmsstatistic\Helper\ReportHelper;
12use DateTime;
13use Exception;
14
15class ReportRequestService
16{
17    protected $totals = ['requestscount'];
18
19    protected $hashset = [
20        'requestscount'
21    ];
22
23    protected $groupfields = [
24        'name',
25        'date'
26    ];
27
28    /**
29     * Get exchange request data based on date range or period
30     */
31    public function getExchangeRequestData(string $scopeId, ?array $dateRange, array $args): mixed
32    {
33        if ($scopeId === '') {
34            return null;
35        }
36
37        if ($dateRange) {
38            return $this->getExchangeRequestForDateRange($scopeId, $dateRange);
39        }
40
41        return isset($args['period'])
42            ? $this->getExchangeRequestForPeriod($scopeId, $args['period'])
43            : null;
44    }
45
46    /**
47     * Get exchange request data for a specific date range
48     */
49    public function getExchangeRequestForDateRange(string $scopeId, array $dateRange): mixed
50    {
51        if (!isset($dateRange['from']) || !isset($dateRange['to'])) {
52            return null;
53        }
54        $fromDate = $dateRange['from'];
55        $toDate = $dateRange['to'];
56
57        try {
58            $reportHelper = new ReportHelper();
59            $years = $reportHelper->getYearsForDateRange($fromDate, $toDate);
60            $combinedData = $this->fetchAndCombineDataFromYears($scopeId, $years, $fromDate, $toDate);
61
62            if (empty($combinedData['data'])) {
63                return null;
64            }
65
66            return $this->createFilteredExchangeRequest(
67                $combinedData['entity'],
68                $combinedData['data'],
69                $fromDate,
70                $toDate
71            );
72        } catch (Exception $exception) {
73            return null;
74        }
75    }
76
77    /**
78     * Get exchange request data for a specific period (legacy functionality)
79     */
80    public function getExchangeRequestForPeriod(string $scopeId, string $period): mixed
81    {
82        try {
83            return \App::$http
84                ->readGetResult('/warehouse/requestscope/' . $scopeId . '/' . $period . '/')
85                ->getEntity()
86                ->toGrouped($this->groupfields, $this->hashset)
87                ->withRequestsSum()
88                ->withAverage('processingtime')
89                ->withUncapturedRequestRowSortedLast();
90        } catch (Exception $exception) {
91            return null;
92        }
93    }
94
95    /**
96     * Get request period data for the current scope
97     */
98    public function getRequestPeriod(string $scopeId): mixed
99    {
100        try {
101            return \App::$http
102                ->readGetResult('/warehouse/requestscope/' . $scopeId . '/')
103                ->getEntity();
104        } catch (Exception $exception) {
105            return null;
106        }
107    }
108
109    /**
110     * Fetch and combine data from multiple years
111     */
112    private function fetchAndCombineDataFromYears(string $scopeId, array $years, string $fromDate, string $toDate): array
113    {
114        $combinedData = [];
115        $baseEntity = null;
116
117        foreach ($years as $year) {
118            try {
119                $exchangeRequest = \App::$http
120                    ->readGetResult(
121                        '/warehouse/requestscope/' . $scopeId . '/' . $year . '/',
122                        [
123                            'groupby' => 'day',
124                            'fromDate' => $fromDate,
125                            'toDate' => $toDate
126                        ]
127                    )
128                    ->getEntity();
129
130                // Use the first successfully fetched entity as the base
131                if ($baseEntity === null) {
132                    $baseEntity = $exchangeRequest;
133                }
134
135                // Combine data from all years
136                if (isset($exchangeRequest->data) && is_array($exchangeRequest->data)) {
137                    $combinedData = array_merge($combinedData, $exchangeRequest->data);
138                }
139            } catch (Exception $exception) {
140                // Continue with other years - don't fail completely if one year is missing
141            }
142        }
143
144        return [
145            'entity' => $baseEntity,
146            'data' => $combinedData
147        ];
148    }
149
150    /**
151     * Create filtered exchange request with updated properties
152     */
153    private function createFilteredExchangeRequest(
154        $exchangeRequestBasic,
155        array $filteredData,
156        string $fromDate,
157        string $toDate
158    ): mixed {
159        $exchangeRequest = $exchangeRequestBasic;
160        $exchangeRequest->data = $filteredData;
161
162        if (!isset($exchangeRequest->period)) {
163            $exchangeRequest->period = 'day';
164        }
165
166        $exchangeRequest->firstDay = (new Day())->setDateTime(new DateTime($fromDate));
167        $exchangeRequest->lastDay = (new Day())->setDateTime(new DateTime($toDate));
168
169        if (!empty($filteredData)) {
170            $exchangeRequest = $exchangeRequest
171                ->toGrouped($this->groupfields, $this->hashset)
172                ->withRequestsSum()
173                ->withAverage('processingtime');
174
175            if (is_array($exchangeRequest->data)) {
176                $exchangeRequest = $exchangeRequest->withUncapturedRequestRowSortedLast();
177            }
178
179            return $exchangeRequest;
180        }
181
182        return $exchangeRequest->toHashed();
183    }
184
185    /**
186     * Prepare download arguments for request report
187     */
188    public function prepareDownloadArgs(
189        array $args,
190        mixed $exchangeRequest,
191        ?array $dateRange,
192        array $selectedScopes = []
193    ): array {
194        $args['category'] = 'requestscope';
195
196        if ($dateRange) {
197            $args['period'] = $dateRange['from'] . '_' . $dateRange['to'];
198        }
199
200        if (!empty($selectedScopes)) {
201            $args['selectedScopes'] = $selectedScopes;
202        }
203
204        if ($exchangeRequest && count($exchangeRequest->data)) {
205            $args['reports'][] = $exchangeRequest;
206        }
207
208        return $args;
209    }
210}