/** * Summarizes all income and expenses, per month, for a given year. * * @param ReportQueryInterface $query * @param $report_type * @param Carbon $start * @param Carbon $end * @param Collection $accounts * * @return \Illuminate\Http\JsonResponse */ public function yearInOut(ReportQueryInterface $query, $report_type, Carbon $start, Carbon $end, Collection $accounts) { // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty('yearInOut'); $cache->addProperty($start); $cache->addProperty($accounts); $cache->addProperty($end); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } // per year? if ($start->diffInMonths($end) > 12) { $entries = new Collection(); while ($start < $end) { $startOfYear = clone $start; $startOfYear->startOfYear(); $endOfYear = clone $startOfYear; $endOfYear->endOfYear(); // total income and total expenses: $incomeSum = $query->incomeInPeriod($startOfYear, $endOfYear, $accounts)->sum('amount_positive'); $expenseSum = $query->expenseInPeriod($startOfYear, $endOfYear, $accounts)->sum('amount_positive'); $entries->push([clone $start, $incomeSum, $expenseSum]); $start->addYear(); } $data = $this->generator->multiYearInOut($entries); $cache->store($data); } else { // per month: $entries = new Collection(); while ($start < $end) { $month = clone $start; $month->endOfMonth(); // total income and total expenses: $incomeSum = $query->incomeInPeriod($start, $month, $accounts)->sum('amount_positive'); $expenseSum = $query->expenseInPeriod($start, $month, $accounts)->sum('amount_positive'); $entries->push([clone $start, $incomeSum, $expenseSum]); $start->addMonth(); } $data = $this->generator->yearInOut($entries); $cache->store($data); } return Response::json($data); }
/** * @param array $earned * @param array $spent * @param Carbon $start * @param Carbon $end * * @return array */ protected function multiYearInOut(array $earned, array $spent, Carbon $start, Carbon $end) { $entries = new Collection(); while ($start < $end) { $incomeSum = $this->pluckFromArray($start->year, $earned); $expenseSum = $this->pluckFromArray($start->year, $spent) * -1; $entries->push([clone $start, $incomeSum, $expenseSum]); $start->addYear(); } $data = $this->generator->multiYearInOut($entries); return $data; }