/** * Summarizes all income and expenses for a given year. Gives a total and an average. * * @param ReportQueryInterface $query * @param $year * @param bool $shared * * @return \Symfony\Component\HttpFoundation\Response */ public function yearInOutSummarized(ReportQueryInterface $query, $year, $shared = false) { // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty('yearInOutSummarized'); $cache->addProperty($year); $cache->addProperty($shared); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } $start = new Carbon($year . '-01-01'); $end = new Carbon($year . '-12-31'); $shared = $shared == 'shared' ? true : false; $income = '0'; $expense = '0'; $count = 0; bcscale(2); while ($start < $end) { $month = clone $start; $month->endOfMonth(); // total income and total expenses: $currentIncome = $query->incomeInPeriodCorrected($start, $month, $shared)->sum('amount_positive'); $currentExpense = $query->expenseInPeriodCorrected($start, $month, $shared)->sum('amount_positive'); Log::debug('Date [' . $month->format('M Y') . ']: income = [' . $income . ' + ' . $currentIncome . '], out = [' . $expense . ' + ' . $currentExpense . ']'); $income = bcadd($income, $currentIncome); $expense = bcadd($expense, $currentExpense); $count++; $start->addMonth(); } $data = $this->generator->yearInOutSummarized($income, $expense, $count); $cache->store($data); return Response::json($data); }
/** * Summarizes all income and expenses for a given year. Gives a total and an average. * * @param ReportQueryInterface $query * @param $report_type * @param Carbon $start * @param Carbon $end * @param Collection $accounts * * @return \Illuminate\Http\JsonResponse */ public function yearInOutSummarized(ReportQueryInterface $query, $report_type, Carbon $start, Carbon $end, Collection $accounts) { // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty('yearInOutSummarized'); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty($accounts); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } $income = '0'; $expense = '0'; $count = 0; bcscale(2); if ($start->diffInMonths($end) > 12) { // per year while ($start < $end) { $startOfYear = clone $start; $startOfYear->startOfYear(); $endOfYear = clone $startOfYear; $endOfYear->endOfYear(); // total income and total expenses: $currentIncome = $query->incomeInPeriod($startOfYear, $endOfYear, $accounts)->sum('amount_positive'); $currentExpense = $query->expenseInPeriod($startOfYear, $endOfYear, $accounts)->sum('amount_positive'); $income = bcadd($income, $currentIncome); $expense = bcadd($expense, $currentExpense); $count++; $start->addYear(); } $data = $this->generator->multiYearInOutSummarized($income, $expense, $count); $cache->store($data); } else { // per month! while ($start < $end) { $month = clone $start; $month->endOfMonth(); // total income and total expenses: $currentIncome = $query->incomeInPeriod($start, $month, $accounts)->sum('amount_positive'); $currentExpense = $query->expenseInPeriod($start, $month, $accounts)->sum('amount_positive'); $income = bcadd($income, $currentIncome); $expense = bcadd($expense, $currentExpense); $count++; $start->addMonth(); } $data = $this->generator->yearInOutSummarized($income, $expense, $count); $cache->store($data); } return Response::json($data); }
/** * @param array $earned * @param array $spent * @param Carbon $start * @param Carbon $end * * @return array */ protected function singleYearInOut(array $earned, array $spent, Carbon $start, Carbon $end) { // per month? simply use each month. $entries = new Collection(); while ($start < $end) { // total income and total expenses: $date = $start->format('Y-m'); $incomeSum = isset($earned[$date]) ? $earned[$date] : 0; $expenseSum = isset($spent[$date]) ? $spent[$date] * -1 : 0; $entries->push([clone $start, $incomeSum, $expenseSum]); $start->addMonth(); } $data = $this->generator->yearInOut($entries); return $data; }