Skip to content

Firefly III: a personal finances manager

Some new code.

James Colebbab370b1e87
diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php
index dc777ba086..3621bd0440 100644
--- a/app/Http/Controllers/TransactionController.php
+++ b/app/Http/Controllers/TransactionController.php
@@ -157,7 +157,7 @@ class TransactionController extends Controller
}
$preFilled['amount'] = $journal->amount;
- $preFilled['account_id'] = $repository->getAssetAccount($journal);
+ $preFilled['account_id'] = $journal->assetAccount->id;
$preFilled['expense_account'] = $transactions[0]->account->name;
$preFilled['revenue_account'] = $transactions[1]->account->name;
$preFilled['account_from_id'] = $transactions[1]->account->id;
diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php
index 0a0170f340..a8a3a12d4a 100644
--- a/app/Models/TransactionJournal.php
+++ b/app/Models/TransactionJournal.php
@@ -68,6 +68,34 @@ class TransactionJournal extends Model
}
}
+ public function getAssetAccountAttribute()
+ {
+ $positive = true; // the asset account is in the transaction with the positive amount.
+ if ($this->transactionType->type === 'Withdrawal') {
+ $positive = false;
+ }
+ /** @var Transaction $transaction */
+ foreach ($this->transactions()->get() as $transaction) {
+ if (floatval($transaction->amount) > 0 && $positive === true) {
+ return $transaction->account_id;
+ }
+ if (floatval($transaction->amount) < 0 && $positive === false) {
+ return $transaction->account_id;
+ }
+
+ }
+
+ return $this->transactions()->first()->account_id;
+ }
+
+ /**
+ * @return \Illuminate\Database\Eloquent\Relations\HasMany
+ */
+ public function transactions()
+ {
+ return $this->hasMany('FireflyIII\Models\Transaction');
+ }
+
/**
* @return array
*/
@@ -201,6 +229,14 @@ class TransactionJournal extends Model
$this->attributes['encrypted'] = true;
}
+ /**
+ * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
+ */
+ public function tags()
+ {
+ return $this->belongsToMany('FireflyIII\Models\Tag');
+ }
+
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
@@ -225,14 +261,6 @@ class TransactionJournal extends Model
return $this->belongsToMany('FireflyIII\Models\TransactionGroup');
}
- /**
- * @return \Illuminate\Database\Eloquent\Relations\HasMany
- */
- public function transactions()
- {
- return $this->hasMany('FireflyIII\Models\Transaction');
- }
-
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
@@ -241,12 +269,4 @@ class TransactionJournal extends Model
return $this->belongsTo('FireflyIII\User');
}
- /**
- * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
- */
- public function tags()
- {
- return $this->belongsToMany('FireflyIII\Models\Tag');
- }
-
}
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php
index 5f00090c97..d63dea1060 100644
--- a/app/Repositories/Journal/JournalRepository.php
+++ b/app/Repositories/Journal/JournalRepository.php
@@ -34,36 +34,6 @@ class JournalRepository implements JournalRepositoryInterface
return Auth::user()->transactionjournals()->orderBy('date', 'ASC')->first(['transaction_journals.*']);
}
- /**
- *
- * Get the account_id, which is the asset account that paid for the transaction.
- *
- * @param TransactionJournal $journal
- *
- * @return int
- */
- public function getAssetAccount(TransactionJournal $journal)
- {
- $positive = true; // the asset account is in the transaction with the positive amount.
- switch ($journal->transactionType->type) {
- case 'Withdrawal':
- $positive = false;
- break;
- }
- /** @var Transaction $transaction */
- foreach ($journal->transactions()->get() as $transaction) {
- if (floatval($transaction->amount) > 0 && $positive === true) {
- return $transaction->account_id;
- }
- if (floatval($transaction->amount) < 0 && $positive === false) {
- return $transaction->account_id;
- }
-
- }
-
- return $journal->transactions()->first()->account_id;
- }
-
/**
* @param TransactionType $dbType
*
@@ -96,61 +66,12 @@ class JournalRepository implements JournalRepositoryInterface
*/
public function saveTags(TransactionJournal $journal, array $array)
{
+ /** @var \FireflyIII\Repositories\Tag\TagRepositoryInterface $tagRepository */
+ $tagRepository = App::make('FireflyIII\Repositories\Tag\TagRepositoryInterface');
+
foreach ($array as $name) {
$tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
-
- if ($tag->tagMode == 'nothing') {
- // save it, no problem:
- $journal->tags()->save($tag);
- }
- if ($tag->tagMode == 'balancingAct') {
- // if Withdrawal, journals that are of type Withdrawal, max 1.
- $withdrawal = $this->getTransactionType('Withdrawal');
- $transfer = $this->getTransactionType('Transfer');
-
- $withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();
- $transfers = $tag->transactionjournals()->where('transaction_type_id', $transfer->id)->count();
-
- // only if this is the only withdrawal.
- if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
- $journal->tags()->save($tag);
- }
- // and only if this is the only transfer
- if ($journal->transaction_type_id == $transfer->id && $transfers < 1) {
- $journal->tags()->save($tag);
- }
-
- // ignore expense
- }
-
- if ($tag->tagMode == 'advancePayment') {
- $withdrawal = $this->getTransactionType('Withdrawal');
- $deposit = $this->getTransactionType('Deposit');
- $withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();
-
- // only if this is the only withdrawal
- if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
- $journal->tags()->save($tag);
- }
-
- // only if this is a deposit.
- if ($journal->transaction_type_id == $deposit->id) {
-
- // if this is a deposit, account must match the current only journal
- // (if already present):
- $currentWithdrawal = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->first();
- if ($currentWithdrawal && $this->getAssetAccount($currentWithdrawal) == $this->getAssetAccount($journal)) {
- $journal->tags()->save($tag);
- } else {
- if (is_null($currentWithdrawal)) {
- $journal->tags()->save($tag);
- }
- }
-
-
- }
- }
-
+ $tagRepository->connect($journal, $tag);
}
}
@@ -287,6 +208,11 @@ class JournalRepository implements JournalRepositoryInterface
*/
public function updateTags(TransactionJournal $journal, array $array)
{
+ // create tag repository
+ /** @var \FireflyIII\Repositories\Tag\TagRepositoryInterface $tagRepository */
+ $tagRepository = App::make('FireflyIII\Repositories\Tag\TagRepositoryInterface');
+
+
// find or create all tags:
$tags = [];
$ids = [];
@@ -304,50 +230,7 @@ class JournalRepository implements JournalRepositoryInterface
// connect each tag to journal (if not yet connected):
/** @var Tag $tag */
foreach ($tags as $tag) {
- if (!$journal->tags()->find($tag->id)) {
- if ($tag->tagMode == 'nothing') {
- // save it, no problem:
- $journal->tags()->save($tag);
- }
- if ($tag->tagMode == 'balancingAct') {
- // if Withdrawal, journals that are of type Withdrawal, max 1.
- $withdrawal = $this->getTransactionType('Withdrawal');
- $transfer = $this->getTransactionType('Transfer');
-
- $withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();
- $transfers = $tag->transactionjournals()->where('transaction_type_id', $transfer->id)->count();
-
- // only if this is the only withdrawal.
- if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
- $journal->tags()->save($tag);
- }
- // and only if this is the only transfer
- if ($journal->transaction_type_id == $transfer->id && $transfers < 1) {
- $journal->tags()->save($tag);
- Log::debug('Saved tag! [' . $journal->transaction_type_id . ':' . $transfer->id . ':' . $transfers . ']');
- } else {
- Log::debug('Did not save tag. [' . $journal->transaction_type_id . ':' . $transfer->id . ':' . $transfers . ']');
- }
-
- // ignore expense
- }
-
- if ($tag->tagMode == 'advancePayment') {
- $withdrawal = $this->getTransactionType('Withdrawal');
- $deposit = $this->getTransactionType('Deposit');
- $withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();
-
- // only if this is the only withdrawal
- if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
- $journal->tags()->save($tag);
- }
-
- // only if this is a deposit.
- if ($journal->transaction_type_id == $deposit->id) {
- $journal->tags()->save($tag);
- }
- }
- }
+ $tagRepository->connect($journal, $tag);
}
}
diff --git a/app/Repositories/Journal/JournalRepositoryInterface.php b/app/Repositories/Journal/JournalRepositoryInterface.php
index 5175c3edaa..54de77e272 100644
--- a/app/Repositories/Journal/JournalRepositoryInterface.php
+++ b/app/Repositories/Journal/JournalRepositoryInterface.php
@@ -32,35 +32,34 @@ interface JournalRepositoryInterface
public function getAssetAccount(TransactionJournal $journal);
/**
- * @param TransactionJournal $journal
- * @param array $array
+ * @param TransactionType $dbType
*
- * @return void
+ * @return Collection
*/
- public function updateTags(TransactionJournal $journal, array $array);
+ public function getJournalsOfType(TransactionType $dbType);
/**
- * @param TransactionJournal $journal
- * @param array $array
+ * @param $type
*
- * @return void
+ * @return TransactionType
*/
- public function saveTags(TransactionJournal $journal, array $array);
-
+ public function getTransactionType($type);
/**
- * @param TransactionType $dbType
+ * @param TransactionJournal $journal
+ * @param array $array
*
- * @return Collection
- */
- public function getJournalsOfType(TransactionType $dbType);
+ * @return void
/**
- * @param $type
*
- * @return TransactionType
+ * @param TransactionJournal $journal
+ * @param array $array
+ *
+ * @return void
*/
- public function getTransactionType($type);
+ public function saveTags(TransactionJournal $journal, array $array);
+
/**
* @param array $data
*
@@ -75,4 +74,12 @@ interface JournalRepositoryInterface
* @return mixed
*/
public function update(TransactionJournal $journal, array $data);
+
+ /**
+ * @param TransactionJournal $journal
+ * @param array $array
+ *
+ * @return mixed
+ */
+ public function updateTags(TransactionJournal $journal, array $array);
}
diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php
index 9ca40a968b..935a6b977d 100644
--- a/app/Repositories/Tag/TagRepository.php
+++ b/app/Repositories/Tag/TagRepository.php
@@ -5,6 +5,8 @@ namespace FireflyIII\Repositories\Tag;
use Auth;
use FireflyIII\Models\Tag;
+use FireflyIII\Models\TransactionJournal;
+use FireflyIII\Models\TransactionType;
use Illuminate\Support\Collection;
/**
@@ -15,6 +17,97 @@ use Illuminate\Support\Collection;
class TagRepository implements TagRepositoryInterface
{
+ /**
+ * @param TransactionJournal $journal
+ * @param Tag $tag
+ *
+ * @return boolean
+ */
+ public function connect(TransactionJournal $journal, Tag $tag)
+ {
+
+ /*
+ * Already connected:
+ */
+ if ($journal->tags()->find($tag->id)) {
+ return false;
+ }
+
+ if ($tag->tagMode == 'nothing') {
+ // save it, no problem:
+ $journal->tags()->save($tag);
+
+ return true;
+ }
+
+ /*
+ * get some withdrawal types:
+ */
+ /** @var TransactionType $withdrawal */
+ $withdrawal = TransactionType::whereType('Withdrawal')->first();
+ /** @var TransactionType $deposit */
+ $deposit = TransactionType::whereType('Deposit')->first();
+ /** @var TransactionType $transfer */
+ $transfer = TransactionType::whereType('Transfer')->first();
+
+ $withdrawals = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->count();
+ $transfers = $tag->transactionjournals()->where('transaction_type_id', $transfer->id)->count();
+
+ if ($tag->tagMode == 'balancingAct') {
+
+ // only if this is the only withdrawal.
+ if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
+ $journal->tags()->save($tag);
+
+ return true;
+ }
+ // and only if this is the only transfer
+ if ($journal->transaction_type_id == $transfer->id && $transfers < 1) {
+ $journal->tags()->save($tag);
+
+ return true;
+ }
+
+ // ignore expense
+ return false;
+ }
+
+ if ($tag->tagMode == 'advancePayment') {
+
+
+ // only if this is the only withdrawal
+ if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
+ $journal->tags()->save($tag);
+
+ return true;
+ }
+
+ // only if this is a deposit.
+ if ($journal->transaction_type_id == $deposit->id) {
+
+ // if this is a deposit, account must match the current only journal
+ // (if already present):
+ $currentWithdrawal = $tag->transactionjournals()->where('transaction_type_id', $withdrawal->id)->first();
+
+ if ($currentWithdrawal && $currentWithdrawal->assetAccount->id == $journal->assetAccount->id) {
+ $journal->tags()->save($tag);
+
+ return true;
+ } else {
+ if (is_null($currentWithdrawal)) {
+ $journal->tags()->save($tag);
+
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ return false;
+ }
+
/**
* @param Tag $tag
*
diff --git a/app/Repositories/Tag/TagRepositoryInterface.php b/app/Repositories/Tag/TagRepositoryInterface.php
index 937bc51b4a..4e0d31c102 100644
--- a/app/Repositories/Tag/TagRepositoryInterface.php
+++ b/app/Repositories/Tag/TagRepositoryInterface.php
@@ -2,6 +2,7 @@
namespace FireflyIII\Repositories\Tag;
use FireflyIII\Models\Tag;
+use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection;
@@ -38,4 +39,12 @@ interface TagRepositoryInterface {
* @return boolean
*/
public function destroy(Tag $tag);
+
+ /**
+ * @param TransactionJournal $journal
+ * @param Tag $tag
+ *
+ * @return boolean
+ */
+ public function connect(TransactionJournal $journal, Tag $tag);
}
\ No newline at end of file