Expand text and routine for YNAB

This commit is contained in:
James Cole 2018-07-31 05:49:03 +02:00
parent a004f27361
commit c1ac2bb156
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
4 changed files with 90 additions and 8 deletions

View File

@ -23,8 +23,13 @@ declare(strict_types=1);
namespace FireflyIII\Support\Import\JobConfiguration\Ynab; namespace FireflyIII\Support\Import\JobConfiguration\Ynab;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\ImportJob; use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag; use Illuminate\Support\MessageBag;
use Log; use Log;
@ -33,6 +38,12 @@ use Log;
*/ */
class SelectBudgetHandler implements YnabJobConfigurationInterface class SelectBudgetHandler implements YnabJobConfigurationInterface
{ {
/** @var AccountRepositoryInterface */
private $accountRepository;
/** @var Collection */
private $accounts;
/** @var CurrencyRepositoryInterface */
private $currencyRepository;
/** @var ImportJob */ /** @var ImportJob */
private $importJob; private $importJob;
/** @var ImportJobRepositoryInterface */ /** @var ImportJobRepositoryInterface */
@ -51,6 +62,7 @@ class SelectBudgetHandler implements YnabJobConfigurationInterface
if ($selectedBudget !== '') { if ($selectedBudget !== '') {
Log::debug(sprintf('Selected budget is %s, config is complete. Return true.', $selectedBudget)); Log::debug(sprintf('Selected budget is %s, config is complete. Return true.', $selectedBudget));
$this->repository->setStage($this->importJob, 'get_accounts'); $this->repository->setStage($this->importJob, 'get_accounts');
return true; return true;
} }
Log::debug('User has not selected a budget yet, config is not yet complete.'); Log::debug('User has not selected a budget yet, config is not yet complete.');
@ -90,13 +102,24 @@ class SelectBudgetHandler implements YnabJobConfigurationInterface
Log::debug('Now in SelectBudgetHandler::getNextData'); Log::debug('Now in SelectBudgetHandler::getNextData');
$configuration = $this->repository->getConfiguration($this->importJob); $configuration = $this->repository->getConfiguration($this->importJob);
$budgets = $configuration['budgets'] ?? []; $budgets = $configuration['budgets'] ?? [];
$return = []; $available = [];
$notAvailable = [];
$total = \count($budgets);
foreach ($budgets as $budget) { foreach ($budgets as $budget) {
$return[$budget['id']] = $budget['name'] . ' (' . $budget['currency_code'] . ')'; if ($this->haveAssetWithCurrency($budget['currency_code'])) {
Log::debug('Add budget to available list.');
$available[$budget['id']] = $budget['name'] . ' (' . $budget['currency_code'] . ')';
continue;
}
Log::debug('Add budget to notAvailable list.');
$notAvailable[$budget['id']] = $budget['name'] . ' (' . $budget['currency_code'] . ')';
} }
return [ return [
'budgets' => $return, 'available' => $available,
'not_available' => $notAvailable,
'total' => $total,
]; ];
} }
@ -119,8 +142,44 @@ class SelectBudgetHandler implements YnabJobConfigurationInterface
*/ */
public function setImportJob(ImportJob $importJob): void public function setImportJob(ImportJob $importJob): void
{ {
$this->importJob = $importJob; $this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class); $this->repository = app(ImportJobRepositoryInterface::class);
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
$this->accountRepository = app(AccountRepositoryInterface::class);
$this->repository->setUser($importJob->user); $this->repository->setUser($importJob->user);
$this->currencyRepository->setUser($importJob->user);
$this->accountRepository->setUser($importJob->user);
$this->accountRepository->setUser($importJob->user);
$this->accounts = $this->accountRepository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]);
}
/**
* @param string $code
*
* @return bool
*/
private function haveAssetWithCurrency(string $code): bool
{
$currency = $this->currencyRepository->findByCodeNull($code);
if (null === $currency) {
Log::debug(sprintf('No currency found with code "%s"', $code));
return false;
}
/** @var Account $account */
foreach ($this->accounts as $account) {
$currencyId = (int)$this->accountRepository->getMetaValue($account, 'currency_id');
Log::debug(sprintf('Currency of %s is %d (looking for %d).', $account->name, $currencyId, $currency->id));
if ($currencyId === $currency->id) {
Log::debug('Return true!');
return true;
}
}
Log::debug('Found nothing, return false.');
return false;
} }
} }

