Code clean up

This commit is contained in:
James Cole 2017-06-24 13:04:41 +02:00
parent 8a175d147b
commit 04a8a0e6a1
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
17 changed files with 29 additions and 398 deletions

View File

@ -87,13 +87,8 @@ class Import extends Command
$this->error(sprintf('Error importing line #%d: %s', $index, $error));
}
// display result to user:
//$this->presentResults($result);
$this->line(sprintf('The import has finished. %d transactions have been imported out of %d records.', $routine->journals->count(), $routine->lines));
// get any errors from the importer:
//$this->presentErrors($job);
return;
}
@ -120,36 +115,4 @@ class Import extends Command
return true;
}
/**
* @param ImportJob $job
*/
private function presentErrors(ImportJob $job)
{
$extendedStatus = $job->extended_status;
if (isset($extendedStatus['errors']) && count($extendedStatus['errors']) > 0) {
$this->line(sprintf('The following %d error(s) occured during the import:', count($extendedStatus['errors'])));
foreach ($extendedStatus['errors'] as $error) {
$this->error($error);
}
}
}
/**
* @param Collection $result
*/
private function presentResults(Collection $result)
{
/**
* @var int $index
* @var TransactionJournal $journal
*/
foreach ($result as $index => $journal) {
if (!is_null($journal->id)) {
$this->line(sprintf('Line #%d has been imported as transaction #%d.', $index, $journal->id));
continue;
}
$this->error(sprintf('Could not store line #%d', $index));
}
}
}

View File

