Bunq and Spectre will ask to apply rules.

This commit is contained in:
James Cole
2018-06-23 10:55:26 +02:00
parent 19a874b274
commit a1d99c1954
8 changed files with 56 additions and 309 deletions

View File

@@ -1,214 +0,0 @@
<?php
/**
* BunqConfigurator.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\Import\Configuration;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Support\Import\Configuration\Bunq\HaveAccounts;
use Log;
/**
* @deprecated
* @codeCoverageIgnore
* Class BunqConfigurator.
*/
class BunqConfigurator implements ConfiguratorInterface
{
/** @var ImportJob */
private $job;
/** @var ImportJobRepositoryInterface */
private $repository;
/** @var string */
private $warning = '';
/**
* ConfiguratorInterface constructor.
*/
public function __construct()
{
}
/**
* Store any data from the $data array into the job.
*
* @param array $data
*
* @return bool
*
* @throws FireflyException
*/
public function configureJob(array $data): bool
{
if (null === $this->job) {
throw new FireflyException('Cannot call configureJob() without a job.');
}
$stage = $this->getConfig()['stage'] ?? 'initial';
Log::debug(sprintf('in getNextData(), for stage "%s".', $stage));
switch ($stage) {
case 'have-accounts':
/** @var HaveAccounts $class */
$class = app(HaveAccounts::class);
$class->setJob($this->job);
$class->storeConfiguration($data);
// update job for next step and set to "configured".
$config = $this->getConfig();
$config['stage'] = 'have-account-mapping';
$this->repository->setConfiguration($this->job, $config);
return true;
default:
throw new FireflyException(sprintf('Cannot store configuration when job is in state "%s"', $stage));
break;
}
}
/**
* Return the data required for the next step in the job configuration.
*
* @return array
*
* @throws FireflyException
*/
public function getNextData(): array
{
if (null === $this->job) {
throw new FireflyException('Cannot call configureJob() without a job.');
}
$config = $this->getConfig();
$stage = $config['stage'] ?? 'initial';
Log::debug(sprintf('in getNextData(), for stage "%s".', $stage));
switch ($stage) {
case 'have-accounts':
/** @var HaveAccounts $class */
$class = app(HaveAccounts::class);
$class->setJob($this->job);
return $class->getData();
default:
return [];
}
}
/**
* @return string
*
* @throws FireflyException
*/
public function getNextView(): string
{
if (null === $this->job) {
throw new FireflyException('Cannot call configureJob() without a job.');
}
$stage = $this->getConfig()['stage'] ?? 'initial';
Log::debug(sprintf('getNextView: in getNextView(), for stage "%s".', $stage));
switch ($stage) {
case 'have-accounts':
return 'import.bunq.accounts';
default:
return '';
}
}
/**
* Return possible warning to user.
*
* @return string
*/
public function getWarningMessage(): string
{
return $this->warning;
}
/**
* @return bool
*
* @throws FireflyException
*/
public function isJobConfigured(): bool
{
if (null === $this->job) {
throw new FireflyException('Cannot call configureJob() without a job.');
}
$stage = $this->getConfig()['stage'] ?? 'initial';
Log::debug(sprintf('in isJobConfigured(), for stage "%s".', $stage));
switch ($stage) {
case 'have-accounts':
Log::debug('isJobConfigured returns false');
return false;
default:
Log::debug('isJobConfigured returns true');
return true;
}
}
/**
* @param ImportJob $job
*/
public function setJob(ImportJob $job): void
{
// make repository
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($job->user);
// set default config:
$defaultConfig = [
'is-redirected' => false,
'stage' => 'initial',
'auto-start' => true,
'apply-rules' => true,
];
$currentConfig = $this->repository->getConfiguration($job);
$finalConfig = array_merge($defaultConfig, $currentConfig);
// set default extended status:
$extendedStatus = $this->repository->getExtendedStatus($job);
$extendedStatus['steps'] = 8;
// save to job:
$job = $this->repository->setConfiguration($job, $finalConfig);
$job = $this->repository->setExtendedStatus($job, $extendedStatus);
$this->job = $job;
}
/**
* Shorthand method.
*
* @return array
*/
private function getConfig(): array
{
return $this->repository->getConfiguration($this->job);
}
}

