/**
  * @param BudgetRepositoryInterface $repository
  * @param                           $report_type
  * @param Carbon                    $start
  * @param Carbon                    $end
  * @param Collection                $accounts
  * @param Collection                $budgets
  *
  * @return \Illuminate\Http\JsonResponse
  */
 public function multiYear(BudgetRepositoryInterface $repository, $report_type, Carbon $start, Carbon $end, Collection $accounts, Collection $budgets)
 {
     // chart properties for cache:
     $cache = new CacheProperties();
     $cache->addProperty($report_type);
     $cache->addProperty($start);
     $cache->addProperty($end);
     $cache->addProperty($accounts);
     $cache->addProperty($budgets);
     $cache->addProperty('multiYearBudget');
     if ($cache->has()) {
         return Response::json($cache->get());
         // @codeCoverageIgnore
     }
     /**
      *  budget
      *   year:
      *    spent: x
      *    budgeted: x
      *   year
      *    spent: x
      *    budgeted: x
      */
     $entries = new Collection();
     // go by budget, not by year.
     foreach ($budgets as $budget) {
         $entry = ['name' => '', 'spent' => [], 'budgeted' => []];
         $currentStart = clone $start;
         while ($currentStart < $end) {
             // fix the date:
             $currentEnd = clone $currentStart;
             $currentEnd->endOfYear();
             // get data:
             if (is_null($budget->id)) {
                 $name = trans('firefly.noBudget');
                 $sum = $repository->getWithoutBudgetSum($currentStart, $currentEnd);
                 $budgeted = 0;
             } else {
                 $name = $budget->name;
                 $sum = $repository->balanceInPeriod($budget, $currentStart, $currentEnd, $accounts);
                 $budgeted = $repository->getBudgetLimitRepetitions($budget, $currentStart, $currentEnd)->sum('amount');
             }
             // save to array:
             $year = $currentStart->year;
             $entry['name'] = $name;
             $entry['spent'][$year] = $sum * -1;
             $entry['budgeted'][$year] = $budgeted;
             // jump to next year.
             $currentStart = clone $currentEnd;
             $currentStart->addDay();
         }
         $entries->push($entry);
     }
     // generate chart with data:
     $data = $this->generator->multiYear($entries);
     return Response::json($data);
 }
Example #2
0
 /**
  * Shows a budget list with spent/left/overspent.
  *
  * @param BudgetRepositoryInterface $repository
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function frontpage(BudgetRepositoryInterface $repository)
 {
     $budgets = $repository->getBudgets();
     $start = Session::get('start', Carbon::now()->startOfMonth());
     $end = Session::get('end', Carbon::now()->endOfMonth());
     $allEntries = new Collection();
     // chart properties for cache:
     $cache = new CacheProperties();
     $cache->addProperty($start);
     $cache->addProperty($end);
     $cache->addProperty('budget');
     $cache->addProperty('all');
     if ($cache->has()) {
         return Response::json($cache->get());
         // @codeCoverageIgnore
     }
     bcscale(2);
     /** @var Budget $budget */
     foreach ($budgets as $budget) {
         $repetitions = $repository->getBudgetLimitRepetitions($budget, $start, $end);
         if ($repetitions->count() == 0) {
             $expenses = $repository->spentInPeriodCorrected($budget, $start, $end, true);
             $allEntries->push([$budget->name, 0, 0, $expenses, 0, 0]);
             continue;
         }
         /** @var LimitRepetition $repetition */
         foreach ($repetitions as $repetition) {
             $expenses = $repository->spentInPeriodCorrected($budget, $repetition->startdate, $repetition->enddate, true);
             // $left can be less than zero.
             // $overspent can be more than zero ( = overspending)
             $left = max(bcsub($repetition->amount, $expenses), 0);
             // limited at zero.
             $overspent = max(bcsub($expenses, $repetition->amount), 0);
             // limited at zero.
             $name = $budget->name;
             // $spent is maxed to the repetition amount:
             $spent = $expenses > $repetition->amount ? $repetition->amount : $expenses;
             $allEntries->push([$name, $left, $spent, $overspent, $repetition->amount, $expenses]);
         }
     }
     $noBudgetExpenses = $repository->getWithoutBudgetSum($start, $end) * -1;
     $allEntries->push([trans('firefly.noBudget'), 0, 0, $noBudgetExpenses, 0, 0]);
     $data = $this->generator->frontpage($allEntries);
     $cache->store($data);
     return Response::json($data);
 }