Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Available budget end point also gives info on spent info #1884
James Cole91593335effb
diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.phpindex 497986a41f..a4c3b4bb31 100644--- a/app/Repositories/Budget/BudgetRepository.php+++ b/app/Repositories/Budget/BudgetRepository.php@@ -793,6 +793,54 @@ class BudgetRepository implements BudgetRepositoryInterfacereturn (string)$set->sum('transaction_amount');}+ /**+ * @param Collection $accounts+ * @param Carbon $start+ * @param Carbon $end+ *+ * @return array+ */+ public function spentInPeriodWoBudgetMc(Collection $accounts, Carbon $start, Carbon $end): array+ {+ /** @var TransactionCollectorInterface $collector */+ $collector = app(TransactionCollectorInterface::class);+ $collector->setUser($this->user);+ $collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->withoutBudget();++ if ($accounts->count() > 0) {+ $collector->setAccounts($accounts);+ }+ if (0 === $accounts->count()) {+ $collector->setAllAssetAccounts();+ }++ $set = $collector->getTransactions();+ $return = [];+ $total = [];+ $currencies = [];+ /** @var Transaction $transaction */+ foreach ($set as $transaction) {+ $code = $transaction->transaction_currency_code;+ if (!isset($currencies[$code])) {+ $currencies[$code] = $transaction->transactionCurrency;+ }+ $total[$code] = isset($total[$code]) ? bcadd($total[$code], $transaction->transaction_amount) : $transaction->transaction_amount;+ }+ foreach ($total as $code => $spent) {+ /** @var TransactionCurrency $currency */+ $currency = $currencies[$code];+ $return[] = [+ 'currency_id' => $currency->id,+ 'currency_code' => $code,+ 'currency_symbol' => $currency->symbol,+ 'currency_decimal_places' => $currency->decimal_places,+ 'amount' => round($spent, $currency->decimal_places),+ ];+ }++ return $return;+ }+/*** @param array $data*diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.phpindex 9cb9fbc4e3..627687d6e5 100644--- a/app/Repositories/Budget/BudgetRepositoryInterface.php+++ b/app/Repositories/Budget/BudgetRepositoryInterface.php@@ -251,6 +251,16 @@ interface BudgetRepositoryInterface*/public function spentInPeriodWoBudget(Collection $accounts, Carbon $start, Carbon $end): string;+ /**+ * @param Collection $accounts+ * @param Carbon $start+ * @param Carbon $end+ *+ * @return array+ */+ public function spentInPeriodWoBudgetMc(Collection $accounts, Carbon $start, Carbon $end): array;++/*** @param array $data*diff --git a/app/Transformers/AvailableBudgetTransformer.php b/app/Transformers/AvailableBudgetTransformer.phpindex 193ea716f4..845eba5f91 100644--- a/app/Transformers/AvailableBudgetTransformer.php+++ b/app/Transformers/AvailableBudgetTransformer.php@@ -25,6 +25,8 @@ namespace FireflyIII\Transformers;use FireflyIII\Models\AvailableBudget;+use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;+use Illuminate\Support\Collection;use Log;/**@@ -32,6 +34,9 @@ use Log;*/class AvailableBudgetTransformer extends AbstractTransformer{+ /** @var BudgetRepositoryInterface */+ private $repository;+/*** CurrencyTransformer constructor.*@@ -39,6 +44,7 @@ class AvailableBudgetTransformer extends AbstractTransformer*/public function __construct(){+ $this->repository = app(BudgetRepositoryInterface::class);if ('testing' === config('app.env')) {Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));}@@ -53,6 +59,8 @@ class AvailableBudgetTransformer extends AbstractTransformer*/public function transform(AvailableBudget $availableBudget): array{+ $this->repository->setUser($availableBudget->user);+$currency = $availableBudget->transactionCurrency;$data = ['id' => (int)$availableBudget->id,@@ -65,16 +73,43 @@ class AvailableBudgetTransformer extends AbstractTransformer'amount' => round($availableBudget->amount, $currency->decimal_places),'start' => $availableBudget->start_date->format('Y-m-d'),'end' => $availableBudget->end_date->format('Y-m-d'),-- 'links' => [+ 'spent_in_budgets' => [],+ 'spent_no_budget' => [],+ 'links' => [['rel' => 'self','uri' => '/available_budgets/' . $availableBudget->id,],],];+ $start = $this->parameters->get('start');+ $end = $this->parameters->get('end');+ if (null !== $start && null !== $end) {+ $data['spent_in_budgets'] = $this->getSpentInBudgets();+ $data['spent_no_budget'] = $this->spentOutsideBudgets();+ }return $data;}+ /**+ * @return array+ */+ private function getSpentInBudgets(): array+ {+ $allActive = $this->repository->getActiveBudgets();+ return $this->repository->spentInPeriodMc(+ $allActive, new Collection, $this->parameters->get('start'), $this->parameters->get('end')+ );++ }++ /**+ * @return array+ */+ private function spentOutsideBudgets(): array+ {+ return $this->repository->spentInPeriodWoBudgetMc(new Collection,$this->parameters->get('start'), $this->parameters->get('end'));+ }+}