2017-06-10 15:09:41 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2017-10-21 08:40:00 +02:00
|
|
|
* Initial.php
|
2017-06-10 15:09:41 +02:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* 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/>.
|
2017-06-10 15:09:41 +02:00
|
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-12-16 08:03:35 +01:00
|
|
|
namespace FireflyIII\Support\Import\Configuration\File;
|
2017-06-10 15:09:41 +02:00
|
|
|
|
|
|
|
use ExpandedForm;
|
|
|
|
use FireflyIII\Models\AccountType;
|
|
|
|
use FireflyIII\Models\ImportJob;
|
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|
|
|
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
|
|
|
|
use Log;
|
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class CsvInitial.
|
2017-06-10 15:09:41 +02:00
|
|
|
*/
|
|
|
|
class Initial implements ConfigurationInterface
|
|
|
|
{
|
|
|
|
private $job;
|
2017-07-07 13:50:15 +02:00
|
|
|
|
2017-06-10 15:09:41 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getData(): array
|
|
|
|
{
|
|
|
|
/** @var AccountRepositoryInterface $accountRepository */
|
|
|
|
$accountRepository = app(AccountRepositoryInterface::class);
|
|
|
|
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
|
|
|
|
$delimiters = [
|
|
|
|
',' => trans('form.csv_comma'),
|
|
|
|
';' => trans('form.csv_semicolon'),
|
|
|
|
'tab' => trans('form.csv_tab'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$specifics = [];
|
|
|
|
|
|
|
|
// collect specifics.
|
|
|
|
foreach (config('csv.import_specifics') as $name => $className) {
|
|
|
|
$specifics[$name] = [
|
|
|
|
'name' => $className::getName(),
|
|
|
|
'description' => $className::getDescription(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'accounts' => ExpandedForm::makeSelectList($accounts),
|
|
|
|
'specifix' => [],
|
|
|
|
'delimiters' => $delimiters,
|
|
|
|
'specifics' => $specifics,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2017-08-12 07:47:42 +02:00
|
|
|
/**
|
|
|
|
* Return possible warning to user.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getWarningMessage(): string
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2017-06-12 19:12:07 +02:00
|
|
|
/**
|
|
|
|
* @param ImportJob $job
|
|
|
|
*
|
|
|
|
* @return ConfigurationInterface
|
|
|
|
*/
|
|
|
|
public function setJob(ImportJob $job): ConfigurationInterface
|
|
|
|
{
|
|
|
|
$this->job = $job;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:09:41 +02:00
|
|
|
/**
|
|
|
|
* Store the result.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function storeConfiguration(array $data): bool
|
|
|
|
{
|
|
|
|
/** @var AccountRepositoryInterface $repository */
|
|
|
|
$repository = app(AccountRepositoryInterface::class);
|
|
|
|
$importId = $data['csv_import_account'] ?? 0;
|
|
|
|
$account = $repository->find(intval($importId));
|
|
|
|
|
2017-11-15 12:25:49 +01:00
|
|
|
$hasHeaders = isset($data['has_headers']) && 1 === intval($data['has_headers']) ? true : false;
|
2017-06-10 15:09:41 +02:00
|
|
|
$config = $this->job->configuration;
|
|
|
|
$config['initial-config-complete'] = true;
|
|
|
|
$config['has-headers'] = $hasHeaders;
|
|
|
|
$config['date-format'] = $data['date_format'];
|
|
|
|
$config['delimiter'] = $data['csv_delimiter'];
|
2017-11-15 12:25:49 +01:00
|
|
|
$config['delimiter'] = 'tab' === $config['delimiter'] ? "\t" : $config['delimiter'];
|
2017-12-01 20:11:29 +01:00
|
|
|
$config['apply_rules'] = isset($data['apply_rules']) && 1 === intval($data['apply_rules']) ? true : false;
|
|
|
|
$config['match_bills'] = isset($data['match_bills']) && 1 === intval($data['match_bills']) ? true : false;
|
2017-06-10 15:09:41 +02:00
|
|
|
|
|
|
|
Log::debug('Entered import account.', ['id' => $importId]);
|
|
|
|
|
2017-11-15 12:25:49 +01:00
|
|
|
if (null !== $account->id) {
|
2017-06-10 15:09:41 +02:00
|
|
|
Log::debug('Found account.', ['id' => $account->id, 'name' => $account->name]);
|
|
|
|
$config['import-account'] = $account->id;
|
|
|
|
}
|
2017-06-14 20:13:19 +02:00
|
|
|
|
2017-11-15 12:25:49 +01:00
|
|
|
if (null === $account->id) {
|
2017-06-10 15:09:41 +02:00
|
|
|
Log::error('Could not find anything for csv_import_account.', ['id' => $importId]);
|
|
|
|
}
|
|
|
|
|
2017-07-07 13:50:15 +02:00
|
|
|
$config = $this->storeSpecifics($data, $config);
|
|
|
|
$this->job->configuration = $config;
|
|
|
|
$this->job->save();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
* @param array $config
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function storeSpecifics(array $data, array $config): array
|
|
|
|
{
|
2017-06-10 15:09:41 +02:00
|
|
|
// loop specifics.
|
|
|
|
if (isset($data['specifics']) && is_array($data['specifics'])) {
|
2017-08-12 10:27:45 +02:00
|
|
|
$names = array_keys($data['specifics']);
|
|
|
|
foreach ($names as $name) {
|
2017-06-10 15:09:41 +02:00
|
|
|
// verify their content.
|
|
|
|
$className = sprintf('FireflyIII\Import\Specifics\%s', $name);
|
|
|
|
if (class_exists($className)) {
|
|
|
|
$config['specifics'][$name] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-07 13:50:15 +02:00
|
|
|
return $config;
|
2017-06-10 15:09:41 +02:00
|
|
|
}
|
2017-07-07 08:09:42 +02:00
|
|
|
}
|