Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jun 15, 2024
2 parents 6575236 + 72d55cb commit d426e09
Show file tree
Hide file tree
Showing 663 changed files with 2,485 additions and 156,951 deletions.
151 changes: 77 additions & 74 deletions .ci/php-cs-fixer/composer.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions THANKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Over time, many people have contributed to Firefly III. Their efforts are not al
Please find below all the people who contributed to the Firefly III code. Their names are mentioned in the year of their first contribution.

## 2024
- Steve Wasiura
- imlonghao
- Rahman Yusuf
- Michael Thomas
Expand Down
163 changes: 69 additions & 94 deletions app/Api/V2/Controllers/Chart/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,30 @@

namespace FireflyIII\Api\V2\Controllers\Chart;

use Carbon\Carbon;
use FireflyIII\Api\V2\Controllers\Controller;
use FireflyIII\Api\V2\Request\Chart\DashboardChartRequest;
use FireflyIII\Api\V2\Request\Chart\ChartRequest;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface;
use FireflyIII\Support\Chart\ChartData;
use FireflyIII\Support\Http\Api\CleansChartData;
use FireflyIII\Support\Http\Api\CollectsAccountsFromFilter;
use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;

/**
* Class AccountController
*/
class AccountController extends Controller
{
use CleansChartData;
use CollectsAccountsFromFilter;
use ValidatesUserGroupTrait;

private AccountRepositoryInterface $repository;
private ChartData $chartData;
private TransactionCurrency $default;

public function __construct()
{
Expand All @@ -54,112 +56,85 @@ public function __construct()
function ($request, $next) {
$this->repository = app(AccountRepositoryInterface::class);
$this->repository->setUserGroup($this->validateUserGroup($request));
$this->chartData = new ChartData();
$this->default = app('amount')->getDefaultCurrency();

return $next($request);
}
);
}

/**
* This endpoint is documented at
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v2)#/charts/getChartAccountOverview
*
* The native currency is the preferred currency on the page /currencies.
*
* If a transaction has foreign currency = native currency, the foreign amount will be used, no conversion
* will take place.
*
* TODO validate and set user_group_id from request
* TODO fix documentation
*
* @throws FireflyException
*/
public function dashboard(DashboardChartRequest $request): JsonResponse
public function dashboard(ChartRequest $request): JsonResponse
{
/** @var Carbon $start */
$start = $this->parameters->get('start');

/** @var Carbon $end */
$end = $this->parameters->get('end');
$end->endOfDay();

/** @var TransactionCurrency $default */
$default = app('amount')->getDefaultCurrency();
$params = $request->getAll();

/** @var Collection $accounts */
$accounts = $params['accounts'];
$chartData = [];
$queryParameters = $request->getParameters();
$accounts = $this->getAccountList($queryParameters);

// user's preferences
if (0 === $accounts->count()) {
$defaultSet = $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT])->pluck('id')->toArray();
$frontpage = app('preferences')->get('frontpageAccounts', $defaultSet);

if (!(is_array($frontpage->data) && count($frontpage->data) > 0)) {
$frontpage->data = $defaultSet;
$frontpage->save();
}

$accounts = $this->repository->getAccountsById($frontpage->data);
}

// both options are overruled by "preselected"
if ('all' === $params['preselected']) {
$accounts = $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]);
}
if ('assets' === $params['preselected']) {
$accounts = $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]);
}
if ('liabilities' === $params['preselected']) {
$accounts = $this->repository->getAccountsByType([AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]);
}
// move date to end of day
$queryParameters['start']->startOfDay();
$queryParameters['end']->endOfDay();

