Skip to content

Firefly III: a personal finances manager

Fix linking piggy banks to transactions.

James Colee878d5ce07e1
diff --git a/app/Repositories/PiggyBank/PiggyBankRepository.php b/app/Repositories/PiggyBank/PiggyBankRepository.php
index 4f62edcf48..abe19853ae 100644
--- a/app/Repositories/PiggyBank/PiggyBankRepository.php
+++ b/app/Repositories/PiggyBank/PiggyBankRepository.php
@@ -154,20 +154,31 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
/** @var Transaction $destination */
$destination = $journal->transactions()->with(['account'])->where('amount', '>', 0)->first();
-
- // matches source, which means amount will be removed from piggy:
- if ($source->account_id === $piggyBank->account_id) {
- $operator = 'negative';
- $currency = $accountRepos->getAccountCurrency($source->account) ?? $defaultCurrency;
- app('log')->debug(sprintf('Currency will draw money out of piggy bank. Source currency is %s', $currency->code));
+ $hits = 0;
+ foreach($piggyBank->accounts as $account) {
+
+ // matches source, which means amount will be removed from piggy:
+ if($account->id === $source->account_id) {
+ $operator = 'negative';
+ $currency = $accountRepos->getAccountCurrency($source->account) ?? $defaultCurrency;
+ app('log')->debug(sprintf('Currency will draw money out of piggy bank. Source currency is %s', $currency->code));
+ $hits++;
+ }
+ // matches destination, which means amount will be added to piggy.
+ if ($account->id === $destination->account_id) {
+ $operator = 'positive';
+ $currency = $accountRepos->getAccountCurrency($destination->account) ?? $defaultCurrency;
+ app('log')->debug(sprintf('Currency will add money to piggy bank. Destination currency is %s', $currency->code));
+ $hits++;
+ }
}
+ if ($hits > 1) {
+ app('log')->debug(sprintf('Transaction journal is related to %d of the accounts, cannot determine what to do. Return "0".', $hits));
- // matches destination, which means amount will be added to piggy.
- if ($destination->account_id === $piggyBank->account_id) {
- $operator = 'positive';
- $currency = $accountRepos->getAccountCurrency($destination->account) ?? $defaultCurrency;
- app('log')->debug(sprintf('Currency will add money to piggy bank. Destination currency is %s', $currency->code));
+ return '0';
}
+
+
if (null === $operator || null === $currency) {
app('log')->debug('Currency is NULL and operator is NULL, return "0".');
diff --git a/app/Rules/BelongsUser.php b/app/Rules/BelongsUser.php
index b223de2165..4d5d21c35d 100644
--- a/app/Rules/BelongsUser.php
+++ b/app/Rules/BelongsUser.php
@@ -80,19 +80,30 @@ class BelongsUser implements ValidationRule
private function validatePiggyBankId(int $value): bool
{
- $count = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')
+
+
+
+
+ $count = PiggyBank
+ ::leftJoin('account_piggy_bank','account_piggy_bank.piggy_bank_id','=','piggy_banks.id')
+ ->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
->where('piggy_banks.id', '=', $value)
->where('accounts.user_id', '=', auth()->user()->id)->count()
;
- return 1 === $count;
+ return $count > 0;
}
private function validatePiggyBankName(string $value): bool
{
- $count = $this->countField(PiggyBank::class, 'name', $value);
+ $count = PiggyBank
+ ::leftJoin('account_piggy_bank','account_piggy_bank.piggy_bank_id','=','piggy_banks.id')
+ ->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
+ ->where('piggy_banks.name', '=', $value)
+ ->where('accounts.user_id', '=', auth()->user()->id)->count()
+ ;
- return 1 === $count;
+ return $count>0;
}
protected function countField(string $class, string $field, string $value): int
diff --git a/resources/views/list/piggy-bank-events.twig b/resources/views/list/piggy-bank-events.twig
index bc7eca413a..a1f36c50b5 100644
--- a/resources/views/list/piggy-bank-events.twig
+++ b/resources/views/list/piggy-bank-events.twig
@@ -23,11 +23,10 @@
</td>
<td style="text-align: right;">
-
{% if event.amount < 0 %}
- <span class="text-danger money-negative">{{ trans('firefly.removed_amount', {amount: formatAmountBySymbol(event.amount,event.piggyBank.transactionCurrency.symbol, false)})|raw }}</span>
+ <span class="text-danger money-negative">{{ trans('firefly.removed_amount', {amount: formatAmountBySymbol(event.amount,event.piggyBank.transactionCurrency.symbol, event.piggyBank.transactionCurrency.decimal_places, false)})|raw }}</span>
{% else %}
- <span class="text-success money-positive">{{ trans('firefly.added_amount', {amount: formatAmountBySymbol(event.amount, event.piggyBank.transactionCurrency.symbol, false)})|raw }}</span>
+ <span class="text-success money-positive">{{ trans('firefly.added_amount', {amount: formatAmountBySymbol(event.amount, event.piggyBank.transactionCurrency.symbol, event.piggyBank.transactionCurrency.decimal_places, false)})|raw }}</span>
{% endif %}
</td>
</tr>