Code cleanup.

This commit is contained in:
James Cole
2016-05-01 09:42:08 +02:00
parent ac8ff4e565
commit c66df3cb2c
6 changed files with 87 additions and 179 deletions

View File

@@ -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;
}
}

View File

@@ -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;
}