diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php index 43349c78a1..22765419d5 100644 --- a/app/Helpers/Report/ReportHelper.php +++ b/app/Helpers/Report/ReportHelper.php @@ -61,9 +61,13 @@ class ReportHelper implements ReportHelperInterface */ /** @var \FireflyIII\Repositories\Category\CategoryRepositoryInterface $repository */ $repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + + /** @var \FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface $singleRepository */ + $singleRepository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface'); + $set = $repository->getCategories(); foreach ($set as $category) { - $spent = $repository->balanceInPeriod($category, $start, $end, $accounts); + $spent = $singleRepository->balanceInPeriod($category, $start, $end, $accounts); $category->spent = $spent; $object->addCategory($category); } diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php index 04ef878492..d878afa9aa 100644 --- a/app/Http/Controllers/CategoryController.php +++ b/app/Http/Controllers/CategoryController.php @@ -5,6 +5,7 @@ use Carbon\Carbon; use FireflyIII\Http\Requests\CategoryFormRequest; use FireflyIII\Models\Category; use FireflyIII\Repositories\Category\CategoryRepositoryInterface; +use FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface; use FireflyIII\Support\CacheProperties; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; @@ -68,12 +69,12 @@ class CategoryController extends Controller } /** - * @param CategoryRepositoryInterface $repository - * @param Category $category + * @param SingleCategoryRepositoryInterface $repository + * @param Category $category * * @return \Illuminate\Http\RedirectResponse */ - public function destroy(CategoryRepositoryInterface $repository, Category $category) + public function destroy(SingleCategoryRepositoryInterface $repository, Category $category) { $name = $category->name; @@ -107,17 +108,18 @@ class CategoryController extends Controller } /** - * @param CategoryRepositoryInterface $repository + * @param CategoryRepositoryInterface $repository + * @param SingleCategoryRepositoryInterface $singleRepository * * @return \Illuminate\View\View */ - public function index(CategoryRepositoryInterface $repository) + public function index(CategoryRepositoryInterface $repository, SingleCategoryRepositoryInterface $singleRepository) { $categories = $repository->getCategories(); $categories->each( - function (Category $category) use ($repository) { - $category->lastActivity = $repository->getLatestActivity($category); + function (Category $category) use ($singleRepository) { + $category->lastActivity = $singleRepository->getLatestActivity($category); } ); @@ -143,14 +145,14 @@ class CategoryController extends Controller } /** - * @param CategoryRepositoryInterface $repository + * @param SingleCategoryRepositoryInterface $repository * @param Category $category * * @param $date * * @return \Illuminate\View\View */ - public function showWithDate(CategoryRepositoryInterface $repository, Category $category, $date) + public function showWithDate(SingleCategoryRepositoryInterface $repository, Category $category, $date) { $carbon = new Carbon($date); $range = Preferences::get('viewRange', '1M')->data; @@ -170,12 +172,12 @@ class CategoryController extends Controller } /** - * @param CategoryRepositoryInterface $repository + * @param SingleCategoryRepositoryInterface $repository * @param Category $category * * @return \Illuminate\View\View */ - public function show(CategoryRepositoryInterface $repository, Category $category) + public function show(SingleCategoryRepositoryInterface $repository, Category $category) { $hideCategory = true; // used in list. $page = intval(Input::get('page')); @@ -226,11 +228,11 @@ class CategoryController extends Controller /** * @param CategoryFormRequest $request - * @param CategoryRepositoryInterface $repository + * @param SingleCategoryRepositoryInterface $repository * * @return \Illuminate\Http\RedirectResponse */ - public function store(CategoryFormRequest $request, CategoryRepositoryInterface $repository) + public function store(CategoryFormRequest $request, SingleCategoryRepositoryInterface $repository) { $categoryData = [ 'name' => $request->input('name'), @@ -253,12 +255,12 @@ class CategoryController extends Controller /** * @param CategoryFormRequest $request - * @param CategoryRepositoryInterface $repository + * @param SingleCategoryRepositoryInterface $repository * @param Category $category * * @return \Illuminate\Http\RedirectResponse */ - public function update(CategoryFormRequest $request, CategoryRepositoryInterface $repository, Category $category) + public function update(CategoryFormRequest $request, SingleCategoryRepositoryInterface $repository, Category $category) { $categoryData = [ 'name' => $request->input('name'), diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index 7d6a97b24a..08f5116adb 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -7,6 +7,7 @@ use Carbon\Carbon; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\Category; use FireflyIII\Repositories\Category\CategoryRepositoryInterface; +use FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface; use FireflyIII\Support\CacheProperties; use Illuminate\Support\Collection; use Navigation; @@ -38,12 +39,12 @@ class CategoryController extends Controller /** * Show an overview for a category for all time, per month/week/year. * - * @param CategoryRepositoryInterface $repository - * @param Category $category + * @param SingleCategoryRepositoryInterface $repository + * @param Category $category * * @return \Symfony\Component\HttpFoundation\Response */ - public function all(CategoryRepositoryInterface $repository, Category $category) + public function all(SingleCategoryRepositoryInterface $repository, Category $category) { // oldest transaction in category: $start = $repository->getFirstActivityDate($category); @@ -137,8 +138,13 @@ class CategoryController extends Controller * * @return \Illuminate\Http\JsonResponse */ - public function multiYear(CategoryRepositoryInterface $repository, $reportType, Carbon $start, Carbon $end, Collection $accounts, Collection $categories) + public function multiYear($reportType, Carbon $start, Carbon $end, Collection $accounts, Collection $categories) { + /** @var CategoryRepositoryInterface $repository */ + $repository = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + /** @var SingleCategoryRepositoryInterface $singleRepository */ + $singleRepository = app('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface'); + // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty($reportType); @@ -180,8 +186,8 @@ class CategoryController extends Controller $earned = $repository->earnedNoCategoryForAccounts($accounts, $currentStart, $currentEnd); } else { $name = $category->name; - $spent = $repository->spentInPeriodForAccounts($category, $accounts, $currentStart, $currentEnd); - $earned = $repository->earnedInPeriodForAccounts($category, $accounts, $currentStart, $currentEnd); + $spent = $singleRepository->spentInPeriodForAccounts($category, $accounts, $currentStart, $currentEnd); + $earned = $singleRepository->earnedInPeriodForAccounts($category, $accounts, $currentStart, $currentEnd); } // save to array: @@ -206,12 +212,12 @@ class CategoryController extends Controller } /** - * @param CategoryRepositoryInterface $repository + * @param SingleCategoryRepositoryInterface $repository * @param Category $category * * @return \Symfony\Component\HttpFoundation\Response */ - public function currentPeriod(CategoryRepositoryInterface $repository, Category $category) + public function currentPeriod(SingleCategoryRepositoryInterface $repository, Category $category) { $start = clone Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth()); @@ -246,14 +252,14 @@ class CategoryController extends Controller } /** - * @param CategoryRepositoryInterface $repository + * @param SingleCategoryRepositoryInterface $repository * @param Category $category * * @param $date * * @return \Symfony\Component\HttpFoundation\Response */ - public function specificPeriod(CategoryRepositoryInterface $repository, Category $category, $date) + public function specificPeriod(SingleCategoryRepositoryInterface $repository, Category $category, $date) { $carbon = new Carbon($date); $range = Preferences::get('viewRange', '1M')->data; diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php index e97bcb2dff..8ccc840780 100644 --- a/app/Repositories/Category/CategoryRepository.php +++ b/app/Repositories/Category/CategoryRepository.php @@ -6,9 +6,7 @@ use Auth; use Carbon\Carbon; use DB; use FireflyIII\Models\Category; -use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; -use FireflyIII\Repositories\Shared\ComponentRepository; use FireflyIII\Support\CacheProperties; use Illuminate\Database\Query\JoinClause; use Illuminate\Support\Collection; @@ -18,32 +16,9 @@ use Illuminate\Support\Collection; * * @package FireflyIII\Repositories\Category */ -class CategoryRepository extends ComponentRepository implements CategoryRepositoryInterface +class CategoryRepository implements CategoryRepositoryInterface { - /** - * @param Category $category - * - * @return int - */ - public function countJournals(Category $category) - { - return $category->transactionJournals()->count(); - - } - - /** - * @param Category $category - * - * @return boolean - */ - public function destroy(Category $category) - { - $category->delete(); - - return true; - } - /** * @return Collection */ @@ -194,62 +169,6 @@ class CategoryRepository extends ComponentRepository implements CategoryReposito } - /** - * @param Category $category - * - * @return Carbon - */ - public function getFirstActivityDate(Category $category) - { - /** @var TransactionJournal $first */ - $first = $category->transactionjournals()->orderBy('date', 'ASC')->first(); - if ($first) { - return $first->date; - } - - return new Carbon; - - } - - /** - * @param Category $category - * @param int $page - * - * @return Collection - */ - public function getJournals(Category $category, $page) - { - $offset = $page > 0 ? $page * 50 : 0; - - return $category->transactionJournals()->withRelevantData()->take(50)->offset($offset) - ->orderBy('transaction_journals.date', 'DESC') - ->orderBy('transaction_journals.order', 'ASC') - ->orderBy('transaction_journals.id', 'DESC') - ->get( - ['transaction_journals.*'] - ); - - } - - /** - * @param Category $category - * - * @return Carbon|null - */ - public function getLatestActivity(Category $category) - { - $latest = $category->transactionjournals() - ->orderBy('transaction_journals.date', 'DESC') - ->orderBy('transaction_journals.order', 'ASC') - ->orderBy('transaction_journals.id', 'DESC') - ->first(); - if ($latest) { - return $latest->date; - } - - return null; - } - /** * @param Carbon $start * @param Carbon $end @@ -270,266 +189,6 @@ class CategoryRepository extends ComponentRepository implements CategoryReposito ->get(['transaction_journals.*']); } - /** - * @param Category $category - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts - * - * @return string - */ - public function balanceInPeriod(Category $category, Carbon $start, Carbon $end, Collection $accounts) - { - return $this->commonBalanceInPeriod($category, $start, $end, $accounts); - } - - /** - * Corrected for tags - * - * @param Category $category - * @param Carbon $date - * - * @return string - */ - public function spentOnDaySum(Category $category, Carbon $date) - { - return $category->transactionjournals()->transactionTypes([TransactionType::WITHDRAWAL])->onDate($date)->get(['transaction_journals.*'])->sum('amount'); - } - - /** - * @param array $data - * - * @return Category - */ - public function store(array $data) - { - $newCategory = new Category( - [ - 'user_id' => $data['user'], - 'name' => $data['name'], - ] - ); - $newCategory->save(); - - return $newCategory; - } - - /** - * @param Category $category - * @param array $data - * - * @return Category - */ - public function update(Category $category, array $data) - { - // update the account: - $category->name = $data['name']; - $category->save(); - - return $category; - } - - /** - * @deprecated - * This method returns the sum of the journals in the category, optionally - * limited by a start or end date. - * - * @param Category $category - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function journalsSum(Category $category, Carbon $start = null, Carbon $end = null) - { - $query = $category->transactionJournals() - ->orderBy('transaction_journals.date', 'DESC') - ->orderBy('transaction_journals.order', 'ASC') - ->orderBy('transaction_journals.id', 'DESC'); - if (!is_null($start)) { - $query->after($start); - } - - if (!is_null($end)) { - $query->before($end); - } - - return $query->get(['transaction_journals.*'])->sum('amount'); - - } - - /** - * @param Category $category - * @param \Carbon\Carbon $start - * @param \Carbon\Carbon $end - * - * @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([TransactionType::WITHDRAWAL])->before($end)->after($start)->get(['transaction_journals.*']) - ->sum( - 'amount' - ); - - $cache->store($sum); - - return $sum; - } - - /** - * @param Category $category - * @param \Carbon\Carbon $start - * @param \Carbon\Carbon $end - * - * @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('earnedInPeriod'); - - if ($cache->has()) { - return $cache->get(); // @codeCoverageIgnore - } - - $sum = $category->transactionjournals()->transactionTypes([TransactionType::DEPOSIT])->before($end)->after($start)->get(['transaction_journals.*']) - ->sum( - 'amount' - ); - - $cache->store($sum); - - return $sum; - } - - - /** - * @param Category $category - * @param int $page - * @param Carbon $start - * @param Carbon $end - * - * @return mixed - */ - public function getJournalsInRange(Category $category, $page, Carbon $start, Carbon $end) - { - $offset = $page > 0 ? $page * 50 : 0; - - return $category->transactionJournals() - ->after($start) - ->before($end) - ->withRelevantData()->take(50)->offset($offset) - ->orderBy('transaction_journals.date', 'DESC') - ->orderBy('transaction_journals.order', 'ASC') - ->orderBy('transaction_journals.id', 'DESC') - ->get( - ['transaction_journals.*'] - ); - } - - /** - * @param Category $category - * - * @param Carbon $start - * @param Carbon $end - * - * @return int - */ - public function countJournalsInRange(Category $category, Carbon $start, Carbon $end) - { - return $category->transactionJournals()->before($end)->after($start)->count(); - } - - /** - * - * Corrected for tags. - * - * @param Category $category - * @param Carbon $date - * - * @return float - */ - public function earnedOnDaySum(Category $category, Carbon $date) - { - return $category->transactionjournals()->transactionTypes([TransactionType::DEPOSIT])->onDate($date)->get(['transaction_journals.*'])->sum('amount'); - } - - /** - * Calculates how much is spent in this period. - * - * @param Category $category - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function spentInPeriodForAccounts(Category $category, Collection $accounts, Carbon $start, Carbon $end) - { - $accountIds = []; - foreach ($accounts as $account) { - $accountIds[] = $account->id; - } - - $sum - = $category - ->transactionjournals() - ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') - ->after($start) - ->before($end) - ->whereIn('transactions.account_id', $accountIds) - ->transactionTypes([TransactionType::WITHDRAWAL]) - ->get(['transaction_journals.*']) - ->sum('amount'); - - return $sum; - - } - - /** - * Calculate how much is earned in this period. - * - * @param Category $category - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function earnedInPeriodForAccounts(Category $category, Collection $accounts, Carbon $start, Carbon $end) - { - $accountIds = []; - foreach ($accounts as $account) { - $accountIds[] = $account->id; - } - $sum - = $category - ->transactionjournals() - ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') - ->before($end) - ->whereIn('transactions.account_id', $accountIds) - ->transactionTypes([TransactionType::DEPOSIT]) - ->after($start) - ->get(['transaction_journals.*']) - ->sum('amount'); - - return $sum; - - } /** * Returns a collection of Categories appended with the amount of money that has been earned diff --git a/app/Repositories/Category/CategoryRepositoryInterface.php b/app/Repositories/Category/CategoryRepositoryInterface.php index a3d82b8db4..9878738d75 100644 --- a/app/Repositories/Category/CategoryRepositoryInterface.php +++ b/app/Repositories/Category/CategoryRepositoryInterface.php @@ -13,29 +13,6 @@ use Illuminate\Support\Collection; */ interface CategoryRepositoryInterface { - /** - * @param Category $category - * - * @return int - */ - public function countJournals(Category $category); - - /** - * @param Category $category - * - * @param Carbon $start - * @param Carbon $end - * - * @return int - */ - public function countJournalsInRange(Category $category, Carbon $start, Carbon $end); - - /** - * @param Category $category - * - * @return boolean - */ - public function destroy(Category $category); /** * Returns a collection of Categories appended with the amount of money that has been earned @@ -94,31 +71,6 @@ interface CategoryRepositoryInterface */ public function getCategories(); - - /** - * Calculates how much is spent in this period. - * - * @param Category $category - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function spentInPeriodForAccounts(Category $category, Collection $accounts, Carbon $start, Carbon $end); - - /** - * Calculate how much is earned in this period. - * - * @param Category $category - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function earnedInPeriodForAccounts(Category $category, Collection $accounts, Carbon $start, Carbon $end); - /** * Returns the amount spent without category by accounts in period. * @@ -151,52 +103,6 @@ interface CategoryRepositoryInterface */ public function getCategoriesAndExpenses(Carbon $start, Carbon $end); - /** - * @param Category $category - * - * @return Carbon - */ - public function getFirstActivityDate(Category $category); - - /** - * @param Category $category - * @param int $page - * - * @return Collection - */ - public function getJournals(Category $category, $page); - - /** - * @param Category $category - * @param int $page - * - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getJournalsInRange(Category $category, $page, Carbon $start, Carbon $end); - - /** - * @deprecated - * This method returns the sum of the journals in the category, optionally - * limited by a start or end date. - * - * @param Category $category - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function journalsSum(Category $category, Carbon $start = null, Carbon $end = null); - - /** - * @param Category $category - * - * @return Carbon|null - */ - public function getLatestActivity(Category $category); - /** * @param Carbon $start * @param Carbon $end @@ -205,71 +111,4 @@ interface CategoryRepositoryInterface */ public function getWithoutCategory(Carbon $start, Carbon $end); - /** - * Corrected for tags and list of accounts. - * - * @param Category $category - * @param \Carbon\Carbon $start - * @param \Carbon\Carbon $end - * @param Collection $accounts - * - * @return string - */ - public function balanceInPeriod(Category $category, Carbon $start, Carbon $end, Collection $accounts); - - /** - * @param Category $category - * @param \Carbon\Carbon $start - * @param \Carbon\Carbon $end - * - * @return string - */ - public function spentInPeriod(Category $category, Carbon $start, Carbon $end); - - /** - * @param Category $category - * @param \Carbon\Carbon $start - * @param \Carbon\Carbon $end - * - * @return string - */ - public function earnedInPeriod(Category $category, Carbon $start, Carbon $end); - - /** - * - * Corrected for tags. - * - * @param Category $category - * @param Carbon $date - * - * @return float - */ - public function spentOnDaySum(Category $category, Carbon $date); - - /** - * - * Corrected for tags. - * - * @param Category $category - * @param Carbon $date - * - * @return float - */ - public function earnedOnDaySum(Category $category, Carbon $date); - - /** - * @param array $data - * - * @return Category - */ - public function store(array $data); - - /** - * @param Category $category - * @param array $data - * - * @return Category - */ - public function update(Category $category, array $data); - } diff --git a/app/Repositories/Category/SingleCategoryRepository.php b/app/Repositories/Category/SingleCategoryRepository.php new file mode 100644 index 0000000000..5c115c9ce1 --- /dev/null +++ b/app/Repositories/Category/SingleCategoryRepository.php @@ -0,0 +1,337 @@ +commonBalanceInPeriod($category, $start, $end, $accounts); + } + + + /** + * @param Category $category + * + * @return int + */ + public function countJournals(Category $category) + { + return $category->transactionJournals()->count(); + + } + + /** + * @param Category $category + * + * @param Carbon $start + * @param Carbon $end + * + * @return int + */ + public function countJournalsInRange(Category $category, Carbon $start, Carbon $end) + { + return $category->transactionJournals()->before($end)->after($start)->count(); + } + + /** + * @param Category $category + * + * @return boolean + */ + public function destroy(Category $category) + { + $category->delete(); + + return true; + } + + /** + * @param Category $category + * @param \Carbon\Carbon $start + * @param \Carbon\Carbon $end + * + * @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('earnedInPeriod'); + + if ($cache->has()) { + return $cache->get(); // @codeCoverageIgnore + } + + $sum = $category->transactionjournals()->transactionTypes([TransactionType::DEPOSIT])->before($end)->after($start)->get(['transaction_journals.*']) + ->sum( + 'amount' + ); + + $cache->store($sum); + + return $sum; + } + + + /** + * Calculate how much is earned in this period. + * + * @param Category $category + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return string + */ + public function earnedInPeriodForAccounts(Category $category, Collection $accounts, Carbon $start, Carbon $end) + { + $accountIds = []; + foreach ($accounts as $account) { + $accountIds[] = $account->id; + } + $sum + = $category + ->transactionjournals() + ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') + ->before($end) + ->whereIn('transactions.account_id', $accountIds) + ->transactionTypes([TransactionType::DEPOSIT]) + ->after($start) + ->get(['transaction_journals.*']) + ->sum('amount'); + + return $sum; + + } + + /** + * + * Corrected for tags. + * + * @param Category $category + * @param Carbon $date + * + * @return float + */ + public function earnedOnDaySum(Category $category, Carbon $date) + { + return $category->transactionjournals()->transactionTypes([TransactionType::DEPOSIT])->onDate($date)->get(['transaction_journals.*'])->sum('amount'); + } + + /** + * @param Category $category + * + * @return Carbon + */ + public function getFirstActivityDate(Category $category) + { + /** @var TransactionJournal $first */ + $first = $category->transactionjournals()->orderBy('date', 'ASC')->first(); + if ($first) { + return $first->date; + } + + return new Carbon; + + } + + /** + * @param Category $category + * @param int $page + * + * @return Collection + */ + public function getJournals(Category $category, $page) + { + $offset = $page > 0 ? $page * 50 : 0; + + return $category->transactionJournals()->withRelevantData()->take(50)->offset($offset) + ->orderBy('transaction_journals.date', 'DESC') + ->orderBy('transaction_journals.order', 'ASC') + ->orderBy('transaction_journals.id', 'DESC') + ->get( + ['transaction_journals.*'] + ); + + } + + /** + * @param Category $category + * @param int $page + * @param Carbon $start + * @param Carbon $end + * + * @return mixed + */ + public function getJournalsInRange(Category $category, $page, Carbon $start, Carbon $end) + { + $offset = $page > 0 ? $page * 50 : 0; + + return $category->transactionJournals() + ->after($start) + ->before($end) + ->withRelevantData()->take(50)->offset($offset) + ->orderBy('transaction_journals.date', 'DESC') + ->orderBy('transaction_journals.order', 'ASC') + ->orderBy('transaction_journals.id', 'DESC') + ->get( + ['transaction_journals.*'] + ); + } + + + /** + * @param Category $category + * + * @return Carbon|null + */ + public function getLatestActivity(Category $category) + { + $latest = $category->transactionjournals() + ->orderBy('transaction_journals.date', 'DESC') + ->orderBy('transaction_journals.order', 'ASC') + ->orderBy('transaction_journals.id', 'DESC') + ->first(); + if ($latest) { + return $latest->date; + } + + return null; + } + + + /** + * Calculates how much is spent in this period. + * + * @param Category $category + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return string + */ + public function spentInPeriodForAccounts(Category $category, Collection $accounts, Carbon $start, Carbon $end) + { + $accountIds = []; + foreach ($accounts as $account) { + $accountIds[] = $account->id; + } + + $sum + = $category + ->transactionjournals() + ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') + ->after($start) + ->before($end) + ->whereIn('transactions.account_id', $accountIds) + ->transactionTypes([TransactionType::WITHDRAWAL]) + ->get(['transaction_journals.*']) + ->sum('amount'); + + return $sum; + + } + + /** + * @param Category $category + * @param \Carbon\Carbon $start + * @param \Carbon\Carbon $end + * + * @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([TransactionType::WITHDRAWAL])->before($end)->after($start)->get(['transaction_journals.*']) + ->sum( + 'amount' + ); + + $cache->store($sum); + + return $sum; + } + + + /** + * Corrected for tags + * + * @param Category $category + * @param Carbon $date + * + * @return string + */ + public function spentOnDaySum(Category $category, Carbon $date) + { + return $category->transactionjournals()->transactionTypes([TransactionType::WITHDRAWAL])->onDate($date)->get(['transaction_journals.*'])->sum('amount'); + } + + /** + * @param array $data + * + * @return Category + */ + public function store(array $data) + { + $newCategory = new Category( + [ + 'user_id' => $data['user'], + 'name' => $data['name'], + ] + ); + $newCategory->save(); + + return $newCategory; + } + + /** + * @param Category $category + * @param array $data + * + * @return Category + */ + public function update(Category $category, array $data) + { + // update the account: + $category->name = $data['name']; + $category->save(); + + return $category; + } + + +} \ No newline at end of file diff --git a/app/Repositories/Category/SingleCategoryRepositoryInterface.php b/app/Repositories/Category/SingleCategoryRepositoryInterface.php new file mode 100644 index 0000000000..c23784e51d --- /dev/null +++ b/app/Repositories/Category/SingleCategoryRepositoryInterface.php @@ -0,0 +1,166 @@ +