1 | <?php |
||
2 | /** |
||
3 | * StageImportDataHandler.php |
||
4 | * Copyright (c) 2019 https://github.com/bnw |
||
5 | * |
||
6 | * This file is part of Firefly III (https://github.com/firefly-iii). |
||
7 | * |
||
8 | * This program is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU Affero General Public License as |
||
10 | * published by the Free Software Foundation, either version 3 of the |
||
11 | * License, or (at your option) any later version. |
||
12 | * |
||
13 | * This program 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 Affero General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU Affero General Public License |
||
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | |||
22 | declare(strict_types=1); |
||
23 | |||
24 | namespace FireflyIII\Support\Import\Routine\FinTS; |
||
25 | |||
26 | |||
27 | use Fhp\Model\StatementOfAccount\Transaction; |
||
28 | use Fhp\Model\StatementOfAccount\Transaction as FinTSTransaction; |
||
29 | use FireflyIII\Exceptions\FireflyException; |
||
30 | use FireflyIII\Models\Account as LocalAccount; |
||
31 | use FireflyIII\Models\AccountType; |
||
32 | use FireflyIII\Models\ImportJob; |
||
33 | use FireflyIII\Models\TransactionType; |
||
34 | use FireflyIII\Repositories\Account\AccountRepositoryInterface; |
||
35 | use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; |
||
36 | use FireflyIII\Support\FinTS\FinTS; |
||
37 | use FireflyIII\Support\FinTS\MetadataParser; |
||
38 | use FireflyIII\Support\Import\Routine\File\OpposingAccountMapper; |
||
39 | use Illuminate\Support\Facades\Log; |
||
40 | |||
41 | /** |
||
42 | * |
||
43 | * Class StageImportDataHandler |
||
44 | * @deprecated |
||
45 | * @codeCoverageIgnore |
||
46 | */ |
||
47 | class StageImportDataHandler |
||
48 | { |
||
49 | /** @var AccountRepositoryInterface */ |
||
50 | private $accountRepository; |
||
51 | /** @var ImportJob */ |
||
52 | private $importJob; |
||
53 | /** @var OpposingAccountMapper */ |
||
54 | private $mapper; |
||
55 | /** @var ImportJobRepositoryInterface */ |
||
56 | private $repository; |
||
57 | /** @var array */ |
||
58 | private $transactions; |
||
59 | |||
60 | /** |
||
61 | * @return array |
||
62 | */ |
||
63 | public function getTransactions(): array |
||
64 | { |
||
65 | return $this->transactions; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @throws FireflyException |
||
70 | */ |
||
71 | public function run(): void |
||
72 | { |
||
73 | Log::debug('Now in StageImportDataHandler::run()'); |
||
74 | |||
75 | $localAccount = $this->accountRepository->findNull((int)$this->importJob->configuration['local_account']); |
||
76 | if (null === $localAccount) { |
||
77 | throw new FireflyException(sprintf('Cannot find Firefly account with id #%d ', $this->importJob->configuration['local_account'])); |
||
78 | } |
||
79 | $finTS = app(FinTS::class, ['config' => $this->importJob->configuration]); |
||
80 | $fintTSAccount = $finTS->getAccount($this->importJob->configuration['fints_account']); |
||
81 | $statementOfAccount = $finTS->getStatementOfAccount( |
||
82 | $fintTSAccount, new \DateTime($this->importJob->configuration['from_date']), new \DateTime($this->importJob->configuration['to_date']) |
||
83 | ); |
||
84 | $collection = []; |
||
85 | foreach ($statementOfAccount->getStatements() as $statement) { |
||
86 | foreach ($statement->getTransactions() as $transaction) { |
||
87 | $collection[] = $this->convertTransaction($transaction, $localAccount); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | $this->transactions = $collection; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param ImportJob $importJob |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | public function setImportJob(ImportJob $importJob): void |
||
100 | { |
||
101 | $this->transactions = []; |
||
102 | $this->importJob = $importJob; |
||
103 | $this->repository = app(ImportJobRepositoryInterface::class); |
||
104 | $this->accountRepository = app(AccountRepositoryInterface::class); |
||
105 | $this->mapper = app(OpposingAccountMapper::class); |
||
106 | $this->mapper->setUser($importJob->user); |
||
107 | $this->repository->setUser($importJob->user); |
||
108 | $this->accountRepository->setUser($importJob->user); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param FinTSTransaction $transaction |
||
113 | * @param LocalAccount $source |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | private function convertTransaction(FinTSTransaction $transaction, LocalAccount $source): array |
||
118 | { |
||
119 | Log::debug(sprintf('Start converting transaction %s', $transaction->getDescription1())); |
||
120 | |||
121 | $amount = (string)$transaction->getAmount(); |
||
122 | $debitOrCredit = $transaction->getCreditDebit(); |
||
123 | // assume deposit. |
||
124 | $type = TransactionType::DEPOSIT; |
||
125 | Log::debug(sprintf('Amount is %s', $amount)); |
||
126 | |||
127 | // inverse if not. |
||
128 | if ($debitOrCredit !== Transaction::CD_CREDIT) { |
||
129 | $type = TransactionType::WITHDRAWAL; |
||
130 | $amount = bcmul($amount, '-1'); |
||
131 | } |
||
132 | |||
133 | $destination = $this->mapper->map( |
||
134 | null, |
||
135 | $amount, |
||
136 | ['iban' => $transaction->getAccountNumber(), 'name' => $transaction->getName()] |
||
137 | ); |
||
138 | if ($debitOrCredit === Transaction::CD_CREDIT) { |
||
139 | [$source, $destination] = [$destination, $source]; |
||
140 | } |
||
141 | |||
142 | if ($source->accountType->type === AccountType::ASSET && $destination->accountType->type === AccountType::ASSET) { |
||
143 | $type = TransactionType::TRANSFER; |
||
144 | Log::debug('Both are assets, will make transfer.'); |
||
145 | } |
||
146 | |||
147 | $metadataParser = new MetadataParser(); |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
![]() |
|||
148 | $description = $metadataParser->getDescription($transaction); |
||
149 | |||
150 | $storeData = [ |
||
151 | 'user' => $this->importJob->user_id, |
||
152 | 'type' => $type, |
||
153 | 'date' => $transaction->getValutaDate()->format('Y-m-d'), |
||
154 | 'description' => null, |
||
155 | 'piggy_bank_id' => null, |
||
156 | 'piggy_bank_name' => null, |
||
157 | 'bill_id' => null, |
||
158 | 'bill_name' => null, |
||
159 | 'tags' => [], |
||
160 | 'internal_reference' => null, |
||
161 | 'external_id' => null, |
||
162 | 'notes' => null, |
||
163 | 'bunq_payment_id' => null, |
||
164 | 'original-source' => sprintf('fints-v%s', config('firefly.version')), |
||
165 | 'transactions' => [ |
||
166 | // single transaction: |
||
167 | [ |
||
168 | 'type' => $type, |
||
169 | 'description' => $description, |
||
170 | 'date' => $transaction->getValutaDate()->format('Y-m-d'), |
||
171 | 'amount' => $amount, |
||
172 | 'currency_id' => null, |
||
173 | 'currency_code' => 'EUR', |
||
174 | 'foreign_amount' => null, |
||
175 | 'foreign_currency_id' => null, |
||
176 | 'foreign_currency_code' => null, |
||
177 | 'budget_id' => null, |
||
178 | 'budget_name' => null, |
||
179 | 'category_id' => null, |
||
180 | 'category_name' => null, |
||
181 | 'source_id' => $source->id, |
||
182 | 'source_name' => null, |
||
183 | 'destination_id' => $destination->id, |
||
184 | 'destination_name' => null, |
||
185 | 'reconciled' => false, |
||
186 | 'identifier' => 0, |
||
187 | ], |
||
188 | ], |
||
189 | ]; |
||
190 | |||
191 | return $storeData; |
||
192 | } |
||
193 | } |
||
194 |