mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Added FinTS import
This commit is contained in:
92
app/Support/FinTS/FinTS.php
Normal file
92
app/Support/FinTS/FinTS.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace FireflyIII\Support\FinTS;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
|
||||
class FinTS
|
||||
{
|
||||
/** @var \Fhp\FinTs */
|
||||
private $finTS;
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function __construct(array $config)
|
||||
{
|
||||
if (
|
||||
!isset($config['fints_url']) or
|
||||
!isset($config['fints_port']) or
|
||||
!isset($config['fints_bank_code']) or
|
||||
!isset($config['fints_username']) or
|
||||
!isset($config['fints_password']))
|
||||
throw new FireflyException(
|
||||
"Constructed FinTS with incomplete config."
|
||||
);
|
||||
$this->finTS = new \Fhp\FinTs(
|
||||
$config['fints_url'],
|
||||
$config['fints_port'],
|
||||
$config['fints_bank_code'],
|
||||
$config['fints_username'],
|
||||
$config['fints_password']
|
||||
);
|
||||
}
|
||||
|
||||
public function checkConnection()
|
||||
{
|
||||
try {
|
||||
$this->finTS->getSEPAAccounts();
|
||||
return true;
|
||||
} catch (\Exception $exception) {
|
||||
return $exception->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Fhp\Model\SEPAAccount[]
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getAccounts()
|
||||
{
|
||||
try {
|
||||
return $this->finTS->getSEPAAccounts();
|
||||
} catch (\Exception $exception) {
|
||||
throw new FireflyException($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accountNumber
|
||||
* @return \Fhp\Model\SEPAAccount
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getAccount($accountNumber)
|
||||
{
|
||||
$accounts = $this->getAccounts();
|
||||
$filteredAccounts = array_filter($accounts, function ($account) use ($accountNumber) {
|
||||
return $account->getAccountNumber() == $accountNumber;
|
||||
});
|
||||
if (count($filteredAccounts) != 1) {
|
||||
throw new FireflyException("Cannot find account with number " . $accountNumber);
|
||||
}
|
||||
return $filteredAccounts[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Fhp\Model\SEPAAccount $account
|
||||
* @param \DateTime $from
|
||||
* @param \DateTIme $to
|
||||
* @return \Fhp\Model\StatementOfAccount\StatementOfAccount|null
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getStatementOfAccount(\Fhp\Model\SEPAAccount $account, \DateTime $from, \DateTIme $to)
|
||||
{
|
||||
try {
|
||||
return $this->finTS->getStatementOfAccount($account, $from, $to);
|
||||
} catch (\Exception $exception) {
|
||||
throw new FireflyException($exception->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
/**
|
||||
* ChooseAccountHandler.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Import\JobConfiguration\FinTS;
|
||||
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Import\JobConfiguration\FinTSConfigurationSteps;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Support\FinTS\FinTS;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
|
||||
class ChooseAccountHandler implements FinTSConfigurationInterface
|
||||
{
|
||||
/** @var ImportJob */
|
||||
private $importJob;
|
||||
/** @var ImportJobRepositoryInterface */
|
||||
private $repository;
|
||||
/** @var AccountRepositoryInterface */
|
||||
private $accountRepository;
|
||||
|
||||
/**
|
||||
* Store data associated with current stage.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function configureJob(array $data): MessageBag
|
||||
{
|
||||
$config = $this->importJob->configuration;
|
||||
$config['fints_account'] = (string)($data['fints_account'] ?? '');
|
||||
$config['local_account'] = (string)($data['local_account'] ?? '');
|
||||
$config['local_account'] = (string)($data['local_account'] ?? '');
|
||||
$config['from_date'] = (string)($data['from_date'] ?? '');
|
||||
$config['to_date'] = (string)($data['to_date'] ?? '');
|
||||
$this->repository->setConfiguration($this->importJob, $config);
|
||||
|
||||
try {
|
||||
$finTS = new FinTS($this->importJob->configuration);
|
||||
$finTS->getAccount($config['fints_account']);
|
||||
} catch (FireflyException $e) {
|
||||
return new MessageBag($e->getMessage());
|
||||
}
|
||||
|
||||
$this->repository->setStage($this->importJob, FinTSConfigurationSteps::GO_FOR_IMPORT);
|
||||
|
||||
return new MessageBag();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data necessary to show the configuration screen.
|
||||
*
|
||||
* @return array
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
public function getNextData(): array
|
||||
{
|
||||
$finTS = new FinTS($this->importJob->configuration);
|
||||
$finTSAccounts = $finTS->getAccounts();
|
||||
$finTSAccountsData = [];
|
||||
foreach ($finTSAccounts as $account) {
|
||||
$finTSAccountsData[$account->getAccountNumber()] = $account->getIban();
|
||||
}
|
||||
|
||||
$localAccounts = [];
|
||||
foreach ($this->accountRepository->getAccountsByType([AccountType::ASSET]) as $localAccount) {
|
||||
$display_name = $localAccount->name;
|
||||
if ($localAccount->iban) {
|
||||
$display_name .= " - $localAccount->iban";
|
||||
}
|
||||
$localAccounts[$localAccount->id] = $display_name;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'fints_accounts' => $finTSAccountsData,
|
||||
'fints_account' => $this->importJob->configuration['fints_account'] ?? null,
|
||||
'local_accounts' => $localAccounts,
|
||||
'local_account' => $this->importJob->configuration['local_account'] ?? null,
|
||||
'from_date' => $this->importJob->configuration['from_date'] ?? (new \DateTime('now - 1 month'))->format('Y-m-d'),
|
||||
'to_date' => $this->importJob->configuration['to_date'] ?? (new \DateTime('now'))->format('Y-m-d')
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportJob $importJob
|
||||
*/
|
||||
public function setImportJob(ImportJob $importJob): void
|
||||
{
|
||||
$this->importJob = $importJob;
|
||||
$this->repository = app(ImportJobRepositoryInterface::class);
|
||||
$this->accountRepository = app(AccountRepositoryInterface::class);
|
||||
$this->repository->setUser($importJob->user);
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/**
|
||||
* FinTSConfigurationInterface.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Import\JobConfiguration\FinTS;
|
||||
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use Illuminate\Support\MessageBag;
|
||||
|
||||
interface FinTSConfigurationInterface
|
||||
{
|
||||
/**
|
||||
* Store data associated with current stage.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function configureJob(array $data): MessageBag;
|
||||
|
||||
/**
|
||||
* Get the data necessary to show the configuration screen.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getNextData(): array;
|
||||
|
||||
/**
|
||||
* @param ImportJob $importJob
|
||||
*/
|
||||
public function setImportJob(ImportJob $importJob): void;
|
||||
}
|
106
app/Support/Import/JobConfiguration/FinTS/NewFinTSJobHandler.php
Normal file
106
app/Support/Import/JobConfiguration/FinTS/NewFinTSJobHandler.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* NewFinTSJobHandler.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace FireflyIII\Support\Import\JobConfiguration\FinTS;
|
||||
|
||||
|
||||
use FireflyIII\Import\JobConfiguration\FinTSConfigurationSteps;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Support\FinTS\FinTS;
|
||||
use Illuminate\Support\MessageBag;
|
||||
|
||||
class NewFinTSJobHandler implements FinTSConfigurationInterface
|
||||
{
|
||||
/** @var ImportJob */
|
||||
private $importJob;
|
||||
/** @var ImportJobRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Store data associated with current stage.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return MessageBag
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
public function configureJob(array $data): MessageBag
|
||||
{
|
||||
$config = [];
|
||||
|
||||
$config['fints_url'] = trim($data['fints_url'] ?? '');
|
||||
$config['fints_port'] = (int)($data['fints_port'] ?? '');
|
||||
$config['fints_bank_code'] = (string)($data['fints_bank_code'] ?? '');
|
||||
$config['fints_username'] = (string)($data['fints_username'] ?? '');
|
||||
$config['fints_password'] = (string)($data['fints_password'] ?? '');
|
||||
|
||||
$this->repository->setConfiguration($this->importJob, $config);
|
||||
|
||||
$incomplete = false;
|
||||
foreach ($config as $value) {
|
||||
$incomplete = $value == '' or $incomplete;
|
||||
}
|
||||
if ($incomplete) {
|
||||
return new MessageBag([trans('import.incomplete_fints_form')]);
|
||||
}
|
||||
|
||||
$finTS = new FinTS($this->importJob->configuration);
|
||||
if (($checkConnection = $finTS->checkConnection()) !== true) {
|
||||
return new MessageBag([trans('import.fints_connection_failed', ['originalError' => $checkConnection])]);
|
||||
}
|
||||
|
||||
$this->repository->setStage($this->importJob, FinTSConfigurationSteps::CHOOSE_ACCOUNT);
|
||||
|
||||
return new MessageBag();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data necessary to show the configuration screen.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getNextData(): array
|
||||
{
|
||||
$config = $this->importJob->configuration;
|
||||
var_dump($config);
|
||||
return [
|
||||
'fints_url' => $config['fints_url'] ?? "",
|
||||
'fints_port' => $config['fints_port'] ?? "443",
|
||||
'fints_bank_code' => $config['fints_bank_code'] ?? "",
|
||||
'fints_username' => $config['fints_username'] ?? "",
|
||||
'fints_password' => $config['fints_password'] ?? "",
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportJob $importJob
|
||||
*/
|
||||
public function setImportJob(ImportJob $importJob): void
|
||||
{
|
||||
$this->importJob = $importJob;
|
||||
$this->repository = app(ImportJobRepositoryInterface::class);
|
||||
$this->repository->setUser($importJob->user);
|
||||
}
|
||||
|
||||
}
|
147
app/Support/Import/Routine/FinTS/StageImportDataHandler.php
Normal file
147
app/Support/Import/Routine/FinTS/StageImportDataHandler.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace FireflyIII\Support\Import\Routine\FinTS;
|
||||
|
||||
|
||||
use Fhp\Model\StatementOfAccount\Transaction as FinTSTransaction;
|
||||
use Fhp\Model\StatementOfAccount\Transaction;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Support\FinTS\FinTS;
|
||||
use FireflyIII\Models\Account as LocalAccount;
|
||||
use FireflyIII\Support\Import\Routine\File\OpposingAccountMapper;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class StageImportDataHandler
|
||||
{
|
||||
/** @var AccountRepositoryInterface */
|
||||
private $accountRepository;
|
||||
/** @var ImportJob */
|
||||
private $importJob;
|
||||
/** @var ImportJobRepositoryInterface */
|
||||
private $repository;
|
||||
/** @var array */
|
||||
private $transactions;
|
||||
/** @var OpposingAccountMapper */
|
||||
private $mapper;
|
||||
|
||||
/**
|
||||
* @param ImportJob $importJob
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setImportJob(ImportJob $importJob): void
|
||||
{
|
||||
$this->transactions = [];
|
||||
$this->importJob = $importJob;
|
||||
$this->repository = app(ImportJobRepositoryInterface::class);
|
||||
$this->accountRepository = app(AccountRepositoryInterface::class);
|
||||
$this->mapper = app(OpposingAccountMapper::class);
|
||||
$this->mapper->setUser($importJob->user);
|
||||
$this->repository->setUser($importJob->user);
|
||||
$this->accountRepository->setUser($importJob->user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
Log::debug('Now in StageImportDataHandler::run()');
|
||||
|
||||
$localAccount = $this->accountRepository->find($this->importJob->configuration['local_account']);
|
||||
$finTS = new FinTS($this->importJob->configuration);
|
||||
$fintTSAccount = $finTS->getAccount($this->importJob->configuration['fints_account']);
|
||||
$statementOfAccount = $finTS->getStatementOfAccount($fintTSAccount, new \DateTime($this->importJob->configuration['from_date']), new \DateTime($this->importJob->configuration['to_date']));
|
||||
$collection = [];
|
||||
foreach ($statementOfAccount->getStatements() as $statement) {
|
||||
foreach ($statement->getTransactions() as $transaction) {
|
||||
$collection[] = $this->convertTransaction($transaction, $localAccount);
|
||||
}
|
||||
}
|
||||
|
||||
$this->transactions = $collection;
|
||||
}
|
||||
|
||||
private function convertTransaction(FinTSTransaction $transaction, LocalAccount $source): array
|
||||
{
|
||||
Log::debug(sprintf('Start converting transaction %s', $transaction->getDescription1()));
|
||||
|
||||
$amount = $transaction->getAmount();
|
||||
$debitOrCredit = $transaction->getCreditDebit();
|
||||
|
||||
Log::debug(sprintf('Amount is %s', $amount));
|
||||
if ($debitOrCredit == Transaction::CD_CREDIT) {
|
||||
$type = TransactionType::DEPOSIT;
|
||||
} else {
|
||||
$type = TransactionType::WITHDRAWAL;
|
||||
$amount = -$amount;
|
||||
}
|
||||
|
||||
$destination = $this->mapper->map(
|
||||
null,
|
||||
$amount,
|
||||
['iban' => $transaction->getAccountNumber(), 'name' => $transaction->getName()]
|
||||
);
|
||||
if ($debitOrCredit == Transaction::CD_CREDIT) {
|
||||
[$source, $destination] = [$destination, $source];
|
||||
}
|
||||
|
||||
if ($source->accountType->type === AccountType::ASSET && $destination->accountType->type === AccountType::ASSET) {
|
||||
$type = TransactionType::TRANSFER;
|
||||
Log::debug('Both are assets, will make transfer.');
|
||||
}
|
||||
|
||||
$storeData = [
|
||||
'user' => $this->importJob->user_id,
|
||||
'type' => $type,
|
||||
'date' => $transaction->getValutaDate()->format('Y-m-d'),
|
||||
'description' => $transaction->getDescription1(),
|
||||
'piggy_bank_id' => null,
|
||||
'piggy_bank_name' => null,
|
||||
'bill_id' => null,
|
||||
'bill_name' => null,
|
||||
'tags' => [],
|
||||
'internal_reference' => null,
|
||||
'external_id' => null,
|
||||
'notes' => null,
|
||||
'bunq_payment_id' => null,
|
||||
'transactions' => [
|
||||
// single transaction:
|
||||
[
|
||||
'description' => null,
|
||||
'amount' => $amount,
|
||||
'currency_id' => null,
|
||||
'currency_code' => 'EUR',
|
||||
'foreign_amount' => null,
|
||||
'foreign_currency_id' => null,
|
||||
'foreign_currency_code' => null,
|
||||
'budget_id' => null,
|
||||
'budget_name' => null,
|
||||
'category_id' => null,
|
||||
'category_name' => null,
|
||||
'source_id' => $source->id,
|
||||
'source_name' => null,
|
||||
'destination_id' => $destination->id,
|
||||
'destination_name' => null,
|
||||
'reconciled' => false,
|
||||
'identifier' => 0,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $storeData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getTransactions(): array
|
||||
{
|
||||
return $this->transactions;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user