/** * @param AccountRepositoryInterface $repository * @param string $reportType * @param Carbon $start * @param Carbon $end * @param Collection $accounts * * @return \Illuminate\Http\JsonResponse */ public function yearInOutSummarized(AccountRepositoryInterface $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts) { // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty('yearInOutSummarized'); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty($reportType); $cache->addProperty($accounts); if ($cache->has()) { return Response::json($cache->get()); } // always per month. $currentStart = clone $start; $spentArray = []; $earnedArray = []; while ($currentStart <= $end) { $currentEnd = Navigation::endOfPeriod($currentStart, '1M'); $date = $currentStart->format('Y-m'); $spent = $repository->spentInPeriod($accounts, $currentStart, $currentEnd); $earned = $repository->earnedInPeriod($accounts, $currentStart, $currentEnd); $spentArray[$date] = bcmul($spent, '-1'); $earnedArray[$date] = $earned; $currentStart = Navigation::addPeriod($currentStart, '1M', 0); } if ($start->diffInMonths($end) > 12) { // per year $data = $this->multiYearInOutSummarized($earnedArray, $spentArray, $start, $end); $cache->store($data); return Response::json($data); } // per month! $data = $this->singleYearInOutSummarized($earnedArray, $spentArray, $start, $end); $cache->store($data); return Response::json($data); }
/** * @param ARI $accountRepository * @param AccountCrudInterface $crud * * @return \Symfony\Component\HttpFoundation\Response */ public function boxOut(ARI $accountRepository, AccountCrudInterface $crud) { $start = session('start', Carbon::now()->startOfMonth()); $end = session('end', Carbon::now()->endOfMonth()); // works for json too! $cache = new CacheProperties(); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('box-out'); if ($cache->has()) { return Response::json($cache->get()); } $accounts = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET, AccountType::CASH]); $amount = $accountRepository->spentInPeriod($accounts, $start, $end); $data = ['box' => 'out', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]; $cache->store($data); return Response::json($data); }