1 | <?php |
||
2 | /** |
||
3 | * Support.php |
||
4 | * Copyright (c) 2017 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III. |
||
7 | * |
||
8 | * Firefly III is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation, either version 3 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * Firefly III is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License |
||
19 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | declare(strict_types=1); |
||
22 | |||
23 | namespace FireflyIII\Generator\Report; |
||
24 | |||
25 | use FireflyIII\Models\Transaction; |
||
26 | use Illuminate\Support\Collection; |
||
27 | |||
28 | /** |
||
29 | * Class Support. |
||
30 | */ |
||
31 | class Support |
||
32 | { |
||
33 | /** |
||
34 | * @return Collection |
||
35 | */ |
||
36 | public function getTopExpenses(): Collection |
||
37 | { |
||
38 | $transactions = $this->getExpenses()->sortBy('transaction_amount'); |
||
39 | |||
40 | return $transactions; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @return Collection |
||
45 | */ |
||
46 | public function getTopIncome(): Collection |
||
47 | { |
||
48 | $transactions = $this->getIncome()->sortByDesc('transaction_amount'); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
49 | |||
50 | return $transactions; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param Collection $collection |
||
55 | * @param int $sortFlag |
||
56 | * |
||
57 | * @return array |
||
58 | */ |
||
59 | protected function getAverages(Collection $collection, int $sortFlag): array |
||
60 | { |
||
61 | $result = []; |
||
62 | /** @var Transaction $transaction */ |
||
63 | foreach ($collection as $transaction) { |
||
64 | // opposing name and ID: |
||
65 | $opposingId = $transaction->opposing_account_id; |
||
66 | |||
67 | // is not set? |
||
68 | if (!isset($result[$opposingId])) { |
||
69 | $name = $transaction->opposing_account_name; |
||
70 | $result[$opposingId] = [ |
||
71 | 'name' => $name, |
||
72 | 'count' => 1, |
||
73 | 'id' => $opposingId, |
||
74 | 'average' => $transaction->transaction_amount, |
||
75 | 'sum' => $transaction->transaction_amount, |
||
76 | ]; |
||
77 | continue; |
||
78 | } |
||
79 | ++$result[$opposingId]['count']; |
||
80 | $result[$opposingId]['sum'] = bcadd($result[$opposingId]['sum'], $transaction->transaction_amount); |
||
81 | $result[$opposingId]['average'] = bcdiv($result[$opposingId]['sum'], strval($result[$opposingId]['count'])); |
||
82 | } |
||
83 | |||
84 | // sort result by average: |
||
85 | $average = []; |
||
86 | foreach ($result as $key => $row) { |
||
87 | $average[$key] = floatval($row['average']); |
||
88 | } |
||
89 | |||
90 | array_multisort($average, $sortFlag, $result); |
||
91 | |||
92 | return $result; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly five. |
||
97 | * |
||
98 | * @param array $spent |
||
99 | * @param array $earned |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | protected function getObjectSummary(array $spent, array $earned): array |
||
104 | { |
||
105 | $return = [ |
||
106 | 'sum' => [ |
||
107 | 'spent' => '0', |
||
108 | 'earned' => '0', |
||
109 | ], |
||
110 | ]; |
||
111 | |||
112 | /** |
||
113 | * @var int |
||
114 | * @var string $entry |
||
115 | */ |
||
116 | foreach ($spent as $objectId => $entry) { |
||
117 | if (!isset($return[$objectId])) { |
||
118 | $return[$objectId] = ['spent' => '0', 'earned' => '0']; |
||
119 | } |
||
120 | |||
121 | $return[$objectId]['spent'] = $entry; |
||
122 | $return['sum']['spent'] = bcadd($return['sum']['spent'], $entry); |
||
123 | } |
||
124 | unset($entry); |
||
125 | |||
126 | /** |
||
127 | * @var int |
||
128 | * @var string $entry |
||
129 | */ |
||
130 | foreach ($earned as $objectId => $entry) { |
||
131 | if (!isset($return[$objectId])) { |
||
132 | $return[$objectId] = ['spent' => '0', 'earned' => '0']; |
||
133 | } |
||
134 | |||
135 | $return[$objectId]['earned'] = $entry; |
||
136 | $return['sum']['earned'] = bcadd($return['sum']['earned'], $entry); |
||
137 | } |
||
138 | |||
139 | return $return; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param Collection $collection |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | protected function summarizeByAccount(Collection $collection): array |
||
148 | { |
||
149 | $result = []; |
||
150 | /** @var Transaction $transaction */ |
||
151 | foreach ($collection as $transaction) { |
||
152 | $accountId = $transaction->account_id; |
||
153 | $result[$accountId] = $result[$accountId] ?? '0'; |
||
154 | $result[$accountId] = bcadd($transaction->transaction_amount, $result[$accountId]); |
||
155 | } |
||
156 | |||
157 | return $result; |
||
158 | } |
||
159 | } |
||
160 |