From 20177200964d0bfa524d1b4d08d06d66d33a2b3d Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 9 Oct 2016 16:22:08 +0200 Subject: [PATCH] Experimental new getJournals method. --- app/Repositories/Account/AccountTasker.php | 61 +++++++++++++++++++ .../Account/AccountTaskerInterface.php | 12 ++++ 2 files changed, 73 insertions(+) diff --git a/app/Repositories/Account/AccountTasker.php b/app/Repositories/Account/AccountTasker.php index 41f44e6ef9..4fe51d3f0d 100644 --- a/app/Repositories/Account/AccountTasker.php +++ b/app/Repositories/Account/AccountTasker.php @@ -189,6 +189,67 @@ class AccountTasker implements AccountTaskerInterface return $object; } + /** + * @param Collection $accounts + * @param array $types + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function getJournalsInPeriod(Collection $accounts, array $types, Carbon $start, Carbon $end): Collection + { + $accountIds = $accounts->pluck('id')->toArray(); + $query = Transaction + ::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') + ->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') + ->whereIn('transactions.account_id', $accountIds) + ->whereNull('transactions.deleted_at') + ->whereNull('transaction_journals.deleted_at') + ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) + ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) + ->where('transaction_journals.user_id', $this->user->id) + ->orderBy('transaction_journals.date', 'DESC') + ->orderBy('transaction_journals.order', 'ASC') + ->orderBy('transaction_journals.id', 'DESC'); + + if (count($types) > 0) { + $query->whereIn('transaction_types.type', $types); + } + + $set = $query->get( + [ + 'transaction_journals.id', + 'transaction_journals.description', + 'transaction_journals.date', + 'transaction_journals.encrypted', + '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_types.type as transaction_type_type', + 'transaction_journals.bill_id', + 'bills.name as bill_name', + 'transactions.id as transaction_id', + 'transactions.amount as transaction_amount', + 'transactions.description as transaction_description', + ] + ); + + // loop for decryption. + $set->each( + function (Transaction $transaction) { + $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) : ''; + } + ); + + return $set; + } + /** * @param Collection $accounts * @param Collection $excluded diff --git a/app/Repositories/Account/AccountTaskerInterface.php b/app/Repositories/Account/AccountTaskerInterface.php index 15a95041cd..9bee71482e 100644 --- a/app/Repositories/Account/AccountTaskerInterface.php +++ b/app/Repositories/Account/AccountTaskerInterface.php @@ -62,6 +62,18 @@ interface AccountTaskerInterface */ public function expenseReport(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): Collection; + /** + * Experimental getJournals method. + * + * @param Collection $accounts + * @param array $types + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function getJournalsInPeriod(Collection $accounts, array $types, Carbon $start, Carbon $end): Collection; + /** * @param Carbon $start * @param Carbon $end