get('attributes'); $attributes = $this->parseAttributes($attributes); $html = ''; switch ($attributes['location']) { default: throw new FireflyException('Firefly cannot handle "' . e($attributes['location']) . '" '); case 'budget-spent-amount': $html = $this->budgetSpentAmount($attributes); break; } return Response::json(['html' => $html]); } /** * @param array $attributes * * @return string * @throws FireflyException */ private function budgetSpentAmount(array $attributes): string { // need to find the budget // then search for expenses in the given period // list them in some table format. /** @var BudgetRepositoryInterface $repository */ $repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); $budget = $repository->find(intval($attributes['budgetId'])); if (is_null($budget->id)) { $journals = $repository->getWithoutBudgetForAccounts($attributes['accounts'], $attributes['startDate'], $attributes['endDate']); } else { // get all expenses in budget in period: $journals = $repository->getExpenses($budget, $attributes['accounts'], $attributes['startDate'], $attributes['endDate']); } $view = view('popup.report.budget-spent-amount', compact('journals'))->render(); return $view; } /** * @param array $attributes * * @return array * @throws FireflyException */ private function parseAttributes(array $attributes): array { $attributes['location'] = $attributes['location'] ?? ''; $attributes['accounts'] = AccountList::routeBinder($attributes['accounts'] ?? '', ''); try { $attributes['startDate'] = Carbon::createFromFormat('Ymd', $attributes['startDate']); } catch (InvalidArgumentException $e) { throw new FireflyException('Could not parse start date "' . e($attributes['startDate']) . '".'); } try { $attributes['endDate'] = Carbon::createFromFormat('Ymd', $attributes['endDate']); } catch (InvalidArgumentException $e) { throw new FireflyException('Could not parse start date "' . e($attributes['endDate']) . '".'); } return $attributes; } }