From 6b277c5e67746c94423058adf7e2c8df14d70aa6 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 2 Mar 2016 12:47:15 +0100 Subject: [PATCH] Fix bill overview. --- app/Models/TransactionJournal.php | 8 ++++++++ .../Account/AccountRepository.php | 1 + app/Repositories/Bill/BillRepository.php | 20 +++++++++---------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php index 334a21225c..51fc7fc9e8 100644 --- a/app/Models/TransactionJournal.php +++ b/app/Models/TransactionJournal.php @@ -92,6 +92,8 @@ class TransactionJournal extends Model 'date' => 'required|date', 'encrypted' => 'required|boolean', ]; + /** @var bool */ + private $joinedTypes = false; /** * @param $value @@ -311,6 +313,7 @@ class TransactionJournal extends Model { // left join transaction type: $query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'); + $this->joinedTypes = true; // left join transaction currency: $query->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transaction_journals.transaction_currency_id'); @@ -352,6 +355,11 @@ class TransactionJournal extends Model */ public function scopeTransactionTypes(EloquentBuilder $query, array $types) { + + if (!$this->joinedTypes) { + $query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'); + $this->joinedTypes = true; + } $query->whereIn('transaction_types.type', $types); } diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index ec883e1c3b..f9db1ebf7a 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -345,6 +345,7 @@ class AccountRepository implements AccountRepositoryInterface ::orderBy('transaction_journals.date', 'ASC') ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') ->where('transactions.account_id', $account->id) + ->transactionTypes([TransactionType::OPENING_BALANCE]) ->orderBy('created_at', 'ASC') ->first(['transaction_journals.*']); diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index 28ebe99313..7455e9f744 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -280,16 +280,11 @@ class BillRepository implements BillRepositoryInterface public function getJournals(Bill $bill): Collection { $set = $bill->transactionjournals() - ->leftJoin( - 'transactions', function (JoinClause $join) { - $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id') - ->where('amount', '<', 0); - } - ) + ->expanded() ->orderBy('transaction_journals.date', 'DESC') ->orderBy('transaction_journals.order', 'ASC') ->orderBy('transaction_journals.id', 'DESC') - ->get(['transaction_journals.*', 'transactions.amount as journalAmount']); + ->get(TransactionJournal::QUERYFIELDS); return $set; } @@ -399,7 +394,7 @@ class BillRepository implements BillRepositoryInterface public function nextExpectedMatch(Bill $bill): Carbon { - $finalDate = Carbon::now(); + $finalDate = Carbon::now(); $finalDate->year = 1900; if ($bill->active == 0) { return $finalDate; @@ -448,6 +443,11 @@ class BillRepository implements BillRepositoryInterface */ public function scan(Bill $bill, TransactionJournal $journal): bool { + // grab the expanded info for this journal. + // looks weird, but is useful: + /** @var TransactionJournal $journal */ + $journal = TransactionJournal::expanded()->where('transaction_journals.id', $journal->id)->get(TransactionJournal::QUERYFIELDS)->first(); + /* * Can only support withdrawals. @@ -457,9 +457,9 @@ class BillRepository implements BillRepositoryInterface } $matches = explode(',', $bill->match); - $description = strtolower($journal->description) . ' ' . strtolower($journal->destination_account->name); + $description = strtolower($journal->description) . ' ' . strtolower($journal->destination_account_name); $wordMatch = $this->doWordMatch($matches, $description); - $amountMatch = $this->doAmountMatch($journal->amount_positive, $bill->amount_min, $bill->amount_max); + $amountMatch = $this->doAmountMatch($journal->destination_amount, $bill->amount_min, $bill->amount_max); Log::debug('Journal #' . $journal->id . ' has description "' . $description . '"');