From 94e51952f4852dcfe727c120617029d49ddb9522 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 28 Dec 2016 06:09:48 +0100 Subject: [PATCH] More fields for query. --- app/Helpers/Collector/JournalCollector.php | 20 ++++++++ .../Controllers/TransactionController.php | 9 ++-- app/Support/Twig/Transaction.php | 47 ++++++++++++++++++- 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/app/Helpers/Collector/JournalCollector.php b/app/Helpers/Collector/JournalCollector.php index c874c9949e..f4a4713546 100644 --- a/app/Helpers/Collector/JournalCollector.php +++ b/app/Helpers/Collector/JournalCollector.php @@ -649,9 +649,17 @@ class JournalCollector implements JournalCollectorInterface // join some extra tables: $this->joinedBudget = true; $this->query->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id'); + $this->query->leftJoin('budgets as transaction_journal_budgets', 'transaction_journal_budgets.id', '=', 'budget_transaction_journal.budget_id'); $this->query->leftJoin('budget_transaction', 'budget_transaction.transaction_id', '=', 'transactions.id'); + $this->query->leftJoin('budgets as transaction_budgets', 'transaction_budgets.id', '=', 'budget_transaction.budget_id'); + $this->fields[] = 'budget_transaction_journal.budget_id as transaction_journal_budget_id'; + $this->fields[] = 'transaction_journal_budgets.encrypted as transaction_journal_budget_encrypted'; + $this->fields[] = 'transaction_journal_budgets.name as transaction_journal_budget_name'; + $this->fields[] = 'budget_transaction.budget_id as transaction_budget_id'; + $this->fields[] = 'transaction_budgets.encrypted as transaction_budget_encrypted'; + $this->fields[] = 'transaction_budgets.name as transaction_budget_name'; } } @@ -664,12 +672,24 @@ class JournalCollector implements JournalCollectorInterface // join some extra tables: $this->joinedCategory = true; $this->query->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id'); + $this->query->leftJoin('categories as transaction_journal_categories', 'transaction_journal_categories.id', '=', 'category_transaction_journal.category_id'); + $this->query->leftJoin('category_transaction', 'category_transaction.transaction_id', '=', 'transactions.id'); + $this->query->leftJoin('categories as transaction_categories', 'transaction_categories.id', '=', 'category_transaction.category_id'); + $this->fields[] = 'category_transaction_journal.category_id as transaction_journal_category_id'; + $this->fields[] = 'transaction_journal_categories.encrypted as transaction_journal_category_encrypted'; + $this->fields[] = 'transaction_journal_categories.name as transaction_journal_category_name'; + $this->fields[] = 'category_transaction.category_id as transaction_category_id'; + $this->fields[] = 'transaction_categories.encrypted as transaction_category_encrypted'; + $this->fields[] = 'transaction_categories.name as transaction_category_name'; } } + /** + * + */ private function joinOpposingTables() { if (!$this->joinedOpposing) { diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index 3a1c4415e7..2ec3e8ce5c 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -71,10 +71,10 @@ class TransactionController extends Controller $start = session('start', Navigation::startOfPeriod(new Carbon, $range)); $end = session('end', Navigation::endOfPeriod(new Carbon, $range)); - + /** @var JournalCollectorInterface $collector */ $collector = app(JournalCollectorInterface::class, [auth()->user()]); $collector->setTypes($types)->setLimit($pageSize)->setPage($page)->setAllAssetAccounts(); - $collector->setRange($start, $end); + $collector->setRange($start, $end)->withBudgetInformation()->withCategoryInformation(); // do not filter transfers if $what = transfer. if (!in_array($what, ['transfer', 'transfers'])) { @@ -123,7 +123,7 @@ class TransactionController extends Controller $page = intval($request->get('page')) === 0 ? 1 : intval($request->get('page')); $collector = app(JournalCollectorInterface::class, [auth()->user()]); - $collector->setTypes($types)->setLimit($pageSize)->setPage($page)->setAllAssetAccounts(); + $collector->setTypes($types)->setLimit($pageSize)->setPage($page)->setAllAssetAccounts()->withBudgetInformation()->withCategoryInformation(); // do not filter transfers if $what = transfer. if (!in_array($what, ['transfer', 'transfers'])) { @@ -160,9 +160,10 @@ class TransactionController extends Controller Log::debug(sprintf('Transaction index by date will show between %s and %s', $start->format('Y-m-d'), $end->format('Y-m-d'))); + /** @var JournalCollectorInterface $collector */ $collector = app(JournalCollectorInterface::class, [auth()->user()]); $collector->setTypes($types)->setLimit($pageSize)->setPage($page)->setAllAssetAccounts(); - $collector->setRange($start, $end); + $collector->setRange($start, $end)->withBudgetInformation()->withCategoryInformation(); // do not filter transfers if $what = transfer. if (!in_array($what, ['transfer', 'transfers'])) { diff --git a/app/Support/Twig/Transaction.php b/app/Support/Twig/Transaction.php index 0e9d851c40..b00889bbc6 100644 --- a/app/Support/Twig/Transaction.php +++ b/app/Support/Twig/Transaction.php @@ -327,6 +327,21 @@ class Transaction extends Twig_Extension ); } + /** + * @param int $isEncrypted + * @param string $value + * + * @return string + */ + private function encrypted(int $isEncrypted, string $value): string + { + if ($isEncrypted === 1) { + return Crypt::decrypt($value); + } + + return $value; + } + /** * @param TransactionModel $transaction * @@ -334,6 +349,20 @@ class Transaction extends Twig_Extension */ private function getTransactionBudgets(TransactionModel $transaction): string { + // journal has a budget: + if (isset($transaction->transaction_journal_budget_id)) { + $name = $this->encrypted(intval($transaction->transaction_journal_budget_encrypted), $transaction->transaction_journal_budget_name); + + return sprintf('%s (a)', route('budgets.show', [$transaction->transaction_journal_budget_id]), $name, $name); + } + + // transaction has a budget + if (isset($transaction->transaction_budget_id)) { + $name = $this->encrypted(intval($transaction->transaction_budget_encrypted), $transaction->transaction_budget_name); + + return sprintf('%s (b)', route('budgets.show', [$transaction->transaction_budget_id]), $name, $name); + } + // see if the transaction has a budget: $budgets = $transaction->budgets()->get(); if ($budgets->count() === 0) { @@ -342,7 +371,7 @@ class Transaction extends Twig_Extension if ($budgets->count() > 0) { $str = []; foreach ($budgets as $budget) { - $str[] = sprintf('%s', route('budgets.show', [$budget->id]), $budget->name, $budget->name); + $str[] = sprintf('%s (c)', route('budgets.show', [$budget->id]), $budget->name, $budget->name); } return join(', ', $str); @@ -359,6 +388,20 @@ class Transaction extends Twig_Extension */ private function getTransactionCategories(TransactionModel $transaction): string { + // journal has a category: + if (isset($transaction->transaction_journal_category_id)) { + $name = $this->encrypted(intval($transaction->transaction_journal_category_encrypted), $transaction->transaction_journal_category_name); + + return sprintf('%s (a)', route('categories.show', [$transaction->transaction_journal_category_id]), $name, $name); + } + + // transaction has a category: + if (isset($transaction->transaction_category_id)) { + $name = $this->encrypted(intval($transaction->transaction_category_encrypted), $transaction->transaction_category_name); + + return sprintf('%s (b)', route('categories.show', [$transaction->transaction_category_id]), $name, $name); + } + // see if the transaction has a category: $categories = $transaction->categories()->get(); if ($categories->count() === 0) { @@ -367,7 +410,7 @@ class Transaction extends Twig_Extension if ($categories->count() > 0) { $str = []; foreach ($categories as $category) { - $str[] = sprintf('%s', route('categories.show', [$category->id]), $category->name, $category->name); + $str[] = sprintf('%s (c)', route('categories.show', [$category->id]), $category->name, $category->name); } return join(', ', $str);