mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
First basic import #145
This commit is contained in:
248
app/Support/Import/Routine/Ynab/ImportDataHandler.php
Normal file
248
app/Support/Import/Routine/Ynab/ImportDataHandler.php
Normal file
@@ -0,0 +1,248 @@
|
||||
<?php
|
||||
/**
|
||||
* ImportDataHandler.php
|
||||
* Copyright (c) 2018 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\Routine\Ynab;
|
||||
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Services\Ynab\Request\GetTransactionsRequest;
|
||||
use FireflyIII\Support\Import\Routine\File\OpposingAccountMapper;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class ImportDataHandler
|
||||
*/
|
||||
class ImportDataHandler
|
||||
{
|
||||
/** @var AccountRepositoryInterface */
|
||||
private $accountRepository;
|
||||
/** @var ImportJob */
|
||||
private $importJob;
|
||||
/** @var OpposingAccountMapper */
|
||||
private $mapper;
|
||||
/** @var ImportJobRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Get list of accounts for the selected budget.
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$config = $this->repository->getConfiguration($this->importJob);
|
||||
$token = $config['access_token'];
|
||||
// make request for each mapping:
|
||||
$mapping = $config['mapping'] ?? [];
|
||||
$total = [[]];
|
||||
|
||||
/**
|
||||
* @var string $ynabId
|
||||
* @var int $localId
|
||||
*/
|
||||
foreach ($mapping as $ynabId => $localId) {
|
||||
$localAccount = $this->getLocalAccount((int)$localId);
|
||||
$transactions = $this->getTransactions($token, $ynabId);
|
||||
$converted = $this->convertToArray($transactions, $localAccount);
|
||||
$total[] = $converted;
|
||||
}
|
||||
|
||||
$totalSet = array_merge(...$total);
|
||||
Log::debug(sprintf('Found %d transactions in total.', \count($totalSet)));
|
||||
$this->repository->setTransactions($this->importJob, $totalSet);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportJob $importJob
|
||||
*/
|
||||
public function setImportJob(ImportJob $importJob): void
|
||||
{
|
||||
$this->importJob = $importJob;
|
||||
$this->repository = app(ImportJobRepositoryInterface::class);
|
||||
$this->accountRepository = app(AccountRepositoryInterface::class);
|
||||
$this->mapper = app(OpposingAccountMapper::class);
|
||||
$this->accountRepository->setUser($importJob->user);
|
||||
$this->repository->setUser($importJob->user);
|
||||
$this->mapper->setUser($importJob->user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $transactions
|
||||
* @param Account $localAccount
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function convertToArray(array $transactions, Account $localAccount): array
|
||||
{
|
||||
$array = [];
|
||||
$total = \count($transactions);
|
||||
$budget = $this->getSelectedBudget();
|
||||
Log::debug(sprintf('Now in StageImportDataHandler::convertToArray() with count %d', \count($transactions)));
|
||||
/** @var array $transaction */
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
Log::debug(sprintf('Now creating array for transaction %d of %d', $index + 1, $total));
|
||||
$amount = (string)($transaction['amount'] ?? 0);
|
||||
if ('0' === $amount) {
|
||||
continue;
|
||||
}
|
||||
$source = $localAccount;
|
||||
$type = 'withdrawal';
|
||||
$tags = [
|
||||
$transaction['cleared'] ?? '',
|
||||
$transaction['approved'] ? 'approved' : 'not-approved',
|
||||
$transaction['flag_color'] ?? '',
|
||||
];
|
||||
$destinationData = [
|
||||
'name' => $transaction['payee_name'],
|
||||
'iban' => null,
|
||||
'number' => $transaction['payee_id'],
|
||||
'bic' => null,
|
||||
];
|
||||
|
||||
$destination = $this->mapper->map(null, $amount, $destinationData);
|
||||
|
||||
if (1 === bccomp($amount, '0')) {
|
||||
[$source, $destination] = [$destination, $source];
|
||||
$type = 'deposit';
|
||||
}
|
||||
|
||||
$entry = [
|
||||
'type' => $type,
|
||||
'date' => $transaction['date'] ?? date('Y-m-d'),
|
||||
'tags' => $tags, // TODO
|
||||
'user' => $this->importJob->user_id,
|
||||
'notes' => null, // TODO
|
||||
|
||||
// all custom fields:
|
||||
'external_id' => $transaction['id'] ?? '',
|
||||
|
||||
// journal data:
|
||||
'description' => $transaction['memo'] ?? '(empty)',
|
||||
'piggy_bank_id' => null,
|
||||
'piggy_bank_name' => null,
|
||||
'bill_id' => null,
|
||||
'bill_name' => null,
|
||||
|
||||
// transaction data:
|
||||
'transactions' => [
|
||||
[
|
||||
'currency_id' => null,
|
||||
'currency_code' => $budget['currency_code'],
|
||||
'description' => null,
|
||||
'amount' => bcdiv((string)$transaction['amount'], '1000'),
|
||||
'budget_id' => null,
|
||||
'budget_name' => null,
|
||||
'category_id' => null,
|
||||
'category_name' => $transaction['category_name'],
|
||||
'source_id' => $source->id,
|
||||
'source_name' => null,
|
||||
'destination_id' => $destination->id,
|
||||
'destination_name' => null,
|
||||
'foreign_currency_id' => null,
|
||||
'foreign_currency_code' => null,
|
||||
'foreign_amount' => null,
|
||||
'reconciled' => false,
|
||||
'identifier' => 0,
|
||||
],
|
||||
],
|
||||
];
|
||||
$array[] = $entry;
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $accountId
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getLocalAccount(int $accountId): Account
|
||||
{
|
||||
$account = $this->accountRepository->findNull($accountId);
|
||||
if (null === $account) {
|
||||
throw new FireflyException(sprintf('Cannot find Firefly III asset account with ID #%d. Job must stop now.', $accountId)); // @codeCoverageIgnore
|
||||
}
|
||||
if ($account->accountType->type !== AccountType::ASSET) {
|
||||
throw new FireflyException(sprintf('Account with ID #%d is not an asset account. Job must stop now.', $accountId)); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getSelectedBudget(): array
|
||||
{
|
||||
$config = $this->repository->getConfiguration($this->importJob);
|
||||
$budgets = $config['budgets'] ?? [];
|
||||
$selected = $config['selected_budget'] ?? '';
|
||||
|
||||
if ('' === $selected) {
|
||||
$firstBudget = $config['budgets'][0] ?? false;
|
||||
if (false === $firstBudget) {
|
||||
throw new FireflyException('The configuration contains no budget. Erroring out.');
|
||||
}
|
||||
$selected = $firstBudget['id'];
|
||||
}
|
||||
|
||||
foreach ($budgets as $budget) {
|
||||
if ($budget['id'] === $selected) {
|
||||
return $budget;
|
||||
}
|
||||
}
|
||||
|
||||
return $budgets[0] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $token
|
||||
* @param string $budget
|
||||
* @param string $account
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getTransactions(string $token, string $account): array
|
||||
{
|
||||
$budget = $this->getSelectedBudget();
|
||||
$request = new GetTransactionsRequest;
|
||||
$request->budgetId = $budget['id'];
|
||||
$request->accountId = $account;
|
||||
|
||||
// todo grab latest date for $ynabId
|
||||
$request->setAccessToken($token);
|
||||
$request->call();
|
||||
|
||||
return $request->transactions;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user