Fix linking piggy banks to transactions.

This commit is contained in:
James Cole
2025-01-02 04:54:29 +01:00
parent f727a38b69
commit e878d5ce07
3 changed files with 39 additions and 18 deletions

View File

@@ -154,20 +154,31 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
/** @var Transaction $destination */
$destination = $journal->transactions()->with(['account'])->where('amount', '>', 0)->first();
$hits = 0;
foreach($piggyBank->accounts as $account) {
// 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));
// 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));
return '0';
}
// 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));
}
if (null === $operator || null === $currency) {
app('log')->debug('Currency is NULL and operator is NULL, return "0".');