Skip to content

Firefly III: a personal finances manager

Code cleanup.

James Colec66df3cb2c41
diff --git a/app/Http/Controllers/JsonController.php b/app/Http/Controllers/JsonController.php
index f5a7a3104d..27e31877f0 100644
--- a/app/Http/Controllers/JsonController.php
+++ b/app/Http/Controllers/JsonController.php
@@ -275,9 +275,9 @@ class JsonController extends Controller
public function transactionJournals(JournalRepositoryInterface $repository, $what)
{
$descriptions = [];
- $dbType = $repository->getTransactionType($what);
-
- $journals = $repository->getJournalsOfType($dbType);
+ $type = config('firefly.transactionTypesByWhat.' . $what);
+ $types = [$type];
+ $journals = $repository->getJournals($types, 1, 50);
foreach ($journals as $j) {
$descriptions[] = $j->description;
}
diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php
index b4b62b445d..2883837f27 100644
--- a/app/Http/Controllers/TransactionController.php
+++ b/app/Http/Controllers/TransactionController.php
@@ -216,7 +216,7 @@ class TransactionController extends Controller
$types = config('firefly.transactionTypesByWhat.' . $what);
$subTitle = trans('firefly.title_' . $what);
$page = intval(Input::get('page'));
- $journals = $repository->getJournalsOfTypes($types, $page, $pageSize);
+ $journals = $repository->getJournals($types, $page, $pageSize);
$journals->setPath('transactions/' . $what);
@@ -384,8 +384,8 @@ class TransactionController extends Controller
$order = 0;
foreach ($ids as $id) {
- $journal = $repository->getWithDate($id, $date);
- if ($journal) {
+ $journal = $repository->find($id);
+ if ($journal && $journal->date->format('Y-m-d') == $date->format('Y-m-d')) {
$journal->order = $order;
$order++;
$journal->save();
diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php
index 6d2b746d1d..f0f7c63c12 100644
--- a/app/Providers/EventServiceProvider.php
+++ b/app/Providers/EventServiceProvider.php
@@ -99,16 +99,6 @@ class EventServiceProvider extends ServiceProvider
*/
protected function registerDeleteEvents()
{
- TransactionJournal::deleted(
- function (TransactionJournal $journal) {
-
- /** @var Transaction $transaction */
- foreach ($journal->transactions()->get() as $transaction) {
- $transaction->delete();
- }
- }
- );
-
Account::deleted(
function (Account $account) {
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php
index 2f38596910..62e749e193 100644
--- a/app/Repositories/Journal/JournalRepository.php
+++ b/app/Repositories/Journal/JournalRepository.php
@@ -46,6 +46,11 @@ class JournalRepository implements JournalRepositoryInterface
*/
public function delete(TransactionJournal $journal): bool
{
+ /** @var Transaction $transaction */
+ foreach ($journal->transactions()->get() as $transaction) {
+ $transaction->delete();
+ }
+
$journal->delete();
return true;
@@ -82,6 +87,8 @@ class JournalRepository implements JournalRepositoryInterface
}
/**
+ * @deprecated
+ *
* @param TransactionJournal $journal
* @param Transaction $transaction
*
@@ -105,37 +112,6 @@ class JournalRepository implements JournalRepositoryInterface
}
- /**
- * @param array $types
- * @param int $offset
- * @param int $count
- *
- * @return Collection
- */
- public function getCollectionOfTypes(array $types, int $offset, int $count): Collection
- {
- $set = $this->user->transactionJournals()
- ->expanded()
- ->transactionTypes($types)
- ->take($count)->offset($offset)
- ->orderBy('date', 'DESC')
- ->orderBy('order', 'ASC')
- ->orderBy('id', 'DESC')
- ->get(TransactionJournal::queryFields());
-
- return $set;
- }
-
- /**
- * @param TransactionType $dbType
- *
- * @return Collection
- */
- public function getJournalsOfType(TransactionType $dbType): Collection
- {
- return $this->user->transactionjournals()->where('transaction_type_id', $dbType->id)->orderBy('id', 'DESC')->take(50)->get();
- }
-
/**
* @param array $types
* @param int $page
@@ -143,14 +119,13 @@ class JournalRepository implements JournalRepositoryInterface
*
* @return LengthAwarePaginator
*/
- public function getJournalsOfTypes(array $types, int $page, int $pageSize = 50): LengthAwarePaginator
+ public function getJournals(array $types, int $page, int $pageSize = 50): LengthAwarePaginator
{
$offset = ($page - 1) * $pageSize;
- $query = $this->user
- ->transactionJournals()
- ->expanded()
- ->transactionTypes($types);
-
+ $query = $this->user->transactionJournals()->expanded();
+ if (count($types) > 0) {
+ $query->transactionTypes($types);
+ }
$count = $query->count();
$set = $query->take($pageSize)->offset($offset)->get(TransactionJournal::queryFields());
@@ -159,54 +134,6 @@ class JournalRepository implements JournalRepositoryInterface
return $journals;
}
- /**
- * @param string $type
- *
- * @return TransactionType
- */
- public function getTransactionType(string $type): TransactionType
- {
- return TransactionType::whereType($type)->first();
- }
-
- /**
- * @param int $journalId
- * @param Carbon $date
- *
- * @return TransactionJournal
- */
- public function getWithDate(int $journalId, Carbon $date): TransactionJournal
- {
- return $this->user->transactionjournals()->where('id', $journalId)->where('date', $date->format('Y-m-d 00:00:00'))->first();
- }
-
- /**
- *
- * * Remember: a balancingAct takes at most one expense and one transfer.
- * an advancePayment takes at most one expense, infinite deposits and NO transfers.
- *
- * @param TransactionJournal $journal
- * @param array $array
- *
- * @return bool
- */
- public function saveTags(TransactionJournal $journal, array $array): bool
- {
- /** @var \FireflyIII\Repositories\Tag\TagRepositoryInterface $tagRepository */
- $tagRepository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface');
-
- foreach ($array as $name) {
- if (strlen(trim($name)) > 0) {
- $tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
- if (!is_null($tag)) {
- $tagRepository->connect($journal, $tag);
- }
- }
- }
-
- return true;
- }
-
/**
* @param array $data
*
@@ -340,44 +267,29 @@ class JournalRepository implements JournalRepositoryInterface
}
/**
+ *
+ * * Remember: a balancingAct takes at most one expense and one transfer.
+ * an advancePayment takes at most one expense, infinite deposits and NO transfers.
+ *
* @param TransactionJournal $journal
* @param array $array
*
* @return bool
*/
- public function updateTags(TransactionJournal $journal, array $array): bool
+ private function saveTags(TransactionJournal $journal, array $array): bool
{
- // create tag repository
/** @var \FireflyIII\Repositories\Tag\TagRepositoryInterface $tagRepository */
$tagRepository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface');
-
- // find or create all tags:
- $tags = [];
- $ids = [];
foreach ($array as $name) {
if (strlen(trim($name)) > 0) {
- $tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
- $tags[] = $tag;
- $ids[] = $tag->id;
+ $tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
+ if (!is_null($tag)) {
+ $tagRepository->connect($journal, $tag);
+ }
}
}
- // delete all tags connected to journal not in this array:
- if (count($ids) > 0) {
- DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->whereNotIn('tag_id', $ids)->delete();
- }
- // if count is zero, delete them all:
- if (count($ids) == 0) {
- DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->delete();
- }
-
- // connect each tag to journal (if not yet connected):
- /** @var Tag $tag */
- foreach ($tags as $tag) {
- $tagRepository->connect($journal, $tag);
- }
-
return true;
}
@@ -388,7 +300,7 @@ class JournalRepository implements JournalRepositoryInterface
* @return array
* @throws FireflyException
*/
- protected function storeAccounts(TransactionType $type, array $data): array
+ private function storeAccounts(TransactionType $type, array $data): array
{
$sourceAccount = null;
$destinationAccount = null;
@@ -429,7 +341,7 @@ class JournalRepository implements JournalRepositoryInterface
*
* @return array
*/
- protected function storeDepositAccounts(array $data): array
+ private function storeDepositAccounts(array $data): array
{
$destinationAccount = Account::where('user_id', $this->user->id)->where('id', $data['destination_account_id'])->first(['accounts.*']);
@@ -455,7 +367,7 @@ class JournalRepository implements JournalRepositoryInterface
*
* @return array
*/
- protected function storeWithdrawalAccounts(array $data): array
+ private function storeWithdrawalAccounts(array $data): array
{
$sourceAccount = Account::where('user_id', $this->user->id)->where('id', $data['source_account_id'])->first(['accounts.*']);
@@ -481,4 +393,46 @@ class JournalRepository implements JournalRepositoryInterface
}
+
+ /**
+ * @param TransactionJournal $journal
+ * @param array $array
+ *
+ * @return bool
+ */
+ private function updateTags(TransactionJournal $journal, array $array): bool
+ {
+ // create tag repository
+ /** @var \FireflyIII\Repositories\Tag\TagRepositoryInterface $tagRepository */
+ $tagRepository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface');
+
+
+ // find or create all tags:
+ $tags = [];
+ $ids = [];
+ foreach ($array as $name) {
+ if (strlen(trim($name)) > 0) {
+ $tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
+ $tags[] = $tag;
+ $ids[] = $tag->id;
+ }
+ }
+
+ // delete all tags connected to journal not in this array:
+ if (count($ids) > 0) {
+ DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->whereNotIn('tag_id', $ids)->delete();
+ }
+ // if count is zero, delete them all:
+ if (count($ids) == 0) {
+ DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->delete();
+ }
+
+ // connect each tag to journal (if not yet connected):
+ /** @var Tag $tag */
+ foreach ($tags as $tag) {
+ $tagRepository->connect($journal, $tag);
+ }
+
+ return true;
+ }
}
diff --git a/app/Repositories/Journal/JournalRepositoryInterface.php b/app/Repositories/Journal/JournalRepositoryInterface.php
index ea4526806c..83ffc138cb 100644
--- a/app/Repositories/Journal/JournalRepositoryInterface.php
+++ b/app/Repositories/Journal/JournalRepositoryInterface.php
@@ -18,6 +18,8 @@ use Illuminate\Support\Collection;
interface JournalRepositoryInterface
{
/**
+ * Deletes a journal.
+ *
* @param TransactionJournal $journal
*
* @return bool
@@ -25,6 +27,8 @@ interface JournalRepositoryInterface
public function delete(TransactionJournal $journal): bool;
/**
+ * Find a specific journal
+ *
* @param int $journalId
*
* @return TransactionJournal
@@ -32,13 +36,17 @@ interface JournalRepositoryInterface
public function find(int $journalId) : TransactionJournal;
/**
- * Get users first transaction journal
+ * Get users very first transaction journal
*
* @return TransactionJournal
*/
public function first(): TransactionJournal;
/**
+ * Returns the amount in the account before the specified transaction took place.
+ *
+ * @deprecated
+ *
* @param TransactionJournal $journal
* @param Transaction $transaction
*
@@ -46,22 +54,6 @@ interface JournalRepositoryInterface
*/
public function getAmountBefore(TransactionJournal $journal, Transaction $transaction): string;
- /**
- * @param array $types
- * @param int $offset
- * @param int $count
- *
- * @return Collection
- */
- public function getCollectionOfTypes(array $types, int $offset, int $count):Collection;
-
- /**
- * @param TransactionType $dbType
- *
- * @return Collection
- */
- public function getJournalsOfType(TransactionType $dbType): Collection;
-
/**
* @param array $types
* @param int $page
@@ -69,31 +61,7 @@ interface JournalRepositoryInterface
*
* @return LengthAwarePaginator
*/
- public function getJournalsOfTypes(array $types, int $page, int $pageSize = 50): LengthAwarePaginator;
-
- /**
- * @param string $type
- *
- * @return TransactionType
- */
- public function getTransactionType(string $type): TransactionType;
-
- /**
- * @param int $journalId
- * @param Carbon $date
- *
- * @return TransactionJournal
- */
- public function getWithDate(int $journalId, Carbon $date): TransactionJournal;
-
- /**
- *
- * @param TransactionJournal $journal
- * @param array $array
- *
- * @return bool
- */
- public function saveTags(TransactionJournal $journal, array $array): bool;
+ public function getJournals(array $types, int $page, int $pageSize = 50): LengthAwarePaginator;
/**
* @param array $data
@@ -110,11 +78,4 @@ interface JournalRepositoryInterface
*/
public function update(TransactionJournal $journal, array $data): TransactionJournal;
- /**
- * @param TransactionJournal $journal
- * @param array $array
- *
- * @return bool
- */
- public function updateTags(TransactionJournal $journal, array $array): bool;
}
diff --git a/app/Rules/TransactionMatcher.php b/app/Rules/TransactionMatcher.php
index 80be3c7366..d14911e212 100644
--- a/app/Rules/TransactionMatcher.php
+++ b/app/Rules/TransactionMatcher.php
@@ -72,8 +72,11 @@ class TransactionMatcher
// - the maximum number of transactions to search in have been searched
do {
// Fetch a batch of transactions from the database
- $offset = $page > 0 ? ($page - 1) * $pagesize : 0;
- $set = $this->repository->getCollectionOfTypes($this->transactionTypes, $offset, $pagesize);
+ //$offset = $page > 0 ? ($page - 1) * $pagesize : 0;
+ //$set = $this->repository->getCollectionOfTypes($this->transactionTypes, $offset, $pagesize);
+ $paginator = $this->repository->getJournals($this->transactionTypes, $page, $pagesize);
+ $set = $paginator->getCollection();
+
// Filter transactions that match the given triggers.
$filtered = $set->filter(