diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php index ee0aac585e..696145827b 100644 --- a/app/Http/Controllers/CategoryController.php +++ b/app/Http/Controllers/CategoryController.php @@ -136,7 +136,7 @@ class CategoryController extends Controller $start = session('start', Carbon::now()->startOfMonth()); /** @var Carbon $end */ $end = session('end', Carbon::now()->startOfMonth()); - $list = $repository->journalsInPeriodWithoutCategory(new Collection(), $start, $end); + $list = $repository->journalsInPeriodWithoutCategory(new Collection(), [], $start, $end); $subTitle = trans( 'firefly.without_category_between', ['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)] diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index c89e4b0110..b6ba76a36c 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -102,46 +102,6 @@ class CategoryController extends Controller return Response::json($data); } - /** - * Returns a chart of what has been earned in this period in each category - * grouped by month. - * - * @param CRI $repository - * @param $reportType - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts - * - * @return \Illuminate\Http\JsonResponse - */ - public function earnedInPeriod(CRI $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts) - { - /** - * $cache = new CacheProperties; // chart properties for cache: - * $cache->addProperty($start); - * $cache->addProperty($end); - * $cache->addProperty($reportType); - * $cache->addProperty($accounts); - * $cache->addProperty('category'); - * $cache->addProperty('earned-in-period'); - * if ($cache->has()) { - * return Response::json($cache->get()); - * } - * - * $set = $repository->earnedForAccountsPerMonth($accounts, $start, $end); - * $categories = $set->unique('id')->sortBy( - * function (Category $category) { - * return $category->name; - * } - * ); - * $entries = $this->filterCollection($start, $end, $set, $categories); - * $data = $this->generator->earnedInPeriod($categories, $entries); - * $cache->store($data); - * - * return $data; - **/ - } - /** * Show this month's category overview. * @@ -196,82 +156,67 @@ class CategoryController extends Controller * * @return \Illuminate\Http\JsonResponse */ - public function multiYear(string $reportType, Carbon $start, Carbon $end, Collection $accounts, Collection $categories) + public function multiYear(Carbon $start, Carbon $end, Collection $accounts, Collection $categories) { - /** - * // /** @var CRI $repository - * // $repository = app(CRI::class); - * - * // chart properties for cache: - * $cache = new CacheProperties(); - * $cache->addProperty($reportType); - * $cache->addProperty($start); - * $cache->addProperty($end); - * $cache->addProperty($accounts); - * $cache->addProperty($categories); - * $cache->addProperty('multiYearCategory'); - * - * if ($cache->has()) { - * return Response::json($cache->get()); - * } - * - * $entries = new Collection; - * $set = $repository->listMultiYear($categories, $accounts, $start, $end); - * - * /** @var Category $category - * foreach ($categories as $category) { - * $entry = ['name' => '', 'spent' => [], 'earned' => []]; - * - * $currentStart = clone $start; - * while ($currentStart < $end) { - * // fix the date: - * $year = $currentStart->year; - * $currentEnd = clone $currentStart; - * $currentEnd->endOfYear(); - * - * - * // get data: - * if (is_null($category->id)) { - * $name = trans('firefly.noCategory'); - * $spent = $repository->sumSpentNoCategory($accounts, $currentStart, $currentEnd); - * $earned = $repository->sumEarnedNoCategory($accounts, $currentStart, $currentEnd); - * } else { - * // get from set: - * $entrySpent = $set->filter( - * function (Category $cat) use ($year, $category) { - * return ($cat->type == 'Withdrawal' && $cat->dateFormatted == $year && $cat->id == $category->id); - * } - * )->first(); - * $entryEarned = $set->filter( - * function (Category $cat) use ($year, $category) { - * return ($cat->type == 'Deposit' && $cat->dateFormatted == $year && $cat->id == $category->id); - * } - * )->first(); - * - * $name = $category->name; - * $spent = !is_null($entrySpent) ? $entrySpent->sum : 0; - * $earned = !is_null($entryEarned) ? $entryEarned->sum : 0; - * } - * - * // save to array: - * $entry['name'] = $name; - * $entry['spent'][$year] = ($spent * -1); - * $entry['earned'][$year] = $earned; - * - * // jump to next year. - * $currentStart = clone $currentEnd; - * $currentStart->addDay(); - * } - * $entries->push($entry); - * } - * // generate chart with data: - * - * $data = $this->generator->multiYear($entries); - * $cache->store($data); - * - * return Response::json($data); - * - */ + + /** @var CRI $repository */ + $repository = app(CRI::class); + + // chart properties for cache: + $cache = new CacheProperties(); + $cache->addProperty($start); + $cache->addProperty($end); + $cache->addProperty($accounts); + $cache->addProperty($categories); + $cache->addProperty('multiYearCategory'); + + if ($cache->has()) { + //return Response::json($cache->get()); + } + + $entries = new Collection; + + /** @var Category $category */ + foreach ($categories as $category) { + $entry = ['name' => '', 'spent' => [], 'earned' => []]; + + $currentStart = clone $start; + while ($currentStart < $end) { + // fix the date: + $year = $currentStart->year; + $currentEnd = clone $currentStart; + $currentEnd->endOfYear(); + + // get data: + if (is_null($category->id)) { + $name = trans('firefly.noCategory'); + $spent = $repository->spentInPeriodWithoutCategory($accounts, $currentStart, $currentEnd); + $earned = $repository->earnedInPeriodWithoutCategory($accounts, $currentStart, $currentEnd); + } else { + + $name = $category->name; + $spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $currentStart, $currentEnd); + $earned = $repository->earnedInPeriod(new Collection([$category]), $accounts, $currentStart, $currentEnd); + } + + // save to array: + $entry['name'] = $name; + $entry['spent'][$year] = ($spent * -1); + $entry['earned'][$year] = $earned; + + // jump to next year. + $currentStart = clone $currentEnd; + $currentStart->addDay(); + } + $entries->push($entry); + } + // generate chart with data: + + $data = $this->generator->multiYear($entries); + $cache->store($data); + + return Response::json($data); + } @@ -347,49 +292,6 @@ class CategoryController extends Controller return Response::json($data); } - /** - * Returns a chart of what has been spent in this period in each category - * grouped by month. - * - * @param CRI $repository - * @param $reportType - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts - * - * - * @return \Illuminate\Http\JsonResponse - */ - public function spentInPeriod(CRI $repository, $reportType, Carbon $start, Carbon $end, Collection $accounts) - { - /** - * $cache = new CacheProperties; // chart properties for cache: - * $cache->addProperty($start); - * $cache->addProperty($end); - * $cache->addProperty($reportType); - * $cache->addProperty($accounts); - * $cache->addProperty('category'); - * $cache->addProperty('spent-in-period'); - * if ($cache->has()) { - * return Response::json($cache->get()); - * } - * - * - * $set = $repository->spentForAccountsPerMonth($accounts, $start, $end); - * $categories = $set->unique('id')->sortBy( - * function (Category $category) { - * return $category->name; - * } - * ); - * $entries = $this->filterCollection($start, $end, $set, $categories); - * $entries = $this->invertSelection($entries); - * $data = $this->generator->spentInPeriod($categories, $entries); - * $cache->store($data); - * - * return $data; - * */ - } - /** * @param Carbon $start * @param Carbon $end diff --git a/app/Http/routes.php b/app/Http/routes.php index 8415a709ad..aea15c53ab 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -211,11 +211,7 @@ Route::group( Route::get('/chart/category/period/{category}/default/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\CategoryController@period']); // these three charts are for reports: - Route::get('/chart/category/earned-in-period/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\CategoryController@earnedInPeriod']); - Route::get('/chart/category/spent-in-period/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\CategoryController@spentInPeriod']); - Route::get( - '/chart/category/multi-year/{reportType}/{start_date}/{end_date}/{accountList}/{categoryList}', ['uses' => 'Chart\CategoryController@multiYear'] - ); + Route::get('/chart/category/multi-year/default/{start_date}/{end_date}/{accountList}/{categoryList}', ['uses' => 'Chart\CategoryController@multiYear']); Route::get('/chart/category/{category}/period', ['uses' => 'Chart\CategoryController@currentPeriod']); Route::get('/chart/category/{category}/period/{date}', ['uses' => 'Chart\CategoryController@specificPeriod']); diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index 0e6c80f587..9e17c3c244 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -37,31 +37,6 @@ class BudgetRepository implements BudgetRepositoryInterface $this->user = $user; } - // /** - // * @param Budget $budget - // * @param Carbon $start - // * @param Carbon $end - // * @param Collection $accounts - // * - // * @return string - // */ - // public function balanceInPeriod(Budget $budget, Carbon $start, Carbon $end, Collection $accounts): string - // { - // return $this->commonBalanceInPeriod($budget, $start, $end, $accounts); - // } - - // /** - // * @return bool - // */ - // public function cleanupBudgets(): bool - // { - // // delete limits with amount 0: - // BudgetLimit::where('amount', 0)->delete(); - // - // return true; - // - // } - /** * @param Budget $budget * @@ -74,23 +49,6 @@ class BudgetRepository implements BudgetRepositoryInterface return true; } - // /** - // * @param Budget $budget - // * @param Account $account - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return Collection - // */ - // public function expensesSplit(Budget $budget, Account $account, Carbon $start, Carbon $end): Collection - // { - // return $budget->transactionjournals()->expanded() - // ->before($end) - // ->after($start) - // ->where('source_account.id', $account->id) - // ->get(TransactionJournal::queryFields()); - // } - /** * Find a budget. * @@ -108,21 +66,6 @@ class BudgetRepository implements BudgetRepositoryInterface return $budget; } - // /** - // * @param Budget $budget - // * - // * @return Carbon - // */ - // public function firstActivity(Budget $budget): Carbon - // { - // $first = $budget->transactionjournals()->orderBy('date', 'ASC')->first(); - // if ($first) { - // return $first->date; - // } - // - // return new Carbon; - // } - /** * This method returns the oldest journal or transaction date known to this budget. * Will cache result. @@ -168,62 +111,6 @@ class BudgetRepository implements BudgetRepositoryInterface return $set; } - // /** - // * @param Account $account - // * @param Carbon $start - // * @param Carbon $end - // * @param Collection $accounts - // * - // * @return Collection - // */ - // public function getAllWithoutBudget(Account $account, Collection $accounts, Carbon $start, Carbon $end): Collection - // { - // $ids = $accounts->pluck('id')->toArray(); - // - // return $this->user - // ->transactionjournals() - // ->expanded() - // ->where('source_account.id', $account->id) - // ->whereNotIn('destination_account.id', $ids) - // ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - // ->whereNull('budget_transaction_journal.id') - // ->before($end) - // ->after($start) - // ->get(TransactionJournal::queryFields()); - // } - - // /** - // * Get the budgeted amounts for each budgets in each year. - // * - // * @param Collection $budgets - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return Collection - // */ - // public function getBudgetedPerYear(Collection $budgets, Carbon $start, Carbon $end): Collection - // { - // $budgetIds = $budgets->pluck('id')->toArray(); - // - // $set = $this->user->budgets() - // ->leftJoin('budget_limits', 'budgets.id', '=', 'budget_limits.budget_id') - // ->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id') - // ->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d')) - // ->where('limit_repetitions.enddate', '<=', $end->format('Y-m-d')) - // ->groupBy('budgets.id') - // ->groupBy('dateFormatted') - // ->whereIn('budgets.id', $budgetIds) - // ->get( - // [ - // 'budgets.*', - // DB::raw('DATE_FORMAT(`limit_repetitions`.`startdate`,"%Y") as `dateFormatted`'), - // DB::raw('SUM(`limit_repetitions`.`amount`) as `budgeted`'), - // ] - // ); - // - // return $set; - // } - /** * @param Carbon $start * @param Carbon $end @@ -244,275 +131,6 @@ class BudgetRepository implements BudgetRepositoryInterface return $set; } - // /** - // * Returns an array with every budget in it and the expenses for each budget - // * per month. - // * - // * @param Collection $accounts - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return array - // */ - // public function getBudgetsAndExpensesPerMonth(Collection $accounts, Carbon $start, Carbon $end): array - // { - // $ids = $accounts->pluck('id')->toArray(); - // - // /** @var Collection $set */ - // $set = $this->user->budgets() - // ->leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id') - // ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id') - // ->leftJoin( - // 'transactions', function (JoinClause $join) { - // $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0); - // } - // ) - // ->groupBy('budgets.id') - // ->groupBy('dateFormatted') - // ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) - // ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) - // ->whereIn('transactions.account_id', $ids) - // ->get( - // [ - // 'budgets.*', - // DB::raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y-%m") AS `dateFormatted`'), - // DB::raw('SUM(`transactions`.`amount`) AS `sumAmount`'), - // ] - // ); - // - // $set = $set->sortBy( - // function (Budget $budget) { - // return strtolower($budget->name); - // } - // ); - // - // $return = []; - // foreach ($set as $budget) { - // $id = $budget->id; - // if (!isset($return[$id])) { - // $return[$id] = [ - // 'budget' => $budget, - // 'entries' => [], - // ]; - // } - // // store each entry: - // $return[$id]['entries'][$budget->dateFormatted] = $budget->sumAmount; - // } - // - // return $return; - // } - - // /** - // * Returns an array with every budget in it and the expenses for each budget - // * per year for. - // * - // * @param Collection $budgets - // * @param Collection $accounts - // * @param Carbon $start - // * @param Carbon $end - // * - // * @deprecated - // * - // * @return array - // */ - // public function getBudgetsAndExpensesPerYear(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array - // { - // // get budgets, - // $ids = $accounts->pluck('id')->toArray(); - // $budgetIds = $budgets->pluck('id')->toArray(); - // - // /** @var Collection $set */ - // $set = $this->user->budgets() - // ->join('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id') - // ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id') - // ->leftJoin( - // 'transactions', function (JoinClause $join) { - // $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0); - // } - // ) - // ->groupBy('budgets.id') - // ->groupBy('dateFormatted') - // ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) - // ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) - // ->whereIn('transactions.account_id', $ids) - // ->whereIn('budgets.id', $budgetIds) - // ->get( - // [ - // 'budgets.*', - // DB::raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y") AS `dateFormatted`'), - // DB::raw('SUM(`transactions`.`amount`) AS `sumAmount`'), - // ] - // ); - // - // // run it again, for transactions this time. - // /** @var Collection $secondSet */ - // $secondSet = $this->user->budgets() - // ->join('budget_transaction', 'budgets.id', '=', 'budget_transaction.budget_id') - // ->leftJoin('transactions', 'transactions.id', '=', 'budget_transaction.transaction_id') - // ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') - // ->where('transactions.amount', '<', 0) - // ->groupBy('budgets.id') - // ->groupBy('dateFormatted') - // ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) - // ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) - // ->whereIn('transactions.account_id', $ids) - // ->whereIn('budgets.id', $budgetIds) - // ->get( - // [ - // 'budgets.*', - // DB::raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y") AS `dateFormatted`'), - // DB::raw('SUM(`transactions`.`amount`) AS `sumAmount`'), - // ] - // ); - // - // $set = $set->sortBy( - // function (Budget $budget) { - // return strtolower($budget->name); - // } - // ); - // - // $return = []; - // foreach ($set as $budget) { - // Log::debug('First set, budget #' . $budget->id . ' (' . $budget->name . ')'); - // $id = $budget->id; - // if (!isset($return[$id])) { - // Log::debug('$return[$id] is not set, now created.'); - // $return[$id] = [ - // 'budget' => $budget, - // 'entries' => [], - // ]; - // } - // Log::debug('Add new entry to entries, for ' . $budget->dateFormatted . ' and amount ' . $budget->sumAmount); - // // store each entry: - // $return[$id]['entries'][$budget->dateFormatted] = $budget->sumAmount; - // } - // unset($budget); - // - // // run the second set: - // foreach ($secondSet as $entry) { - // $id = $entry->id; - // // create it if it still does not exist (not really likely) - // if (!isset($return[$id])) { - // $return[$id] = [ - // 'budget' => $entry, - // 'entries' => [], - // ]; - // } - // // this one might be filled too: - // $startAmount = $return[$id]['entries'][$entry->dateFormatted] ?? '0'; - // // store each entry: - // $return[$id]['entries'][$entry->dateFormatted] = bcadd($startAmount, $entry->sumAmount); - // } - // - // return $return; - // } - - // /** - // * Returns a list of budgets, budget limits and limit repetitions - // * (doubling any of them in a left join) - // * - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return Collection - // */ - // public function getBudgetsAndLimitsInRange(Carbon $start, Carbon $end): Collection - // { - // /** @var Collection $set */ - // $set = $this->user - // ->budgets() - // ->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budgets.id') - // ->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id') - // ->where( - // function (Builder $query) use ($start, $end) { - // $query->where( - // function (Builder $query) use ($start, $end) { - // $query->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d')); - // $query->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d')); - // } - // ); - // $query->orWhere( - // function (Builder $query) { - // $query->whereNull('limit_repetitions.startdate'); - // $query->whereNull('limit_repetitions.enddate'); - // } - // ); - // } - // ) - // ->orderBy('budgets.id', 'budget_limits.startdate', 'limit_repetitions.enddate') - // ->get(['budgets.*', 'limit_repetitions.startdate', 'limit_repetitions.enddate', 'limit_repetitions.amount']); - // - // $set = $set->sortBy( - // function (Budget $budget) { - // return strtolower($budget->name); - // } - // ); - // - // return $set; - // - // } - - // /** - // * @param Budget $budget - // * @param string $repeatFreq - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return LimitRepetition - // */ - // public function getCurrentRepetition(Budget $budget, string $repeatFreq, Carbon $start, Carbon $end): LimitRepetition - // { - // $data = $budget->limitrepetitions() - // ->where('budget_limits.repeat_freq', $repeatFreq) - // ->where('limit_repetitions.startdate', $start->format('Y-m-d 00:00:00')) - // ->where('limit_repetitions.enddate', $end->format('Y-m-d 00:00:00')) - // ->first(['limit_repetitions.*']); - // if (is_null($data)) { - // return new LimitRepetition; - // } - // - // return $data; - // } - - // /** - // * Returns all expenses for the given budget and the given accounts, in the given period. - // * - // * @param Budget $budget - // * @param Collection $accounts - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return Collection - // */ - // public function getExpenses(Budget $budget, Collection $accounts, Carbon $start, Carbon $end):Collection - // { - // $ids = $accounts->pluck('id')->toArray(); - // $set = $budget->transactionjournals() - // ->before($end) - // ->after($start) - // ->expanded() - // ->where('transaction_types.type', TransactionType::WITHDRAWAL) - // ->whereIn('source_account.id', $ids) - // ->get(TransactionJournal::queryFields()); - // - // return $set; - // } - - // /** - // * @param Budget $budget - // * - // * @return Carbon - // */ - // public function getFirstBudgetLimitDate(Budget $budget): Carbon - // { - // $limit = $budget->budgetlimits()->orderBy('startdate', 'ASC')->first(); - // if ($limit) { - // return $limit->startdate; - // } - // - // return Carbon::now()->startOfYear(); - // } - /** * @return Collection */ @@ -530,316 +148,6 @@ class BudgetRepository implements BudgetRepositoryInterface return $set; } - // /** - // * Returns all the transaction journals for a limit, possibly limited by a limit repetition. - // * - // * @param Budget $budget - // * @param LimitRepetition $repetition - // * @param int $take - // * - // * @return LengthAwarePaginator - // */ - // public function getJournals(Budget $budget, LimitRepetition $repetition = null, int $take = 50): LengthAwarePaginator - // { - // $offset = intval(Input::get('page')) > 0 ? intval(Input::get('page')) * $take : 0; - // $setQuery = $budget->transactionjournals()->expanded() - // ->take($take)->offset($offset) - // ->orderBy('transaction_journals.date', 'DESC') - // ->orderBy('transaction_journals.order', 'ASC') - // ->orderBy('transaction_journals.id', 'DESC'); - // $countQuery = $budget->transactionjournals(); - // - // - // if (!is_null($repetition->id)) { - // $setQuery->after($repetition->startdate)->before($repetition->enddate); - // $countQuery->after($repetition->startdate)->before($repetition->enddate); - // } - // - // - // $set = $setQuery->get(TransactionJournal::queryFields()); - // $count = $countQuery->count(); - // - // - // $paginator = new LengthAwarePaginator($set, $count, $take, $offset); - // - // return $paginator; - // } - - // /** - // * Returns a list of budget limits that are valid in the current given range. - // * $ignore is optional. Send an empty limit rep. - // * - // * @param Budget $budget - // * @param Carbon $start - // * @param Carbon $end - // * @param LimitRepetition $ignore - // * - // * @return Collection - // */ - // public function getValidRepetitions(Budget $budget, Carbon $start, Carbon $end, LimitRepetition $ignore) : Collection - // { - // $query = $budget->limitrepetitions() - // // starts before start time, and the end also after start time. - // ->where('limit_repetitions.enddate', '>=', $start->format('Y-m-d 00:00:00')) - // ->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00')); - // if (!is_null($ignore->id)) { - // $query->where('limit_repetitions.id', '!=', $ignore->id); - // } - // $data = $query->get(['limit_repetitions.*']); - // - // return $data; - // } - - // /** - // * @param Carbon $start - // * @param Carbon $end - // * @param int $page - // * @param int $pageSize - // * - // * @return LengthAwarePaginator - // */ - // public function getWithoutBudget(Carbon $start, Carbon $end, int $page, int $pageSize = 50): LengthAwarePaginator - // { - // $offset = ($page - 1) * $pageSize; - // $query = $this->user - // ->transactionjournals() - // ->expanded() - // ->where('transaction_types.type', TransactionType::WITHDRAWAL) - // ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - // ->whereNull('budget_transaction_journal.id') - // ->before($end) - // ->after($start); - // - // $count = $query->count(); - // $set = $query->take($pageSize)->offset($offset)->get(TransactionJournal::queryFields()); - // $paginator = new LengthAwarePaginator($set, $count, $pageSize, $page); - // - // return $paginator; - // } - - // /** - // * @param Collection $accounts - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return Collection - // */ - // public function getWithoutBudgetForAccounts(Collection $accounts, Carbon $start, Carbon $end): Collection - // { - // $ids = $accounts->pluck('id')->toArray(); - // - // return $this->user - // ->transactionjournals() - // ->expanded() - // ->whereIn('source_account.id', $ids) - // ->where('transaction_types.type', TransactionType::WITHDRAWAL) - // ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - // ->whereNull('budget_transaction_journal.id') - // ->before($end) - // ->after($start) - // ->get(TransactionJournal::queryFields()); - // } - - // /** - // * @param Collection $accounts - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return string - // */ - // public function getWithoutBudgetSum(Collection $accounts, Carbon $start, Carbon $end): string - // { - // $ids = $accounts->pluck('id')->toArray(); - // $entry = $this->user - // ->transactionjournals() - // ->whereNotIn( - // 'transaction_journals.id', function (QueryBuilder $query) use ($start, $end) { - // $query - // ->select('transaction_journals.id') - // ->from('transaction_journals') - // ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - // ->where('transaction_journals.date', '>=', $start->format('Y-m-d 00:00:00')) - // ->where('transaction_journals.date', '<=', $end->format('Y-m-d 00:00:00')) - // ->whereNotNull('budget_transaction_journal.budget_id'); - // } - // ) - // ->after($start) - // ->before($end) - // ->leftJoin( - // 'transactions', function (JoinClause $join) { - // $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0); - // } - // ) - // ->whereIn('transactions.account_id', $ids) - // //->having('transaction_count', '=', 1) TO DO check if this still works - // ->transactionTypes([TransactionType::WITHDRAWAL]) - // ->first( - // [ - // DB::raw('SUM(`transactions`.`amount`) as `journalAmount`'), - // DB::raw('COUNT(`transactions`.`id`) as `transaction_count`'), - // ] - // ); - // if (is_null($entry)) { - // return '0'; - // } - // if (is_null($entry->journalAmount)) { - // return '0'; - // } - // - // return $entry->journalAmount; - // } - - // /** - // * Returns an array with the following key:value pairs: - // * - // * yyyy-mm-dd: - // * - // * That array contains: - // * - // * budgetid: - // * - // * Where yyyy-mm-dd is the date and is the money spent using WITHDRAWALS in the $budget - // * from the given users accounts.. - // * - // * @param Collection $accounts - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return array - // */ - // public function spentAllPerDayForAccounts(Collection $accounts, Carbon $start, Carbon $end): array - // { - // $ids = $accounts->pluck('id')->toArray(); - // /** @var Collection $query */ - // $query = $this->user->transactionJournals() - // ->transactionTypes([TransactionType::WITHDRAWAL]) - // ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') - // ->leftJoin('budget_transaction_journal', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id') - // ->whereIn('transactions.account_id', $ids) - // ->where('transactions.amount', '<', 0) - // ->before($end) - // ->after($start) - // ->groupBy('budget_id') - // ->groupBy('dateFormatted') - // ->get( - // ['transaction_journals.date as dateFormatted', 'budget_transaction_journal.budget_id', - // DB::raw('SUM(`transactions`.`amount`) AS `sum`')] - // ); - // - // $return = []; - // foreach ($query->toArray() as $entry) { - // $budgetId = $entry['budget_id']; - // if (!isset($return[$budgetId])) { - // $return[$budgetId] = []; - // } - // $return[$budgetId][$entry['dateFormatted']] = $entry['sum']; - // } - // - // return $return; - // } - - // /** - // * Returns a list of expenses (in the field "spent", grouped per budget per account. - // * - // * @param Collection $budgets - // * @param Collection $accounts - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return Collection - // */ - // public function spentPerBudgetPerAccount(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): Collection - // { - // $accountIds = $accounts->pluck('id')->toArray(); - // $budgetIds = $budgets->pluck('id')->toArray(); - // $set = $this->user->transactionjournals() - // ->leftJoin( - // 'transactions AS t_from', function (JoinClause $join) { - // $join->on('transaction_journals.id', '=', 't_from.transaction_journal_id')->where('t_from.amount', '<', 0); - // } - // ) - // ->leftJoin( - // 'transactions AS t_to', function (JoinClause $join) { - // $join->on('transaction_journals.id', '=', 't_to.transaction_journal_id')->where('t_to.amount', '>', 0); - // } - // ) - // ->leftJoin('budget_transaction_journal', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id') - // ->whereIn('t_from.account_id', $accountIds) - // ->whereNotIn('t_to.account_id', $accountIds) - // ->where( - // function (Builder $q) use ($budgetIds) { - // $q->whereIn('budget_transaction_journal.budget_id', $budgetIds); - // $q->orWhereNull('budget_transaction_journal.budget_id'); - // } - // ) - // ->after($start) - // ->before($end) - // ->groupBy('t_from.account_id') - // ->groupBy('budget_transaction_journal.budget_id') - // ->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])// opening balance is not an expense. - // ->get( - // [ - // 't_from.account_id', 'budget_transaction_journal.budget_id', - // DB::raw('SUM(`t_from`.`amount`) AS `spent`'), - // ] - // ); - // - // return $set; - // - // } - - // /** - // * Returns an array with the following key:value pairs: - // * - // * yyyy-mm-dd: - // * - // * Where yyyy-mm-dd is the date and is the money spent using DEPOSITS in the $budget - // * from all the users accounts. - // * - // * @param Budget $budget - // * @param Carbon $start - // * @param Carbon $end - // * @param Collection $accounts - // * - // * @return array - // */ - // public function spentPerDay(Budget $budget, Carbon $start, Carbon $end, Collection $accounts): array - // { - // /** @var Collection $query */ - // $query = $budget->transactionjournals() - // ->transactionTypes([TransactionType::WITHDRAWAL]) - // ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') - // ->where('transactions.amount', '<', 0) - // ->before($end) - // ->after($start) - // ->groupBy('dateFormatted')->get(['transaction_journals.date as dateFormatted', DB::raw('SUM(`transactions`.`amount`) AS `sum`')]); - // - // $return = []; - // foreach ($query->toArray() as $entry) { - // $return[$entry['dateFormatted']] = $entry['sum']; - // } - // - // // also search transactions: - // $query = $budget->transactions() - // ->transactionTypes([TransactionType::WITHDRAWAL]) - // ->where('transactions.amount', '<', 0) - // ->before($end) - // ->after($start) - // ->groupBy('dateFormatted')->get(['transaction_journals.date as dateFormatted', DB::raw('SUM(`transactions`.`amount`) AS `sum`')]); - // foreach ($query as $newEntry) { - // // add to return array. - // $date = $newEntry['dateFormatted']; - // if (isset($return[$date])) { - // $return[$date] = bcadd($newEntry['sum'], $return[$date]); - // continue; - // } - // - // $return[$date] = $newEntry['sum']; - // } - // - // return $return; - // } - /** * @return Collection */ diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php index f76c5fc3bb..7c1eff2d91 100644 --- a/app/Repositories/Category/CategoryRepository.php +++ b/app/Repositories/Category/CategoryRepository.php @@ -18,10 +18,6 @@ use Illuminate\Support\Collection; */ class CategoryRepository implements CategoryRepositoryInterface { - // const SPENT = 1; - // const EARNED = 2; - - /** @var User */ private $user; @@ -35,280 +31,6 @@ class CategoryRepository implements CategoryRepositoryInterface $this->user = $user; } - // /** - // * Returns a collection of Categories appended with the amount of money that has been earned - // * in these categories, based on the $accounts involved, in period X, grouped per month. - // * The amount earned in category X in period X is saved in field "earned". - // * - // * @param $accounts - // * @param $start - // * @param $end - // * - // * @return Collection - // */ - // public function earnedForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end): Collection - // { - // - // $collection = $this->user->categories() - // ->leftJoin('category_transaction_journal', 'category_transaction_journal.category_id', '=', 'categories.id') - // ->leftJoin('transaction_journals', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - // ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id') - // ->leftJoin( - // 'transactions AS t_src', function (JoinClause $join) { - // $join->on('t_src.transaction_journal_id', '=', 'transaction_journals.id')->where('t_src.amount', '<', 0); - // } - // ) - // ->leftJoin( - // 'transactions AS t_dest', function (JoinClause $join) { - // $join->on('t_dest.transaction_journal_id', '=', 'transaction_journals.id')->where('t_dest.amount', '>', 0); - // } - // ) - // ->whereIn('t_dest.account_id', $accounts->pluck('id')->toArray())// to these accounts (earned) - // ->whereNotIn('t_src.account_id', $accounts->pluck('id')->toArray())//-- but not from these accounts - // ->whereIn( - // 'transaction_types.type', [TransactionType::DEPOSIT, TransactionType::TRANSFER, TransactionType::OPENING_BALANCE] - // ) - // ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) - // ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) - // ->groupBy('categories.id') - // ->groupBy('dateFormatted') - // ->get( - // [ - // 'categories.*', - // DB::raw('DATE_FORMAT(`transaction_journals`.`date`,"%Y-%m") as `dateFormatted`'), - // DB::raw('SUM(`t_dest`.`amount`) AS `earned`'), - // ] - // ); - // - // return $collection; - // - // - // } - - // /** - // * @param Category $category - // * @param Carbon|null $start - // * @param Carbon|null $end - // * - // * @return int - // */ - // public function countJournals(Category $category, Carbon $start = null, Carbon $end = null): int - // { - // $query = $category->transactionjournals(); - // if (!is_null($start)) { - // $query->after($start); - // } - // if (!is_null($end)) { - // $query->before($end); - // } - // - // return $query->count(); - // - // } - - // /** - // * This method returns a very special collection for each category: - // * - // * category, year, expense/earned, amount - // * - // * categories can be duplicated. - // * - // * @param Collection $categories - // * @param Collection $accounts - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return Collection - // */ - // public function listMultiYear(Collection $categories, Collection $accounts, Carbon $start, Carbon $end): Collection - // { - // - // $set = $this->user->categories() - // ->leftJoin('category_transaction_journal', 'category_transaction_journal.category_id', '=', 'categories.id') - // ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'category_transaction_journal.transaction_journal_id') - // ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id') - // ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') - // ->whereIn('transaction_types.type', [TransactionType::DEPOSIT, TransactionType::WITHDRAWAL]) - // ->whereIn('transactions.account_id', $accounts->pluck('id')->toArray()) - // ->whereIn('categories.id', $categories->pluck('id')->toArray()) - // ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) - // ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) - // ->groupBy('categories.id') - // ->groupBy('transaction_types.type') - // ->groupBy('dateFormatted') - // ->get( - // [ - // 'categories.*', - // DB::raw('DATE_FORMAT(`transaction_journals`.`date`,"%Y") as `dateFormatted`'), - // 'transaction_types.type', - // DB::raw('SUM(`amount`) as `sum`'), - // ] - // ); - // - // return $set; - // - // } - - // /** - // * Returns a list of transaction journals in the range (all types, all accounts) that have no category - // * associated to them. - // * - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return Collection - // */ - // public function listNoCategory(Carbon $start, Carbon $end): Collection - // { - // return $this->user - // ->transactionjournals() - // ->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - // ->whereNull('category_transaction_journal.id') - // ->before($end) - // ->after($start) - // ->orderBy('transaction_journals.date', 'DESC') - // ->orderBy('transaction_journals.order', 'ASC') - // ->orderBy('transaction_journals.id', 'DESC') - // ->get(['transaction_journals.*']); - // } - - // /** - // * Returns a collection of Categories appended with the amount of money that has been spent - // * in these categories, based on the $accounts involved, in period X, grouped per month. - // * The amount spent in category X in period X is saved in field "spent". - // * - // * @param $accounts - // * @param $start - // * @param $end - // * - // * @return Collection - // */ - // public function spentForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end): Collection - // { - // $accountIds = $accounts->pluck('id')->toArray(); - // $query = $this->user->categories() - // ->leftJoin('category_transaction_journal', 'category_transaction_journal.category_id', '=', 'categories.id') - // ->leftJoin('transaction_journals', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - // ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id') - // ->leftJoin( - // 'transactions AS t_src', function (JoinClause $join) { - // $join->on('t_src.transaction_journal_id', '=', 'transaction_journals.id')->where('t_src.amount', '<', 0); - // } - // ) - // ->leftJoin( - // 'transactions AS t_dest', function (JoinClause $join) { - // $join->on('t_dest.transaction_journal_id', '=', 'transaction_journals.id')->where('t_dest.amount', '>', 0); - // } - // ) - // ->whereIn( - // 'transaction_types.type', [TransactionType::WITHDRAWAL, TransactionType::TRANSFER, TransactionType::OPENING_BALANCE] - // )// spent on these things. - // ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) - // ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) - // ->groupBy('categories.id') - // ->groupBy('dateFormatted'); - // - // if (count($accountIds) > 0) { - // $query->whereIn('t_src.account_id', $accountIds)// from these accounts (spent) - // ->whereNotIn('t_dest.account_id', $accountIds);//-- but not from these accounts (spent internally) - // } - // - // $collection = $query->get( - // [ - // 'categories.*', - // DB::raw('DATE_FORMAT(`transaction_journals`.`date`,"%Y-%m") as `dateFormatted`'), - // DB::raw('SUM(`t_src`.`amount`) AS `spent`'), - // ] - // ); - // - // return $collection; - // } - - // /** - // * Returns the total amount of money related to transactions without any category connected to - // * it. Returns either the earned amount. - // * - // * @param Collection $accounts - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return string - // */ - // public function sumEarnedNoCategory(Collection $accounts, Carbon $start, Carbon $end): string - // { - // return $this->sumNoCategory($accounts, $start, $end, self::EARNED); - // } - - // /** - // * Returns the total amount of money related to transactions without any category connected to - // * it. Returns either the spent amount. - // * - // * @param Collection $accounts - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return string - // */ - // public function sumSpentNoCategory(Collection $accounts, Carbon $start, Carbon $end): string - // { - // $sum = $this->sumNoCategory($accounts, $start, $end, self::SPENT); - // if (is_null($sum)) { - // return '0'; - // } - // - // return $sum; - // } - - // /** - // * Returns the total amount of money related to transactions without any category connected to - // * it. Returns either the earned or the spent amount. - // * - // * @param Collection $accounts - // * @param Carbon $start - // * @param Carbon $end - // * @param int $group - // * - // * @return string - // */ - // protected function sumNoCategory(Collection $accounts, Carbon $start, Carbon $end, $group = self::EARNED) - // { - // $accountIds = $accounts->pluck('id')->toArray(); - // if ($group == self::EARNED) { - // $types = [TransactionType::DEPOSIT]; - // } else { - // $types = [TransactionType::WITHDRAWAL]; - // } - // - // // is withdrawal or transfer AND account_from is in the list of $accounts - // $query = $this->user - // ->transactionjournals() - // ->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - // ->whereNull('category_transaction_journal.id') - // ->before($end) - // ->after($start) - // ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') - // ->having('transaction_count', '=', 1) - // ->transactionTypes($types); - // - // if (count($accountIds) > 0) { - // $query->whereIn('transactions.account_id', $accountIds); - // } - // - // - // $single = $query->first( - // [ - // DB::raw('SUM(`transactions`.`amount`) as `sum`'), - // DB::raw('COUNT(`transactions`.`id`) as `transaction_count`'), - // ] - // ); - // if (!is_null($single)) { - // return $single->sum; - // } - // - // return '0'; - // - // } - /** * @param Category $category * @@ -321,53 +43,6 @@ class CategoryRepository implements CategoryRepositoryInterface return true; } - // /** - // * Returns an array with the following key:value pairs: - // * - // * yyyy-mm-dd: - // * - // * 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 Collection $accounts - // * - // * @return array - // */ - // public function earnedPerDay(Category $category, Carbon $start, Carbon $end, Collection $accounts): array - // { - // /** @var Collection $query */ - // $query = $category->transactionjournals() - // ->expanded() - // ->transactionTypes([TransactionType::DEPOSIT]) - // ->before($end) - // ->after($start) - // ->groupBy('transaction_journals.date'); - // - // $query->leftJoin( - // 'transactions as destination', function (JoinClause $join) { - // $join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0); - // } - // ); - // - // - // 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 ($result->toArray() as $entry) { - // $return[$entry['dateFormatted']] = $entry['sum']; - // } - // - // return $return; - // } - /** * @param Collection $categories * @param Collection $accounts @@ -405,162 +80,6 @@ class CategoryRepository implements CategoryRepositoryInterface return $category; } - // /** - // * @param Category $category - // * - // * @return Carbon - // */ - // public function getFirstActivityDate(Category $category): Carbon - // { - // /** @var TransactionJournal $first */ - // $first = $category->transactionjournals()->orderBy('date', 'ASC')->first(); - // if ($first) { - // return $first->date; - // } - // - // return new Carbon; - // - // } - - // /** - // * @param Category $category - // * @param int $page - // * @param int $pageSize - // * - // * @return Collection - // */ - // public function getJournals(Category $category, int $page, int $pageSize = 50): Collection - // { - // $offset = $page > 0 ? $page * $pageSize : 0; - // - // return $category->transactionjournals()->expanded()->take($pageSize)->offset($offset)->get(TransactionJournal::queryFields()); - // - // } - // - // /** - // * @param Category $category - // * @param Collection $accounts - // * - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return Collection - // */ - // public function getJournalsForAccountsInRange(Category $category, Collection $accounts, Carbon $start, Carbon $end): Collection - // { - // $ids = $accounts->pluck('id')->toArray(); - // - // return $category->transactionjournals() - // ->after($start) - // ->before($end) - // ->expanded() - // ->whereIn('source_account.id', $ids) - // ->whereNotIn('destination_account.id', $ids) - // ->get(TransactionJournal::queryFields()); - // } - - // /** - // * @param Category $category - // * @param Carbon $start - // * @param Carbon $end - // * @param int $page - // * @param int $pageSize - // * - // * - // * @return Collection - // */ - // public function getJournalsInRange(Category $category, Carbon $start, Carbon $end, int $page, int $pageSize = 50): Collection - // { - // $offset = $page > 0 ? $page * $pageSize : 0; - // - // return $category->transactionjournals() - // ->after($start) - // ->before($end) - // ->expanded() - // ->take($pageSize) - // ->offset($offset) - // ->get(TransactionJournal::queryFields()); - // } - - // /** - // * @param Category $category - // * - // * @return Carbon - // */ - // public function getLatestActivity(Category $category): Carbon - // { - // $first = new Carbon('1900-01-01'); - // $second = new Carbon('1900-01-01'); - // $latest = $category->transactionjournals() - // ->orderBy('transaction_journals.date', 'DESC') - // ->orderBy('transaction_journals.order', 'ASC') - // ->orderBy('transaction_journals.id', 'DESC') - // ->first(); - // if ($latest) { - // $first = $latest->date; - // } - // - // // could also be a transaction, nowadays: - // $latestTransaction = $category->transactions() - // ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') - // ->orderBy('transaction_journals.date', 'DESC') - // ->orderBy('transaction_journals.order', 'ASC') - // ->orderBy('transaction_journals.id', 'DESC') - // ->first(['transactions.*', 'transaction_journals.date']); - // if ($latestTransaction) { - // $second = new Carbon($latestTransaction->date); - // } - // if ($first > $second) { - // return $first; - // } - // - // return $second; - // } - - // /** - // * Returns an array with the following key:value pairs: - // * - // * yyyy-mm-dd: - // * - // * 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 Collection $accounts - // * - // * @return array - // */ - // public function spentPerDay(Category $category, Carbon $start, Carbon $end, Collection $accounts): array - // { - // /** @var Collection $query */ - // $query = $category->transactionjournals() - // ->expanded() - // ->transactionTypes([TransactionType::WITHDRAWAL]) - // ->before($end) - // ->after($start) - // ->groupBy('transaction_journals.date'); - // $query->leftJoin( - // 'transactions as source', function (JoinClause $join) { - // $join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0); - // } - // ); - // - // 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(`source`.`amount`) AS `sum`')]); - // - // $return = []; - // foreach ($result->toArray() as $entry) { - // $return[$entry['dateFormatted']] = $entry['sum']; - // } - // - // return $return; - // } /** * @param Category $category @@ -731,20 +250,25 @@ class CategoryRepository implements CategoryRepositoryInterface /** * @param Collection $accounts + * @param array $types * @param Carbon $start * @param Carbon $end * * @return Collection */ - public function journalsInPeriodWithoutCategory(Collection $accounts, Carbon $start, Carbon $end) : Collection + public function journalsInPeriodWithoutCategory(Collection $accounts, array $types, Carbon $start, Carbon $end) : Collection { /** @var Collection $set */ $query = $this->user - ->transactionjournals() - ->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - ->whereNull('category_transaction_journal.id') - ->before($end) - ->after($start); + ->transactionjournals(); + if (count($types) > 0) { + $query->transactionTypes($types); + } + + $query->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') + ->whereNull('category_transaction_journal.id') + ->before($end) + ->after($start); if ($accounts->count() > 0) { $accountIds = $accounts->pluck('id')->toArray(); @@ -777,7 +301,9 @@ class CategoryRepository implements CategoryRepositoryInterface // this second set REALLY doesn't have any categories. $secondSet = $secondQuery->get(['transactions.transaction_journal_id']); $allIds = $secondSet->pluck('transaction_journal_id')->toArray(); - $return = $this->user->transactionjournals()->sortCorrectly()->expanded()->whereIn('transaction_journals.id', $allIds)->get(TransactionJournal::queryFields()); + $return = $this->user->transactionjournals()->sortCorrectly()->expanded()->whereIn('transaction_journals.id', $allIds)->get( + TransactionJournal::queryFields() + ); return $return; @@ -859,7 +385,8 @@ class CategoryRepository implements CategoryRepositoryInterface */ public function spentInPeriodWithoutCategory(Collection $accounts, Carbon $start, Carbon $end) : string { - $journals = $this->journalsInPeriodWithoutCategory($accounts, $start, $end); + $types = [TransactionType::WITHDRAWAL, TransactionType::TRANSFER]; + $journals = $this->journalsInPeriodWithoutCategory($accounts, $types, $start, $end); $sum = '0'; foreach ($journals as $journal) { $sum = bcadd(TransactionJournal::amount($journal), $sum); @@ -900,4 +427,23 @@ class CategoryRepository implements CategoryRepositoryInterface return $category; } + + /** + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return string + */ + public function earnedInPeriodWithoutCategory(Collection $accounts, Carbon $start, Carbon $end) :string + { + $types = [TransactionType::DEPOSIT, TransactionType::TRANSFER]; + $journals = $this->journalsInPeriodWithoutCategory($accounts, $types, $start, $end); + $sum = '0'; + foreach ($journals as $journal) { + $sum = bcadd(TransactionJournal::amount($journal), $sum); + } + + return $sum; + } } diff --git a/app/Repositories/Category/CategoryRepositoryInterface.php b/app/Repositories/Category/CategoryRepositoryInterface.php index b561dd2060..71f5f42998 100644 --- a/app/Repositories/Category/CategoryRepositoryInterface.php +++ b/app/Repositories/Category/CategoryRepositoryInterface.php @@ -16,20 +16,6 @@ use Illuminate\Support\Collection; interface CategoryRepositoryInterface { - - // /** - // * Returns a collection of Categories appended with the amount of money that has been earned - // * in these categories, based on the $accounts involved, in period X, grouped per month. - // * The amount earned in category X in period X is saved in field "earned". - // * - // * @param $accounts - // * @param $start - // * @param $end - // * - // * @return Collection - // */ - // public function earnedForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end): Collection; - /** * @param Category $category * @@ -37,80 +23,6 @@ interface CategoryRepositoryInterface */ public function destroy(Category $category): bool; - // /** - // * This method returns a very special collection for each category: - // * - // * category, year, expense/earned, amount - // * - // * categories can be duplicated. - // * - // * @param Collection $categories - // * @param Collection $accounts - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return Collection - // */ - // public function listMultiYear(Collection $categories, Collection $accounts, Carbon $start, Carbon $end): Collection; - - // /** - // * Returns a list of transaction journals in the range (all types, all accounts) that have no category - // * associated to them. - // * - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return Collection - // */ - // public function listNoCategory(Carbon $start, Carbon $end): Collection; - - // /** - // * Returns a collection of Categories appended with the amount of money that has been spent - // * in these categories, based on the $accounts involved, in period X, grouped per month. - // * The amount earned in category X in period X is saved in field "spent". - // * - // * @param $accounts - // * @param $start - // * @param $end - // * - // * @return Collection - // */ - // public function spentForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end): Collection; - - // /** - // * Returns the total amount of money related to transactions without any category connected to - // * it. Returns either the earned amount. - // * - // * @param Collection $accounts - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return string - // */ - // public function sumEarnedNoCategory(Collection $accounts, Carbon $start, Carbon $end): string; - - // /** - // * Returns the total amount of money related to transactions without any category connected to - // * it. Returns either the spent amount. - // * - // * @param Collection $accounts - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return string - // */ - // public function sumSpentNoCategory(Collection $accounts, Carbon $start, Carbon $end): string; - - - // /** - // * @param Category $category - // * @param Carbon|null $start - // * @param Carbon|null $end - // * - // * @return int - // */ - // public function countJournals(Category $category, Carbon $start = null, Carbon $end = null): int; - /** * @param Collection $categories * @param Collection $accounts @@ -121,22 +33,14 @@ interface CategoryRepositoryInterface */ public function earnedInPeriod(Collection $categories, Collection $accounts, Carbon $start, Carbon $end): string; - // /** - // * Returns an array with the following key:value pairs: - // * - // * yyyy-mm-dd: - // * - // * 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 Collection $accounts - // * - // * @return array - // */ - // public function earnedPerDay(Category $category, Carbon $start, Carbon $end, Collection $accounts): array; + /** + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return string + */ + public function earnedInPeriodWithoutCategory(Collection $accounts, Carbon $start, Carbon $end) :string; /** * Find a category @@ -147,12 +51,6 @@ interface CategoryRepositoryInterface */ public function find(int $categoryId) : Category; - // /** - // * @param Category $category - // * - // * @return Carbon - // */ - /** * @param Category $category * @param Collection $accounts @@ -199,12 +97,13 @@ interface CategoryRepositoryInterface /** * @param Collection $accounts + * @param array $types * @param Carbon $start * @param Carbon $end * * @return Collection */ - public function journalsInPeriodWithoutCategory(Collection $accounts, Carbon $start, Carbon $end) : Collection; + public function journalsInPeriodWithoutCategory(Collection $accounts, array $types, Carbon $start, Carbon $end) : Collection; /** * Return most recent transaction(journal) date. @@ -225,61 +124,6 @@ interface CategoryRepositoryInterface * @return string */ public function spentInPeriod(Collection $categories, Collection $accounts, Carbon $start, Carbon $end): string; - // /** - // * @param Category $category - // * @param int $page - // * @param int $pageSize - // * - // * @return Collection - // */ - // public function getJournals(Category $category, int $page, int $pageSize = 50): Collection; - - // /** - // * @param Category $category - // * @param Collection $accounts - // * - // * @param Carbon $start - // * @param Carbon $end - // * - // * @return Collection - // */ - // public function getJournalsForAccountsInRange(Category $category, Collection $accounts, Carbon $start, Carbon $end): Collection; - - // /** - // * @param Category $category - // * @param Carbon $start - // * @param Carbon $end - // * @param int $page - // * @param int $pageSize - // * - // * - // * @return Collection - // */ - // public function getJournalsInRange(Category $category, Carbon $start, Carbon $end, int $page, int $pageSize = 50): Collection; - - // /** - // * @param Category $category - // * - // * @return Carbon - // */ - // public function getLatestActivity(Category $category): Carbon; - - // /** - // * Returns an array with the following key:value pairs: - // * - // * yyyy-mm-dd: - // * - // * 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 Collection $accounts - // * - // * @return array - // */ - // public function spentPerDay(Category $category, Carbon $start, Carbon $end, Collection $accounts): array; /** * @param array $data diff --git a/public/js/ff/reports/default/year.js b/public/js/ff/reports/default/year.js index 521989587f..22d6797f08 100644 --- a/public/js/ff/reports/default/year.js +++ b/public/js/ff/reports/default/year.js @@ -36,11 +36,6 @@ function drawChart() { columnChart('chart/category/period/' + categoryId + '/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, id); }); - - //stackedColumnChart('chart/budget/year/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'budgets'); - stackedColumnChart('chart/category/spent-in-period/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'categories-spent-in-period'); - stackedColumnChart('chart/category/earned-in-period/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'categories-earned-in-period'); - }