Skip to content

Firefly III: a personal finances manager

Two small method improvements.

James Cole64462812fcad
diff --git a/app/Api/V1/Requests/RecurrenceUpdateRequest.php b/app/Api/V1/Requests/RecurrenceUpdateRequest.php
index 6485a1b107..0b15f6884d 100644
--- a/app/Api/V1/Requests/RecurrenceUpdateRequest.php
+++ b/app/Api/V1/Requests/RecurrenceUpdateRequest.php
@@ -177,6 +177,38 @@ class RecurrenceUpdateRequest extends Request
return $return;
}
+ /**
+ * @param array $transaction
+ *
+ * @return array
+ */
+ private function getSingleData(array $transaction): array
+ {
+ return [
+ 'amount' => $transaction['amount'],
+ 'currency_id' => isset($transaction['currency_id']) ? (int)$transaction['currency_id'] : null,
+ 'currency_code' => $transaction['currency_code'] ?? null,
+ 'foreign_amount' => $transaction['foreign_amount'] ?? null,
+ 'foreign_currency_id' => isset($transaction['foreign_currency_id']) ? (int)$transaction['foreign_currency_id'] : null,
+ 'foreign_currency_code' => $transaction['foreign_currency_code'] ?? null,
+ 'source_id' => isset($transaction['source_id']) ? (int)$transaction['source_id'] : null,
+ 'source_name' => isset($transaction['source_name']) ? (string)$transaction['source_name'] : null,
+ 'destination_id' => isset($transaction['destination_id']) ? (int)$transaction['destination_id'] : null,
+ 'destination_name' => isset($transaction['destination_name']) ? (string)$transaction['destination_name'] : null,
+ 'description' => $transaction['description'],
+ 'type' => $this->string('type'),
+
+ // new and updated fields:
+ 'piggy_bank_id' => isset($transaction['piggy_bank_id']) ? (int)$transaction['piggy_bank_id'] : null,
+ 'piggy_bank_name' => $transaction['piggy_bank_name'] ?? null,
+ 'tags' => $transaction['tags'] ?? [],
+ 'budget_id' => isset($transaction['budget_id']) ? (int)$transaction['budget_id'] : null,
+ 'budget_name' => $transaction['budget_name'] ?? null,
+ 'category_id' => isset($transaction['category_id']) ? (int)$transaction['category_id'] : null,
+ 'category_name' => $transaction['category_name'] ?? null,
+ ];
+ }
+
/**
* Returns the transaction data as it is found in the submitted data. It's a complex method according to code
* standards but it just has a lot of ??-statements because of the fields that may or may not exist.
@@ -194,29 +226,7 @@ class RecurrenceUpdateRequest extends Request
}
/** @var array $transaction */
foreach ($transactions as $transaction) {
- $return[] = [
- 'amount' => $transaction['amount'],
- 'currency_id' => isset($transaction['currency_id']) ? (int)$transaction['currency_id'] : null,
- 'currency_code' => $transaction['currency_code'] ?? null,
- 'foreign_amount' => $transaction['foreign_amount'] ?? null,
- 'foreign_currency_id' => isset($transaction['foreign_currency_id']) ? (int)$transaction['foreign_currency_id'] : null,
- 'foreign_currency_code' => $transaction['foreign_currency_code'] ?? null,
- 'source_id' => isset($transaction['source_id']) ? (int)$transaction['source_id'] : null,
- 'source_name' => isset($transaction['source_name']) ? (string)$transaction['source_name'] : null,
- 'destination_id' => isset($transaction['destination_id']) ? (int)$transaction['destination_id'] : null,
- 'destination_name' => isset($transaction['destination_name']) ? (string)$transaction['destination_name'] : null,
- 'description' => $transaction['description'],
- 'type' => $this->string('type'),
-
- // new and updated fields:
- 'piggy_bank_id' => isset($transaction['piggy_bank_id']) ? (int)$transaction['piggy_bank_id'] : null,
- 'piggy_bank_name' => $transaction['piggy_bank_name'] ?? null,
- 'tags' => $transaction['tags'] ?? [],
- 'budget_id' => isset($transaction['budget_id']) ? (int)$transaction['budget_id'] : null,
- 'budget_name' => $transaction['budget_name'] ?? null,
- 'category_id' => isset($transaction['category_id']) ? (int)$transaction['category_id'] : null,
- 'category_name' => $transaction['category_name'] ?? null,
- ];
+ $return[] = $this->getSingleData($transaction);
}
return $return;
diff --git a/app/Services/Internal/Update/AccountUpdateService.php b/app/Services/Internal/Update/AccountUpdateService.php
index 12794d1a49..f8a88c3b87 100644
--- a/app/Services/Internal/Update/AccountUpdateService.php
+++ b/app/Services/Internal/Update/AccountUpdateService.php
@@ -76,28 +76,11 @@ class AccountUpdateService
{
$this->accountRepository->setUser($account->user);
$this->user = $account->user;
-
- // update the account itself:
- $account->name = $data['name'] ?? $account->name;
- $account->active = $data['active'] ?? $account->active;
- $account->iban = $data['iban'] ?? $account->iban;
-
- // if account type is a liability, the liability type (account type)
- // can be updated to another one.
- if ($this->isLiability($account) && $this->isLiabilityTypeId((int)($data['account_type_id'] ?? 0))) {
- $account->account_type_id = (int)$data['account_type_id'];
- }
-
- // update virtual balance (could be set to zero if empty string).
- if (null !== $data['virtual_balance']) {
- $account->virtual_balance = '' === trim($data['virtual_balance']) ? '0' : $data['virtual_balance'];
- }
-
- $account->save();
+ $account = $this->updateAccount($account, $data);
// find currency, or use default currency instead.
if (isset($data['currency_id']) && (null !== $data['currency_id'] || null !== $data['currency_code'])) {
- $currency = $this->getCurrency((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null));
+ $currency = $this->getCurrency((int) ($data['currency_id'] ?? null), (string) ($data['currency_code'] ?? null));
unset($data['currency_code']);
$data['currency_id'] = $currency->id;
}
@@ -178,4 +161,33 @@ class AccountUpdateService
return 1 === AccountType::whereIn('type', [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE])->where('id', $accountTypeId)->count();
}
+
+ /**
+ * @param Account $account
+ * @param array $data
+ *
+ * @return Account
+ */
+ private function updateAccount(Account $account, array $data): Account
+ {
+ // update the account itself:
+ $account->name = $data['name'] ?? $account->name;
+ $account->active = $data['active'] ?? $account->active;
+ $account->iban = $data['iban'] ?? $account->iban;
+
+ // if account type is a liability, the liability type (account type)
+ // can be updated to another one.
+ if ($this->isLiability($account) && $this->isLiabilityTypeId((int) ($data['account_type_id'] ?? 0))) {
+ $account->account_type_id = (int) $data['account_type_id'];
+ }
+
+ // update virtual balance (could be set to zero if empty string).
+ if (null !== $data['virtual_balance']) {
+ $account->virtual_balance = '' === trim($data['virtual_balance']) ? '0' : $data['virtual_balance'];
+ }
+
+ $account->save();
+
+ return $account;
+ }
}