View File

@@ -1,80 +0,0 @@
<?php
/**
* ConfiguratorInterface.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\Import\Configuration;
use FireflyIII\Models\ImportJob;
/**
* @deprecated
* @codeCoverageIgnore
* Interface ConfiguratorInterface.
*/
interface ConfiguratorInterface
{
/**
* ConfiguratorInterface constructor.
*/
public function __construct();
/**
* Store any data from the $data array into the job.
*
* @param array $data
*
* @return bool
*/
public function configureJob(array $data): bool;
/**
* Return the data required for the next step in the job configuration.
*
* @return array
*/
public function getNextData(): array;
/**
* Returns the view of the next step in the job configuration.
*
* @return string
*/
public function getNextView(): string;
/**
* Return possible warning to user.
*
* @return string
*/
public function getWarningMessage(): string;
/**
* Returns true when the initial configuration for this job is complete.
*
* @return bool
*/
public function isJobConfigured(): bool;
/**
* @param ImportJob $job
*/
public function setJob(ImportJob $job);
}

View File

@@ -76,10 +76,11 @@ class ChooseAccountsHandler implements BunqJobConfigurationInterface
*/ */
public function configureJob(array $data): MessageBag public function configureJob(array $data): MessageBag
{ {
$config = $this->repository->getConfiguration($this->importJob); $config = $this->repository->getConfiguration($this->importJob);
$accounts = $config['accounts'] ?? []; $accounts = $config['accounts'] ?? [];
$mapping = $data['account_mapping'] ?? []; $mapping = $data['account_mapping'] ?? [];
$final = []; $applyRules = (int)$data['apply_rules'] === 1;
$final = [];
if (\count($accounts) === 0) { if (\count($accounts) === 0) {
throw new FireflyException('No bunq accounts found. Import cannot continue.'); // @codeCoverageIgnore throw new FireflyException('No bunq accounts found. Import cannot continue.'); // @codeCoverageIgnore
} }
@@ -98,7 +99,8 @@ class ChooseAccountsHandler implements BunqJobConfigurationInterface
$accountId = $this->validLocalAccount($localId); $accountId = $this->validLocalAccount($localId);
$final[$bunqId] = $accountId; $final[$bunqId] = $accountId;
} }
$config['mapping'] = $final; $config['mapping'] = $final;
$config['apply-rules'] = $applyRules;
$this->repository->setConfiguration($this->importJob, $config); $this->repository->setConfiguration($this->importJob, $config);
return new MessageBag; return new MessageBag;

View File

@@ -45,11 +45,6 @@ class NewBunqJobHandler implements BunqJobConfigurationInterface
*/ */
public function configurationComplete(): bool public function configurationComplete(): bool
{ {
// simply set the job configuration "apply-rules" to true.
$config = $this->repository->getConfiguration($this->importJob);
$config['apply-rules'] = true;
$this->repository->setConfiguration($this->importJob, $config);
return true; return true;
} }

View File

