This commit is contained in:
James Cole 2020-07-12 17:32:48 +02:00
parent 0c9a25a073
commit c4979bdd27
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
14 changed files with 79 additions and 60 deletions

View File

@ -151,7 +151,7 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface
'currency_name' => $currency['name'], 'currency_name' => $currency['name'],
'currency_symbol' => $currency['symbol'], 'currency_symbol' => $currency['symbol'],
'currency_decimal_places' => $currency['decimal_places'], 'currency_decimal_places' => $currency['decimal_places'],
'amount' => $spent, 'amount' => number_format((float)$spent,$currency['decimal_places'], '.',''),
]; ];
} }

View File

@ -352,7 +352,7 @@ class OperationsRepository implements OperationsRepositoryInterface
'currency_name' => $currency['name'], 'currency_name' => $currency['name'],
'currency_symbol' => $currency['symbol'], 'currency_symbol' => $currency['symbol'],
'currency_decimal_places' => $currency['decimal_places'], 'currency_decimal_places' => $currency['decimal_places'],
'amount' => $spent, 'amount' => number_format((float) $spent, $currency['decimal_places'], '.', ''),
]; ];
} }

View File

@ -78,6 +78,7 @@ class AccountTransformer extends AbstractTransformer
[$openingBalance, $openingBalanceDate] = $this->getOpeningBalance($account, $accountType, $decimalPlaces); [$openingBalance, $openingBalanceDate] = $this->getOpeningBalance($account, $accountType, $decimalPlaces);
[$interest, $interestPeriod] = $this->getInterest($account, $accountType); [$interest, $interestPeriod] = $this->getInterest($account, $accountType);
$openingBalance = number_format((float) $openingBalance, $decimalPlaces, '.', '');
$liabilityAmount = null; $liabilityAmount = null;
$liabilityStart = null; $liabilityStart = null;
if (null !== $liabilityType) { if (null !== $liabilityType) {
@ -94,7 +95,6 @@ class AccountTransformer extends AbstractTransformer
$latitude = $location->latitude; $latitude = $location->latitude;
$zoomLevel = $location->zoom_level; $zoomLevel = $location->zoom_level;
} }
return [ return [
'id' => (int)$account->id, 'id' => (int)$account->id,
'created_at' => $account->created_at->toAtomString(), 'created_at' => $account->created_at->toAtomString(),
@ -107,7 +107,7 @@ class AccountTransformer extends AbstractTransformer
'currency_code' => $currencyCode, 'currency_code' => $currencyCode,
'currency_symbol' => $currencySymbol, 'currency_symbol' => $currencySymbol,
'currency_decimal_places' => $decimalPlaces, 'currency_decimal_places' => $decimalPlaces,
'current_balance' => (string) round(app('steam')->balance($account, $date), $decimalPlaces), 'current_balance' => number_format((float) app('steam')->balance($account, $date), $decimalPlaces, '.', ''),
'current_balance_date' => $date->format('Y-m-d'), 'current_balance_date' => $date->format('Y-m-d'),
'notes' => $this->repository->getNoteText($account), 'notes' => $this->repository->getNoteText($account),
'monthly_payment_date' => $monthlyPaymentDate, 'monthly_payment_date' => $monthlyPaymentDate,
@ -115,7 +115,7 @@ class AccountTransformer extends AbstractTransformer
'account_number' => $this->repository->getMetaValue($account, 'account_number'), 'account_number' => $this->repository->getMetaValue($account, 'account_number'),
'iban' => '' === $account->iban ? null : $account->iban, 'iban' => '' === $account->iban ? null : $account->iban,
'bic' => $this->repository->getMetaValue($account, 'BIC'), 'bic' => $this->repository->getMetaValue($account, 'BIC'),
'virtual_balance' => round($account->virtual_balance, $decimalPlaces), 'virtual_balance' => number_format((float) $account->virtual_balance, $decimalPlaces, '.', ''),
'opening_balance' => $openingBalance, 'opening_balance' => $openingBalance,
'opening_balance_date' => $openingBalanceDate, 'opening_balance_date' => $openingBalanceDate,
'liability_type' => $liabilityType, 'liability_type' => $liabilityType,
@ -240,7 +240,7 @@ class AccountTransformer extends AbstractTransformer
$openingBalanceDate = null; $openingBalanceDate = null;
if (in_array($accountType, ['asset', 'liabilities'], true)) { if (in_array($accountType, ['asset', 'liabilities'], true)) {
$amount = $this->repository->getOpeningBalanceAmount($account); $amount = $this->repository->getOpeningBalanceAmount($account);
$openingBalance = null === $amount ? null : round($amount, $decimalPlaces); $openingBalance = $amount;
$openingBalanceDate = $this->repository->getOpeningBalanceDate($account); $openingBalanceDate = $this->repository->getOpeningBalanceDate($account);
} }

View File

@ -78,7 +78,7 @@ class AvailableBudgetTransformer extends AbstractTransformer
'currency_code' => $currency->code, 'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol, 'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places, 'currency_decimal_places' => $currency->decimal_places,
'amount' => round($availableBudget->amount, $currency->decimal_places), 'amount' => number_format((float) $availableBudget->amount, $currency->decimal_places, '.', ''),
'start' => $availableBudget->start_date->format('Y-m-d'), 'start' => $availableBudget->start_date->format('Y-m-d'),
'end' => $availableBudget->end_date->format('Y-m-d'), 'end' => $availableBudget->end_date->format('Y-m-d'),
'spent_in_budgets' => [], 'spent_in_budgets' => [],

View File

@ -89,8 +89,8 @@ class BillTransformer extends AbstractTransformer
'currency_symbol' => $currency->symbol, 'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places, 'currency_decimal_places' => $currency->decimal_places,
'name' => $bill->name, 'name' => $bill->name,
'amount_min' => round((float) $bill->amount_min, $currency->decimal_places), 'amount_min' => number_format((float) $bill->amount_min, $currency->decimal_places, '.', ''),
'amount_max' => round((float) $bill->amount_max, $currency->decimal_places), 'amount_max' => number_format((float) $bill->amount_max, $currency->decimal_places, '.', ''),
'date' => $bill->date->format('Y-m-d'), 'date' => $bill->date->format('Y-m-d'),
'repeat_freq' => $bill->repeat_freq, 'repeat_freq' => $bill->repeat_freq,
'skip' => (int) $bill->skip, 'skip' => (int) $bill->skip,

View File

@ -52,32 +52,36 @@ class BudgetLimitTransformer extends AbstractTransformer
*/ */
public function transform(BudgetLimit $budgetLimit): array public function transform(BudgetLimit $budgetLimit): array
{ {
$currency = $budgetLimit->transactionCurrency; $currency = $budgetLimit->transactionCurrency;
$amount = $budgetLimit->amount; $amount = $budgetLimit->amount;
$currencyId = null; $currencyDecimalPlaces = 2;
$currencyName = null; $currencyId = null;
$currencyCode = null; $currencyName = null;
$currencySymbol = null; $currencyCode = null;
$currencySymbol = null;
if (null !== $currency) { if (null !== $currency) {
$amount = round($budgetLimit->amount, $budgetLimit->transactionCurrency->decimal_places); $amount = $budgetLimit->amount;
$currencyId = $currency->id; $currencyId = $currency->id;
$currencyName = $currency->name; $currencyName = $currency->name;
$currencyCode = $currency->code; $currencyCode = $currency->code;
$currencySymbol = $currency->symbol; $currencySymbol = $currency->symbol;
$currencyDecimalPlaces = $currency->decimal_places;
} }
$data = [ $amount = number_format((float) $amount, $currencyDecimalPlaces, '.', '');
'id' => (int)$budgetLimit->id, $data = [
'created_at' => $budgetLimit->created_at->toAtomString(), 'id' => (int) $budgetLimit->id,
'updated_at' => $budgetLimit->updated_at->toAtomString(), 'created_at' => $budgetLimit->created_at->toAtomString(),
'start' => $budgetLimit->start_date->format('Y-m-d'), 'updated_at' => $budgetLimit->updated_at->toAtomString(),
'end' => $budgetLimit->end_date->format('Y-m-d'), 'start' => $budgetLimit->start_date->format('Y-m-d'),
'budget_id' => $budgetLimit->budget_id, 'end' => $budgetLimit->end_date->format('Y-m-d'),
'currency_id' => $currencyId, 'budget_id' => $budgetLimit->budget_id,
'currency_code' => $currencyCode, 'currency_id' => $currencyId,
'currency_name' => $currencyName, 'currency_code' => $currencyCode,
'currency_symbol' => $currencySymbol, 'currency_name' => $currencyName,
'amount' => $amount, 'currency_decimal_places' => $currencyName,
'links' => [ 'currency_symbol' => $currencySymbol,
'amount' => $amount,
'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/budgets/limits/' . $budgetLimit->id, 'uri' => '/budgets/limits/' . $budgetLimit->id,

View File

@ -70,7 +70,7 @@ class BudgetTransformer extends AbstractTransformer
$autoBudget = $this->repository->getAutoBudget($budget); $autoBudget = $this->repository->getAutoBudget($budget);
$spent = []; $spent = [];
if (null !== $start && null !== $end) { if (null !== $start && null !== $end) {
$spent = array_values($this->opsRepository->sumExpenses($start, $end, null, new Collection([$budget]))); $spent = $this->beautify($this->opsRepository->sumExpenses($start, $end, null, new Collection([$budget])));
} }
$abCurrencyId = null; $abCurrencyId = null;
@ -88,7 +88,7 @@ class BudgetTransformer extends AbstractTransformer
$abCurrencyId = $autoBudget->transactionCurrency->id; $abCurrencyId = $autoBudget->transactionCurrency->id;
$abCurrencyCode = $autoBudget->transactionCurrency->code; $abCurrencyCode = $autoBudget->transactionCurrency->code;
$abType = $types[$autoBudget->auto_budget_type]; $abType = $types[$autoBudget->auto_budget_type];
$abAmount = $autoBudget->amount; $abAmount = number_format((float) $autoBudget->amount, $autoBudget->transactionCurrency->decimal_places, '.', '');
$abPeriod = $autoBudget->period; $abPeriod = $autoBudget->period;
} }
@ -115,4 +115,20 @@ class BudgetTransformer extends AbstractTransformer
return $data; return $data;
} }
/**
* @param array $array
*
* @return array
*/
private function beautify(array $array): array
{
$return = [];
foreach ($array as $data) {
$data['sum'] = number_format((float) $data['sum'], (int) $data['currency_decimal_places'], '.', '');
$return[] = $data;
}
return $return;
}
} }

View File

@ -96,7 +96,7 @@ class CategoryTransformer extends AbstractTransformer
{ {
$return = []; $return = [];
foreach ($array as $data) { foreach ($array as $data) {
$data['sum'] = round($data['sum'], (int)$data['currency_decimal_places']); $data['sum'] = number_format((float) $data['sum'], (int) $data['currency_decimal_places'], '.', '');
$return[] = $data; $return[] = $data;
} }

View File

@ -52,7 +52,7 @@ class CurrencyExchangeRateTransformer extends AbstractTransformer
*/ */
public function transform(CurrencyExchangeRate $rate): array public function transform(CurrencyExchangeRate $rate): array
{ {
$result = round((float)$rate->rate * (float)$this->parameters->get('amount'), $rate->toCurrency->decimal_places); $result = number_format((float) $rate->rate * (float) $this->parameters->get('amount'), $rate->toCurrency->decimal_places, '.', '');
$result = 0.0 === $result ? null : $result; $result = 0.0 === $result ? null : $result;
$data = [ $data = [
'id' => (int)$rate->id, 'id' => (int)$rate->id,

View File

@ -58,7 +58,7 @@ class CurrencyTransformer extends AbstractTransformer
$isDefault = (int) $defaultCurrency->id === (int) $currency->id; $isDefault = (int) $defaultCurrency->id === (int) $currency->id;
} }
$data = [ $data = [
'id' => (int)$currency->id, 'id' => (int) $currency->id,
'created_at' => $currency->created_at->toAtomString(), 'created_at' => $currency->created_at->toAtomString(),
'updated_at' => $currency->updated_at->toAtomString(), 'updated_at' => $currency->updated_at->toAtomString(),
'default' => $isDefault, 'default' => $isDefault,
@ -66,7 +66,7 @@ class CurrencyTransformer extends AbstractTransformer
'name' => $currency->name, 'name' => $currency->name,
'code' => $currency->code, 'code' => $currency->code,
'symbol' => $currency->symbol, 'symbol' => $currency->symbol,
'decimal_places' => (int)$currency->decimal_places, 'decimal_places' => (int) $currency->decimal_places,
'links' => [ 'links' => [
[ [
'rel' => 'self', 'rel' => 'self',

View File

@ -75,12 +75,7 @@ class PiggyBankEventTransformer extends AbstractTransformer
$this->piggyRepos->setUser($account->user); $this->piggyRepos->setUser($account->user);
// get associated currency or fall back to the default: // get associated currency or fall back to the default:
// TODO we can use getAccountCurrency() instead $currency = $this->repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUser($account->user);
$currencyId = (int)$this->repository->getMetaValue($account, 'currency_id');
$currency = $this->currencyRepos->findNull($currencyId);
if (null === $currency) {
$currency = app('amount')->getDefaultCurrencyByUser($account->user);
}
// get associated journal and transaction, if any: // get associated journal and transaction, if any:
$journalId = (int)$event->transaction_journal_id; $journalId = (int)$event->transaction_journal_id;
@ -89,10 +84,10 @@ class PiggyBankEventTransformer extends AbstractTransformer
$groupId = (int)$event->transactionJournal->transaction_group_id; $groupId = (int)$event->transactionJournal->transaction_group_id;
} }
$data = [ $data = [
'id' => (int)$event->id, 'id' => (int) $event->id,
'created_at' => $event->created_at->toAtomString(), 'created_at' => $event->created_at->toAtomString(),
'updated_at' => $event->updated_at->toAtomString(), 'updated_at' => $event->updated_at->toAtomString(),
'amount' => round($event->amount, $currency->decimal_places), 'amount' => number_format((float) $event->amount, $currency->decimal_places, '.', ''),
'currency_id' => $currency->id, 'currency_id' => $currency->id,
'currency_code' => $currency->code, 'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol, 'currency_symbol' => $currency->symbol,

View File

@ -97,7 +97,7 @@ class PiggyBankTransformer extends AbstractTransformer
// get currently saved amount: // get currently saved amount:
$currentAmountStr = $this->piggyRepos->getCurrentAmount($piggyBank); $currentAmountStr = $this->piggyRepos->getCurrentAmount($piggyBank);
$currentAmount = round($currentAmountStr, $currency->decimal_places); $currentAmount = number_format((float) $currentAmountStr, $currency->decimal_places, '.', '');
// left to save: // left to save:
$leftToSave = bcsub($piggyBank->targetamount, $currentAmountStr); $leftToSave = bcsub($piggyBank->targetamount, $currentAmountStr);
@ -105,11 +105,11 @@ class PiggyBankTransformer extends AbstractTransformer
$targetDate = null === $piggyBank->targetdate ? null : $piggyBank->targetdate->format('Y-m-d'); $targetDate = null === $piggyBank->targetdate ? null : $piggyBank->targetdate->format('Y-m-d');
// target and percentage: // target and percentage:
$targetAmount = round($piggyBank->targetamount, $currency->decimal_places); $targetAmount = $piggyBank->targetamount;
$targetAmount = 1 === bccomp('0.01', (string)$targetAmount) ? '0.01' : $targetAmount; $targetAmount = 1 === bccomp('0.01', (string) $targetAmount) ? '0.01' : $targetAmount;
$percentage = (int)(0 !== bccomp('0', $currentAmountStr) ? $currentAmount / $targetAmount * 100 : 0); $percentage = (int) (0 !== bccomp('0', $currentAmountStr) ? $currentAmountStr / $targetAmount * 100 : 0);
$data = [ $data = [
'id' => (int)$piggyBank->id, 'id' => (int) $piggyBank->id,
'created_at' => $piggyBank->created_at->toAtomString(), 'created_at' => $piggyBank->created_at->toAtomString(),
'updated_at' => $piggyBank->updated_at->toAtomString(), 'updated_at' => $piggyBank->updated_at->toAtomString(),
'account_id' => $piggyBank->account_id, 'account_id' => $piggyBank->account_id,
@ -119,11 +119,11 @@ class PiggyBankTransformer extends AbstractTransformer
'currency_code' => $currency->code, 'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol, 'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places, 'currency_decimal_places' => $currency->decimal_places,
'target_amount' => $targetAmount, 'target_amount' => number_format((float) $targetAmount, $currency->decimal_places, '.', ''),
'percentage' => $percentage, 'percentage' => $percentage,
'current_amount' => $currentAmount, 'current_amount' => $currentAmount,
'left_to_save' => round($leftToSave, $currency->decimal_places), 'left_to_save' => number_format((float) $leftToSave, $currency->decimal_places, '.', ''),
'save_per_month' => round($this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places), 'save_per_month' => number_format((float) $this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places, '.', ''),
'start_date' => $startDate, 'start_date' => $startDate,
'target_date' => $targetDate, 'target_date' => $targetDate,
'order' => (int) $piggyBank->order, 'order' => (int) $piggyBank->order,

View File

@ -260,10 +260,10 @@ class RecurrenceTransformer extends AbstractTransformer
} }
$amount = round($transaction->amount, $transaction->transactionCurrency->decimal_places); $amount = number_format((float) $transaction->amount, $transaction->transactionCurrency->decimal_places, '.', '');
$foreignAmount = null; $foreignAmount = null;
if (null !== $transaction->foreign_currency_id && null !== $transaction->foreign_amount) { if (null !== $transaction->foreign_currency_id && null !== $transaction->foreign_amount) {
$foreignAmount = round($transaction->foreign_amount, $foreignCurrencyDp); $foreignAmount = number_format($transaction->foreign_amount, $foreignCurrencyDp, '.', '');
} }
$transactionArray = [ $transactionArray = [
'currency_id' => $transaction->transaction_currency_id, 'currency_id' => $transaction->transaction_currency_id,

View File

@ -335,8 +335,12 @@ class TransactionGroupTransformer extends AbstractTransformer
$category = $this->getCategory($journal->categories->first()); $category = $this->getCategory($journal->categories->first());
$bill = $this->getBill($journal->bill); $bill = $this->getBill($journal->bill);
if (null !== $foreignAmount && null !== $foreignCurrency) {
$foreignAmount = number_format($foreignAmount, $foreignCurrency['decimal_places'], '.', '');
}
return [ return [
'user' => (int)$journal->user_id, 'user' => (int) $journal->user_id,
'transaction_journal_id' => $journal->id, 'transaction_journal_id' => $journal->id,
'type' => strtolower($type), 'type' => strtolower($type),
'date' => $journal->date->toAtomString(), 'date' => $journal->date->toAtomString(),
@ -352,7 +356,7 @@ class TransactionGroupTransformer extends AbstractTransformer
'foreign_currency_symbol' => $foreignCurrency['symbol'], 'foreign_currency_symbol' => $foreignCurrency['symbol'],
'foreign_currency_decimal_places' => $foreignCurrency['decimal_places'], 'foreign_currency_decimal_places' => $foreignCurrency['decimal_places'],
'amount' => $amount, 'amount' => number_format((float) $amount, $currency->decimal_places, '.', ''),
'foreign_amount' => $foreignAmount, 'foreign_amount' => $foreignAmount,
'description' => $journal->description, 'description' => $journal->description,
@ -390,7 +394,7 @@ class TransactionGroupTransformer extends AbstractTransformer
'sepa_cc' => $metaFieldData['sepa_cc'], 'sepa_cc' => $metaFieldData['sepa_cc'],
'sepa_ct_op' => $metaFieldData['sepa_ct_op'], 'sepa_ct_op' => $metaFieldData['sepa_ct_op'],
'sepa_ct_id' => $metaFieldData['sepa_ct_id'], 'sepa_ct_id' => $metaFieldData['sepa_ct_id'],
'sepa_db' => $metaFieldData['sepa_ddb'], 'sepa_db' => $metaFieldData['sepa_db'],
'sepa_country' => $metaFieldData['sepa_country'], 'sepa_country' => $metaFieldData['sepa_country'],
'sepa_ep' => $metaFieldData['sepa_ep'], 'sepa_ep' => $metaFieldData['sepa_ep'],
'sepa_ci' => $metaFieldData['sepa_ci'], 'sepa_ci' => $metaFieldData['sepa_ci'],