diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index e95d4ccb38..f7d735aef6 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -65,8 +65,9 @@ class CategoryController extends Controller while ($start <= $end) { $currentEnd = Navigation::endOfPeriod($start, $range); - $spent = $repository->balanceInPeriod($category, $start, $currentEnd, true); - $entries->push([clone $start, $spent]); + $spent = $repository->spentInPeriod($category, $start, $currentEnd); + $earned = $repository->earnedInPeriod($category, $start, $currentEnd); + $entries->push([clone $start, $spent, $earned]); $start = Navigation::addPeriod($start, $range, 0); } diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php index b5494c5dcf..a1825a049c 100644 --- a/app/Repositories/Category/CategoryRepository.php +++ b/app/Repositories/Category/CategoryRepository.php @@ -8,6 +8,7 @@ use Crypt; use FireflyIII\Models\Category; use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Shared\ComponentRepository; +use FireflyIII\Support\CacheProperties; use Illuminate\Support\Collection; /** @@ -262,4 +263,64 @@ class CategoryRepository extends ComponentRepository implements CategoryReposito return $query->get(['transaction_journals.*'])->sum('correct_amount'); } + + /** + * @param Category $category + * @param \Carbon\Carbon $start + * @param \Carbon\Carbon $end + * + * @param bool $shared + * + * @return string + */ + public function spentInPeriod(Category $category, Carbon $start, Carbon $end) + { + $cache = new CacheProperties; // we must cache this. + $cache->addProperty($category->id); + $cache->addProperty($start); + $cache->addProperty($end); + $cache->addProperty('spentInPeriod'); + + if ($cache->has()) { + return $cache->get(); // @codeCoverageIgnore + } + + $sum = $category->transactionjournals()->transactionTypes(['Withdrawal'])->before($end)->after($start)->get(['transaction_journals.*'])->sum( + 'correct_amount' + ); + + $cache->store($sum); + + return $sum; + } + + /** + * @param Category $category + * @param \Carbon\Carbon $start + * @param \Carbon\Carbon $end + * + * @param bool $shared + * + * @return string + */ + public function earnedInPeriod(Category $category, Carbon $start, Carbon $end) + { + $cache = new CacheProperties; // we must cache this. + $cache->addProperty($category->id); + $cache->addProperty($start); + $cache->addProperty($end); + $cache->addProperty('spentInPeriod'); + + if ($cache->has()) { + return $cache->get(); // @codeCoverageIgnore + } + + $sum = $category->transactionjournals()->transactionTypes(['Deposit'])->before($end)->after($start)->get(['transaction_journals.*'])->sum( + 'correct_amount' + ); + + $cache->store($sum); + + return $sum; + } } diff --git a/app/Repositories/Category/CategoryRepositoryInterface.php b/app/Repositories/Category/CategoryRepositoryInterface.php index b7364e518b..2f2e3d1edc 100644 --- a/app/Repositories/Category/CategoryRepositoryInterface.php +++ b/app/Repositories/Category/CategoryRepositoryInterface.php @@ -97,6 +97,28 @@ interface CategoryRepositoryInterface */ public function balanceInPeriod(Category $category, Carbon $start, Carbon $end, $shared = false); + /** + * @param Category $category + * @param \Carbon\Carbon $start + * @param \Carbon\Carbon $end + * + * @param bool $shared + * + * @return string + */ + public function spentInPeriod(Category $category, Carbon $start, Carbon $end); + + /** + * @param Category $category + * @param \Carbon\Carbon $start + * @param \Carbon\Carbon $end + * + * @param bool $shared + * + * @return string + */ + public function earnedInPeriod(Category $category, Carbon $start, Carbon $end); + /** * * Corrected for tags.