mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-06 22:21:42 +00:00
Refactored accountRepository::getJournals > accountTasker > getJournals
This commit is contained in:
@@ -111,63 +111,6 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param array $types
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function journalsInPeriod(Collection $accounts, array $types, Carbon $start, Carbon $end): Collection
|
||||
{
|
||||
// first collect actual transaction journals (fairly easy)
|
||||
$query = $this->user->transactionJournals()->expanded()->sortCorrectly();
|
||||
|
||||
if ($end >= $start) {
|
||||
$query->before($end)->after($start);
|
||||
}
|
||||
|
||||
if (count($types) > 0) {
|
||||
$query->transactionTypes($types);
|
||||
}
|
||||
if ($accounts->count() > 0) {
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
$query->leftJoin(
|
||||
'transactions as source', function (JoinClause $join) {
|
||||
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0);
|
||||
}
|
||||
);
|
||||
$query->leftJoin(
|
||||
'transactions as destination', function (JoinClause $join) {
|
||||
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
|
||||
}
|
||||
);
|
||||
$query->where(
|
||||
// source.account_id in accountIds XOR destination.account_id in accountIds
|
||||
function (Builder $query) use ($accountIds) {
|
||||
$query->where(
|
||||
function (Builder $q1) use ($accountIds) {
|
||||
$q1->whereIn('source.account_id', $accountIds)
|
||||
->whereNotIn('destination.account_id', $accountIds);
|
||||
}
|
||||
)->orWhere(
|
||||
function (Builder $q2) use ($accountIds) {
|
||||
$q2->whereIn('destination.account_id', $accountIds)
|
||||
->whereNotIn('source.account_id', $accountIds);
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// that should do it:
|
||||
$fields = TransactionJournal::queryFields();
|
||||
$complete = $query->get($fields);
|
||||
|
||||
return $complete;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param Account $account
|
||||
|
||||
@@ -60,16 +60,6 @@ interface AccountRepositoryInterface
|
||||
*/
|
||||
public function getFirstTransaction(TransactionJournal $journal, Account $account): Transaction;
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param array $types
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function journalsInPeriod(Collection $accounts, array $types, Carbon $start, Carbon $end): Collection;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param Account $account
|
||||
|
||||
@@ -190,6 +190,8 @@ class AccountTasker implements AccountTaskerInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* It might be worth it to expand this query to include all account information required.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param array $types
|
||||
* @param Carbon $start
|
||||
@@ -205,6 +207,8 @@ class AccountTasker implements AccountTaskerInterface
|
||||
->leftJoin('transaction_currencies', 'transaction_currencies.id', 'transaction_journals.transaction_currency_id')
|
||||
->leftJoin('transaction_types', 'transaction_types.id', 'transaction_journals.transaction_type_id')
|
||||
->leftJoin('bills', 'bills.id', 'transaction_journals.bill_id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
||||
->leftJoin('account_types', 'accounts.account_type_id', 'account_types.id')
|
||||
->whereIn('transactions.account_id', $accountIds)
|
||||
->whereNull('transactions.deleted_at')
|
||||
->whereNull('transaction_journals.deleted_at')
|
||||
@@ -221,29 +225,35 @@ class AccountTasker implements AccountTaskerInterface
|
||||
|
||||
$set = $query->get(
|
||||
[
|
||||
'transaction_journals.id',
|
||||
'transaction_journals.id as journal_id',
|
||||
'transaction_journals.description',
|
||||
'transaction_journals.date',
|
||||
'transaction_journals.encrypted',
|
||||
'transaction_journals.transaction_currency_id',
|
||||
//'transaction_journals.transaction_currency_id',
|
||||
'transaction_currencies.code as transaction_currency_code',
|
||||
'transaction_currencies.symbol as transaction_currency_symbol',
|
||||
'transaction_journals.transaction_type_id',
|
||||
//'transaction_currencies.symbol as transaction_currency_symbol',
|
||||
'transaction_types.type as transaction_type_type',
|
||||
'transaction_journals.bill_id',
|
||||
'bills.name as bill_name',
|
||||
'transactions.id as transaction_id',
|
||||
'transactions.id as id',
|
||||
'transactions.amount as transaction_amount',
|
||||
'transactions.description as transaction_description',
|
||||
'transactions.account_id',
|
||||
'transactions.identifier',
|
||||
'transactions.transaction_journal_id',
|
||||
'accounts.name as account_name',
|
||||
'accounts.encrypted as account_encrypted',
|
||||
'account_types.type as account_type',
|
||||
|
||||
]
|
||||
);
|
||||
|
||||
// loop for decryption.
|
||||
$set->each(
|
||||
function (Transaction $transaction) {
|
||||
$transaction->date = new Carbon($transaction->date);
|
||||
$transaction->date = new Carbon($transaction->date);
|
||||
$transaction->description = intval($transaction->encrypted) === 1 ? Crypt::decrypt($transaction->description) : $transaction->description;
|
||||
$transaction->bill_name = !is_null($transaction->bill_name) ? Crypt::decrypt($transaction->bill_name) : '';
|
||||
$transaction->bill_name = !is_null($transaction->bill_name) ? Crypt::decrypt($transaction->bill_name) : '';
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user