Skip to content

Firefly III: a personal finances manager

Various code clean up.

James Cole69eef30ad352
diff --git a/app/Generator/Report/Audit/MonthReportGenerator.php b/app/Generator/Report/Audit/MonthReportGenerator.php
index d02815963a..43e23ad1a4 100644
--- a/app/Generator/Report/Audit/MonthReportGenerator.php
+++ b/app/Generator/Report/Audit/MonthReportGenerator.php
@@ -163,7 +163,7 @@ class MonthReportGenerator implements ReportGeneratorInterface
/** @var Transaction $journal */
foreach ($journals as $transaction) {
$transaction->before = $startBalance;
- $transactionAmount = $transaction->transaction_amount;
+ $transactionAmount = $transaction->transaction_amount;
if ($currency->id === $transaction->foreign_currency_id) {
$transactionAmount = $transaction->transaction_foreign_amount;
diff --git a/app/Http/Controllers/Chart/TagReportController.php b/app/Http/Controllers/Chart/TagReportController.php
index 5c3ee98ed1..2aca50bc77 100644
--- a/app/Http/Controllers/Chart/TagReportController.php
+++ b/app/Http/Controllers/Chart/TagReportController.php
@@ -364,4 +364,4 @@ class TagReportController extends Controller
return $grouped;
}
-}
+}
\ No newline at end of file
diff --git a/app/Http/Controllers/Report/CategoryController.php b/app/Http/Controllers/Report/CategoryController.php
index 969ea8afdc..250e54dde9 100644
--- a/app/Http/Controllers/Report/CategoryController.php
+++ b/app/Http/Controllers/Report/CategoryController.php
@@ -120,7 +120,7 @@ class CategoryController extends Controller
foreach ($categories as $category) {
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $start, $end);
if (bccomp($spent, '0') !== 0) {
- $report[$category->id] = ['name' => $category->name, 'spent' => $spent,'id' => $category->id];
+ $report[$category->id] = ['name' => $category->name, 'spent' => $spent, 'id' => $category->id];
}
}
diff --git a/app/Import/Object/ImportJournal.php b/app/Import/Object/ImportJournal.php
index 8c6c86384b..adebaea1f6 100644
--- a/app/Import/Object/ImportJournal.php
+++ b/app/Import/Object/ImportJournal.php
@@ -62,8 +62,6 @@ class ImportJournal
private $modifiers = [];
/** @var array */
private $tags = [];
- /** @var string */
- private $transactionType = '';
/** @var User */
private $user;
diff --git a/app/Import/Object/ImportTransaction.php b/app/Import/Object/ImportTransaction.php
deleted file mode 100644
index 696aaaac6c..0000000000
--- a/app/Import/Object/ImportTransaction.php
+++ /dev/null
@@ -1,157 +0,0 @@
-<?php
-/**
- * ImportTransaction.php
- * Copyright (c) 2017 [email protected]
- * 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\Import\Object;
-
-
-use FireflyIII\Import\Converter\ConverterInterface;
-use Steam;
-
-class ImportTransaction
-{
- /** @var string */
- private $amount;
-
- /** @var ImportCurrency */
- private $currency;
-
- /** @var string */
- private $date;
-
- /** @var string */
- private $description;
- private $modifiers = [];
- /** @var bool */
- private $positive = true;
-
- public function __construct()
- {
- $this->currency = new ImportCurrency;
- }
-
- public function addToModifier(array $modifier)
- {
- $this->modifiers[] = $modifier;
- }
-
- /**
- * @return string
- */
- public function getAmount(): string
- {
- // use converter:
- $this->amount = strval($this->parseAmount());
-
-
- // also apply modifiers:
- $this->amount = Steam::positive($this->amount);
-
- // Can handle ING
- foreach ($this->modifiers as $modifier) {
- $class = sprintf('FireflyIII\Import\Converter\%s', config(sprintf('csv.import_roles.%s.converter', $modifier['role'])));
- /** @var ConverterInterface $converter */
- $converter = app($class);
- if ($converter->convert($modifier['value']) === -1) {
- $this->amount = Steam::negative($this->amount);
- }
- }
-
- return $this->amount;
- }
-
- /**
- * @param string $amount
- */
- public function setAmount(string $amount)
- {
- $this->amount = $amount;
- }
-
- /**
- * @return ImportCurrency
- */
- public function getCurrency(): ImportCurrency
- {
- return $this->currency;
- }
-
- /**
- * @param ImportCurrency $currency
- */
- public function setCurrency(ImportCurrency $currency)
- {
- $this->currency = $currency;
- }
-
- /**
- * @param string $date
- */
- public function setDate(string $date)
- {
- $this->date = $date;
- }
-
- /**
- * @param string $description
- */
- public function setDescription(string $description)
- {
- $this->description = $description;
- }
-
- /**
- * @param bool $positive
- */
- public function setPositive(bool $positive)
- {
- $this->positive = $positive;
- }
-
- /**
- * Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
- * - Jamie Zawinski
- *
- * @return float
- */
- private function parseAmount()
- {
- $value = $this->amount;
- $len = strlen($value);
- $decimalPosition = $len - 3;
- $decimal = null;
-
- if (($len > 2 && $value{$decimalPosition} == '.') || ($len > 2 && strpos($value, '.') > $decimalPosition)) {
- $decimal = '.';
- }
- if ($len > 2 && $value{$decimalPosition} == ',') {
- $decimal = ',';
- }
-
- // if decimal is dot, replace all comma's and spaces with nothing. then parse as float (round to 4 pos)
- if ($decimal === '.') {
- $search = [',', ' '];
- $value = str_replace($search, '', $value);
- }
- if ($decimal === ',') {
- $search = ['.', ' '];
- $value = str_replace($search, '', $value);
- $value = str_replace(',', '.', $value);
- }
- if (is_null($decimal)) {
- // replace all:
- $search = ['.', ' ', ','];
- $value = str_replace($search, '', $value);
- }
-
- return round(floatval($value), 12);
- }
-
-}
diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php
index bef618f634..e0a9f262b7 100644
--- a/app/Repositories/Category/CategoryRepository.php
+++ b/app/Repositories/Category/CategoryRepository.php
@@ -160,42 +160,19 @@ class CategoryRepository implements CategoryRepositoryInterface
*/
public function lastUseDate(Category $category, Collection $accounts): Carbon
{
- $last = null;
+ $last = new Carbon('1900-01-01');
+ $lastJournalDate = $this->getLastJournalDate($category, $accounts);
- /** @var TransactionJournal $first */
- $lastJournalQuery = $category->transactionJournals()->orderBy('date', 'DESC');
-
- if ($accounts->count() > 0) {
- // filter journals:
- $ids = $accounts->pluck('id')->toArray();
- $lastJournalQuery->leftJoin('transactions as t', 't.transaction_journal_id', '=', 'transaction_journals.id');
- $lastJournalQuery->whereIn('t.account_id', $ids);
- }
-
- $lastJournal = $lastJournalQuery->first(['transaction_journals.*']);
-
- if ($lastJournal) {
- $last = $lastJournal->date;
+ if ($lastJournalDate->year !== 1900) {
+ $last = clone $lastJournalDate;
+ unset($lastJournalDate);
}
- // check transactions:
-
- $lastTransactionQuery = $category->transactions()
- ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
- ->orderBy('transaction_journals.date', 'DESC');
- if ($accounts->count() > 0) {
- // filter journals:
- $ids = $accounts->pluck('id')->toArray();
- $lastTransactionQuery->whereIn('transactions.account_id', $ids);
- }
+ $lastTransactionDate = $this->getLastTransactionDate($category, $accounts);
- $lastTransaction = $lastTransactionQuery->first(['transaction_journals.*']);
- if (!is_null($lastTransaction) && ((!is_null($last) && $lastTransaction->date < $last) || is_null($last))) {
- $last = new Carbon($lastTransaction->date);
- }
-
- if (is_null($last)) {
- return new Carbon('1900-01-01');
+ if ($lastTransactionDate->year !== 1900 && $lastTransactionDate < $last) {
+ $last = clone $lastTransactionDate;
+ unset($lastTransactionDate);
}
return $last;
@@ -479,4 +456,53 @@ class CategoryRepository implements CategoryRepositoryInterface
return $category;
}
+ /**
+ * @param Category $category
+ * @param Collection $accounts
+ *
+ * @return Carbon
+ */
+ private function getLastJournalDate(Category $category, Collection $accounts): Carbon
+ {
+ $query = $category->transactionJournals()->orderBy('date', 'DESC');
+
+ if ($accounts->count() > 0) {
+ $query->leftJoin('transactions as t', 't.transaction_journal_id', '=', 'transaction_journals.id');
+ $query->whereIn('t.account_id', $accounts->pluck('id')->toArray());
+ }
+
+ $result = $query->first(['transaction_journals.*']);
+
+ if (!is_null($result)) {
+ return $result->date;
+ }
+
+ return new Carbon('1900-01-01');
+ }
+
+ /**
+ * @param Category $category
+ * @param Collection $accounts
+ *
+ * @return Carbon
+ */
+ private function getLastTransactionDate(Category $category, Collection $accounts): Carbon
+ {
+ // check transactions:
+ $query = $category->transactions()
+ ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
+ ->orderBy('transaction_journals.date', 'DESC');
+ if ($accounts->count() > 0) {
+ // filter journals:
+ $query->whereIn('transactions.account_id', $accounts->pluck('id')->toArray());
+ }
+
+ $lastTransaction = $query->first(['transaction_journals.*']);
+ if (!is_null($lastTransaction)) {
+ return new Carbon($lastTransaction->date);
+ }
+
+ return new Carbon('1900-01-01');
+ }
+
}
diff --git a/app/Repositories/ImportJob/ImportJobRepositoryInterface.php b/app/Repositories/ImportJob/ImportJobRepositoryInterface.php
index 421a7849b2..bfdc0493b9 100644
--- a/app/Repositories/ImportJob/ImportJobRepositoryInterface.php
+++ b/app/Repositories/ImportJob/ImportJobRepositoryInterface.php
@@ -42,17 +42,17 @@ interface ImportJobRepositoryInterface
* @param ImportJob $job
* @param UploadedFile $file
*
- * @return mixed
+ * @return bool
*/
- public function processFile(ImportJob $job, UploadedFile $file): bool;
+ public function processConfiguration(ImportJob $job, UploadedFile $file): bool;
/**
* @param ImportJob $job
* @param UploadedFile $file
*
- * @return bool
+ * @return mixed
*/
- public function processConfiguration(ImportJob $job, UploadedFile $file): bool;
+ public function processFile(ImportJob $job, UploadedFile $file): bool;
/**
* @param ImportJob $job
diff --git a/app/Support/Import/Configuration/Csv/Initial.php b/app/Support/Import/Configuration/Csv/Initial.php
index 30eabec6dc..7884a83f6b 100644
--- a/app/Support/Import/Configuration/Csv/Initial.php
+++ b/app/Support/Import/Configuration/Csv/Initial.php
@@ -26,6 +26,7 @@ use Log;
class Initial implements ConfigurationInterface
{
private $job;
+
/**
* @return array
*/
@@ -106,6 +107,21 @@ class Initial implements ConfigurationInterface
Log::error('Could not find anything for csv_import_account.', ['id' => $importId]);
}
+ $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
+ {
// loop specifics.
if (isset($data['specifics']) && is_array($data['specifics'])) {
foreach ($data['specifics'] as $name => $enabled) {
@@ -116,9 +132,7 @@ class Initial implements ConfigurationInterface
}
}
}
- $this->job->configuration = $config;
- $this->job->save();
- return true;
+ return $config;
}
}
diff --git a/app/Support/Import/Configuration/Csv/Map.php b/app/Support/Import/Configuration/Csv/Map.php
index 813cecd071..eb51c04e4d 100644
--- a/app/Support/Import/Configuration/Csv/Map.php
+++ b/app/Support/Import/Configuration/Csv/Map.php
@@ -265,4 +265,4 @@ class Map implements ConfigurationInterface
return $column;
}
-}
+}
\ No newline at end of file