Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Lots of import related code.
James Colec9e46a4dd190
diff --git a/app/Console/Commands/Import.php b/app/Console/Commands/Import.phpnew file mode 100644index 0000000000..8a139083df--- /dev/null+++ b/app/Console/Commands/Import.php@@ -0,0 +1,85 @@+<?php+/**+ * Import.php+ * Copyright (C) 2016 [email protected]+ *+ * This software may be modified and distributed under the terms+ * of the MIT license. See the LICENSE file for details.+ */++declare(strict_types = 1);++namespace FireflyIII\Console\Commands;++use FireflyIII\Import\Importer\ImporterInterface;+use FireflyIII\Import\Logging\CommandHandler;+use FireflyIII\Models\ImportJob;+use Illuminate\Console\Command;+use Log;++/**+ * Class Import+ *+ * @package FireflyIII\Console\Commands+ */+class Import extends Command+{+ /**+ * The console command description.+ *+ * @var string+ */+ protected $description = 'Import stuff into Firefly III.';++ /**+ * The name and signature of the console command.+ *+ * @var string+ */+ protected $signature = 'firefly:import {key}';++ /**+ * Create a new command instance.+ *+ */+ public function __construct()+ {+ parent::__construct();+ }++ /**+ * Execute the console command.+ *+ * @return mixed+ */+ public function handle()+ {+ $jobKey = $this->argument('key');+ $job = ImportJob::whereKey($jobKey)->first();+ if (is_null($job)) {+ $this->error('This job does not seem to exist.');++ return;+ }++ if ($job->status != 'settings_complete') {+ $this->error('This job is not ready to be imported.');++ return;+ }++ $this->line('Going to import job with key "' . $job->key . '" of type ' . $job->file_type);+ $class = config('firefly.import_formats.' . $job->file_type);+ /** @var ImporterInterface $importer */+ $importer = app($class);+ $importer->setJob($job);+ // intercept logging by importer.+ $monolog = Log::getMonolog();+ $handler = new CommandHandler($this);+ $monolog->pushHandler($handler);+ $importer->start();+++ $this->line('Something something import: ' . $jobKey);+ }+}diff --git a/app/Console/Commands/UpgradeFireflyInstructions.php b/app/Console/Commands/UpgradeFireflyInstructions.phpindex 4535d17a7f..7881f87646 100644--- a/app/Console/Commands/UpgradeFireflyInstructions.php+++ b/app/Console/Commands/UpgradeFireflyInstructions.php@@ -25,7 +25,7 @@ class UpgradeFireflyInstructions extends Command** @var string*/- protected $description = 'Command description';+ protected $description = 'Instructions in case of upgrade trouble.';/*** The name and signature of the console command.*diff --git a/app/Console/Kernel.php b/app/Console/Kernel.phpindex ea6b228eea..5e9147a429 100644--- a/app/Console/Kernel.php+++ b/app/Console/Kernel.php@@ -11,6 +11,7 @@ declare(strict_types = 1);namespace FireflyIII\Console;+use FireflyIII\Console\Commands\Import;use FireflyIII\Console\Commands\UpgradeFireflyInstructions;use FireflyIII\Console\Commands\VerifyDatabase;use Illuminate\Foundation\Console\Kernel as ConsoleKernel;@@ -51,5 +52,6 @@ class Kernel extends ConsoleKernel= [UpgradeFireflyInstructions::class,VerifyDatabase::class,+ Import::class,];}diff --git a/app/Import/Converter/AssetAccountIban.php b/app/Import/Converter/AssetAccountIban.phpnew file mode 100644index 0000000000..a0fcd9e40d--- /dev/null+++ b/app/Import/Converter/AssetAccountIban.php@@ -0,0 +1,48 @@+<?php+/**+ * AssetAccountIban.php+ * Copyright (C) 2016 [email protected]+ *+ * This software may be modified and distributed under the terms+ * of the MIT license. See the LICENSE file for details.+ */++declare(strict_types = 1);++namespace FireflyIII\Import\Converter;++use FireflyIII\Crud\Account\AccountCrudInterface;+use Log;++/**+ * Class AssetAccountIban+ *+ * @package FireflyIII\Import\Converter+ */+class AssetAccountIban extends BasicConverter implements ConverterInterface+{++ /**+ * @param $value+ *+ */+ public function convert($value)+ {+ Log::debug('Going to convert value ' . $value);++ /** @var AccountCrudInterface $repository */+ $repository = app(AccountCrudInterface::class, [$this->user]);+++ if (isset($this->mapping[$value])) {+ Log::debug('Found account in mapping. Should exist.',['value' => $value]);+ $account = $repository->find(intval($value));+ Log::debug('Found account ', ['id' => $account->id]);++ }++ Log::debug('Given map is ', $this->mapping);++ exit;+ }+}\ No newline at end of filediff --git a/app/Import/Converter/BasicConverter.php b/app/Import/Converter/BasicConverter.phpnew file mode 100644index 0000000000..29e5c70924--- /dev/null+++ b/app/Import/Converter/BasicConverter.php@@ -0,0 +1,57 @@+<?php+/**+ * BasicConverter.php+ * Copyright (C) 2016 [email protected]+ *+ * This software may be modified and distributed under the terms+ * of the MIT license. See the LICENSE file for details.+ */++declare(strict_types = 1);++namespace FireflyIII\Import\Converter;++use FireflyIII\User;+++/**+ * Class BasicConverter+ *+ * @package FireflyIII\Import\Converter+ */+class BasicConverter+{+ /** @var bool */+ public $doMap;++ /** @var array */+ public $mapping = [];++ /** @var User */+ public $user;++ /**+ * @param mixed $doMap+ */+ public function setDoMap(bool $doMap)+ {+ $this->doMap = $doMap;+ }++ /**+ * @param array $mapping+ *+ */+ public function setMapping(array $mapping)+ {+ $this->mapping = $mapping;+ }++ /**+ * @param User $user+ */+ public function setUser(User $user)+ {+ $this->user = $user;+ }+}\ No newline at end of filediff --git a/app/Import/Converter/ConverterInterface.php b/app/Import/Converter/ConverterInterface.phpnew file mode 100644index 0000000000..c0d9b187fe--- /dev/null+++ b/app/Import/Converter/ConverterInterface.php@@ -0,0 +1,43 @@+<?php+/**+ * ConverterInterface.php+ * Copyright (C) 2016 [email protected]+ *+ * This software may be modified and distributed under the terms+ * of the MIT license. See the LICENSE file for details.+ */++declare(strict_types = 1);++namespace FireflyIII\Import\Converter;+use FireflyIII\User;++/**+ * Interface ConverterInterface+ *+ * @package FireflyIII\Import\Converter+ */+interface ConverterInterface+{+ /**+ * @param $value+ *+ */+ public function convert($value);++ /**+ * @param bool $doMap+ */+ public function setDoMap(bool $doMap);++ /**+ * @param array $mapping+ *+ */+ public function setMapping(array $mapping);++ /**+ * @param User $user+ */+ public function setUser(User $user);+}\ No newline at end of filediff --git a/app/Import/Converter/SomeConverter.php b/app/Import/Converter/SomeConverter.phpnew file mode 100644index 0000000000..3b6d3b31c1--- /dev/null+++ b/app/Import/Converter/SomeConverter.php@@ -0,0 +1,31 @@+<?php+/**+ * SomeConverter.php+ * Copyright (C) 2016 [email protected]+ *+ * This software may be modified and distributed under the terms+ * of the MIT license. See the LICENSE file for details.+ */++declare(strict_types = 1);++namespace FireflyIII\Import\Converter;++/**+ * Class SomeConverter+ *+ * @package FireflyIII\Import\Converter+ */+class SomeConverter extends BasicConverter implements ConverterInterface+{++ /**+ * @param $value+ *+ */+ public function convert($value)+ {+// // TODO: Implement convert() method.+// throw new NotImplementedException;+ }+}\ No newline at end of filediff --git a/app/Import/ImportEntry.php b/app/Import/ImportEntry.phpnew file mode 100644index 0000000000..c69ab325e9--- /dev/null+++ b/app/Import/ImportEntry.php@@ -0,0 +1,41 @@+<?php+/**+ * ImportEntry.php+ * Copyright (C) 2016 [email protected]+ *+ * This software may be modified and distributed under the terms+ * of the MIT license. See the LICENSE file for details.+ */++declare(strict_types = 1);++namespace FireflyIII\Import;++use FireflyIII\Exceptions\FireflyException;+use FireflyIII\Models\Account;++/**+ * Class ImportEntry+ *+ * @package FireflyIII\Import+ */+class ImportEntry+{+ /** @var Account */+ public $assetAccount;++ /**+ * @param $role+ * @param $value+ *+ * @throws FireflyException+ */+ public function fromRawValue($role, $value)+ {+ switch ($role) {+ default:+ throw new FireflyException('Cannot handle role of type "' . $role . '".');++ }+ }+}\ No newline at end of filediff --git a/app/Import/Importer/CsvImporter.php b/app/Import/Importer/CsvImporter.phpindex cccfcdde74..ec60721ef8 100644--- a/app/Import/Importer/CsvImporter.php+++ b/app/Import/Importer/CsvImporter.php@@ -14,6 +14,8 @@ namespace FireflyIII\Import\Importer;use ExpandedForm;use FireflyIII\Crud\Account\AccountCrud;+use FireflyIII\Import\Converter\ConverterInterface;+use FireflyIII\Import\ImportEntry;use FireflyIII\Import\Mapper\MapperInterface;use FireflyIII\Models\AccountType;use FireflyIII\Models\ImportJob;@@ -211,6 +213,32 @@ class CsvImporter implements ImporterInterface$this->job = $job;}+ /**+ * Run the actual import+ *+ * @return bool+ */+ public function start(): bool+ {+ $config = $this->job->configuration;+ $content = $this->job->uploadFileContents();++ // create CSV reader.+ $reader = Reader::createFromString($content);+ $start = $config['has-headers'] ? 1 : 0;+ $results = $reader->fetch();+ foreach ($results as $index => $row) {+ if ($index >= $start) {+ Log::debug(sprintf('Now going to import row %d.', $index));+ $this->importSingleRow($row);+ }+ }++ Log::debug('This call should be intercepted somehow.');++ return true;+ }+/*** Store the settings filled in by the user, if applicable.*@@ -387,4 +415,44 @@ class CsvImporter implements ImporterInterface}++ /**+ * @param array $row+ *+ * @return bool+ */+ private function importSingleRow(array $row): bool+ {+ $object = new ImportEntry;+ $config = $this->job->configuration;++ foreach ($row as $index => $value) {+ // find the role for this column:+ $role = $config['column-roles'][$index] ?? '_ignore';+ $doMap = $config['column-do-mapping'][$index] ?? false;+ $converterClass = config('csv.import_roles.' . $role . '.converter');+ $mapping = $config['column-mapping-config'][$index] ?? [];+ /** @var ConverterInterface $converter */+ $converter = app('FireflyIII\\Import\\Converter\\' . $converterClass);+ // set some useful values for the converter:+ $converter->setMapping($mapping);+ $converter->setDoMap($doMap);+ $converter->setUser($this->job->user);++ // run the converter for this value:+ $convertedValue = $converter->convert($value);++ // log it.+ Log::debug('Value ', ['index' => $index, 'value' => $value, 'role' => $role]);++ // store in import entry:+ // $object->fromRawValue($role, $value);+++ }++ exit;++ return true;+ }}\ No newline at end of filediff --git a/app/Import/Importer/ImporterInterface.php b/app/Import/Importer/ImporterInterface.phpindex 6e332da8fe..66b72a6f08 100644--- a/app/Import/Importer/ImporterInterface.php+++ b/app/Import/Importer/ImporterInterface.php@@ -23,6 +23,14 @@ use Symfony\Component\HttpFoundation\FileBag;*/interface ImporterInterface{++ /**+ * Run the actual import+ *+ * @return bool+ */+ public function start(): bool;+/*** After uploading, and after setJob(), prepare anything that is* necessary for the configure() line.diff --git a/app/Import/Logging/CommandHandler.php b/app/Import/Logging/CommandHandler.phpnew file mode 100644index 0000000000..6b4d3106c1--- /dev/null+++ b/app/Import/Logging/CommandHandler.php@@ -0,0 +1,49 @@+<?php+/**+ * Handler.php+ * Copyright (C) 2016 [email protected]+ *+ * This software may be modified and distributed under the terms+ * of the MIT license. See the LICENSE file for details.+ */++declare(strict_types = 1);++namespace FireflyIII\Import\Logging;++use Illuminate\Console\Command;+use Monolog\Handler\AbstractProcessingHandler;++/**+ * Class CommandHandler+ *+ * @package FireflyIII\Import\Logging+ */+class CommandHandler extends AbstractProcessingHandler+{++ /** @var Command */+ private $command;++ /**+ * Handler constructor.+ *+ * @param Command $command+ */+ public function __construct(Command $command)+ {+ $this->command = $command;+ }++ /**+ * Writes the record down to the log of the implementing handler+ *+ * @param array $record+ *+ * @return void+ */+ protected function write(array $record)+ {+ $this->command->line((string) $record['formatted']);+ }+}\ No newline at end of filediff --git a/app/Import/Mapper/AssetAccountIbans.php b/app/Import/Mapper/AssetAccountIbans.phpnew file mode 100644index 0000000000..d3a4c246e6--- /dev/null+++ b/app/Import/Mapper/AssetAccountIbans.php@@ -0,0 +1,56 @@+<?php+/**+ * AssetAccountIbans.php+ * Copyright (C) 2016 [email protected]+ *+ * This software may be modified and distributed under the terms+ * of the MIT license. See the LICENSE file for details.+ */++declare(strict_types = 1);++namespace FireflyIII\Import\Mapper;++use FireflyIII\Crud\Account\AccountCrudInterface;+use FireflyIII\Models\Account;+use FireflyIII\Models\AccountType;++/**+ * Class AssetAccounts+ *+ * @package FireflyIII\Import\Mapper+ */+class AssetAccountIbans implements MapperInterface+{++ /**+ * @return array+ */+ public function getMap(): array+ {+ /** @var AccountCrudInterface $crud */+ $crud = app(AccountCrudInterface::class);+ $set = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);+ $topList = [];+ $list = [];++ /** @var Account $account */+ foreach ($set as $account) {+ $iban = $account->iban ?? '';+ if (strlen($iban) > 0) {+ $topList[$account->id] = $account->iban . ' (' . $account->name . ')';+ }+ if (strlen($iban) == 0) {+ $list[$account->id] = $account->name;+ }+ }+ asort($topList);+ asort($list);++ $list = $topList + $list;+ $list = [0 => trans('csv.do_not_map')] + $list;++ return $list;++ }+}\ No newline at end of filediff --git a/app/Import/Mapper/OpposingAccountIbans.php b/app/Import/Mapper/OpposingAccountIbans.phpnew file mode 100644index 0000000000..f788345483--- /dev/null+++ b/app/Import/Mapper/OpposingAccountIbans.php@@ -0,0 +1,61 @@+<?php+/**+ * OpposingAccountIbans.php+ * Copyright (C) 2016 [email protected]+ *+ * This software may be modified and distributed under the terms+ * of the MIT license. See the LICENSE file for details.+ */++declare(strict_types = 1);++namespace FireflyIII\Import\Mapper;++use FireflyIII\Crud\Account\AccountCrudInterface;+use FireflyIII\Models\Account;+use FireflyIII\Models\AccountType;++/**+ * Class OpposingAccounts+ *+ * @package FireflyIII\Import\Mapper+ */+class OpposingAccountIbans implements MapperInterface+{++ /**+ * @return array+ */+ public function getMap(): array+ {+ /** @var AccountCrudInterface $crud */+ $crud = app(AccountCrudInterface::class);+ $set = $crud->getAccountsByType(+ [+ AccountType::DEFAULT, AccountType::ASSET,+ AccountType::EXPENSE, AccountType::BENEFICIARY,+ AccountType::REVENUE+ ]);+ $topList = [];+ $list = [];++ /** @var Account $account */+ foreach ($set as $account) {+ $iban = $account->iban ?? '';+ if (strlen($iban) > 0) {+ $topList[$account->id] = $account->iban . ' (' . $account->name . ')';+ }+ if (strlen($iban) == 0) {+ $list[$account->id] = $account->name;+ }+ }+ asort($topList);+ asort($list);++ $list = $topList + $list;+ $list = [0 => trans('csv.do_not_map')] + $list;+++ return $list;+ }+}\ No newline at end of filediff --git a/config/csv.php b/config/csv.phpindex 3481ca2769..20fb48a91d 100644--- a/config/csv.php+++ b/config/csv.php@@ -135,7 +135,7 @@ return ['mappable' => true,'field' => 'asset-account-iban','converter' => 'AssetAccountIban',- 'mapper' => 'AssetAccounts',+ 'mapper' => 'AssetAccountIbans',],'account-number' => [@@ -160,7 +160,7 @@ return ['mappable' => true,'field' => 'opposing-account-iban','converter' => 'OpposingAccountIban',- 'mapper' => 'OpposingAccounts',+ 'mapper' => 'OpposingAccountIbans',],'opposing-number' => ['mappable' => true,diff --git a/resources/lang/en_US/csv.php b/resources/lang/en_US/csv.phpindex 3f2003ff22..ae51cf1f68 100644--- a/resources/lang/en_US/csv.php+++ b/resources/lang/en_US/csv.php@@ -33,6 +33,15 @@ return ['no_example_data' => 'No example data available','store_column_roles' => 'Continue import','do_not_map' => '(do not map)',+ 'map_title' => 'Connect data in your files',+ 'map_text' => 'Connect data in your files',++ 'field_value' => 'Field value',+ 'field_mapped_to' => 'Mapped to',+ 'store_column_mapping' => 'Store mapping',++ // map things.+'column__ignore' => '(ignore this column)','column_account-iban' => 'Asset account (IBAN)',diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.phpindex ecb93831af..c7982f10b1 100644--- a/resources/lang/en_US/firefly.php+++ b/resources/lang/en_US/firefly.php@@ -759,4 +759,8 @@ return ['configure_import' => 'Further configure your import','import_finish_configuration' => 'Finish configuration','settings_for_import' => 'Settings',+ 'import_complete' => 'Import configuration complete!',+ 'import_complete_text' => 'Download the config file. You can also run it from the command line.',+ 'import_download_config' => 'Download configuration',+ 'import_start_import' => 'Start import',];diff --git a/resources/views/import/complete.twig b/resources/views/import/complete.twigindex 0a7aaaff7b..e7e7a82629 100644--- a/resources/views/import/complete.twig+++ b/resources/views/import/complete.twig@@ -14,12 +14,15 @@<p>{{ 'import_complete_text'|_ }}</p>+ <p>+ <code>php artisan firefly:import {{ job.key }}</code>+ </p><div class="row">- <div class="col-lg-2">- <a href="{{ route('import.download', [job.key]) }}" class="btn btn-default"><i class="fa fa-fw fa-download"></i> {{ 'import_download_config' }}</a>+ <div class="col-lg-4">+ <a href="{{ route('import.download', [job.key]) }}" class="btn btn-default"><i class="fa fa-fw fa-download"></i> {{ 'import_download_config'|_ }}</a></div>- <div class="col-lg-2">- <a href="#" class="btn btn-default"><i class="fa fa-fw fa-gears"></i> {{ 'import_start_import' }}</a>+ <div class="col-lg-4">+ <a href="#" class="btn btn-default"><i class="fa fa-fw fa-gears"></i> {{ 'import_start_import'|_ }}</a></div></div></div>diff --git a/resources/views/import/csv/configure.twig b/resources/views/import/csv/configure.twigindex 0cbab054f3..fd3949e778 100644--- a/resources/views/import/csv/configure.twig+++ b/resources/views/import/csv/configure.twig@@ -32,44 +32,47 @@<h3 class="box-title">{{ trans('csv.import_configure_form') }}</h3></div><div class="box-body">-- {{ ExpandedForm.checkbox('has_headers',1,job.configuration['has-headers'],{helpText: trans('csv.header_help')}) }}- {{ ExpandedForm.text('date_format',job.configuration['date-format'],{helpText: trans('csv.date_help', {dateExample: phpdate('Ymd')}) }) }}- {{ ExpandedForm.select('csv_delimiter', data.delimiters, job.configuration['delimiter'], {helpText: trans('csv.delimiter_help') } ) }}- {{ ExpandedForm.select('csv_import_account', data.accounts, 0, {helpText: trans('csv.import_account_help')} ) }}-- {% for type, specific in data.specifics %}- <div class="form-group">- <label for="{{ type }}_label" class="col-sm-4 control-label">- {{ specific.name }}- </label>-- <div class="col-sm-8">- <div class="radio"><label>- {{ Form.checkbox('specifics['~type~']', '1',- job.configuration.specifics[type] == '1', {'id': type ~ '_label'}) }}- {{ specific.description }}+ <div class="row">+ <div class="col-lg-6">++ {{ ExpandedForm.checkbox('has_headers',1,job.configuration['has-headers'],{helpText: trans('csv.header_help')}) }}+ {{ ExpandedForm.text('date_format',job.configuration['date-format'],{helpText: trans('csv.date_help', {dateExample: phpdate('Ymd')}) }) }}+ {{ ExpandedForm.select('csv_delimiter', data.delimiters, job.configuration['delimiter'], {helpText: trans('csv.delimiter_help') } ) }}+ {{ ExpandedForm.select('csv_import_account', data.accounts, 0, {helpText: trans('csv.import_account_help')} ) }}++ {% for type, specific in data.specifics %}+ <div class="form-group">+ <label for="{{ type }}_label" class="col-sm-4 control-label">+ {{ specific.name }}</label>- </div>- </div>- </div>- {% endfor %}- {% if not data.is_upload_possible %}- <div class="form-group" id="csv_holder">- <div class="col-sm-4">- - </div>-- <div class="col-sm-8">- <pre>{{ data.upload_path }}</pre>- <p class="text-danger">- {{ trans('csv.upload_not_writeable') }}- </p>- </div>+ <div class="col-sm-8">+ <div class="radio"><label>+ {{ Form.checkbox('specifics['~type~']', '1',+ job.configuration.specifics[type] == '1', {'id': type ~ '_label'}) }}+ {{ specific.description }}+ </label>+ </div>+ </div>+ </div>+ {% endfor %}++ {% if not data.is_upload_possible %}+ <div class="form-group" id="csv_holder">+ <div class="col-sm-4">+ + </div>++ <div class="col-sm-8">+ <pre>{{ data.upload_path }}</pre>+ <p class="text-danger">+ {{ trans('csv.upload_not_writeable') }}+ </p>+ </div>+ </div>+ {% endif %}</div>- {% endif %}-+ </div></div></div></div>diff --git a/resources/views/import/csv/map.twig b/resources/views/import/csv/map.twigindex 7119e382aa..5f21f1a9d0 100644--- a/resources/views/import/csv/map.twig+++ b/resources/views/import/csv/map.twig@@ -31,7 +31,7 @@<div class="col-lg-12"><div class="box"><div class="box-header with-border">- <h3 class="box-title">{{ field.name }}</h3>+ <h3 class="box-title">{{ trans('csv.column_'~field.name) }}</h3></div><div class="box-body no-padding"><table class="table table-hover">