From 3745d79f1ff02a509412f29fd3c4e6ebc545e704 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 2 Aug 2025 07:16:30 +0200 Subject: [PATCH] Clean up account transformer. --- app/Support/Amount.php | 2 +- app/Support/Steam.php | 2 +- app/Transformers/AccountTransformer.php | 216 +++++++++++------------ app/Transformers/CurrencyTransformer.php | 1 + public/v1/js/.gitkeep | 0 resources/assets/v1/mix-manifest.json | 1 + 6 files changed, 104 insertions(+), 118 deletions(-) mode change 100644 => 100755 public/v1/js/.gitkeep diff --git a/app/Support/Amount.php b/app/Support/Amount.php index 57377bc672..995f534603 100644 --- a/app/Support/Amount.php +++ b/app/Support/Amount.php @@ -142,7 +142,7 @@ class Amount $cache->addProperty('getPrimaryCurrencyByGroup'); $cache->addProperty($userGroup->id); if ($cache->has()) { - return $cache->get(); + return $cache->get(); } /** @var null|TransactionCurrency $primary */ diff --git a/app/Support/Steam.php b/app/Support/Steam.php index 53f78888ce..7ef6df2963 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -381,7 +381,7 @@ class Steam } // if there is a request to convert, convert to "pc_balance" and use "balance" for whichever amount is in the primary currency. if ($convertToPrimary) { - $return['primary_balance'] = $this->convertAllBalances($others, $primary, $date); // todo sum all and convert. + $return['pc_balance'] = $this->convertAllBalances($others, $primary, $date); // todo sum all and convert. // Log::debug(sprintf('Set pc_balance to %s', $return['pc_balance'])); } diff --git a/app/Transformers/AccountTransformer.php b/app/Transformers/AccountTransformer.php index 77069a3fa5..65b3a564fe 100644 --- a/app/Transformers/AccountTransformer.php +++ b/app/Transformers/AccountTransformer.php @@ -67,95 +67,68 @@ class AccountTransformer extends AbstractTransformer } // get account type: - $accountType = (string)config(sprintf('firefly.shortNamesByFullName.%s', $account->full_account_type)); - $liabilityType = (string)config(sprintf('firefly.shortLiabilityNameByFullName.%s', $account->full_account_type)); - $liabilityType = '' === $liabilityType ? null : strtolower($liabilityType); - - $liabilityDirection = $account->meta['liability_direction'] ?? null; - // get account role (will only work if the type is asset). - $accountRole = $this->getAccountRole($account, $accountType); + $accountType = (string)config(sprintf('firefly.shortNamesByFullName.%s', $account->full_account_type)); + $liabilityType = (string)config(sprintf('firefly.shortLiabilityNameByFullName.%s', $account->full_account_type)); + $liabilityType = '' === $liabilityType ? null : strtolower($liabilityType); + $liabilityDirection = $account->meta['liability_direction'] ?? null; + $accountRole = $this->getAccountRole($account, $accountType); + $hasCurrencySettings = array_key_exists('currency_id', $account->meta) && (int)$account->meta['currency_id'] > 0; + $includeNetWorth = 1 === (int)($account->meta['include_net_worth'] ?? 0); + $longitude = $account->meta['location']['longitude'] ?? null; + $latitude = $account->meta['location']['latitude'] ?? null; + $zoomLevel = $account->meta['location']['zoom_level'] ?? null; + $order = $account->order; // date (for balance etc.) - $date = $this->getDate(); + $date = $this->getDate(); $date->endOfDay(); - [$creditCardType, $monthlyPaymentDate] = $this->getCCInfo($account, $accountRole, $accountType); - [$openingBalance, $pcOpeningBalance, $openingBalanceDate] = $this->getOpeningBalance($account, $accountType); - [$interest, $interestPeriod] = $this->getInterest($account, $accountType); - - $primary = $this->primary; - if (!$this->convertToPrimary) { - // reset primary currency to NULL, not interesting. - $primary = null; - } - - $decimalPlaces = (int)$account->meta['currency']?->decimal_places; - $decimalPlaces = 0 === $decimalPlaces ? 2 : $decimalPlaces; - $openingBalanceRounded = Steam::bcround($openingBalance, $decimalPlaces); - $includeNetWorth = 1 === (int)($account->meta['include_net_worth'] ?? 0); - $longitude = $account->meta['location']['longitude'] ?? null; - $latitude = $account->meta['location']['latitude'] ?? null; - $zoomLevel = $account->meta['location']['zoom_level'] ?? null; - // no order for some accounts: - $order = $account->order; if (!in_array(strtolower($accountType), ['liability', 'liabilities', 'asset'], true)) { $order = null; } - Log::debug(sprintf('transform: Call finalAccountBalance with date/time "%s"', $date->toIso8601String())); - $finalBalance = Steam::finalAccountBalance($account, $date, $this->primary, $this->convertToPrimary); - if ($this->convertToPrimary) { - $finalBalance['balance'] = $finalBalance[$account->meta['currency']?->code] ?? '0'; + + // get some listed information from the account meta-data: + [$creditCardType, $monthlyPaymentDate] = $this->getCCInfo($account, $accountRole, $accountType); + [$openingBalance, $openingBalanceDate] = $this->getOpeningBalance($account, $accountType); + [$interest, $interestPeriod] = $this->getInterest($account, $accountType); + + // get currencies: + $currency = $this->primary; // assume primary currency + if ($hasCurrencySettings) { + $currency = $account->meta['currency']; } - $currentBalance = Steam::bcround($finalBalance['balance'] ?? '0', $decimalPlaces); - $pcCurrentBalance = $this->convertToPrimary ? Steam::bcround($finalBalance['pc_balance'] ?? '0', $primary->decimal_places) : null; + // get the current balance: + $finalBalance = Steam::finalAccountBalance($account, $date, $this->primary, $this->convertToPrimary); + Log::debug(sprintf('Call finalAccountBalance(%s) with date/time "%s"', var_export($this->convertToPrimary, true), $date->toIso8601String()), $finalBalance); - // set up balances array: - $balances = []; - $balances[] - = [ - 'type' => 'current', - 'amount' => $currentBalance, - 'currency_id' => $account->meta['currency_id'] ?? null, - 'currency_code' => $account->meta['currency']?->code, - 'currency_symbol' => $account->meta['currency']?->symbol, - 'currency_decimal_places' => $account->meta['currency']?->decimal_places, - 'date' => $date->toAtomString(), - ]; - if (null !== $pcCurrentBalance) { - $balances[] = [ - 'type' => 'pc_current', - 'amount' => $pcCurrentBalance, - 'currency_id' => $primary instanceof TransactionCurrency ? (string)$primary->id : null, - 'currency_code' => $primary?->code, - 'currency_symbol' => $primary?->symbol, - 'ccurrency_decimal_places' => $primary?->decimal_places, - 'date' => $date->toAtomString(), + // set some pc_ default values to NULL: + $pcCurrentBalance = null; + $pcOpeningBalance = null; + $pcVirtualBalance = null; + $pcDebtAmount = null; - ]; + // collect current balances: + $currentBalance = Steam::bcround($finalBalance[$currency->code] ?? '0', $currency->decimal_places); + $openingBalance = Steam::bcround($openingBalance ?? '0', $currency->decimal_places); + $virtualBalance = Steam::bcround($account->virtual_balance ?? '0', $currency->decimal_places); + $debtAmount = $account->meta['current_debt'] ?? null; + + // convert to primary currency if needed: + if ($this->convertToPrimary && $currency->id !== $this->primary->id) { + Log::debug(sprintf('Convert to primary, from %s to %s', $currency->code, $this->primary->code)); + $converter = new ExchangeRateConverter(); + $pcCurrentBalance = $converter->convert($currency, $this->primary, $date, $currentBalance); + $pcOpeningBalance = $converter->convert($currency, $this->primary, $date, $openingBalance); + $pcVirtualBalance = $converter->convert($currency, $this->primary, $date, $virtualBalance); + $pcDebtAmount = null === $debtAmount ? null : $converter->convert($currency, $this->primary, $date, $debtAmount); } - if (null !== $openingBalance) { - $balances[] = [ - 'type' => 'opening', - 'amount' => $openingBalanceRounded, - 'currency_id' => $account->meta['currency_id'] ?? null, - 'currency_code' => $account->meta['currency']?->code, - 'currency_symbol' => $account->meta['currency']?->symbol, - 'currency_decimal_places' => $account->meta['currency']?->decimal_places, - 'date' => $openingBalanceDate, - ]; - } - if (null !== $account->virtual_balance) { - $balances[] = [ - 'type' => 'virtual', - 'amount' => Steam::bcround($account->virtual_balance, $decimalPlaces), - 'currency_id' => $account->meta['currency_id'] ?? null, - 'currency_code' => $account->meta['currency']?->code, - 'currency_symbol' => $account->meta['currency']?->symbol, - 'currency_decimal_places' => $account->meta['currency']?->decimal_places, - 'date' => $date->toAtomString(), - ]; + + // set opening balance(s) to NULL if the date is null + if (null === $openingBalanceDate) { + $openingBalance = null; + $pcOpeningBalance = null; } return [ @@ -167,40 +140,51 @@ class AccountTransformer extends AbstractTransformer 'name' => $account->name, 'type' => strtolower($accountType), 'account_role' => $accountRole, - 'currency_id' => $account->meta['currency_id'] ?? null, - 'currency_code' => $account->meta['currency']?->code, - 'currency_symbol' => $account->meta['currency']?->symbol, - 'currency_decimal_places' => $account->meta['currency']?->decimal_places, - 'primary_currency_id' => $primary instanceof TransactionCurrency ? (string)$primary->id : null, - 'primary_currency_code' => $primary?->code, - 'primary_currency_symbol' => $primary?->symbol, - 'primary_currency_decimal_places' => $primary?->decimal_places, + + // currency information, structured for 6.3.0. + 'object_has_currency_setting' => $hasCurrencySettings, + + // currency is object specific or primary, already determined above. + 'currency_id' => (string)$currency['id'], + 'currency_code' => $currency['code'], + 'currency_symbol' => $currency['symbol'], + 'currency_decimal_places' => $currency['decimal_places'], + 'primary_currency_id' => (string)$this->primary->id, + 'primary_currency_code' => $this->primary->code, + 'primary_currency_symbol' => $this->primary->symbol, + 'primary_currency_decimal_places' => $this->primary->decimal_places, + + // balances, structured for 6.3.0. 'current_balance' => $currentBalance, 'pc_current_balance' => $pcCurrentBalance, - 'current_balance_date' => $date->toAtomString(), - 'notes' => $account->meta['notes'] ?? null, - 'monthly_payment_date' => $monthlyPaymentDate, - 'credit_card_type' => $creditCardType, - 'account_number' => $account->meta['account_number'] ?? null, - 'iban' => '' === $account->iban ? null : $account->iban, - 'bic' => $account->meta['BIC'] ?? null, - 'virtual_balance' => Steam::bcround($account->virtual_balance, $decimalPlaces), - 'pc_virtual_balance' => $this->convertToPrimary ? Steam::bcround($account->native_virtual_balance, $primary->decimal_places) : null, - 'opening_balance' => $openingBalanceRounded, - 'pc_opening_balance' => $pcOpeningBalance, - 'opening_balance_date' => $openingBalanceDate, - 'liability_type' => $liabilityType, - 'liability_direction' => $liabilityDirection, - 'interest' => $interest, - 'interest_period' => $interestPeriod, - 'current_debt' => $account->meta['current_debt'] ?? null, - 'include_net_worth' => $includeNetWorth, - 'longitude' => $longitude, - 'latitude' => $latitude, - 'zoom_level' => $zoomLevel, - 'last_activity' => array_key_exists('last_activity', $account->meta) ? $account->meta['last_activity']->toAtomString() : null, - 'balances' => $balances, - 'links' => [ + + 'opening_balance' => $openingBalance, + 'pc_opening_balance' => $pcOpeningBalance, + + 'virtual_balance' => $virtualBalance, + 'pc_virtual_balance' => $pcVirtualBalance, + + 'debt_amount' => $debtAmount, + 'pc_debt_amount' => $pcDebtAmount, + + 'current_balance_date' => $date->toAtomString(), + 'notes' => $account->meta['notes'] ?? null, + 'monthly_payment_date' => $monthlyPaymentDate, + 'credit_card_type' => $creditCardType, + 'account_number' => $account->meta['account_number'] ?? null, + 'iban' => '' === $account->iban ? null : $account->iban, + 'bic' => $account->meta['BIC'] ?? null, + 'opening_balance_date' => $openingBalanceDate, + 'liability_type' => $liabilityType, + 'liability_direction' => $liabilityDirection, + 'interest' => $interest, + 'interest_period' => $interestPeriod, + 'include_net_worth' => $includeNetWorth, + 'longitude' => $longitude, + 'latitude' => $latitude, + 'zoom_level' => $zoomLevel, + 'last_activity' => array_key_exists('last_activity', $account->meta) ? $account->meta['last_activity']->toAtomString() : null, + 'links' => [ [ 'rel' => 'self', 'uri' => sprintf('/accounts/%d', $account->id), @@ -242,7 +226,7 @@ class AccountTransformer extends AbstractTransformer if (null !== $monthlyPaymentDate) { // try classic date: if (10 === strlen($monthlyPaymentDate)) { - $object = Carbon::createFromFormat('!Y-m-d', $monthlyPaymentDate, config('app.timezone')); + $object = Carbon::createFromFormat('!Y-m-d', $monthlyPaymentDate, config('app.timezone')); if (!$object instanceof Carbon) { $object = today(config('app.timezone')); } @@ -260,7 +244,7 @@ class AccountTransformer extends AbstractTransformer { $openingBalance = null; $openingBalanceDate = null; - $pcOpeningBalance = null; +// $pcOpeningBalance = null; if (in_array($accountType, ['asset', 'liabilities'], true)) { // grab from meta. $openingBalance = $account->meta['opening_balance_amount'] ?? null; @@ -268,21 +252,21 @@ class AccountTransformer extends AbstractTransformer $openingBalanceDate = $account->meta['opening_balance_date'] ?? null; } if (null !== $openingBalanceDate) { - $object = Carbon::createFromFormat('Y-m-d H:i:s', $openingBalanceDate, config('app.timezone')); + $object = Carbon::createFromFormat('Y-m-d H:i:s', $openingBalanceDate, config('app.timezone')); if (!$object instanceof Carbon) { $object = today(config('app.timezone')); } $openingBalanceDate = $object->toAtomString(); // NOW do conversion. - if ($this->convertToPrimary && null !== $account->meta['currency']) { - $converter = new ExchangeRateConverter(); - $pcOpeningBalance = $converter->convert($account->meta['currency'], $this->primary, $object, $openingBalance); - } +// if ($this->convertToPrimary && null !== $account->meta['currency']) { +// $converter = new ExchangeRateConverter(); +// $pcOpeningBalance = $converter->convert($account->meta['currency'], $this->primary, $object, $openingBalance); +// } } - return [$openingBalance, $pcOpeningBalance, $openingBalanceDate]; + return [$openingBalance, $openingBalanceDate]; } private function getInterest(Account $account, string $accountType): array diff --git a/app/Transformers/CurrencyTransformer.php b/app/Transformers/CurrencyTransformer.php index eec4cbeb2a..649bbe6fb9 100644 --- a/app/Transformers/CurrencyTransformer.php +++ b/app/Transformers/CurrencyTransformer.php @@ -42,6 +42,7 @@ class CurrencyTransformer extends AbstractTransformer 'updated_at' => $currency->updated_at->toAtomString(), 'native' => $currency->userGroupNative, 'default' => $currency->userGroupNative, + 'primary' => $currency->userGroupNative, 'enabled' => $currency->userGroupEnabled, 'name' => $currency->name, 'code' => $currency->code, diff --git a/public/v1/js/.gitkeep b/public/v1/js/.gitkeep old mode 100644 new mode 100755 diff --git a/resources/assets/v1/mix-manifest.json b/resources/assets/v1/mix-manifest.json index ff2938f703..33ab615cb3 100644 --- a/resources/assets/v1/mix-manifest.json +++ b/resources/assets/v1/mix-manifest.json @@ -110,6 +110,7 @@ "/public/v1/js/lib/jquery.autocomplete.min.js": "/public/v1/js/lib/jquery.autocomplete.min.js", "/public/v1/js/lib/jquery.color-2.1.2.min.js": "/public/v1/js/lib/jquery.color-2.1.2.min.js", "/public/v1/js/lib/modernizr-custom.js": "/public/v1/js/lib/modernizr-custom.js", + "/public/v1/js/lib/moment/af_ZA.js": "/public/v1/js/lib/moment/af_ZA.js", "/public/v1/js/lib/moment/bg_BG.js": "/public/v1/js/lib/moment/bg_BG.js", "/public/v1/js/lib/moment/ca_ES.js": "/public/v1/js/lib/moment/ca_ES.js", "/public/v1/js/lib/moment/cs_CZ.js": "/public/v1/js/lib/moment/cs_CZ.js",