mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Fix auto complete
This commit is contained in:
@@ -101,11 +101,16 @@ class AccountController extends Controller
|
||||
|
||||
private function parseAccount(Account $account): array
|
||||
{
|
||||
$currency = $this->adminRepository->getAccountCurrency($account);
|
||||
return [
|
||||
'id' => (string) $account->id,
|
||||
'title' => $account->name,
|
||||
'meta' => [
|
||||
'type' => $account->accountType->type,
|
||||
'currency_id' => null === $currency ? null : (string) $currency->id,
|
||||
'currency_code' => $currency?->code,
|
||||
'currency_symbol' => $currency?->symbol,
|
||||
'currency_decimal' => $currency?->decimal_places,
|
||||
'account_balances' => $this->getAccountBalances($account),
|
||||
],
|
||||
];
|
||||
|
@@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Api\V2\Request\Autocomplete;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Carbon\Exceptions\InvalidFormatException;
|
||||
use FireflyIII\JsonApi\Rules\IsValidFilter;
|
||||
use FireflyIII\JsonApi\Rules\IsValidPage;
|
||||
use FireflyIII\Models\AccountType;
|
||||
@@ -33,7 +34,7 @@ use FireflyIII\Support\Request\ConvertsDataTypes;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use LaravelJsonApi\Core\Query\QueryParameters;
|
||||
use LaravelJsonApi\Validation\Rule as JsonApiRule;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
/**
|
||||
* Class AutocompleteRequest
|
||||
*/
|
||||
@@ -54,10 +55,15 @@ class AutocompleteRequest extends FormRequest
|
||||
public function getParameters(): array
|
||||
{
|
||||
$queryParameters = QueryParameters::cast($this->all());
|
||||
$date = Carbon::createFromFormat('Y-m-d', $queryParameters->filter()->value('date', date('Y-m-d')), config('app.timezone'));
|
||||
$query = $queryParameters->filter()->value('query', '');
|
||||
try {
|
||||
$date = Carbon::createFromFormat('Y-m-d', $queryParameters->filter()?->value('date', date('Y-m-d')), config('app.timezone'));
|
||||
} catch(InvalidFormatException $e) {
|
||||
Log::debug(sprintf('Invalid date format in autocomplete request. Using today: %s', $e->getMessage()));
|
||||
$date = today();
|
||||
}
|
||||
$query = $queryParameters->filter()?->value('query', '') ?? '';
|
||||
$size = (int) ($queryParameters->page()['size'] ?? 50);
|
||||
$accountTypes = $this->getAccountTypeParameter($queryParameters->filter()->value('account_types', ''));
|
||||
$accountTypes = $this->getAccountTypeParameter($queryParameters->filter()?->value('account_types', '') ?? '');
|
||||
|
||||
|
||||
return [
|
||||
|
@@ -37,9 +37,13 @@ class AccountBalanceCalculator
|
||||
// first collect normal amounts (in whatever currency), and set them.
|
||||
|
||||
// select account_id, transaction_currency_id, foreign_currency_id, sum(amount), sum(foreign_amount) from transactions group by account_id, transaction_currency_id, foreign_currency_id
|
||||
$result = Transaction
|
||||
::groupBy(['account_id', 'transaction_currency_id', 'foreign_currency_id'])
|
||||
->get(['account_id', 'transaction_currency_id', 'foreign_currency_id', DB::raw('SUM(amount) as sum_amount'), DB::raw('SUM(foreign_amount) as sum_foreign_amount')]);
|
||||
$query = Transaction::groupBy(['account_id', 'transaction_currency_id', 'foreign_currency_id']);
|
||||
|
||||
if (null !== $account) {
|
||||
$query->where('account_id', $account->id);
|
||||
}
|
||||
|
||||
$result = $query->get(['account_id', 'transaction_currency_id', 'foreign_currency_id', DB::raw('SUM(amount) as sum_amount'), DB::raw('SUM(foreign_amount) as sum_foreign_amount')]);
|
||||
|
||||
// reset account balances:
|
||||
self::resetAccountBalances($account);
|
||||
@@ -60,7 +64,7 @@ class AccountBalanceCalculator
|
||||
|
||||
// then do foreign amount, if present:
|
||||
if ($foreignCurrency > 0) {
|
||||
$entry = self::getBalance('balance', $account, $foreignCurrency);
|
||||
$entry = self::getBalance('balance', $account, $foreignCurrency);
|
||||
$entry->balance = bcadd($entry->balance, $sumForeignAmount);
|
||||
$entry->save();
|
||||
Log::debug(sprintf('Set balance entry #%d to amount %s', $entry->id, $entry->balance));
|
||||
@@ -68,6 +72,7 @@ class AccountBalanceCalculator
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
private static function getBalance(string $title, int $account, int $currency): AccountBalance
|
||||
{
|
||||
$entry = AccountBalance::where('title', $title)->where('account_id', $account)->where('transaction_currency_id', $currency)->first();
|
||||
|
Reference in New Issue
Block a user