. */ declare(strict_types=1); namespace FireflyIII\Api\V2\Controllers\Summary; use Carbon\Carbon; use Exception; use FireflyIII\Api\V2\Controllers\Controller; use FireflyIII\Api\V2\Request\Generic\DateRequest; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Helpers\Report\NetWorthInterface; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionType; use FireflyIII\Models\UserGroup; use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface; use FireflyIII\Repositories\UserGroups\Bill\BillRepositoryInterface; use FireflyIII\Repositories\UserGroups\Budget\AvailableBudgetRepositoryInterface; use FireflyIII\Repositories\UserGroups\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\UserGroups\Budget\OperationsRepositoryInterface; use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface; use FireflyIII\Support\Http\Api\ExchangeRateConverter; use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait; use FireflyIII\User; use Illuminate\Http\JsonResponse; /** * Class BasicController */ class BasicController extends Controller { use ValidatesUserGroupTrait; private AvailableBudgetRepositoryInterface $abRepository; private AccountRepositoryInterface $accountRepository; private BillRepositoryInterface $billRepository; private BudgetRepositoryInterface $budgetRepository; private CurrencyRepositoryInterface $currencyRepos; private OperationsRepositoryInterface $opsRepository; /** * BasicController constructor. * */ public function __construct() { parent::__construct(); $this->middleware( function ($request, $next) { $this->abRepository = app(AvailableBudgetRepositoryInterface::class); $this->accountRepository = app(AccountRepositoryInterface::class); $this->billRepository = app(BillRepositoryInterface::class); $this->budgetRepository = app(BudgetRepositoryInterface::class); $this->currencyRepos = app(CurrencyRepositoryInterface::class); $this->opsRepository = app(OperationsRepositoryInterface::class); $userGroup = $this->validateUserGroup($request); if (null !== $userGroup) { $this->abRepository->setUserGroup($userGroup); $this->accountRepository->setUserGroup($userGroup); $this->billRepository->setUserGroup($userGroup); $this->budgetRepository->setUserGroup($userGroup); $this->opsRepository->setUserGroup($userGroup); } return $next($request); } ); } /** * This endpoint is documented at: * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v2)#/summary/getBasicSummary * * @param DateRequest $request * * @return JsonResponse * @throws Exception */ public function basic(DateRequest $request): JsonResponse { // parameters for boxes: $start = $this->parameters->get('start'); $end = $this->parameters->get('end'); // balance information: $balanceData = $this->getBalanceInformation($start, $end); $billData = $this->getBillInformation($start, $end); $spentData = $this->getLeftToSpendInfo($start, $end); $netWorthData = $this->getNetWorthInfo($start, $end); $total = array_merge($balanceData, $billData, $spentData, $netWorthData); return response()->json($total); } /** * @param Carbon $start * @param Carbon $end * * @return array * @throws FireflyException */ private function getBalanceInformation(Carbon $start, Carbon $end): array { // prep some arrays: $incomes = [ 'native' => '0', ]; $expenses = [ 'native' => '0', ]; $sums = [ 'native' => '0', ]; $return = []; $currencies = []; $converter = new ExchangeRateConverter(); $default = app('amount')->getDefaultCurrency(); /** @var User $user */ $user = auth()->user(); // collect income of user using the new group collector. /** @var GroupCollectorInterface $collector */ $collector = app(GroupCollectorInterface::class); $collector ->setRange($start, $end) ->setUserGroup($user->userGroup) // set page to retrieve ->setPage($this->parameters->get('page')) // set types of transactions to return. ->setTypes([TransactionType::DEPOSIT]) ->setRange($start, $end); $set = $collector->getExtractedJournals(); /** @var array $transactionJournal */ foreach ($set as $transactionJournal) { // transaction info: $currencyId = (int)$transactionJournal['currency_id']; $amount = bcmul($transactionJournal['amount'], '-1'); $currency = $currencies[$currencyId] ?? TransactionCurrency::find($currencyId); $currencies[$currencyId] = $currency; $nativeAmount = $converter->convert($currency, $default, $transactionJournal['date'], $amount); if ((int)$transactionJournal['foreign_currency_id'] === (int)$default->id) { // use foreign amount instead $nativeAmount = $transactionJournal['foreign_amount']; } // prep the arrays $incomes[$currencyId] = $incomes[$currencyId] ?? '0'; $incomes['native'] = $incomes['native'] ?? '0'; $sums[$currencyId] = $sums[$currencyId] ?? '0'; $sums['native'] = $sums['native'] ?? '0'; // add values: $incomes[$currencyId] = bcadd($incomes[$currencyId], $amount); $sums[$currencyId] = bcadd($sums[$currencyId], $amount); $incomes['native'] = bcadd($incomes['native'], $nativeAmount); $sums['native'] = bcadd($sums['native'], $nativeAmount); } // collect expenses of user using the new group collector. /** @var GroupCollectorInterface $collector */ $collector = app(GroupCollectorInterface::class); $collector ->setRange($start, $end) ->setUserGroup($user->userGroup) // set page to retrieve ->setPage($this->parameters->get('page')) // set types of transactions to return. ->setTypes([TransactionType::WITHDRAWAL]) ->setRange($start, $end); $set = $collector->getExtractedJournals(); /** @var array $transactionJournal */ foreach ($set as $transactionJournal) { // transaction info $currencyId = (int)$transactionJournal['currency_id']; $amount = $transactionJournal['amount']; $currency = $currencies[$currencyId] ?? $this->currencyRepos->find($currencyId); $currencies[$currencyId] = $currency; $nativeAmount = $converter->convert($currency, $default, $transactionJournal['date'], $amount); if ((int)$transactionJournal['foreign_currency_id'] === (int)$default->id) { // use foreign amount instead $nativeAmount = $transactionJournal['foreign_amount']; } // prep arrays $expenses[$currencyId] = $expenses[$currencyId] ?? '0'; $expenses['native'] = $expenses['native'] ?? '0'; $sums[$currencyId] = $sums[$currencyId] ?? '0'; $sums['native'] = $sums['native'] ?? '0'; // add values $expenses[$currencyId] = bcadd($expenses[$currencyId], $amount); $sums[$currencyId] = bcadd($sums[$currencyId], $amount); $expenses['native'] = bcadd($expenses['native'], $nativeAmount); $sums['native'] = bcadd($sums['native'], $nativeAmount); } // create special array for native currency: $return[] = [ 'key' => 'balance-in-native', 'value' => $sums['native'], 'currency_id' => (string)$default->id, 'currency_code' => $default->code, 'currency_symbol' => $default->symbol, 'currency_decimal_places' => $default->decimal_places, ]; $return[] = [ 'key' => 'spent-in-native', 'value' => $expenses['native'], 'currency_id' => (string)$default->id, 'currency_code' => $default->code, 'currency_symbol' => $default->symbol, 'currency_decimal_places' => $default->decimal_places, ]; $return[] = [ 'key' => 'earned-in-native', 'value' => $incomes['native'], 'currency_id' => (string)$default->id, 'currency_code' => $default->code, 'currency_symbol' => $default->symbol, 'currency_decimal_places' => $default->decimal_places, ]; // format amounts: $keys = array_keys($sums); foreach ($keys as $currencyId) { if ('native' === $currencyId) { // skip native entries. continue; } $currency = $currencies[$currencyId] ?? $this->currencyRepos->find($currencyId); $currencies[$currencyId] = $currency; // create objects for big array. $return[] = [ 'key' => sprintf('balance-in-%s', $currency->code), 'value' => $sums[$currencyId] ?? '0', 'currency_id' => (string)$currency->id, 'currency_code' => $currency->code, 'currency_symbol' => $currency->symbol, 'currency_decimal_places' => $currency->decimal_places, ]; $return[] = [ 'key' => sprintf('spent-in-%s', $currency->code), 'value' => $expenses[$currencyId] ?? '0', 'currency_id' => (string)$currency->id, 'currency_code' => $currency->code, 'currency_symbol' => $currency->symbol, 'currency_decimal_places' => $currency->decimal_places, ]; $return[] = [ 'key' => sprintf('earned-in-%s', $currency->code), 'value' => $incomes[$currencyId] ?? '0', 'currency_id' => (string)$currency->id, 'currency_code' => $currency->code, 'currency_symbol' => $currency->symbol, 'currency_decimal_places' => $currency->decimal_places, ]; } return $return; } /** * @param Carbon $start * @param Carbon $end * * @return array */ private function getBillInformation(Carbon $start, Carbon $end): array { /* * Since both this method and the chart use the exact same data, we can suffice * with calling the one method in the bill repository that will get this amount. */ $paidAmount = $this->billRepository->sumPaidInRange($start, $end); $unpaidAmount = $this->billRepository->sumUnpaidInRange($start, $end); $return = []; /** * @var array $info */ foreach ($paidAmount as $info) { $amount = bcmul($info['sum'], '-1'); $nativeAmount = bcmul($info['native_sum'], '-1'); $return[] = [ 'key' => sprintf('bills-paid-in-%s', $info['currency_code']), 'value' => $amount, 'currency_id' => (string)$info['currency_id'], 'currency_code' => $info['currency_code'], 'currency_symbol' => $info['currency_symbol'], 'currency_decimal_places' => $info['currency_decimal_places'], ]; $return[] = [ 'key' => 'bills-paid-in-native', 'value' => $nativeAmount, 'currency_id' => (string)$info['native_id'], 'currency_code' => $info['native_code'], 'currency_symbol' => $info['native_symbol'], 'currency_decimal_places' => $info['native_decimal_places'], ]; } /** * @var array $info */ foreach ($unpaidAmount as $info) { $amount = bcmul($info['sum'], '-1'); $nativeAmount = bcmul($info['native_sum'], '-1'); $return[] = [ 'key' => sprintf('bills-unpaid-in-%s', $info['currency_code']), 'value' => $amount, 'currency_id' => (string)$info['currency_id'], 'currency_code' => $info['currency_code'], 'currency_symbol' => $info['currency_symbol'], 'currency_decimal_places' => $info['currency_decimal_places'], ]; $return[] = [ 'key' => 'bills-unpaid-in-native', 'value' => $nativeAmount, 'currency_id' => (string)$info['native_id'], 'currency_code' => $info['native_code'], 'currency_symbol' => $info['native_symbol'], 'currency_decimal_places' => $info['native_decimal_places'], ]; } return $return; } /** * @param Carbon $start * @param Carbon $end * * @return array * @throws Exception */ private function getLeftToSpendInfo(Carbon $start, Carbon $end): array { app('log')->debug('Now in getLeftToSpendInfo'); $return = []; $today = today(config('app.timezone')); $available = $this->abRepository->getAvailableBudgetWithCurrency($start, $end); $budgets = $this->budgetRepository->getActiveBudgets(); $spent = $this->opsRepository->listExpenses($start, $end, null, $budgets); $default = app('amount')->getDefaultCurrency(); $currencies = []; $converter = new ExchangeRateConverter(); // native info: $nativeLeft = [ 'key' => 'left-to-spend-in-native', 'value' => '0', 'currency_id' => (string)$default->id, 'currency_code' => $default->code, 'currency_symbol' => $default->symbol, 'currency_decimal_places' => (int)$default->decimal_places, ]; $nativePerDay = [ 'key' => 'left-per-day-to-spend-in-native', 'value' => '0', 'currency_id' => (string)$default->id, 'currency_code' => $default->code, 'currency_symbol' => $default->symbol, 'currency_decimal_places' => (int)$default->decimal_places, ]; /** * @var int $currencyId * @var array $row */ foreach ($spent as $currencyId => $row) { app('log')->debug(sprintf('Processing spent array in currency #%d', $currencyId)); $currencyId = (int)$currencyId; $spent = '0'; $spentNative = '0'; // get the sum from the array of transactions (double loop but who cares) /** @var array $budget */ foreach ($row['budgets'] as $budget) { app('log')->debug(sprintf('Processing expenses in budget "%s".', $budget['name'])); /** @var array $journal */ foreach ($budget['transaction_journals'] as $journal) { $journalCurrencyId = $journal['currency_id']; $currency = $currencies[$journalCurrencyId] ?? $this->currencyRepos->find($journalCurrencyId); $currencies[$currencyId] = $currency; $amount = app('steam')->negative($journal['amount']); $amountNative = $converter->convert($default, $currency, $start, $amount); if ((int)$journal['foreign_currency_id'] === (int)$default->id) { $amountNative = $journal['foreign_amount']; } $spent = bcadd($spent, $amount); $spentNative = bcadd($spentNative, $amountNative); } app('log')->debug(sprintf('Total spent in budget "%s" is %s', $budget['name'], $spent)); } // either an amount was budgeted or 0 is available. $currency = $currencies[$currencyId] ?? $this->currencyRepos->find($currencyId); $currencies[$currencyId] = $currency; $amount = $available[$currencyId]['amount'] ?? '0'; $amountNative = $available[$currencyId]['native_amount'] ?? '0'; $left = bcadd($amount, $spent); $leftNative = bcadd($amountNative, $spentNative); app('log')->debug(sprintf('Available amount is %s', $amount)); app('log')->debug(sprintf('Amount left is %s', $left)); // how much left per day? $days = $today->diffInDays($end) + 1; $perDay = '0'; $perDayNative = '0'; if (0 !== $days && bccomp($left, '0') > -1) { $perDay = bcdiv($left, (string)$days); } if (0 !== $days && bccomp($leftNative, '0') > -1) { $perDayNative = bcdiv($leftNative, (string)$days); } // left $return[] = [ 'key' => sprintf('left-to-spend-in-%s', $row['currency_code']), 'value' => $left, 'currency_id' => (string)$row['currency_id'], 'currency_code' => $row['currency_code'], 'currency_symbol' => $row['currency_symbol'], 'currency_decimal_places' => (int)$row['currency_decimal_places'], ]; // left (native) $nativeLeft['value'] = $leftNative; // left per day: $return[] = [ 'key' => sprintf('left-per-day-to-spend-in-%s', $row['currency_code']), 'value' => $perDay, 'currency_id' => (string)$row['currency_id'], 'currency_code' => $row['currency_code'], 'currency_symbol' => $row['currency_symbol'], 'currency_decimal_places' => (int)$row['currency_decimal_places'], ]; // left per day (native) $nativePerDay['value'] = $perDayNative; } $return[] = $nativeLeft; $return[] = $nativePerDay; return $return; } /** * @param Carbon $start * @param Carbon $end * * @return array */ private function getNetWorthInfo(Carbon $start, Carbon $end): array { /** @var UserGroup $userGroup */ $userGroup = auth()->user()->userGroup; $date = today(config('app.timezone'))->startOfDay(); // start and end in the future? use $end if ($this->notInDateRange($date, $start, $end)) { /** @var Carbon $date */ $date = session('end', today(config('app.timezone'))->endOfMonth()); } /** @var NetWorthInterface $netWorthHelper */ $netWorthHelper = app(NetWorthInterface::class); $netWorthHelper->setUserGroup($userGroup); $allAccounts = $this->accountRepository->getActiveAccountsByType( [AccountType::ASSET, AccountType::DEFAULT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::DEBT] ); // filter list on preference of being included. $filtered = $allAccounts->filter( function (Account $account) { $includeNetWorth = $this->accountRepository->getMetaValue($account, 'include_net_worth'); return null === $includeNetWorth || '1' === $includeNetWorth; } ); $netWorthSet = $netWorthHelper->byAccounts($filtered, $date); $return = []; // in native amount $return[] = [ 'key' => 'net-worth-in-native', 'value' => $netWorthSet['native']['balance'], 'currency_id' => (string)$netWorthSet['native']['currency_id'], 'currency_code' => $netWorthSet['native']['currency_code'], 'currency_symbol' => $netWorthSet['native']['currency_symbol'], 'currency_decimal_places' => $netWorthSet['native']['currency_decimal_places'], ]; foreach ($netWorthSet as $key => $data) { if ('native' === $key) { continue; } $return[] = [ 'key' => sprintf('net-worth-in-%s', $data['currency_code']), 'value' => $data['balance'], 'currency_id' => (string)$data['currency_id'], 'currency_code' => $data['currency_code'], 'currency_symbol' => $data['currency_symbol'], 'currency_decimal_places' => $data['currency_decimal_places'], ]; } return $return; } /** * Check if date is outside session range. * * @param Carbon $date * * @param Carbon $start * @param Carbon $end * * @return bool */ protected function notInDateRange(Carbon $date, Carbon $start, Carbon $end): bool // Validate a preference { $result = false; if ($start->greaterThanOrEqualTo($date) && $end->greaterThanOrEqualTo($date)) { $result = true; } // start and end in the past? use $end if ($start->lessThanOrEqualTo($date) && $end->lessThanOrEqualTo($date)) { $result = true; } return $result; } }