// loop each account, and collect info:
/** @var Account $account */
foreach ($accounts as $account) {
$currency = $this->repository->getAccountCurrency($account);
if (null === $currency) {
$currency = $default;
}
$currentSet = [
'label' => $account->name,
// the currency that belongs to the account.
'currency_id' => (string)$currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,

// the default currency of the user (could be the same!)
'native_currency_id' => (string)$default->id,
'native_currency_code' => $default->code,
'native_currency_symbol' => $default->symbol,
'native_currency_decimal_places' => $default->decimal_places,
'start' => $start->toAtomString(),
'end' => $end->toAtomString(),
'period' => '1D',
'entries' => [],
'native_entries' => [],
];
$currentStart = clone $start;
$range = app('steam')->balanceInRange($account, $start, clone $end, $currency);
$rangeConverted = app('steam')->balanceInRangeConverted($account, $start, clone $end, $default);

$previous = array_values($range)[0];
$previousConverted = array_values($rangeConverted)[0];
while ($currentStart <= $end) {
$format = $currentStart->format('Y-m-d');
$label = $currentStart->toAtomString();
$balance = array_key_exists($format, $range) ? $range[$format] : $previous;
$balanceConverted = array_key_exists($format, $rangeConverted) ? $rangeConverted[$format] : $previousConverted;
$previous = $balance;
$previousConverted = $balanceConverted;

$currentStart->addDay();
$currentSet['entries'][$label] = $balance;
$currentSet['native_entries'][$label] = $balanceConverted;
}
$chartData[] = $currentSet;
$this->renderAccountData($queryParameters, $account);
}

return response()->json($this->clean($chartData));
return response()->json($this->chartData->render());
}

/**
* @throws FireflyException
*/
private function renderAccountData(array $params, Account $account): void
{
$currency = $this->repository->getAccountCurrency($account);
if (null === $currency) {
$currency = $this->default;
}
$currentSet = [
'label' => $account->name,

// the currency that belongs to the account.
'currency_id' => (string) $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,

// the default currency of the user (could be the same!)
'native_currency_id' => (string) $this->default->id,
'native_currency_code' => $this->default->code,
'native_currency_symbol' => $this->default->symbol,
'native_currency_decimal_places' => $this->default->decimal_places,
'date' => $params['start']->toAtomString(),
'start' => $params['start']->toAtomString(),
'end' => $params['end']->toAtomString(),
'period' => '1D',
'entries' => [],
'native_entries' => [],
];
$currentStart = clone $params['start'];
$range = app('steam')->balanceInRange($account, $params['start'], clone $params['end'], $currency);
$rangeConverted = app('steam')->balanceInRangeConverted($account, $params['start'], clone $params['end'], $this->default);

$previous = array_values($range)[0];
$previousConverted = array_values($rangeConverted)[0];
while ($currentStart <= $params['end']) {
$format = $currentStart->format('Y-m-d');
$label = $currentStart->toAtomString();
$balance = array_key_exists($format, $range) ? $range[$format] : $previous;
$balanceConverted = array_key_exists($format, $rangeConverted) ? $rangeConverted[$format] : $previousConverted;
$previous = $balance;
$previousConverted = $balanceConverted;

$currentStart->addDay();
$currentSet['entries'][$label] = $balance;
$currentSet['native_entries'][$label] = $balanceConverted;
}
$this->chartData->add($currentSet);
}
}
86 changes: 51 additions & 35 deletions app/Api/V2/Controllers/Chart/BalanceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,49 @@

namespace FireflyIII\Api\V2\Controllers\Chart;

use Carbon\Carbon;
use FireflyIII\Api\V2\Controllers\Controller;
use FireflyIII\Api\V2\Request\Chart\BalanceChartRequest;
use FireflyIII\Enums\UserRoleEnum;
use FireflyIII\Api\V2\Request\Chart\ChartRequest;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface;
use FireflyIII\Support\Chart\ChartData;
use FireflyIII\Support\Http\Api\AccountBalanceGrouped;
use FireflyIII\Support\Http\Api\CleansChartData;
use FireflyIII\Support\Http\Api\CollectsAccountsFromFilter;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;

/**
* Class BalanceController
*/
class BalanceController extends Controller
{
use CleansChartData;
protected array $acceptedRoles = [UserRoleEnum::READ_ONLY];
use CollectsAccountsFromFilter;

private AccountRepositoryInterface $repository;
private GroupCollectorInterface $collector;
private ChartData $chartData;
private TransactionCurrency $default;

public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(AccountRepositoryInterface::class);
$this->collector = app(GroupCollectorInterface::class);
$userGroup = $this->validateUserGroup($request);
$this->repository->setUserGroup($userGroup);
$this->collector->setUserGroup($userGroup);
$this->chartData = new ChartData();
$this->default = app('amount')->getDefaultCurrency();

return $next($request);
}
);
}

