Skip to content

Firefly III: a personal finances manager

Can now actually store transactions without an expense / revenue account.

James Cole58faa189ac8b
diff --git a/app/Models/Account.php b/app/Models/Account.php
index 2d4934b005..00bf153516 100644
--- a/app/Models/Account.php
+++ b/app/Models/Account.php
@@ -113,6 +113,30 @@ class Account extends Model
// @codeCoverageIgnoreEnd
}
+ /**
+ * @param array $fields
+ * @return Account|null
+ */
+ public static function firstOrCreateEncrypted(array $fields) {
+ // everything but the name:
+ $query = Account::orderBy('id');
+ foreach($fields as $name => $value) {
+ if($name != 'name') {
+ $query->where($name,$value);
+ }
+ }
+ $set = $query->get(['accounts.*']);
+ /** @var Account $account */
+ foreach($set as $account) {
+ if($account->name == $fields['name']) {
+ return $account;
+ }
+ }
+ // create it!
+ return Account::create($fields);
+
+ }
+
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php
index 1c726ae6dc..3c4f13eaf3 100644
--- a/app/Repositories/Journal/JournalRepository.php
+++ b/app/Repositories/Journal/JournalRepository.php
@@ -149,7 +149,7 @@ class JournalRepository implements JournalRepositoryInterface
'amount' => $data['amount'] * -1
]
);
- Transaction::create( // second transaction.
+ $transaction = Transaction::create( // second transaction.
[
'account_id' => $to->id,
'transaction_journal_id' => $journal->id,
@@ -225,6 +225,8 @@ class JournalRepository implements JournalRepositoryInterface
*/
protected function storeAccounts(TransactionType $type, array $data)
{
+ $from = null;
+ $to = null;
switch ($type->type) {
case 'Withdrawal':
@@ -237,7 +239,7 @@ class JournalRepository implements JournalRepositoryInterface
);
} else {
$toType = AccountType::where('type', 'Cash account')->first();
- $to = Account::firstOrCreate(['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => 'Cash account', 'active' => 1]);
+ $to = Account::firstOrCreateEncrypted(['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => 'Cash account', 'active' => 1]);
}
break;
@@ -246,12 +248,12 @@ class JournalRepository implements JournalRepositoryInterface
if (strlen($data['revenue_account']) > 0) {
$fromType = AccountType::where('type', 'Revenue account')->first();
- $from = Account::firstOrCreate(
+ $from = Account::firstOrCreateEncrypted(
['user_id' => $data['user'], 'account_type_id' => $fromType->id, 'name' => $data['revenue_account'], 'active' => 1]
);
} else {
$toType = AccountType::where('type', 'Cash account')->first();
- $from = Account::firstOrCreate(['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => 'Cash account', 'active' => 1]);
+ $from = Account::firstOrCreateEncrypted(['user_id' => $data['user'], 'account_type_id' => $toType->id, 'name' => 'Cash account', 'active' => 1]);
}
break;