Files
firefly-iii/app/Support/Import/JobConfiguration/Ynab/SelectBudgetHandler.php

185 lines
5.9 KiB
PHP
Raw Normal View History

2018-07-29 21:02:03 +02:00
<?php
/**
2018-07-30 20:39:19 +02:00
* SelectBudgetHandler.php
2018-07-29 21:02:03 +02:00
* 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;
2018-07-31 05:49:03 +02:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
2018-07-29 21:02:03 +02:00
use FireflyIII\Models\ImportJob;
2018-07-31 05:49:03 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2018-07-29 21:02:03 +02:00
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
2018-07-31 05:49:03 +02:00
use Illuminate\Support\Collection;
2018-07-29 21:02:03 +02:00
use Illuminate\Support\MessageBag;
use Log;
/**
2018-07-30 20:39:19 +02:00
* Class SelectBudgetHandler
2018-07-29 21:02:03 +02:00
*/
2018-07-30 20:39:19 +02:00
class SelectBudgetHandler implements YnabJobConfigurationInterface
2018-07-29 21:02:03 +02:00
{
2018-07-31 05:49:03 +02:00
/** @var AccountRepositoryInterface */
private $accountRepository;
/** @var Collection */
private $accounts;
/** @var CurrencyRepositoryInterface */
private $currencyRepository;
2018-07-29 21:02:03 +02:00
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
* Return true when this stage is complete.
*
* @return bool
*/
public function configurationComplete(): bool
{
2018-07-30 20:39:19 +02:00
Log::debug('Now in SelectBudgetHandler::configComplete');
2018-07-29 21:02:03 +02:00
$configuration = $this->repository->getConfiguration($this->importJob);
$selectedBudget = $configuration['selected_budget'] ?? '';
2018-08-06 19:14:30 +02:00
if ('' !== $selectedBudget) {
2018-07-29 21:02:03 +02:00
Log::debug(sprintf('Selected budget is %s, config is complete. Return true.', $selectedBudget));
$this->repository->setStage($this->importJob, 'get_accounts');
2018-07-31 05:49:03 +02:00
2018-07-29 21:02:03 +02:00
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
{
2018-07-30 20:39:19 +02:00
Log::debug('Now in SelectBudgetHandler::configureJob');
2018-07-29 21:02:03 +02:00
$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
{
2018-07-30 20:39:19 +02:00
Log::debug('Now in SelectBudgetHandler::getNextData');
2018-07-29 21:02:03 +02:00
$configuration = $this->repository->getConfiguration($this->importJob);
$budgets = $configuration['budgets'] ?? [];
2018-07-31 05:49:03 +02:00
$available = [];
$notAvailable = [];
$total = \count($budgets);
2018-07-29 21:02:03 +02:00
foreach ($budgets as $budget) {
2018-07-31 05:49:03 +02:00
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'] . ')';
2018-07-29 21:02:03 +02:00
}
return [
2018-07-31 05:49:03 +02:00
'available' => $available,
'not_available' => $notAvailable,
'total' => $total,
2018-07-29 21:02:03 +02:00
];
}
/**
* Get the view for this stage.
*
* @return string
*/
public function getNextView(): string
{
2018-07-30 20:39:19 +02:00
Log::debug('Now in SelectBudgetHandler::getNextView');
2018-07-29 21:02:03 +02:00
return 'import.ynab.select-budgets';
}
/**
* Set the import job.
*
* @param ImportJob $importJob
*/
public function setImportJob(ImportJob $importJob): void
{
2018-07-31 05:49:03 +02:00
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
$this->accountRepository = app(AccountRepositoryInterface::class);
2018-07-29 21:02:03 +02:00
$this->repository->setUser($importJob->user);
2018-07-31 05:49:03 +02:00
$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;
2018-07-29 21:02:03 +02:00
}
}