/**
* The code is practically a duplicate of ReportController::operations.
Expand All @@ -54,50 +77,43 @@ class BalanceController extends Controller
* If the transaction being processed is already in native currency OR if the
* foreign amount is in the native currency, the amount will not be converted.
*
* TODO validate and set user_group_id
* TODO collector set group, not user
*
* @throws FireflyException
*/
public function balance(BalanceChartRequest $request): JsonResponse
public function balance(ChartRequest $request): JsonResponse
{
$params = $request->getAll();

/** @var Carbon $start */
$start = $this->parameters->get('start');

/** @var Carbon $end */
$end = $this->parameters->get('end');
$end->endOfDay();
$queryParameters = $request->getParameters();
$accounts = $this->getAccountList($queryParameters);

/** @var Collection $accounts */
$accounts = $params['accounts'];

/** @var string $preferredRange */
$preferredRange = $params['period'];
// move date to end of day
$queryParameters['start']->startOfDay();
$queryParameters['end']->endOfDay();

// prepare for currency conversion and data collection:
/** @var TransactionCurrency $default */
$default = app('amount')->getDefaultCurrency();
$default = app('amount')->getDefaultCurrency();

// get journals for entire period:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setRange($start, $end)->withAccountInformation();
$collector->setXorAccounts($accounts);
$collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::RECONCILIATION, TransactionType::TRANSFER]);
$journals = $collector->getExtractedJournals();

$object = new AccountBalanceGrouped();
$object->setPreferredRange($preferredRange);
$this->collector->setRange($queryParameters['start'], $queryParameters['end'])
->withAccountInformation()
->setXorAccounts($accounts)
->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::RECONCILIATION, TransactionType::TRANSFER])
;
$journals = $this->collector->getExtractedJournals();

$object = new AccountBalanceGrouped();
$object->setPreferredRange($queryParameters['period']);
$object->setDefault($default);
$object->setAccounts($accounts);
$object->setJournals($journals);
$object->setStart($start);
$object->setEnd($end);
$object->setStart($queryParameters['start']);
$object->setEnd($queryParameters['end']);
$object->groupByCurrencyAndPeriod();
$chartData = $object->convertToChartData();
$data = $object->convertToChartData();
foreach ($data as $entry) {
$this->chartData->add($entry);
}

return response()->json($this->clean($chartData));
return response()->json($this->chartData->render());
}
}
18 changes: 12 additions & 6 deletions app/Api/V2/Request/Chart/ChartRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use FireflyIII\Enums\UserRoleEnum;
use FireflyIII\JsonApi\Rules\IsValidFilter;
use FireflyIII\Rules\IsFilterValueIn;
use FireflyIII\Support\Http\Api\ParsesQueryFilters;
use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait;
use FireflyIII\Support\Request\ChecksLogin;
Expand Down Expand Up @@ -52,15 +53,17 @@ public function getParameters(): array
$queryParameters = QueryParameters::cast($this->all());

return [
'start' => $this->dateOrToday($queryParameters, 'start'),
'end' => $this->dateOrToday($queryParameters, 'end'),

// preselected heeft maar een paar toegestane waardes.

'start' => $this->dateOrToday($queryParameters, 'start'),
'end' => $this->dateOrToday($queryParameters, 'end'),
'preselected' => $this->stringFromQueryParams($queryParameters, 'preselected', 'empty'),
'period' => $this->stringFromQueryParams($queryParameters, 'period', '1M'),
'accounts' => $this->arrayOfStrings($queryParameters, 'accounts'),
// preselected heeft maar een paar toegestane waardes, dat moet ook goed gaan.
// 'query' => $this->arrayOfStrings($queryParameters, 'query'),
// 'size' => $this->integerFromQueryParams($queryParameters,'size', 50),
// 'account_types' => $this->getAccountTypeParameter($this->arrayOfStrings($queryParameters, 'account_types')),
];
// collect accounts based on this list?
}

// return [
Expand All @@ -76,7 +79,10 @@ public function rules(): array
{
return [
'fields' => JsonApiRule::notSupported(),
'filter' => ['nullable', 'array', new IsValidFilter(['start', 'end', 'preselected', 'accounts'])],
'filter' => ['nullable', 'array',
new IsValidFilter(['start', 'end', 'preselected', 'accounts']),
new IsFilterValueIn('preselected', config('firefly.preselected_accounts')),
],
'include' => JsonApiRule::notSupported(),
'page' => JsonApiRule::notSupported(),
'sort' => JsonApiRule::notSupported(),
Expand Down
Loading

0 comments on commit d426e09

Please sign in to comment.