/** * This chart will only show income. * * @param CategoryRepositoryInterface $repository * @param $year * @param bool $shared * * @return \Symfony\Component\HttpFoundation\Response */ public function earnedInYear(CategoryRepositoryInterface $repository, $year, $shared = false) { $start = new Carbon($year . '-01-01'); $end = new Carbon($year . '-12-31'); $cache = new CacheProperties(); // chart properties for cache: $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('category'); $cache->addProperty('earned-in-year'); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } $shared = $shared == 'shared' ? true : false; $allCategories = $repository->getCategories(); $allEntries = new Collection(); $categories = $allCategories->filter(function (Category $category) use($repository, $start, $end, $shared) { $spent = $repository->balanceInPeriod($category, $start, $end, $shared); if ($spent > 0) { return $category; } return null; }); while ($start < $end) { $month = clone $start; // month is the current end of the period $month->endOfMonth(); $row = [clone $start]; // make a row: foreach ($categories as $category) { // each budget, fill the row $spent = $repository->balanceInPeriod($category, $start, $month, $shared); if ($spent > 0) { $row[] = $spent; } else { $row[] = 0; } } $allEntries->push($row); $start->addMonth(); } $data = $this->generator->earnedInYear($categories, $allEntries); $cache->store($data); return Response::json($data); }
/** * Returns a chart of what has been spent in this period in each category * grouped by month. * * @param CategoryRepositoryInterface $repository * @param $report_type * @param Carbon $start * @param Carbon $end * @param Collection $accounts * * @return \Illuminate\Http\JsonResponse */ public function spentInPeriod(CategoryRepositoryInterface $repository, $report_type, Carbon $start, Carbon $end, Collection $accounts) { $original = clone $start; $cache = new CacheProperties(); // chart properties for cache: $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty($report_type); $cache->addProperty($accounts); $cache->addProperty('category'); $cache->addProperty('spent-in-period'); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } $categories = new Collection(); $sets = new Collection(); $entries = new Collection(); // run a very special query each month: $start = clone $original; while ($start < $end) { $currentEnd = clone $start; $currentStart = clone $start; $currentStart->startOfMonth(); $currentEnd->endOfMonth(); $set = $repository->spentForAccounts($accounts, $currentStart, $currentEnd); $categories = $categories->merge($set); $sets->push([$currentStart, $set]); $start->addMonth(); } $categories = $categories->unique('id'); $categories = $categories->sortBy(function (Category $category) { return $category->name; }); $start = clone $original; while ($start < $end) { $currentEnd = clone $start; $currentStart = clone $start; $currentStart->startOfMonth(); $currentEnd->endOfMonth(); $currentSet = $sets->first(function ($key, $value) use($currentStart) { // set for this date. return $value[0] == $currentStart; }); $row = [clone $currentStart]; /** @var Category $category */ foreach ($categories as $category) { /** @var Category $entry */ $entry = $currentSet[1]->first(function ($key, $value) use($category) { return $value->id == $category->id; }); if (!is_null($entry)) { $row[] = $entry->spent; } else { $row[] = 0; } } $entries->push($row); $start->addMonth(); } $data = $this->generator->spentInPeriod($categories, $entries); $cache->store($data); return $data; }
/** * @param CategoryFormRequest $request * @param CategoryRepositoryInterface $repository * @param Category $category * * @return \Illuminate\Http\RedirectResponse */ public function update(CategoryFormRequest $request, CategoryRepositoryInterface $repository, Category $category) { $categoryData = ['name' => $request->input('name')]; $repository->update($category, $categoryData); Session::flash('success', 'Category "' . $category->name . '" updated.'); Preferences::mark(); if (intval(Input::get('return_to_edit')) === 1) { Session::put('categories.edit.fromUpdate', true); return Redirect::route('categories.edit', [$category->id]); } // redirect to previous URL. return Redirect::to(Session::get('categories.edit.url')); }
/** * 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()); // @codeCoverageIgnore } $set = $repository->spentForAccountsPerMonth($accounts, $start, $end); $categories = $set->unique('id')->sortBy(function (Category $category) { return $category->name; }); $entries = new Collection(); while ($start < $end) { // filter the set: $row = [clone $start]; // get possibly relevant entries from the big $set $currentSet = $set->filter(function (Category $category) use($start) { return $category->dateFormatted == $start->format("Y-m"); }); // check for each category if its in the current set. /** @var Category $category */ foreach ($categories as $category) { // if its in there, use the value. $entry = $currentSet->filter(function (Category $cat) use($category) { return $cat->id == $category->id; })->first(); if (!is_null($entry)) { $row[] = round($entry->spent * -1, 2); } else { $row[] = 0; } } $entries->push($row); $start->addMonth(); } $data = $this->generator->spentInPeriod($categories, $entries); $cache->store($data); return $data; }
/** * @param CRI $repository * * @return \Illuminate\View\View */ public function noCategory(CRI $repository) { $start = Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->startOfMonth()); $list = $repository->listNoCategory($start, $end); $subTitle = trans('firefly.without_category_between', ['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]); return view('categories.noCategory', compact('list', 'subTitle')); }
/** * @param CRI $repository * @param Category $category * @param Carbon $start * @param Carbon $end * * @return array */ private function makePeriodChart(CRI $repository, Category $category, Carbon $start, Carbon $end) { $categoryCollection = new Collection([$category]); $cache = new CacheProperties(); /** @var AccountCrudInterface $crud */ $crud = app(AccountCrudInterface::class); $accounts = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty($accounts); $cache->addProperty($category->id); $cache->addProperty('specific-period'); if ($cache->has()) { return $cache->get(); } $entries = new Collection(); while ($start <= $end) { $spent = $repository->spentInPeriod($categoryCollection, $accounts, $start, $start); $earned = $repository->earnedInPeriod($categoryCollection, $accounts, $start, $start); $date = Navigation::periodShow($start, '1D'); $entries->push([clone $start, $date, $spent, $earned]); $start->addDay(); } $data = $this->generator->period($entries); $cache->store($data); return $data; }
/** * Returns a list of categories. * * @param CategoryRepositoryInterface $repository * * @return \Illuminate\Http\JsonResponse */ public function categories(CategoryRepositoryInterface $repository) { $list = $repository->getCategories(); $return = []; foreach ($list as $entry) { $return[] = $entry->name; } sort($return); return Response::json($return); }
/** * @param CategoryFormRequest $request * @param CRI $repository * @param Category $category * * @return \Illuminate\Http\RedirectResponse */ public function update(CategoryFormRequest $request, CRI $repository, Category $category) { $categoryData = ['name' => $request->input('name')]; $repository->update($category, $categoryData); Session::flash('success', strval(trans('firefly.updated_category', ['name' => e($category->name)]))); Preferences::mark(); if (intval(Input::get('return_to_edit')) === 1) { Session::put('categories.edit.fromUpdate', true); return redirect(route('categories.edit', [$category->id])); } // redirect to previous URL. return redirect(session('categories.edit.url')); }
/** * Returns a list of categories. * * @param CRI $repository * * @return \Illuminate\Http\JsonResponse */ public function categories(CRI $repository) { $list = $repository->getCategories(); $return = []; foreach ($list as $entry) { $return[] = $entry->name; } return Response::json($return); }
/** * This chart will only show expenses. * * @param CategoryRepositoryInterface $repository * @param $year * @param bool $shared * * @return \Symfony\Component\HttpFoundation\Response */ public function year(CategoryRepositoryInterface $repository, $year, $shared = false) { $start = new Carbon($year . '-01-01'); $end = new Carbon($year . '-12-31'); // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('category'); $cache->addProperty('year'); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } $shared = $shared == 'shared' ? true : false; $categories = $repository->getCategories(); $entries = new Collection(); while ($start < $end) { // month is the current end of the period: $month = clone $start; $month->endOfMonth(); // make a row: $row = [clone $start]; // each budget, fill the row: foreach ($categories as $category) { $spent = $repository->spentInPeriodCorrected($category, $start, $month, $shared); $row[] = $spent; } $entries->push($row); $start->addMonth(); } $data = $this->generator->year($categories, $entries); $cache->store($data); return Response::json($data); }