Skip to content

Firefly III: a personal finances manager

Fixed a bug in the import routine that would mislabel the import account and botch any import process.

James Colea7d75ea94a5e
diff --git a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php
index 221cd8b877..1613b7cbbc 100644
--- a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php
+++ b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php
@@ -35,16 +35,18 @@ class EloquentAccountRepository implements AccountRepositoryInterface
{
return $this->_user->accounts()->find($id);
}
+
/**
* @param \Account $from
* @param \Account $to
- * @param int $amount
+ * @param int $amount
*
* @throws FireflyException
*
* @return \TransactionType|null
*/
- public function transactionTypeByAccounts(\Account $from, \Account $to, $amount = 0) {
+ public function transactionTypeByAccounts(\Account $from, \Account $to, $amount = 0)
+ {
// account types for both:
$toAT = $to->accountType->type;
$fromAT = $from->accountType->type;
@@ -83,20 +85,36 @@ class EloquentAccountRepository implements AccountRepositoryInterface
}
-
/**
+ * This method finds the expense account mentioned by name. This method is a sneaky little hobbits,
+ * because when you feed it "Import account" it will always return an import account of that type.
+ *
* @param $name
*
- * @return |Account|null
+ * @return null|\Account
*/
public function findExpenseAccountByName($name)
{
+ // catch Import account:
+ if ($name == 'Import account') {
+ $importType = $this->findAccountType('Import account');
+ $import = $this->firstOrCreate(
+ [
+ 'name' => 'Import account',
+ 'user_id' => $this->_user->id,
+ 'account_type_id' => $importType->id,
+ 'active' => 1
+ ]
+ );
+ return $import;
+ }
+
// find account:
$type = $this->findAccountType('Expense account');
$account = $this->_user->accounts()->where('name', $name)->where('account_type_id', $type->id)->first();
// create if not found:
- if(strlen($name) > 0) {
+ if (strlen($name) > 0) {
$set = [
'name' => $name,
'user_id' => $this->_user->id,
@@ -129,6 +147,22 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return $account;
}
+
+ /**
+ * @param $type
+ *
+ * @return \AccountType|null
+ */
+ public function findAccountType($type)
+ {
+ return \AccountType::where('type', $type)->first();
+ }
+
+ public function firstOrCreate(array $data)
+ {
+ return \Account::firstOrCreate($data);
+ }
+
/**
* @param $name
*
@@ -136,6 +170,20 @@ class EloquentAccountRepository implements AccountRepositoryInterface
*/
public function findRevenueAccountByName($name)
{
+ // catch Import account:
+ if ($name == 'Import account') {
+ $importType = $this->findAccountType('Import account');
+ $import = $this->firstOrCreate(
+ [
+ 'name' => 'Import account',
+ 'user_id' => $this->_user->id,
+ 'account_type_id' => $importType->id,
+ 'active' => 1
+ ]
+ );
+ return $import;
+ }
+
// find account:
$type = $this->findAccountType('Revenue account');
$account = $this->_user->accounts()->where('name', $name)->where('account_type_id', $type->id)->first();
@@ -164,21 +212,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return $account;
}
- /**
- * @param $type
- *
- * @return \AccountType|null
- */
- public function findAccountType($type)
- {
- return \AccountType::where('type', $type)->first();
- }
-
- public function firstOrCreate(array $data)
- {
- return \Account::firstOrCreate($data);
- }
-
/**
* Takes a transaction/account component and updates the transaction journal to match.
*
@@ -265,18 +298,37 @@ class EloquentAccountRepository implements AccountRepositoryInterface
*/
$importType = $this->findAccountType('Import account');
/** @var \Transaction $transaction */
- foreach ($journal->transactions as $transaction) {
+ $updated = false;
+ \Log::debug(
+ 'Connect "' . $account->name . '" (#' . $account->id . ') to "' . $journal->description . '" (#'
+ . $journal->id . ')'
+ );
+ foreach ($journal->transactions as $index => $transaction) {
/*
* If it's of the right type, update it!
*/
+ \Log::debug(
+ 'Transaction ' . $index . ' (#' . $transaction->id . '): [' . $transaction->account->account_type_id
+ . ' vs. ' . $importType->id . ']'
+ );
if ($transaction->account->account_type_id == $importType->id) {
$transaction->account()->associate($account);
$transaction->save();
+ $updated = true;
+ \Log::debug(
+ 'Connected expense account "' . $account->name . '" to journal "' . $journal->description . '"'
+ );
}
}
+ if ($updated === false) {
+ \Log::error(
+ 'Did not connect transactions of journal #' . $journal->id . ' to expense account ' . $account->id
+ );
+
+ }
$journal->save();
- \Log::debug('Connected expense account "' . $account->name . '" to journal "' . $journal->description . '"');
+
$importMap->jobsdone++;
$importMap->save();
@@ -536,14 +588,14 @@ class EloquentAccountRepository implements AccountRepositoryInterface
* create new transaction journal (and transactions):
*/
- $set =[
+ $set = [
'account_from_id' => $initial->id,
- 'account_to_id' => $account->id,
- 'description' => 'Initial Balance for ' . $account->name,
- 'what' => 'Opening balance',
- 'amount' => $amount,
- 'category' => '',
- 'date' => $date->format('Y-m-d')
+ 'account_to_id' => $account->id,
+ 'description' => 'Initial Balance for ' . $account->name,
+ 'what' => 'Opening balance',
+ 'amount' => $amount,
+ 'category' => '',
+ 'date' => $date->format('Y-m-d')
];
$transactions->store($set);
diff --git a/app/models/TransactionJournal.php b/app/models/TransactionJournal.php
index 1f0c35849c..9bd8ec33b7 100644
--- a/app/models/TransactionJournal.php
+++ b/app/models/TransactionJournal.php
@@ -199,6 +199,18 @@ use LaravelBook\Ardent\Builder;
* 'Budget[] $budgets
* @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Category[] $categories
+ * @property-read \Illuminate\Database\Eloquent\Collection|\
+ * 'Budget[] $budgets
+ * @property-read \Illuminate\Database\Eloquent\Collection|\
+ * 'Category[] $categories
+ * @property-read \Illuminate\Database\Eloquent\Collection|\
+ * 'Budget[] $budgets
+ * @property-read \Illuminate\Database\Eloquent\Collection|\
+ * 'Category[] $categories
+ * @property-read \Illuminate\Database\Eloquent\Collection|\
+ * 'Budget[] $budgets
+ * @property-read \Illuminate\Database\Eloquent\Collection|\
+ * 'Category[] $categories
*/
class TransactionJournal extends Ardent
{