/**
  * @param CategoryRepositoryInterface $repository
  * @param                             $report_type
  * @param Carbon                      $start
  * @param Carbon                      $end
  * @param Collection                  $accounts
  * @param Collection                  $categories
  *
  * @return \Illuminate\Http\JsonResponse
  */
 public function multiYear(CategoryRepositoryInterface $repository, $report_type, Carbon $start, Carbon $end, Collection $accounts, Collection $categories)
 {
     // chart properties for cache:
     $cache = new CacheProperties();
     $cache->addProperty($report_type);
     $cache->addProperty($start);
     $cache->addProperty($end);
     $cache->addProperty($accounts);
     $cache->addProperty($categories);
     $cache->addProperty('multiYearCategory');
     if ($cache->has()) {
         return Response::json($cache->get());
         // @codeCoverageIgnore
     }
     /**
      *  category
      *   year:
      *    spent: x
      *    earned: x
      *   year
      *    spent: x
      *    earned: x
      */
     $entries = new Collection();
     // go by budget, not by year.
     /** @var Category $category */
     foreach ($categories as $category) {
         $entry = ['name' => '', 'spent' => [], 'earned' => []];
         $currentStart = clone $start;
         while ($currentStart < $end) {
             // fix the date:
             $currentEnd = clone $currentStart;
             $currentEnd->endOfYear();
             // get data:
             if (is_null($category->id)) {
                 $name = trans('firefly.noCategory');
                 $spent = $repository->spentNoCategoryForAccounts($accounts, $currentStart, $currentEnd);
                 $earned = $repository->earnedNoCategoryForAccounts($accounts, $currentStart, $currentEnd);
             } else {
                 $name = $category->name;
                 $spent = $repository->spentInPeriodForAccounts($category, $accounts, $currentStart, $currentEnd);
                 $earned = $repository->earnedInPeriodForAccounts($category, $accounts, $currentStart, $currentEnd);
             }
             // save to array:
             $year = $currentStart->year;
             $entry['name'] = $name;
             $entry['spent'][$year] = $spent * -1;
             $entry['earned'][$year] = $earned;
             // jump to next year.
             $currentStart = clone $currentEnd;
             $currentStart->addDay();
         }
         $entries->push($entry);
     }
     // generate chart with data:
     $data = $this->generator->multiYear($entries);
     $cache->store($data);
     return Response::json($data);
 }
 /**
  * @param                             $reportType
  * @param Carbon                      $start
  * @param Carbon                      $end
  * @param Collection                  $accounts
  * @param Collection                  $categories
  *
  * @return \Illuminate\Http\JsonResponse
  */
 public function multiYear($reportType, Carbon $start, Carbon $end, Collection $accounts, Collection $categories)
 {
     /** @var CRI $repository */
     $repository = app('FireflyIII\\Repositories\\Category\\CategoryRepositoryInterface');
     // chart properties for cache:
     $cache = new CacheProperties();
     $cache->addProperty($reportType);
     $cache->addProperty($start);
     $cache->addProperty($end);
     $cache->addProperty($accounts);
     $cache->addProperty($categories);
     $cache->addProperty('multiYearCategory');
     if ($cache->has()) {
         return Response::json($cache->get());
         // @codeCoverageIgnore
     }
     /**
      *  category
      *   year:
      *    spent: x
      *    earned: x
      *   year
      *    spent: x
      *    earned: x
      */
     $entries = new Collection();
     // go by category, not by year.
     // given a set of categories and accounts, it should not be difficult to get
     // the exact array of data we need.
     // then get the data for "no category".
     $set = $repository->listMultiYear($categories, $accounts, $start, $end);
     /** @var Category $category */
     foreach ($categories as $category) {
         $entry = ['name' => '', 'spent' => [], 'earned' => []];
         $currentStart = clone $start;
         while ($currentStart < $end) {
             // fix the date:
             $year = $currentStart->year;
             $currentEnd = clone $currentStart;
             $currentEnd->endOfYear();
             // get data:
             if (is_null($category->id)) {
                 $name = trans('firefly.noCategory');
                 $spent = $repository->sumSpentNoCategory($accounts, $currentStart, $currentEnd);
                 $earned = $repository->sumEarnedNoCategory($accounts, $currentStart, $currentEnd);
             } else {
                 // get from set:
                 $entrySpent = $set->filter(function (Category $cat) use($year, $category) {
                     return $cat->type == 'Withdrawal' && $cat->dateFormatted == $year && $cat->id == $category->id;
                 })->first();
                 $entryEarned = $set->filter(function (Category $cat) use($year, $category) {
                     return $cat->type == 'Deposit' && $cat->dateFormatted == $year && $cat->id == $category->id;
                 })->first();
                 $name = $category->name;
                 $spent = !is_null($entrySpent) ? $entrySpent->sum : 0;
                 $earned = !is_null($entryEarned) ? $entryEarned->sum : 0;
             }
             // save to array:
             $entry['name'] = $name;
             $entry['spent'][$year] = $spent * -1;
             $entry['earned'][$year] = $earned;
             // jump to next year.
             $currentStart = clone $currentEnd;
             $currentStart->addDay();
         }
         $entries->push($entry);
     }
     // generate chart with data:
     $data = $this->generator->multiYear($entries);
     $cache->store($data);
     return Response::json($data);
 }