@@ -83,9 +83,10 @@ class ChooseAccountsHandler implements SpectreJobConfigurationInterface
public function configureJob(array $data): MessageBag public function configureJob(array $data): MessageBag
{ {
Log::debug('Now in ChooseAccountsHandler::configureJob()', $data); Log::debug('Now in ChooseAccountsHandler::configureJob()', $data);
$config = $this->importJob->configuration; $config = $this->importJob->configuration;
$mapping = $data['account_mapping'] ?? []; $mapping = $data['account_mapping'] ?? [];
$final = []; $final = [];
$applyRules = (int)$data['apply_rules'] === 1;
foreach ($mapping as $spectreId => $localId) { foreach ($mapping as $spectreId => $localId) {
// validate each // validate each
$spectreId = $this->validSpectreAccount((int)$spectreId); $spectreId = $this->validSpectreAccount((int)$spectreId);
@@ -96,7 +97,7 @@ class ChooseAccountsHandler implements SpectreJobConfigurationInterface
Log::debug('Final mapping is:', $final); Log::debug('Final mapping is:', $final);
$messages = new MessageBag; $messages = new MessageBag;
$config['account_mapping'] = $final; $config['account_mapping'] = $final;
$config['apply-rules'] = $applyRules;
$this->repository->setConfiguration($this->importJob, $config); $this->repository->setConfiguration($this->importJob, $config);
if ($final === [0 => 0] || \count($final) === 0) { if ($final === [0 => 0] || \count($final) === 0) {
$messages->add('count', trans('import.spectre_no_mapping')); $messages->add('count', trans('import.spectre_no_mapping'));

View File

@@ -127,13 +127,16 @@ return [
'spectre_no_mapping' => 'It seems you have not selected any accounts to import from.', 'spectre_no_mapping' => 'It seems you have not selected any accounts to import from.',
'imported_from_account' => 'Imported from ":account"', 'imported_from_account' => 'Imported from ":account"',
'spectre_account_with_number' => 'Account :number', 'spectre_account_with_number' => 'Account :number',
'job_config_spectre_apply_rules' => 'Apply rules',
'job_config_spectre_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 bunq: // job configuration for bunq:
'job_config_bunq_accounts_title' => 'bunq accounts', 'job_config_bunq_accounts_title' => 'bunq accounts',
'job_config_bunq_accounts_text' => 'These are the accounts associated with your bunq account. Please select the accounts from which you want to import, and in which account the transactions must be imported.', 'job_config_bunq_accounts_text' => 'These are the accounts associated with your bunq account. Please select the accounts from which you want to import, and in which account the transactions must be imported.',
'bunq_no_mapping' => 'It seems you have not selected any accounts.', 'bunq_no_mapping' => 'It seems you have not selected any accounts.',
'should_download_config' => 'You should download <a href=":route">the configuration file</a> for this job. This will make future imports way easier.', 'should_download_config' => 'You should download <a href=":route">the configuration file</a> for this job. This will make future imports way easier.',
'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.', '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.',
// keys from "extra" array: // keys from "extra" array:
'spectre_extra_key_iban' => 'IBAN', 'spectre_extra_key_iban' => 'IBAN',
'spectre_extra_key_swift' => 'SWIFT', 'spectre_extra_key_swift' => 'SWIFT',

View File

@@ -7,6 +7,26 @@
<div class="row"> <div class="row">
<form class="form-horizontal" action="{{ route('import.job.configuration.post',[importJob.key]) }}" method="post"> <form class="form-horizontal" action="{{ route('import.job.configuration.post',[importJob.key]) }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/> <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_bunq_apply_rules') }}</h3>
</div>
<div class="box-body">
<div class="row">
<div class="col-lg-6">
<p>
{{ trans('import.job_config_bunq_apply_rules_text') }}
</p>
{{ ExpandedForm.checkbox('apply_rules', 1, true) }}
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-12 col-md-12 col-sm-12"> <div class="col-lg-12 col-md-12 col-sm-12">
<div class="box box-default"> <div class="box box-default">
<div class="box-header with-border"> <div class="box-header with-border">

View File

@@ -7,6 +7,26 @@
<div class="row"> <div class="row">
<form class="form-horizontal" action="{{ route('import.job.configuration.post',[importJob.key]) }}" method="post"> <form class="form-horizontal" action="{{ route('import.job.configuration.post',[importJob.key]) }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/> <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_spectre_apply_rules') }}</h3>
</div>
<div class="box-body">
<div class="row">
<div class="col-lg-6">
<p>
{{ trans('import.job_config_spectre_apply_rules_text') }}
</p>
{{ ExpandedForm.checkbox('apply_rules', 1, true) }}
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-12 col-md-12 col-sm-12"> <div class="col-lg-12 col-md-12 col-sm-12">
<div class="box box-default"> <div class="box box-default">
<div class="box-header with-border"> <div class="box-header with-border">