. */ declare(strict_types=1); namespace FireflyIII\Repositories\Administration\Budget; use Carbon\Carbon; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Models\TransactionType; use FireflyIII\Support\Repositories\Administration\AdministrationTrait; use Illuminate\Support\Collection; /** * Class OperationsRepository */ class OperationsRepository implements OperationsRepositoryInterface { use AdministrationTrait; /** * @inheritDoc * @throws FireflyException */ public function listExpenses(Carbon $start, Carbon $end, ?Collection $accounts = null, ?Collection $budgets = null): array { /** @var GroupCollectorInterface $collector */ $collector = app(GroupCollectorInterface::class); $collector->setUserGroup($this->userGroup)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL]); if (null !== $accounts && $accounts->count() > 0) { $collector->setAccounts($accounts); } if (null !== $budgets && $budgets->count() > 0) { $collector->setBudgets($budgets); } if (null === $budgets || (0 === $budgets->count())) { $collector->setBudgets($this->getBudgets()); } $collector->withBudgetInformation()->withAccountInformation()->withCategoryInformation(); $journals = $collector->getExtractedJournals(); $array = []; foreach ($journals as $journal) { $currencyId = (int)$journal['currency_id']; $budgetId = (int)$journal['budget_id']; $budgetName = (string)$journal['budget_name']; // catch "no budget" entries. if (0 === $budgetId) { continue; } // info about the currency: $array[$currencyId] = $array[$currencyId] ?? [ 'budgets' => [], 'currency_id' => $currencyId, 'currency_name' => $journal['currency_name'], 'currency_symbol' => $journal['currency_symbol'], 'currency_code' => $journal['currency_code'], 'currency_decimal_places' => $journal['currency_decimal_places'], ]; // info about the budgets: $array[$currencyId]['budgets'][$budgetId] = $array[$currencyId]['budgets'][$budgetId] ?? [ 'id' => $budgetId, 'name' => $budgetName, 'transaction_journals' => [], ]; // add journal to array: // only a subset of the fields. $journalId = (int)$journal['transaction_journal_id']; $final = [ 'amount' => app('steam')->negative($journal['amount']), 'currency_id' => $journal['currency_id'], 'foreign_amount' => null, 'foreign_currency_id' => null, 'foreign_currency_code' => null, 'foreign_currency_symbol' => null, 'foreign_currency_name' => null, 'foreign_currency_decimal_places' => null, 'destination_account_id' => $journal['destination_account_id'], 'destination_account_name' => $journal['destination_account_name'], 'source_account_id' => $journal['source_account_id'], 'source_account_name' => $journal['source_account_name'], 'category_name' => $journal['category_name'], 'description' => $journal['description'], 'transaction_group_id' => $journal['transaction_group_id'], 'date' => $journal['date'], ]; if (null !== $journal['foreign_amount']) { $final['foreign_amount'] = app('steam')->negative($journal['foreign_amount']); $final['foreign_currency_id'] = $journal['foreign_currency_id']; $final['foreign_currency_code'] = $journal['foreign_currency_code']; $final['foreign_currency_symbol'] = $journal['foreign_currency_symbol']; $final['foreign_currency_name'] = $journal['foreign_currency_name']; $final['foreign_currency_decimal_places'] = $journal['foreign_currency_decimal_places']; } $array[$currencyId]['budgets'][$budgetId]['transaction_journals'][$journalId] = $final; } return $array; } /** * @return Collection * @throws FireflyException */ private function getBudgets(): Collection { /** @var BudgetRepositoryInterface $repos */ $repos = app(BudgetRepositoryInterface::class); $repos->setAdministrationId($this->getAdministrationId()); return $repos->getActiveBudgets(); } }