|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* GetAccounts.php |
|
5
|
|
|
* Copyright (c) 2020 [email protected] |
|
6
|
|
|
* |
|
7
|
|
|
* This file is part of the Firefly III CSV importer |
|
8
|
|
|
* (https://github.com/firefly-iii/csv-importer). |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
13
|
|
|
* License, or (at your option) any later version. |
|
14
|
|
|
* |
|
15
|
|
|
* This program is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU Affero General Public License for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
21
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace App\Services\CSV\Mapper; |
|
25
|
|
|
|
|
26
|
|
|
use App\Exceptions\ImportException; |
|
27
|
|
|
use GrumpyDictator\FFIIIApiSupport\Exceptions\ApiHttpException; |
|
28
|
|
|
use GrumpyDictator\FFIIIApiSupport\Model\Account; |
|
29
|
|
|
use GrumpyDictator\FFIIIApiSupport\Request\GetAccountsRequest; |
|
30
|
|
|
use GrumpyDictator\FFIIIApiSupport\Response\GetAccountsResponse; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Trait GetAccounts |
|
34
|
|
|
*/ |
|
35
|
|
|
trait GetAccounts |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Returns a combined list of asset accounts and all liability accounts. |
|
40
|
|
|
* |
|
41
|
|
|
* @throws ImportException |
|
42
|
|
|
* @throws ApiHttpException |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function getAllAccounts(): array |
|
46
|
|
|
{ |
|
47
|
|
|
// get list of asset accounts: |
|
48
|
|
|
$accounts = []; |
|
49
|
|
|
$uri = (string) config('csv_importer.uri'); |
|
50
|
|
|
$token = (string) config('csv_importer.access_token'); |
|
51
|
|
|
$request = new GetAccountsRequest($uri, $token); |
|
52
|
|
|
$request->setType(GetAccountsRequest::ALL); |
|
53
|
|
|
|
|
54
|
|
|
/** @var GetAccountsResponse $response */ |
|
55
|
|
|
$response = $request->get(); |
|
56
|
|
|
|
|
57
|
|
|
if ($response instanceof GetAccountsResponse) { |
|
|
|
|
|
|
58
|
|
|
$accounts = $this->toArray($response); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (!$response instanceof GetAccountsResponse) { |
|
|
|
|
|
|
62
|
|
|
throw new ImportException('Could not get list of ALL accounts.'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return array_merge($accounts); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns a combined list of asset accounts and all liability accounts. |
|
70
|
|
|
* |
|
71
|
|
|
* @throws ImportException |
|
72
|
|
|
* @throws ApiHttpException |
|
73
|
|
|
* @throws ApiHttpException |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function getAssetAccounts(): array |
|
77
|
|
|
{ |
|
78
|
|
|
// get list of asset accounts: |
|
79
|
|
|
$accounts = []; |
|
80
|
|
|
$liabilities = []; |
|
81
|
|
|
$uri = (string) config('csv_importer.uri'); |
|
82
|
|
|
$token = (string) config('csv_importer.access_token'); |
|
83
|
|
|
$request = new GetAccountsRequest($uri, $token); |
|
84
|
|
|
$request->setType(GetAccountsRequest::ASSET); |
|
85
|
|
|
|
|
86
|
|
|
/** @var GetAccountsResponse $response */ |
|
87
|
|
|
$response = $request->get(); |
|
88
|
|
|
|
|
89
|
|
|
if ($response instanceof GetAccountsResponse) { |
|
|
|
|
|
|
90
|
|
|
$accounts = $this->toArray($response); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (!$response instanceof GetAccountsResponse) { |
|
|
|
|
|
|
94
|
|
|
throw new ImportException('Could not get list of asset accounts.'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$request = new GetAccountsRequest($uri, $token); |
|
98
|
|
|
$request->setType(GetAccountsRequest::LIABILITIES); |
|
99
|
|
|
|
|
100
|
|
|
/** @var GetAccountsResponse $response */ |
|
101
|
|
|
$response = $request->get(); |
|
102
|
|
|
|
|
103
|
|
|
if ($response instanceof GetAccountsResponse) { |
|
|
|
|
|
|
104
|
|
|
$liabilities = $this->toArray($response); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (!$response instanceof GetAccountsResponse) { |
|
|
|
|
|
|
108
|
|
|
throw new ImportException('Could not get list of asset accounts.'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return array_merge($accounts, $liabilities); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Merge all arrays into <select> ready list. |
|
116
|
|
|
* |
|
117
|
|
|
* @param array $accounts |
|
118
|
|
|
* |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function mergeAll(array $accounts): array |
|
122
|
|
|
{ |
|
123
|
|
|
$result = []; |
|
124
|
|
|
/** @var Account $account */ |
|
125
|
|
|
foreach ($accounts as $account) { |
|
126
|
|
|
$name = $account->name; |
|
127
|
|
|
if (null !== $account->iban) { |
|
128
|
|
|
$name = sprintf('%s (%s)', $account->name, $account->iban); |
|
129
|
|
|
} |
|
130
|
|
|
// add optgroup to result: |
|
131
|
|
|
$group = trans(sprintf('import.account_types_%s', $account->type)); |
|
132
|
|
|
$result[$group] = $result[$group] ?? []; |
|
133
|
|
|
$result[$group][$account->id] = $name; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $result; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Merge all arrays into <select> ready list. |
|
142
|
|
|
* |
|
143
|
|
|
* @param array $accounts |
|
144
|
|
|
* |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function mergeWithIBAN(array $accounts): array |
|
148
|
|
|
{ |
|
149
|
|
|
$result = []; |
|
150
|
|
|
/** @var Account $account */ |
|
151
|
|
|
foreach ($accounts as $account) { |
|
152
|
|
|
// only merge if IBAN is not null. |
|
153
|
|
|
if (null !== $account->iban) { |
|
154
|
|
|
$name = sprintf('%s (%s)', $account->name, $account->iban); |
|
155
|
|
|
// add optgroup to result: |
|
156
|
|
|
$group = trans(sprintf('import.account_types_%s', $account->type)); |
|
157
|
|
|
$result[$group] = $result[$group] ?? []; |
|
158
|
|
|
$result[$group][$account->id] = $name; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $result; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param GetAccountsResponse $list |
|
167
|
|
|
* |
|
168
|
|
|
* @return array |
|
169
|
|
|
*/ |
|
170
|
|
|
private function toArray(GetAccountsResponse $list): array |
|
171
|
|
|
{ |
|
172
|
|
|
$return = []; |
|
173
|
|
|
foreach ($list as $account) { |
|
174
|
|
|
$return[] = $account; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return $return; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|