View File

@ -171,6 +171,9 @@ return [
'job_config_ynab_no_budgets' => 'There are no budgets available to be imported from.', 'job_config_ynab_no_budgets' => 'There are no budgets available to be imported from.',
'ynab_no_mapping' => 'It seems you have not selected any accounts to import from.', 'ynab_no_mapping' => 'It seems you have not selected any accounts to import from.',
'job_config_ynab_bad_currency' => 'You cannot import from the following budget(s), because you do not have accounts with the same currency as these budgets.', 'job_config_ynab_bad_currency' => 'You cannot import from the following budget(s), because you do not have accounts with the same currency as these budgets.',
'job_config_ynab_accounts_title' => 'Select accounts',
'job_config_ynab_accounts_text' => 'You have the following accounts available in this budget. Please select from which accounts you want to import, and where the transactions should be stored.',
// keys from "extra" array: // keys from "extra" array:
'spectre_extra_key_iban' => 'IBAN', 'spectre_extra_key_iban' => 'IBAN',

View File

@ -104,6 +104,7 @@ return [
'sum_transfers' => 'Sum of transfers', 'sum_transfers' => 'Sum of transfers',
'reconcile' => 'Reconcile', 'reconcile' => 'Reconcile',
'account_on_spectre' => 'Account (Spectre)', 'account_on_spectre' => 'Account (Spectre)',
'account_on_ynab' => 'Account (YNAB)',
'do_import' => 'Import from this account', 'do_import' => 'Import from this account',
'sepa-ct-id' => 'SEPA End to End Identifier', 'sepa-ct-id' => 'SEPA End to End Identifier',
'sepa-ct-op' => 'SEPA Opposing Account Identifier', 'sepa-ct-op' => 'SEPA Opposing Account Identifier',

View File

@ -11,21 +11,40 @@
<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">
<h3 class="box-title">{{ trans('import.job_config_select_budgets') }}</h3> <h3 class="box-title">{{ trans('import.job_config_ynab_select_budgets') }}</h3>
</div> </div>
<div class="box-body"> <div class="box-body">
<div class="row"> <div class="row">
<div class="col-lg-6"> <div class="col-lg-6">
<p> <p>
{{ trans('import.job_config_spectre_select_budgets_text', {count: data.budgets|length}) }} {{ trans('import.job_config_ynab_select_budgets_text', {count: data.total}) }}
</p> </p>
{{ ExpandedForm.select('budget_id', data.budgets) }} {% if data.available|length == 0 %}
<p class="text-danger">
{{ trans('import.job_config_ynab_no_budgets') }}
</p>
{% else %}
{{ ExpandedForm.select('budget_id', data.available) }}
{% endif %}
{% if data.not_available|length > 0 %}
<p class="text-warning">
{{ trans('import.job_config_ynab_bad_currency') }}
</p>
<ul>
{% for budget in data.not_available %}
<li>{{ budget }}</li>
{% endfor %}
</ul>
{% endif %}
</div> </div>
</div> </div>
<div class="box-footer"> <div class="box-footer">
{% if data.available|length != 0 %}
<button type="submit" class="btn pull-right btn-success"> <button type="submit" class="btn pull-right btn-success">
{{ ('submit')|_ }} {{ ('submit')|_ }}
</button> </button>
{% endif %}
</div> </div>
</div> </div>
</div> </div>