From 1591b61b7784d58ee9a81c2d61388b14d9e7aaaa Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 26 Apr 2016 22:30:53 +0200 Subject: [PATCH] Expand new charts. --- app/Http/Controllers/CategoryController.php | 4 +- .../Controllers/Chart/CategoryController.php | 12 ++--- .../Category/SingleCategoryRepository.php | 47 ++++++++++++------- .../SingleCategoryRepositoryInterface.php | 18 +++---- 4 files changed, 49 insertions(+), 32 deletions(-) diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php index 6b1456a5fa..2189b5a22f 100644 --- a/app/Http/Controllers/CategoryController.php +++ b/app/Http/Controllers/CategoryController.php @@ -181,8 +181,8 @@ class CategoryController extends Controller // get all spent and earned data: // get amount earned in period, grouped by day. - $spentArray = $repository->spentPerDay($category, $start, $end); - $earnedArray = $repository->earnedPerDay($category, $start, $end); + $spentArray = $repository->spentPerDay($category, $start, $end, new Collection); + $earnedArray = $repository->earnedPerDay($category, $start, $end, new Collection); if ($cache->has()) { $entries = $cache->get(); diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index a7e9c9ae59..57a524d94b 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -68,8 +68,8 @@ class CategoryController extends Controller if ($cache->has()) { return Response::json($cache->get()); } - $spentArray = $repository->spentPerDay($category, $start, $end); - $earnedArray = $repository->earnedPerDay($category, $start, $end); + $spentArray = $repository->spentPerDay($category, $start, $end, new Collection); + $earnedArray = $repository->earnedPerDay($category, $start, $end, new Collection); while ($start <= $end) { $currentEnd = Navigation::endOfPeriod($start, $range); @@ -417,8 +417,8 @@ class CategoryController extends Controller // get amount earned in period, grouped by day. // get amount spent in period, grouped by day. - $spentArray = $repository->spentPerDay($category, $start, $end); - $earnedArray = $repository->earnedPerDay($category, $start, $end); + $spentArray = $repository->spentPerDay($category, $start, $end, new Collection); + $earnedArray = $repository->earnedPerDay($category, $start, $end, new Collection); while ($start <= $end) { $str = $start->format('Y-m-d'); @@ -470,8 +470,8 @@ class CategoryController extends Controller $currentStart = clone $current; $currentEnd = Navigation::endOfPeriod($currentStart, $viewRange); - $spent = strval(array_sum($repository->spentPerDay($category, $currentStart, $currentEnd))); - $earned = strval(array_sum($repository->earnedPerDay($category, $currentStart, $currentEnd))); + $spent = strval(array_sum($repository->spentPerDay($category, $currentStart, $currentEnd, new Collection))); + $earned = strval(array_sum($repository->earnedPerDay($category, $currentStart, $currentEnd, new Collection))); $entry = [ $category->name, diff --git a/app/Repositories/Category/SingleCategoryRepository.php b/app/Repositories/Category/SingleCategoryRepository.php index 5fa7ffe2c2..8592cbb361 100644 --- a/app/Repositories/Category/SingleCategoryRepository.php +++ b/app/Repositories/Category/SingleCategoryRepository.php @@ -76,25 +76,32 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate * Where yyyy-mm-dd is the date and is the money earned using DEPOSITS in the $category * from all the users $accounts. * - * @param Category $category - * @param Carbon $start - * @param Carbon $end + * @param Category $category + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts * * @return array */ - public function earnedPerDay(Category $category, Carbon $start, Carbon $end): array + public function earnedPerDay(Category $category, Carbon $start, Carbon $end, Collection $accounts): array { /** @var Collection $query */ $query = $category->transactionjournals() + ->expanded() ->transactionTypes([TransactionType::DEPOSIT]) - ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') - ->where('transactions.amount', '>', 0) ->before($end) ->after($start) - ->groupBy('date')->get(['transaction_journals.date as dateFormatted', DB::raw('SUM(`transactions`.`amount`) AS `sum`')]); + ->groupBy('transaction_journals.date'); + + if ($accounts->count() > 0) { + $ids = $accounts->pluck('id')->toArray(); + $query->whereIn('destination_account.id', $ids); + } + + $result = $query->get(['transaction_journals.date as dateFormatted', DB::raw('SUM(`destination`.`amount`) AS `sum`')]); $return = []; - foreach ($query->toArray() as $entry) { + foreach ($result->toArray() as $entry) { $return[$entry['dateFormatted']] = $entry['sum']; } @@ -226,25 +233,33 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate * Where yyyy-mm-dd is the date and is the money spent using DEPOSITS in the $category * from all the users accounts. * - * @param Category $category - * @param Carbon $start - * @param Carbon $end + * @param Category $category + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts * * @return array */ - public function spentPerDay(Category $category, Carbon $start, Carbon $end): array + public function spentPerDay(Category $category, Carbon $start, Carbon $end, Collection $accounts): array { /** @var Collection $query */ $query = $category->transactionjournals() + ->expanded() ->transactionTypes([TransactionType::WITHDRAWAL]) - ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') - ->where('transactions.amount', '<', 0) ->before($end) ->after($start) - ->groupBy('date')->get(['transaction_journals.date as dateFormatted', DB::raw('SUM(`transactions`.`amount`) AS `sum`')]); + ->groupBy('transaction_journals.date'); + + if ($accounts->count() > 0) { + $ids = $accounts->pluck('id')->toArray(); + $query->whereIn('source_account.id', $ids); + } + + + $result = $query->get(['transaction_journals.date as dateFormatted', DB::raw('SUM(`transactions`.`amount`) AS `sum`')]); $return = []; - foreach ($query->toArray() as $entry) { + foreach ($result->toArray() as $entry) { $return[$entry['dateFormatted']] = $entry['sum']; } diff --git a/app/Repositories/Category/SingleCategoryRepositoryInterface.php b/app/Repositories/Category/SingleCategoryRepositoryInterface.php index 011fc522a1..7e0e5c9110 100644 --- a/app/Repositories/Category/SingleCategoryRepositoryInterface.php +++ b/app/Repositories/Category/SingleCategoryRepositoryInterface.php @@ -47,13 +47,14 @@ interface SingleCategoryRepositoryInterface * Where yyyy-mm-dd is the date and is the money earned using DEPOSITS in the $category * from all the users accounts. * - * @param Category $category - * @param Carbon $start - * @param Carbon $end + * @param Category $category + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts * * @return array */ - public function earnedPerDay(Category $category, Carbon $start, Carbon $end): array; + public function earnedPerDay(Category $category, Carbon $start, Carbon $end, Collection $accounts): array; /** * Find a category @@ -118,13 +119,14 @@ interface SingleCategoryRepositoryInterface * Where yyyy-mm-dd is the date and is the money spent using WITHDRAWALS in the $category * from all the users accounts. * - * @param Category $category - * @param Carbon $start - * @param Carbon $end + * @param Category $category + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts * * @return array */ - public function spentPerDay(Category $category, Carbon $start, Carbon $end): array; + public function spentPerDay(Category $category, Carbon $start, Carbon $end, Collection $accounts): array; /**