mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-23 14:26:58 +00:00
Code cleanup.
This commit is contained in:
@@ -275,9 +275,9 @@ class JsonController extends Controller
|
|||||||
public function transactionJournals(JournalRepositoryInterface $repository, $what)
|
public function transactionJournals(JournalRepositoryInterface $repository, $what)
|
||||||
{
|
{
|
||||||
$descriptions = [];
|
$descriptions = [];
|
||||||
$dbType = $repository->getTransactionType($what);
|
$type = config('firefly.transactionTypesByWhat.' . $what);
|
||||||
|
$types = [$type];
|
||||||
$journals = $repository->getJournalsOfType($dbType);
|
$journals = $repository->getJournals($types, 1, 50);
|
||||||
foreach ($journals as $j) {
|
foreach ($journals as $j) {
|
||||||
$descriptions[] = $j->description;
|
$descriptions[] = $j->description;
|
||||||
}
|
}
|
||||||
|
@@ -216,7 +216,7 @@ class TransactionController extends Controller
|
|||||||
$types = config('firefly.transactionTypesByWhat.' . $what);
|
$types = config('firefly.transactionTypesByWhat.' . $what);
|
||||||
$subTitle = trans('firefly.title_' . $what);
|
$subTitle = trans('firefly.title_' . $what);
|
||||||
$page = intval(Input::get('page'));
|
$page = intval(Input::get('page'));
|
||||||
$journals = $repository->getJournalsOfTypes($types, $page, $pageSize);
|
$journals = $repository->getJournals($types, $page, $pageSize);
|
||||||
|
|
||||||
$journals->setPath('transactions/' . $what);
|
$journals->setPath('transactions/' . $what);
|
||||||
|
|
||||||
@@ -384,8 +384,8 @@ class TransactionController extends Controller
|
|||||||
$order = 0;
|
$order = 0;
|
||||||
foreach ($ids as $id) {
|
foreach ($ids as $id) {
|
||||||
|
|
||||||
$journal = $repository->getWithDate($id, $date);
|
$journal = $repository->find($id);
|
||||||
if ($journal) {
|
if ($journal && $journal->date->format('Y-m-d') == $date->format('Y-m-d')) {
|
||||||
$journal->order = $order;
|
$journal->order = $order;
|
||||||
$order++;
|
$order++;
|
||||||
$journal->save();
|
$journal->save();
|
||||||
|
@@ -99,16 +99,6 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
*/
|
*/
|
||||||
protected function registerDeleteEvents()
|
protected function registerDeleteEvents()
|
||||||
{
|
{
|
||||||
TransactionJournal::deleted(
|
|
||||||
function (TransactionJournal $journal) {
|
|
||||||
|
|
||||||
/** @var Transaction $transaction */
|
|
||||||
foreach ($journal->transactions()->get() as $transaction) {
|
|
||||||
$transaction->delete();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
Account::deleted(
|
Account::deleted(
|
||||||
function (Account $account) {
|
function (Account $account) {
|
||||||
|
|
||||||
|
@@ -46,6 +46,11 @@ class JournalRepository implements JournalRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function delete(TransactionJournal $journal): bool
|
public function delete(TransactionJournal $journal): bool
|
||||||
{
|
{
|
||||||
|
/** @var Transaction $transaction */
|
||||||
|
foreach ($journal->transactions()->get() as $transaction) {
|
||||||
|
$transaction->delete();
|
||||||
|
}
|
||||||
|
|
||||||
$journal->delete();
|
$journal->delete();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -82,6 +87,8 @@ class JournalRepository implements JournalRepositoryInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*
|
||||||
* @param TransactionJournal $journal
|
* @param TransactionJournal $journal
|
||||||
* @param Transaction $transaction
|
* @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 array $types
|
||||||
* @param int $page
|
* @param int $page
|
||||||
@@ -143,14 +119,13 @@ class JournalRepository implements JournalRepositoryInterface
|
|||||||
*
|
*
|
||||||
* @return LengthAwarePaginator
|
* @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;
|
$offset = ($page - 1) * $pageSize;
|
||||||
$query = $this->user
|
$query = $this->user->transactionJournals()->expanded();
|
||||||
->transactionJournals()
|
if (count($types) > 0) {
|
||||||
->expanded()
|
$query->transactionTypes($types);
|
||||||
->transactionTypes($types);
|
}
|
||||||
|
|
||||||
|
|
||||||
$count = $query->count();
|
$count = $query->count();
|
||||||
$set = $query->take($pageSize)->offset($offset)->get(TransactionJournal::queryFields());
|
$set = $query->take($pageSize)->offset($offset)->get(TransactionJournal::queryFields());
|
||||||
@@ -159,54 +134,6 @@ class JournalRepository implements JournalRepositoryInterface
|
|||||||
return $journals;
|
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
|
* @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 TransactionJournal $journal
|
||||||
* @param array $array
|
* @param array $array
|
||||||
*
|
*
|
||||||
* @return bool
|
* @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 */
|
/** @var \FireflyIII\Repositories\Tag\TagRepositoryInterface $tagRepository */
|
||||||
$tagRepository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface');
|
$tagRepository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface');
|
||||||
|
|
||||||
|
|
||||||
// find or create all tags:
|
|
||||||
$tags = [];
|
|
||||||
$ids = [];
|
|
||||||
foreach ($array as $name) {
|
foreach ($array as $name) {
|
||||||
if (strlen(trim($name)) > 0) {
|
if (strlen(trim($name)) > 0) {
|
||||||
$tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
|
$tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]);
|
||||||
$tags[] = $tag;
|
if (!is_null($tag)) {
|
||||||
$ids[] = $tag->id;
|
$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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -388,7 +300,7 @@ class JournalRepository implements JournalRepositoryInterface
|
|||||||
* @return array
|
* @return array
|
||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
protected function storeAccounts(TransactionType $type, array $data): array
|
private function storeAccounts(TransactionType $type, array $data): array
|
||||||
{
|
{
|
||||||
$sourceAccount = null;
|
$sourceAccount = null;
|
||||||
$destinationAccount = null;
|
$destinationAccount = null;
|
||||||
@@ -429,7 +341,7 @@ class JournalRepository implements JournalRepositoryInterface
|
|||||||
*
|
*
|
||||||
* @return array
|
* @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.*']);
|
$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
|
* @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.*']);
|
$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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,6 +18,8 @@ use Illuminate\Support\Collection;
|
|||||||
interface JournalRepositoryInterface
|
interface JournalRepositoryInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
* Deletes a journal.
|
||||||
|
*
|
||||||
* @param TransactionJournal $journal
|
* @param TransactionJournal $journal
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
@@ -25,6 +27,8 @@ interface JournalRepositoryInterface
|
|||||||
public function delete(TransactionJournal $journal): bool;
|
public function delete(TransactionJournal $journal): bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Find a specific journal
|
||||||
|
*
|
||||||
* @param int $journalId
|
* @param int $journalId
|
||||||
*
|
*
|
||||||
* @return TransactionJournal
|
* @return TransactionJournal
|
||||||
@@ -32,13 +36,17 @@ interface JournalRepositoryInterface
|
|||||||
public function find(int $journalId) : TransactionJournal;
|
public function find(int $journalId) : TransactionJournal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get users first transaction journal
|
* Get users very first transaction journal
|
||||||
*
|
*
|
||||||
* @return TransactionJournal
|
* @return TransactionJournal
|
||||||
*/
|
*/
|
||||||
public function first(): TransactionJournal;
|
public function first(): TransactionJournal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Returns the amount in the account before the specified transaction took place.
|
||||||
|
*
|
||||||
|
* @deprecated
|
||||||
|
*
|
||||||
* @param TransactionJournal $journal
|
* @param TransactionJournal $journal
|
||||||
* @param Transaction $transaction
|
* @param Transaction $transaction
|
||||||
*
|
*
|
||||||
@@ -46,22 +54,6 @@ interface JournalRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getAmountBefore(TransactionJournal $journal, Transaction $transaction): string;
|
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 array $types
|
||||||
* @param int $page
|
* @param int $page
|
||||||
@@ -69,31 +61,7 @@ interface JournalRepositoryInterface
|
|||||||
*
|
*
|
||||||
* @return LengthAwarePaginator
|
* @return LengthAwarePaginator
|
||||||
*/
|
*/
|
||||||
public function getJournalsOfTypes(array $types, int $page, int $pageSize = 50): LengthAwarePaginator;
|
public function getJournals(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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
@@ -110,11 +78,4 @@ interface JournalRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function update(TransactionJournal $journal, array $data): TransactionJournal;
|
public function update(TransactionJournal $journal, array $data): TransactionJournal;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param TransactionJournal $journal
|
|
||||||
* @param array $array
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function updateTags(TransactionJournal $journal, array $array): bool;
|
|
||||||
}
|
}
|
||||||
|
@@ -72,8 +72,11 @@ class TransactionMatcher
|
|||||||
// - the maximum number of transactions to search in have been searched
|
// - the maximum number of transactions to search in have been searched
|
||||||
do {
|
do {
|
||||||
// Fetch a batch of transactions from the database
|
// Fetch a batch of transactions from the database
|
||||||
$offset = $page > 0 ? ($page - 1) * $pagesize : 0;
|
//$offset = $page > 0 ? ($page - 1) * $pagesize : 0;
|
||||||
$set = $this->repository->getCollectionOfTypes($this->transactionTypes, $offset, $pagesize);
|
//$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.
|
// Filter transactions that match the given triggers.
|
||||||
$filtered = $set->filter(
|
$filtered = $set->filter(
|
||||||
|
Reference in New Issue
Block a user