New code for YNAB import.

This commit is contained in:
James Cole 2018-07-29 21:02:03 +02:00
parent 7ad09da4e9
commit dfd9cf0874
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
19 changed files with 1341 additions and 264 deletions

View File

@ -132,11 +132,12 @@ class JobStatusController extends Controller
*/
public function start(ImportJob $importJob): JsonResponse
{
Log::debug('Now in JobStatusController::start');
// catch impossible status:
$allowed = ['ready_to_run', 'need_job_config'];
if (null !== $importJob && !\in_array($importJob->status, $allowed, true)) {
Log::error('Job is not ready.');
Log::error(sprintf('Job is not ready. Status should be in array, but is %s', $importJob->status), $allowed);
$this->repository->setStatus($importJob, 'error');
return response()->json(
@ -157,7 +158,11 @@ class JobStatusController extends Controller
/** @var RoutineInterface $routine */
$routine = app($className);
$routine->setImportJob($importJob);
Log::debug(sprintf('Created class of type %s', $className));
try {
Log::debug(sprintf('Try to call %s:run()', $className));
$routine->run();
} catch (FireflyException|Exception $e) {
$message = 'The import routine crashed: ' . $e->getMessage();
@ -189,7 +194,7 @@ class JobStatusController extends Controller
// catch impossible status:
$allowed = ['provider_finished', 'storing_data'];
if (null !== $importJob && !\in_array($importJob->status, $allowed, true)) {
Log::error('Job is not ready.');
Log::error(sprintf('Job is not ready. Status should be in array, but is %s', $importJob->status), $allowed);
return response()->json(
['status' => 'NOK', 'message' => sprintf('JobStatusController::start expects status "provider_finished" instead of "%s".', $importJob->status)]

View File

@ -23,8 +23,12 @@ declare(strict_types=1);
namespace FireflyIII\Import\JobConfiguration;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Support\Import\JobConfiguration\Ynab\NewYnabJobHandler;
use FireflyIII\Support\Import\JobConfiguration\Ynab\SelectBudgetsHandler;
use FireflyIII\Support\Import\JobConfiguration\Ynab\YnabJobConfigurationInterface;
use Illuminate\Support\MessageBag;
use Log;
@ -33,6 +37,8 @@ use Log;
*/
class YnabJobConfiguration implements JobConfigurationInterface
{
/** @var YnabJobConfigurationInterface The job handler. */
private $handler;
/** @var ImportJob The import job */
private $importJob;
/** @var ImportJobRepositoryInterface Import job repository */
@ -45,16 +51,7 @@ class YnabJobConfiguration implements JobConfigurationInterface
*/
public function configurationComplete(): bool
{
// config is only needed when the job is in stage "new".
if ($this->importJob->stage === 'new') {
Log::debug('YNAB configurationComplete: stage is new, return false');
return false;
}
Log::debug('YNAB configurationComplete: stage is not new, return true');
return true;
return $this->handler->configurationComplete();
}
/**
@ -67,10 +64,7 @@ class YnabJobConfiguration implements JobConfigurationInterface
*/
public function configureJob(array $data): MessageBag
{
Log::debug('YNAB configureJob: nothing to do.');
// there is never anything to store from this job.
return new MessageBag;
return $this->handler->configureJob($data);
}
/**
@ -80,24 +74,7 @@ class YnabJobConfiguration implements JobConfigurationInterface
*/
public function getNextData(): array
{
$data = [];
// here we update the job so it can redirect properly to YNAB
if ($this->importJob->stage === 'new') {
// update stage to make sure we catch the token.
$this->repository->setStage($this->importJob, 'catch-auth-code');
$clientId = (string)config('import.options.ynab.client_id');
$callBackUri = route('import.callback.ynab');
$uri = sprintf(
'https://app.youneedabudget.com/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code&state=%s', $clientId, $callBackUri,
$this->importJob->key
);
$data['token-url'] = $uri;
Log::debug(sprintf('YNAB getNextData: URI to redirect to is %s', $uri));
}
return $data;
return $this->handler->getNextData();
}
/**
@ -107,19 +84,53 @@ class YnabJobConfiguration implements JobConfigurationInterface
*/
public function getNextView(): string
{
Log::debug('Return YNAB redirect view.');
return 'import.ynab.redirect';
return $this->handler->getNextView();
}
/**
* Set import job.
*
* @param ImportJob $importJob
*
* @throws FireflyException
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
$this->handler = $this->getHandler();
}
/**
* Get correct handler.
*
* @return YnabJobConfigurationInterface
* @throws FireflyException
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function getHandler(): YnabJobConfigurationInterface
{
Log::debug(sprintf('Now in YnabJobConfiguration::getHandler() with stage "%s"', $this->importJob->stage));
$handler = null;
switch ($this->importJob->stage) {
case 'new':
/** @var NewYnabJobHandler $handler */
$handler = app(NewYnabJobHandler::class);
$handler->setImportJob($this->importJob);
break;
case 'select_budgets':
/** @var SelectBudgetsHandler $handler */
$handler = app(SelectBudgetsHandler::class);
$handler->setImportJob($this->importJob);
break;
default:
// @codeCoverageIgnoreStart
throw new FireflyException(sprintf('Firefly III cannot create a YNAB configuration handler for stage "%s"', $this->importJob->stage));
// @codeCoverageIgnoreEnd
}
return $handler;
}
}

View File

@ -26,7 +26,9 @@ namespace FireflyIII\Import\Routine;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Support\Import\Routine\Ynab\GetAccountsHandler;
use FireflyIII\Support\Import\Routine\Ynab\StageGetAccessHandler;
use FireflyIII\Support\Import\Routine\Ynab\StageGetBudgetsHandler;
use Log;
/**
@ -61,12 +63,69 @@ class YnabRoutine implements RoutineInterface
$handler = app(StageGetAccessHandler::class);
$handler->setImportJob($this->importJob);
$handler->run();
$this->repository->setStage($this->importJob, 'get_transactions');
// back to correct stage:
$this->repository->setStatus($this->importJob, 'ready_to_run');
$this->repository->setStage($this->importJob, 'get_budgets');
return;
}
if ('get_budgets' === $this->importJob->stage) {
$this->repository->setStatus($this->importJob, 'running');
/** @var StageGetBudgetsHandler $handler */
$handler = app(StageGetBudgetsHandler::class);
$handler->setImportJob($this->importJob);
$handler->run();
// count budgets in job, to determine next step.
$configuration = $this->repository->getConfiguration($this->importJob);
$budgets = $configuration['budgets'] ?? [];
// if more than 1 budget, select budget first.
if (\count($budgets) > 0) { // TODO should be 1
$this->repository->setStage($this->importJob, 'select_budgets');
$this->repository->setStatus($this->importJob, 'need_job_config');
return;
}
if (\count($budgets) === 1) {
$this->repository->setStage($this->importJob, 'match_accounts');
}
return;
}
if('get_accounts' === $this->importJob->stage) {
$this->repository->setStatus($this->importJob, 'running');
/** @var GetAccountsHandler $handler */
$handler = app(GetAccountsHandler::class);
$handler->setImportJob($this->importJob);
$handler->run();
$this->repository->setStage($this->importJob, 'select_accounts');
$this->repository->setStatus($this->importJob, 'need_job_config');
}
// if ('match_accounts' === $this->importJob->stage) {
// // $this->repository->setStatus($this->importJob, 'running');
// /** @var StageGetBudgetsHandler $handler */
// $handler = app(StageGetBudgetsHandler::class);
// $handler->setImportJob($this->importJob);
// $handler->run();
// $this->repository->setStage($this->importJob, 'get_transactions');
// }
//
// if ('get_transactions' === $this->importJob->stage) {
// // $this->repository->setStatus($this->importJob, 'running');
// /** @var StageGetBudgetsHandler $handler */
// $handler = app(StageGetBudgetsHandler::class);
// $handler->setImportJob($this->importJob);
// $handler->run();
// $this->repository->setStage($this->importJob, 'get_transactions');
// }
throw new FireflyException(sprintf('YNAB import routine cannot handle stage "%s"', $this->importJob->stage));
}
throw new FireflyException(sprintf('YNAB import routine cannot handle status "%s"', $this->importJob->status));
}
/**

View File

@ -184,7 +184,6 @@ class ImportJobRepository implements ImportJobRepositoryInterface
$newConfig = array_merge($currentConfig, $configuration);
$job->configuration = $newConfig;
$job->save();
//Log::debug(sprintf('Set config of job "%s" to: ', $job->key), $newConfig);
return $job;

View File

@ -0,0 +1,53 @@
<?php
/**
* GetAccountsRequest.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\Services\Ynab\Request;
use Log;
/**
* Class GetAccountsRequest
*/
class GetAccountsRequest extends YnabRequest
{
/** @var array */
public $accounts;
/** @var string */
public $budgetId;
/**
*
*/
public function call(): void
{
Log::debug('Now in GetAccountsRequest::call()');
$uri = $this->api . sprintf('/budgets/%s/accounts', $this->budgetId);
Log::debug(sprintf('URI is %s', $uri));
$result = $this->authenticatedGetRequest($uri, []);
// expect data in [data][accounts]
$this->accounts = $result['data']['accounts'] ?? [];
}
}

View File

@ -0,0 +1,66 @@
<?php
/**
* GetBudgetsRequest.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\Services\Ynab\Request;
use Log;
/**
* Class GetBudgetsRequest
*/
class GetBudgetsRequest extends YnabRequest
{
/** @var array */
public $budgets;
public function __construct()
{
parent::__construct();
$this->budgets = [];
}
/**
*
*/
public function call(): void
{
Log::debug('Now in GetBudgetsRequest::call()');
$uri = $this->api . '/budgets';
Log::debug(sprintf('URI is %s', $uri));
$result = $this->authenticatedGetRequest($uri, []);
// expect data in [data][budgets]
$rawBudgets = $result['data']['budgets'] ?? [];
$freshBudgets = [];
foreach ($rawBudgets as $rawBudget) {
$freshBudgets[] = [
'id' => $rawBudget['id'],
'name' => $rawBudget['name'],
'currency_code' => $rawBudget['currency_format']['iso_code'],
];
}
$this->budgets = $freshBudgets;
}
}

View File

@ -0,0 +1,100 @@
<?php
/**
* YnabRequest.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\Services\Ynab\Request;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Log;
use RuntimeException;
/**
* Class YnabRequest
*/
abstract class YnabRequest
{
/** @var string */
protected $api;
/** @var string */
protected $token;
public function __construct()
{
$this->api = 'https://' . config('import.options.ynab.live') . '/' . config('import.options.ynab.version');
}
/**
* @param string $uri
* @param array|null $params
*
* @return array
*/
public function authenticatedGetRequest(string $uri, array $params = null): array
{
Log::debug(sprintf('Now in YnabRequest::authenticatedGetRequest(%s)', $uri), $params);
$client = new Client;
$params = $params ?? [];
$options = [
'headers' => [
'Authorization' => 'Bearer ' . $this->token,
],
];
if (\count($params) > 0) {
$uri = $uri . '?' . http_build_query($params);
}
Log::debug(sprintf('Going to call YNAB on URI: %s', $uri), $options);
try {
$res = $client->request('get', $uri, $options);
} catch (GuzzleException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
return [];
}
try {
$content = trim($res->getBody()->getContents());
} catch (RuntimeException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
return [];
}
return json_decode($content, true) ?? [];
}
/**
*
*/
abstract public function call(): void;
/**
* @param string $token
*/
public function setAccessToken(string $token): void
{
$this->token = $token;
}
}

View File

@ -0,0 +1,252 @@
<?php
/**
* NewYnabJobHandler.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\JobConfiguration\Ynab;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\MessageBag;
use Log;
use RuntimeException;
/**
* Class NewYnabJobHandler
*/
class NewYnabJobHandler implements YnabJobConfigurationInterface
{
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
* Return true when this stage is complete.
*
* @return bool
* @throws FireflyException
*/
public function configurationComplete(): bool
{
if (!$this->hasRefreshToken()) {
Log::debug('YNAB NewYnabJobHandler configurationComplete: stage is new, no refresh token, return false');
return false;
}
if ($this->hasRefreshToken() && $this->hasClientId() && $this->hasClientSecret()) {
Log::debug('YNAB NewYnabJobHandler configurationComplete: stage is new, has a refresh token, return true');
// need to grab access token using refresh token
$this->getAccessToken();
$this->repository->setStatus($this->importJob, 'ready_to_run');
$this->repository->setStage($this->importJob, 'get_budgets');
return true;
}
Log::error('YNAB NewYnabJobHandler configurationComplete: something broke, return true');
return true;
}
/**
* Store the job configuration. There is never anything to store for this stage.
*
* @param array $data
*
* @return MessageBag
*/
public function configureJob(array $data): MessageBag
{
Log::debug('YNAB NewYnabJobHandler configureJob: nothing to do.');
return new MessageBag;
}
/**
* Get data for config view.
*
* @return array
*/
public function getNextData(): array
{
$data = [];
// here we update the job so it can redirect properly to YNAB
if (!$this->hasRefreshToken() && $this->hasClientSecret() && $this->hasClientId()) {
// update stage to make sure we catch the token.
$this->repository->setStage($this->importJob, 'catch-auth-code');
$clientId = app('preferences')->get('ynab_client_id')->data;
$callBackUri = route('import.callback.ynab');
$uri = sprintf(
'https://app.youneedabudget.com/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code&state=%s', $clientId, $callBackUri,
$this->importJob->key
);
$data['token-url'] = $uri;
Log::debug(sprintf('YNAB getNextData: URI to redirect to is %s', $uri));
}
return $data;
}
/**
* Get the view for this stage.
*
* @return string
*/
public function getNextView(): string
{
Log::debug('Return YNAB redirect view.');
return 'import.ynab.redirect';
}
/**
* Set the import job.
*
* @param ImportJob $importJob
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
/**
* @throws FireflyException
*/
private function getAccessToken(): void
{
$clientId = app('preferences')->get('ynab_client_id')->data;
$clientSecret = app('preferences')->get('ynab_client_secret')->data;
$refreshToken = app('preferences')->get('ynab_refresh_token')->data;
$uri = sprintf(
'https://app.youneedabudget.com/oauth/token?client_id=%s&client_secret=%s&grant_type=refresh_token&refresh_token=%s', $clientId, $clientSecret,
$refreshToken
);
$client = new Client();
try {
$res = $client->request('post', $uri, []);
} catch (GuzzleException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
throw new FireflyException($e->getMessage());
}
$statusCode = $res->getStatusCode();
try {
$content = trim($res->getBody()->getContents());
} catch (RuntimeException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
throw new FireflyException($e->getMessage());
}
$json = json_decode($content, true) ?? [];
Log::debug(sprintf('Status code from YNAB is %d', $statusCode));
Log::debug(sprintf('Body of result is %s', $content), $json);
// store refresh token (if present?) as preference
// store token in job:
$configuration = $this->repository->getConfiguration($this->importJob);
$configuration['access_token'] = $json['access_token'];
$configuration['access_token_expires'] = (int)$json['created_at'] + (int)$json['expires_in'];
$this->repository->setConfiguration($this->importJob, $configuration);
// also store new refresh token:
$refreshToken = (string)($json['refresh_token'] ?? '');
if ('' !== $refreshToken) {
app('preferences')->set('ynab_refresh_token', $refreshToken);
}
Log::debug('end of NewYnabJobHandler::getAccessToken()');
}
/**
* Check if we have the client ID.
*
* @return bool
*/
private function hasClientId(): bool
{
$clientId = app('preferences')->getForUser($this->importJob->user, 'ynab_client_id', null);
if (null === $clientId) {
Log::debug('user has no YNAB client ID');
return false;
}
if ('' === (string)$clientId->data) {
Log::debug('user has no YNAB client ID (empty)');
return false;
}
Log::debug('user has a YNAB client ID');
return true;
}
/**
* Check if we have the client secret
*
* @return bool
*/
private function hasClientSecret(): bool
{
$clientSecret = app('preferences')->getForUser($this->importJob->user, 'ynab_client_secret', null);
if (null === $clientSecret) {
Log::debug('user has no YNAB client secret');
return false;
}
if ('' === (string)$clientSecret->data) {
Log::debug('user has no YNAB client secret (empty)');
return false;
}
Log::debug('user has a YNAB client secret');
return true;
}
/**
* @return bool
*/
private function hasRefreshToken(): bool
{
$preference = app('preferences')->get('ynab_refresh_token');
if (null === $preference) {
Log::debug('user has no YNAB refresh token.');
return false;
}
if ('' === (string)$preference->data) {
Log::debug('user has no YNAB refresh token (empty).');
return false;
}
Log::debug(sprintf('user has YNAB refresh token: %s', $preference->data));
return true;
}
}

View File

@ -0,0 +1,126 @@
<?php
/**
* SelectBudgetsHandler.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\JobConfiguration\Ynab;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use Illuminate\Support\MessageBag;
use Log;
/**
* Class SelectBudgetsHandler
*/
class SelectBudgetsHandler implements YnabJobConfigurationInterface
{
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
* Return true when this stage is complete.
*
* @return bool
*/
public function configurationComplete(): bool
{
Log::debug('Now in SelectBudgetsHandler::configComplete');
$configuration = $this->repository->getConfiguration($this->importJob);
$selectedBudget = $configuration['selected_budget'] ?? '';
if ($selectedBudget !== '') {
Log::debug(sprintf('Selected budget is %s, config is complete. Return true.', $selectedBudget));
$this->repository->setStage($this->importJob, 'get_accounts');
return true;
}
Log::debug('User has not selected a budget yet, config is not yet complete.');
return false;
}
/**
* Store the job configuration.
*
* @param array $data
*
* @return MessageBag
*/
public function configureJob(array $data): MessageBag
{
Log::debug('Now in SelectBudgetsHandler::configureJob');
$configuration = $this->repository->getConfiguration($this->importJob);
$configuration['selected_budget'] = $data['budget_id'];
Log::debug(sprintf('Set selected budget to %s', $data['budget_id']));
Log::debug('Mark job as ready for next stage.');
$this->repository->setConfiguration($this->importJob, $configuration);
return new MessageBag;
}
/**
* Get data for config view.
*
* @return array
*/
public function getNextData(): array
{
Log::debug('Now in SelectBudgetsHandler::getNextData');
$configuration = $this->repository->getConfiguration($this->importJob);
$budgets = $configuration['budgets'] ?? [];
$return = [];
foreach ($budgets as $budget) {
$return[$budget['id']] = $budget['name'] . ' (' . $budget['currency_code'] . ')';
}
return [
'budgets' => $return,
];
}
/**
* Get the view for this stage.
*
* @return string
*/
public function getNextView(): string
{
Log::debug('Now in SelectBudgetsHandler::getNextView');
return 'import.ynab.select-budgets';
}
/**
* Set the import job.
*
* @param ImportJob $importJob
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
}

View File

@ -0,0 +1,72 @@
<?php
/**
* YnabJobConfigurationInterface.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\JobConfiguration\Ynab;
use FireflyIII\Models\ImportJob;
use Illuminate\Support\MessageBag;
/**
* Interface YnabJobConfigurationInterface
*
* @package FireflyIII\Support\Import\JobConfiguration\Ynab
*/
interface YnabJobConfigurationInterface
{
/**
* Return true when this stage is complete.
*
* @return bool
*/
public function configurationComplete(): bool;
/**
* Store the job configuration.
*
* @param array $data
*
* @return MessageBag
*/
public function configureJob(array $data): MessageBag;
/**
* Get data for config view.
*
* @return array
*/
public function getNextData(): array;
/**
* Get the view for this stage.
*
* @return string
*/
public function getNextView(): string;
/**
* Set the import job.
*
* @param ImportJob $importJob
*/
public function setImportJob(ImportJob $importJob): void;
}

View File

@ -0,0 +1,80 @@
<?php
/**
* GetAccountsHandler.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\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Services\Ynab\Request\GetAccountsRequest;
/**
* Class GetAccountsHandler
*/
class GetAccountsHandler
{
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
* Get list of accounts for the selected budget.
*
* @throws FireflyException
*/
public function run(): void
{
$config = $this->repository->getConfiguration($this->importJob);
$selectedBudget = $config['selected_budget'] ?? '';
if ('' === $selectedBudget) {
$firstBudget = $config['budgets'][0] ?? false;
if (false === $firstBudget) {
throw new FireflyException('The configuration contains no budget. Erroring out.');
}
$selectedBudget = $firstBudget['id'];
$config['selected_budget'] = $selectedBudget;
}
$token = $config['access_token'];
$request = new GetAccountsRequest;
$request->budgetId = $selectedBudget;
$request->setAccessToken($token);
$request->call();
$config['accounts'] = $request->accounts;
$this->repository->setConfiguration($this->importJob, $config);
if (0 === \count($config['accounts'])) {
throw new FireflyException('This budget contains zero accounts.');
}
}
/**
* @param ImportJob $importJob
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
}

View File

@ -30,6 +30,7 @@ use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Log;
use RuntimeException;
/**
* Class StageGetAccessHandler
@ -66,12 +67,31 @@ class StageGetAccessHandler
throw new FireflyException($e->getMessage());
}
$statusCode = $res->getStatusCode();
try {
$content = trim($res->getBody()->getContents());
} catch(RuntimeException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
throw new FireflyException($e->getMessage());
}
$json = json_decode($content, true) ?? [];
Log::debug(sprintf('Status code from YNAB is %d', $statusCode));
Log::debug(sprintf('Body of result is %s', $content), $json);
Log::error('Hard exit');
exit;
// store refresh token (if present?) as preference
// store token in job:
$configuration = $this->repository->getConfiguration($this->importJob);
$configuration['access_token'] = $json['access_token'];
$configuration['access_token_expires'] = (int)$json['created_at'] + (int)$json['expires_in'];
$this->repository->setConfiguration($this->importJob, $configuration);
Log::debug('end of StageGetAccessHandler::run()');
$refreshToken = (string)($json['refresh_token'] ?? '');
if ('' !== $refreshToken) {
app('preferences')->set('ynab_refresh_token', $refreshToken);
}
}
/**

View File

@ -0,0 +1,75 @@
<?php
/**
* StageGetBudgetsHandler.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\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Services\Ynab\Request\GetBudgetsRequest;
use Log;
/**
* Class StageGetBudgetsHandler
*/
class StageGetBudgetsHandler
{
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
*
* @throws FireflyException
*/
public function run(): void
{
Log::debug('Now in StageGetBudgetsHandler::run()');
// grab access token from job:
$configuration = $this->repository->getConfiguration($this->importJob);
$token = $configuration['access_token'];
$request = new GetBudgetsRequest;
$request->setAccessToken($token);
$request->call();
// store budgets in users preferences.
$configuration['budgets'] = $request->budgets;
$this->repository->setConfiguration($this->importJob, $configuration);
Log::debug(sprintf('Found %d budgets', \count($request->budgets)));
if (\count($request->budgets) === 0) {
throw new FireflyException('It seems this user has zero budgets or an error prevented Firefly III from reading them.');
}
}
/**
* @param ImportJob $importJob
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
}

View File

@ -0,0 +1,55 @@
<?php
/**
* StageGetTransactionsHandler.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\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
/**
* Class StageGetTransactionsHandler
*/
class StageGetTransactionsHandler
{
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
*
*/
public function run(): void
{
}
/**
* @param ImportJob $importJob
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
}

View File

@ -0,0 +1,57 @@
<?php
/**
* StageMatchAccountsHandler.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\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
/**
* Class StageMatchAccountsHandler
*/
class StageMatchAccountsHandler
{
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
*
*/
public function run(): void
{
}
/**
* @param ImportJob $importJob
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
}

View File

@ -172,7 +172,7 @@ class Preferences
$lastActivity = implode(',', $lastActivity);
}
$hash = md5($lastActivity);
Log::debug(sprintf('Value of last activity is %s, hash is %s', $lastActivity, $hash));
//Log::debug(sprintf('Value of last activity is %s, hash is %s', $lastActivity, $hash));
return $hash;
}

View File

@ -152,7 +152,8 @@ return [
'server' => 'www.saltedge.com',
],
'ynab' => [
'client_id' => '666db19f6c5a2299bf44999a6ea802e96a5f488c3a5c8a5cbb417b59dcf18b72',
'live' => 'api.youneedabudget.com',
'version' => 'v1',
],
'plaid' => [],
'quovo' => [],

View File

@ -59,6 +59,8 @@ return [
'do_prereq_plaid' => 'Prerequisites for imports using Plaid',
'do_prereq_yodlee' => 'Prerequisites for imports using Yodlee',
'do_prereq_quovo' => 'Prerequisites for imports using Quovo',
'do_prereq_ynab' => 'Prerequisites for imports from YNAB',
// provider config box (index)
'can_config_title' => 'Import configuration',
'can_config_intro' => 'Some import methods can be configured to your liking. They have extra settings you can tweak.',
@ -144,6 +146,11 @@ return [
'share_config_file' => 'If you have imported data from a public bank, you should <a href="https://github.com/firefly-iii/import-configurations/wiki">share your configuration file</a> so it will be easy for other users to import their data. Sharing your configuration file will not expose your financial details.',
'job_config_bunq_apply_rules' => 'Apply rules',
'job_config_bunq_apply_rules_text' => 'By default, your rules will be applied to the transactions created during this import routine. If you do not want this to happen, deselect this checkbox.',
// job configuration for YNAB:
'job_config_select_budgets' => 'Select your budget',
'job_config_spectre_select_budgets_text' => 'You have :count budgets stored at YNAB. Please select the one from which Firefly III will import the transactions.',
// keys from "extra" array:
'spectre_extra_key_iban' => 'IBAN',
'spectre_extra_key_swift' => 'SWIFT',

View File

@ -0,0 +1,39 @@
{% extends "./layout/default" %}
{% block breadcrumbs %}
{{ Breadcrumbs.render }}
{% endblock %}
{% block content %}
<div class="row">
<form class="form-horizontal" action="{{ route('import.job.configuration.post',[importJob.key]) }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="box box-default">
<div class="box-header with-border">
<h3 class="box-title">{{ trans('import.job_config_select_budgets') }}</h3>
</div>
<div class="box-body">
<div class="row">
<div class="col-lg-6">
<p>
{{ trans('import.job_config_spectre_select_budgets_text', {count: data.budgets|length}) }}
</p>
{{ ExpandedForm.select('budget_id', data.budgets) }}
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn pull-right btn-success">
{{ ('submit')|_ }}
</button>
</div>
</div>
</div>
</div>
</form>
</div>
{% endblock %}
{% block scripts %}
{% endblock %}
{% block styles %}
{% endblock %}