@ -85,7 +85,7 @@ class ImportController extends Controller
*
* @param ImportJob $job
*
* @return string
* @return LaravelResponse
*/
public function download(ImportJob $job)
{

View File

@ -229,8 +229,8 @@ class ConvertController extends Controller
switch ($joined) {
default:
throw new FireflyException('Cannot handle ' . $joined); // @codeCoverageIgnore
case TransactionType::WITHDRAWAL . '-' . TransactionType::DEPOSIT: // one
case TransactionType::TRANSFER . '-' . TransactionType::DEPOSIT: // six
case TransactionType::WITHDRAWAL . '-' . TransactionType::DEPOSIT:
case TransactionType::TRANSFER . '-' . TransactionType::DEPOSIT:
if ($data['source_account_revenue'] === '') {
// destination is a cash account.
@ -248,14 +248,14 @@ class ConvertController extends Controller
];
$source = $accountRepository->store($data);
break;
case TransactionType::WITHDRAWAL . '-' . TransactionType::TRANSFER: // two
case TransactionType::TRANSFER . '-' . TransactionType::WITHDRAWAL: // five
case TransactionType::WITHDRAWAL . '-' . TransactionType::TRANSFER:
case TransactionType::TRANSFER . '-' . TransactionType::WITHDRAWAL:
$source = $sourceAccount;
break;
case TransactionType::DEPOSIT . '-' . TransactionType::WITHDRAWAL: // three
case TransactionType::DEPOSIT . '-' . TransactionType::WITHDRAWAL:
$source = $destinationAccount;
break;
case TransactionType::DEPOSIT . '-' . TransactionType::TRANSFER: // four
case TransactionType::DEPOSIT . '-' . TransactionType::TRANSFER:
$source = $accountRepository->find(intval($data['source_account_asset']));
break;
}

View File

@ -218,7 +218,7 @@ class SplitController extends Controller
'journal_destination_account_id' => $request->old('journal_destination_account_id', $destinationAccounts->first()->id),
'destinationAccounts' => $destinationAccounts,
'what' => strtolower($journal->transactionTypeStr()),
'date' => $request->old('date', $journal->date),
'date' => $request->old('date', $journal->date->format('Y-m-d')),
'tags' => join(',', $journal->tags->pluck('tag')->toArray()),
// all custom fields:

View File

@ -134,7 +134,7 @@ class CsvConfigurator implements ConfiguratorInterface
{
$class = false;
switch (true) {
case(!$this->job->configuration['initial-config-complete']):
case (!$this->job->configuration['initial-config-complete']):
$class = Initial::class;
break;
case (!$this->job->configuration['column-roles-complete']):

View File

@ -192,8 +192,7 @@ class CsvProcessor implements FileProcessorInterface
$string = json_encode($array);
$hash = hash('sha256', json_encode($string));
$json = json_encode($hash);
$entry = TransactionJournalMeta::
leftJoin('transaction_journals', 'transaction_journals.id', '=', 'journal_meta.transaction_journal_id')
$entry = TransactionJournalMeta::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'journal_meta.transaction_journal_id')
->where('data', $json)
->where('name', 'importHash')
->first();

View File

@ -140,7 +140,7 @@ class ImportAccount
}
/** @var Collection $accounts */
$accounts = $this->repository->getAccountsByType([$accountType->type]);
// 2: find by IBAN (and type):
// Two: find by IBAN (and type):
if (count($this->accountIban) === 3) {
$iban = $this->accountIban['value'];
Log::debug(sprintf('Finding account of type %d and IBAN %s', $accountType->id, $iban));
@ -163,7 +163,7 @@ class ImportAccount
Log::debug('Found nothing.');
}
// 3: find by name (and type):
// Three: find by name (and type):
if (count($this->accountName) === 3) {
$name = $this->accountName['value'];
Log::debug(sprintf('Finding account of type %d and name %s', $accountType->id, $name));

View File

@ -54,7 +54,6 @@ class ImportCurrency
Log::debug('In createCurrency()');
// check if any of them is mapped:
$mapped = $this->findMappedObject();
$searched = null;
if (!is_null($mapped->id)) {

View File

@ -251,7 +251,6 @@ class ImportJournal
case 'tags-space':
$this->tags[] = $array;
break;
// 'interest_date', 'book_date', 'process_date', 'due_date', 'payment_date', 'invoice_date',
case 'date-interest':
$this->metaDates['interest_date'] = $array['value'];
break;

View File

@ -155,7 +155,7 @@ class ImportStorage
$transaction->amount = $amount;
$transaction->save();
if (is_null($transaction->id)) {
$errorText = join(', ', $one->getErrors()->all());
$errorText = join(', ', $transaction->getErrors()->all());
throw new FireflyException($errorText);
}
Log::debug(sprintf('Created transaction with ID #%d and account #%d', $transaction->id, $accountId));

View File

@ -13,11 +13,14 @@ namespace FireflyIII\Repositories\Account;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
use Log;
/**
* @property User $user
*
* Trait FindAccountsTrait
*
* @package FireflyIII\Repositories\Account

View File

@ -19,10 +19,13 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
/**
* @property User $user
*
* Trait CreateJournalsTrait
*
* @package FireflyIII\Repositories\Journal

View File

@ -55,6 +55,7 @@ class Map implements ConfigurationInterface
$results = $reader->setOffset($offset)->fetch();
$this->validSpecifics = array_keys(config('csv.import_specifics'));
$indexes = array_keys($this->data);
$rowIndex = 0;
foreach ($results as $rowIndex => $row) {
$row = $this->runSpecifics($row);
@ -73,13 +74,13 @@ class Map implements ConfigurationInterface
// which is exclusively to fix the tags:
if (!is_null($this->data[$index]['preProcessMap']) && strlen($this->data[$index]['preProcessMap']) > 0) {
/** @var PreProcessorInterface $preProcessor */
$preProcessor = app($this->data[$index]['preProcessMap']);
$result = $preProcessor->run($value);
$data[$index]['values'] = array_merge($this->data[$index]['values'], $result);
$preProcessor = app($this->data[$index]['preProcessMap']);
$result = $preProcessor->run($value);
$this->data[$index]['values'] = array_merge($this->data[$index]['values'], $result);
Log::debug($rowIndex . ':' . $index . 'Value before preprocessor', ['value' => $value]);
Log::debug($rowIndex . ':' . $index . 'Value after preprocessor', ['value-new' => $result]);
Log::debug($rowIndex . ':' . $index . 'Value after joining', ['value-complete' => $data[$index]['values']]);
Log::debug($rowIndex . ':' . $index . 'Value after joining', ['value-complete' => $this->data[$index]['values']]);
continue;
@ -95,7 +96,7 @@ class Map implements ConfigurationInterface
// save number of rows, thus number of steps, in job:
$steps = $rowIndex * 5;
$extended = $this->job->extended_status;
$extended['steps'] = $steps;
$extended['steps'] = $steps;
$this->job->extended_status = $extended;
$this->job->save();

View File

@ -1,245 +0,0 @@
<?php
/**
* CsvImportSupportTrait.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Import;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Import\Mapper\MapperInterface;
use FireflyIII\Import\MapperPreProcess\PreProcessorInterface;
use FireflyIII\Import\Specifics\SpecificInterface;
use FireflyIII\Models\ImportJob;
use League\Csv\Reader;
use Log;
/**
* Trait CsvImportSupportTrait
*
* @property ImportJob $job
*
* @package FireflyIII\Support\Import
*/
trait CsvImportSupportTrait
{
/**
* @return bool
*/
protected function doColumnMapping(): bool
{
$mapArray = $this->job->configuration['column-do-mapping'] ?? [];
$doMap = false;
foreach ($mapArray as $value) {
if ($value === true) {
$doMap = true;
break;
}
}
return $this->job->configuration['column-mapping-complete'] === false && $doMap;
}
/**
* @return bool
*/
protected function doColumnRoles(): bool
{
return $this->job->configuration['column-roles-complete'] === false;
}
/**
* @return array
* @throws FireflyException
*/
protected function getDataForColumnMapping(): array
{
$config = $this->job->configuration;
$data = [];
$indexes = [];
foreach ($config['column-do-mapping'] as $index => $mustBeMapped) {
if ($mustBeMapped) {
$column = $config['column-roles'][$index] ?? '_ignore';
// is valid column?
$validColumns = array_keys(config('csv.import_roles'));
if (!in_array($column, $validColumns)) {
throw new FireflyException(sprintf('"%s" is not a valid column.', $column));
}
$canBeMapped = config('csv.import_roles.' . $column . '.mappable');
$preProcessMap = config('csv.import_roles.' . $column . '.pre-process-map');
if ($canBeMapped) {
$mapperClass = config('csv.import_roles.' . $column . '.mapper');
$mapperName = sprintf('\\FireflyIII\\Import\Mapper\\%s', $mapperClass);
/** @var MapperInterface $mapper */
$mapper = new $mapperName;
$indexes[] = $index;
$data[$index] = [
'name' => $column,
'mapper' => $mapperName,
'index' => $index,
'options' => $mapper->getMap(),
'preProcessMap' => null,
'values' => [],
];
if ($preProcessMap) {
$preClass = sprintf(
'\\FireflyIII\\Import\\MapperPreProcess\\%s',
config('csv.import_roles.' . $column . '.pre-process-mapper')
);
$data[$index]['preProcessMap'] = $preClass;
}
}
}
}
// in order to actually map we also need all possible values from the CSV file.
$content = $this->job->uploadFileContents();
/** @var Reader $reader */
$reader = Reader::createFromString($content);
$reader->setDelimiter($config['delimiter']);
$results = $reader->fetch();
$validSpecifics = array_keys(config('csv.import_specifics'));
foreach ($results as $rowIndex => $row) {
// skip first row?
if ($rowIndex === 0 && $config['has-headers']) {
continue;
}
// run specifics here:
// and this is the point where the specifix go to work.
foreach ($config['specifics'] as $name => $enabled) {
if (!in_array($name, $validSpecifics)) {
throw new FireflyException(sprintf('"%s" is not a valid class name', $name));
}
$class = config('csv.import_specifics.' . $name);
/** @var SpecificInterface $specific */
$specific = app($class);
// it returns the row, possibly modified:
$row = $specific->run($row);
}
//do something here
foreach ($indexes as $index) { // this is simply 1, 2, 3, etc.
if (!isset($row[$index])) {
// don't really know how to handle this. Just skip, for now.
continue;
}
$value = $row[$index];
if (strlen($value) > 0) {
// we can do some preprocessing here,
// which is exclusively to fix the tags:
if (!is_null($data[$index]['preProcessMap'])) {
/** @var PreProcessorInterface $preProcessor */
$preProcessor = app($data[$index]['preProcessMap']);
$result = $preProcessor->run($value);
$data[$index]['values'] = array_merge($data[$index]['values'], $result);
Log::debug($rowIndex . ':' . $index . 'Value before preprocessor', ['value' => $value]);
Log::debug($rowIndex . ':' . $index . 'Value after preprocessor', ['value-new' => $result]);
Log::debug($rowIndex . ':' . $index . 'Value after joining', ['value-complete' => $data[$index]['values']]);
continue;
}
$data[$index]['values'][] = $value;
}
}
}
foreach ($data as $index => $entry) {
$data[$index]['values'] = array_unique($data[$index]['values']);
}
return $data;
}
/**
* This method collects the data that will enable a user to choose column content.
*
* @return array
*/
protected function getDataForColumnRoles(): array
{
Log::debug('Now in getDataForColumnRoles()');
$config = $this->job->configuration;
$data = [
'columns' => [],
'columnCount' => 0,
'columnHeaders' => [],
];
// show user column role configuration.
$content = $this->job->uploadFileContents();
// create CSV reader.
$reader = Reader::createFromString($content);
$reader->setDelimiter($config['delimiter']);
$start = $config['has-headers'] ? 1 : 0;
$end = $start + config('csv.example_rows');
$header = [];
if ($config['has-headers']) {
$header = $reader->fetchOne(0);
}
// collect example data in $data['columns']
Log::debug(sprintf('While %s is smaller than %d', $start, $end));
while ($start < $end) {
$row = $reader->fetchOne($start);
Log::debug(sprintf('Row %d has %d columns', $start, count($row)));
// run specifics here:
// and this is the point where the specifix go to work.
foreach ($config['specifics'] as $name => $enabled) {
/** @var SpecificInterface $specific */
$specific = app('FireflyIII\Import\Specifics\\' . $name);
Log::debug(sprintf('Will now apply specific "%s" to row %d.', $name, $start));
// it returns the row, possibly modified:
$row = $specific->run($row);
}
foreach ($row as $index => $value) {
$value = trim($value);
$data['columnHeaders'][$index] = $header[$index] ?? '';
if (strlen($value) > 0) {
$data['columns'][$index][] = $value;
}
}
$start++;
$data['columnCount'] = count($row) > $data['columnCount'] ? count($row) : $data['columnCount'];
}
// make unique example data
foreach ($data['columns'] as $index => $values) {
$data['columns'][$index] = array_unique($values);
}
$data['set_roles'] = [];
// collect possible column roles:
$data['available_roles'] = [];
foreach (array_keys(config('csv.import_roles')) as $role) {
$data['available_roles'][$role] = trans('csv.column_' . $role);
}
$config['column-count'] = $data['columnCount'];
$this->job->configuration = $config;
$this->job->save();
return $data;
}
}

View File

@ -13,7 +13,7 @@ declare(strict_types=1);
namespace FireflyIII\Support;
use Amount;
use \Amount as GlobalAmount;
use Carbon\Carbon;
use Crypt;
use DB;
@ -48,9 +48,9 @@ class Steam
return $cache->get(); // @codeCoverageIgnore
}
$currencyId = intval($account->getMeta('currency_id'));
// if null, use system default currency:
// use system default currency:
if ($currencyId === 0) {
$currency = Amount::getDefaultCurrency();
$currency = GlobalAmount::getDefaultCurrency();
$currencyId = $currency->id;
}
// first part: get all balances in own currency:

View File

@ -8,7 +8,7 @@
* See the LICENSE file for details.
*/
/** global: spent, budgeted, available, currencySymbol, budgetIndexURI */
/** global: spent, budgeted, available, currencySymbol, budgetIndexURI, accounting */
function drawSpentBar() {
"use strict";

View File

@ -133,97 +133,6 @@
</div>
</div>
{#
<div class="row status_finished" style="display:none;">
<div class="col-lg-8 col-lg-offset-2 col-md-12 col-sm-12">
<div class="box box-default">
<div class="box-header with-border">
<h3 class="box-title">{{ 'import_status_report'|_ }}</h3>
</div>
<div class="box-body">
<p id="import-status-intro">
{{ 'import_finished_report'|_ }}
</p>
<p id="import-status-more-info"></p>
</div>
</div>
</div>
</div>
<div class="row info_errors" style="display:none;">
<div class="col-lg-8 col-lg-offset-2 col-md-12 col-sm-12">
<div class="box box-danger">
<div class="box-header with-border">
<h3 class="box-title">{{ 'import_status_errors'|_ }}</h3>
</div>
<div class="box-body">
<p id="import-status-error-intro">
No errors detected.
</p>
<ul id="import-status-error-list"></ul>
</div>
</div>
</div>
</div>
#}
{#
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-12 col-sm-12">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">{{ 'import_status_header'|_ }}</h3>
</div>
<div class="box-body">
<div class="progress" id="import-status-holder">
<div id="import-status-bar" class="progress-bar progress-bar-info active progress-bar-striped" role="progressbar"
aria-valuenow="100" aria-valuemin="0"
aria-valuemax="100" style="width: 100%">
</div>
</div>
<p id="import-status-txt">{{ 'import_status_settings_complete'|_ }}</p>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-12 col-sm-12">
<div class="box box-danger">
<div class="box-header with-border">
<h3 class="box-title">{{ 'import_status_errors'|_ }}</h3>
</div>
<div class="box-body">
<p id="import-status-error-intro">
No errors detected.
</p>
<ul id="import-status-error-list"></ul>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-12 col-sm-12">
<div class="box box-default">
<div class="box-header with-border">
<h3 class="box-title">{{ 'import_status_report'|_ }}</h3>
</div>
<div class="box-body">
<p id="import-status-intro">
{{ 'import_finished_report'|_ }}
</p>
<p id="import-status-more-info"></p>
</div>
</div>
</div>
</div>
#}
{% endblock %}
{% block scripts %}
<script type="text/javascript">