Fix some cases in loans

This commit is contained in:
James Cole
2021-04-26 07:29:39 +02:00
parent be3cb791a5
commit 0b94851623
2 changed files with 38 additions and 6 deletions

View File

@@ -72,11 +72,8 @@ class CreditRecalculateService
return; return;
} }
$this->processWork();
Log::debug('Will now do CreditRecalculationService'); Log::debug('Will now do CreditRecalculationService');
// do something $this->processWork();
} }
/** /**
@@ -111,7 +108,7 @@ class CreditRecalculateService
$transactions = $account->transactions()->get(); $transactions = $account->transactions()->get();
/** @var Transaction $transaction */ /** @var Transaction $transaction */
foreach ($transactions as $transaction) { foreach ($transactions as $transaction) {
$leftOfDebt = $this->processTransaction($transaction, $leftOfDebt); $leftOfDebt = $this->processTransaction($account, $transaction, $leftOfDebt);
} }
$factory->crud($account, 'current_debt', $leftOfDebt); $factory->crud($account, 'current_debt', $leftOfDebt);
@@ -242,13 +239,28 @@ class CreditRecalculateService
* *
* @return string * @return string
*/ */
private function processTransaction(Transaction $transaction, string $amount): string private function processTransaction(Account $account, Transaction $transaction, string $amount): string
{ {
Log::debug(sprintf('Now in %s(#%d, %s)', __METHOD__, $transaction->id, $amount));
$journal = $transaction->transactionJournal; $journal = $transaction->transactionJournal;
$type = $journal->transactionType->type; $type = $journal->transactionType->type;
Log::debug(sprintf('Type is "%s"', $type));
if (in_array($type, [TransactionType::WITHDRAWAL]) && (int)$account->id === (int)$transaction->account_id && 1 === bccomp($transaction->amount, '0')) {
Log::debug(sprintf('Transaction #%d is withdrawal into liability #%d, does not influence the amount left.', $account->id, $transaction->account_id));
return $amount;
}
if (in_array($type, [TransactionType::DEPOSIT]) && (int)$account->id === (int)$transaction->account_id && -1 === bccomp($transaction->amount, '0')) {
Log::debug(sprintf('Transaction #%d is deposit from liability #%d,does not influence the amount left.', $account->id, $transaction->account_id));
return $amount;
}
if (in_array($type, [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER], true)) { if (in_array($type, [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER], true)) {
$amount = bcadd($amount, bcmul($transaction->amount, '-1')); $amount = bcadd($amount, bcmul($transaction->amount, '-1'));
} }
Log::debug(sprintf('Amount is now %s', $amount));
return $amount; return $amount;
} }

View File

@@ -139,11 +139,13 @@
{% endif %} {% endif %}
</td> </td>
<td style=" {{ style|raw }}"> <td style=" {{ style|raw }}">
{# deposit #}
{% if transaction.transaction_type_type == 'Deposit' %} {% if transaction.transaction_type_type == 'Deposit' %}
{{ formatAmountBySymbol(transaction.amount*-1, transaction.currency_symbol, transaction.currency_decimal_places) }} {{ formatAmountBySymbol(transaction.amount*-1, transaction.currency_symbol, transaction.currency_decimal_places) }}
{% if null != transaction.foreign_amount %} {% if null != transaction.foreign_amount %}
({{ formatAmountBySymbol(transaction.foreign_amount*-1, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }}) ({{ formatAmountBySymbol(transaction.foreign_amount*-1, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }})
{% endif %} {% endif %}
{# transfer #}
{% elseif transaction.transaction_type_type == 'Transfer' %} {% elseif transaction.transaction_type_type == 'Transfer' %}
<span class="text-info"> <span class="text-info">
{{ formatAmountBySymbol(transaction.amount*-1, transaction.currency_symbol, transaction.currency_decimal_places, false) }} {{ formatAmountBySymbol(transaction.amount*-1, transaction.currency_symbol, transaction.currency_decimal_places, false) }}
@@ -151,6 +153,7 @@
({{ formatAmountBySymbol(transaction.foreign_amount*-1, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places, false) }}) ({{ formatAmountBySymbol(transaction.foreign_amount*-1, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places, false) }})
{% endif %} {% endif %}
</span> </span>
{# opening balance #}
{% elseif transaction.transaction_type_type == 'Opening balance' %} {% elseif transaction.transaction_type_type == 'Opening balance' %}
{% if transaction.source_account_type == 'Initial balance account' %} {% if transaction.source_account_type == 'Initial balance account' %}
{{ formatAmountBySymbol(transaction.amount*-1, transaction.currency_symbol, transaction.currency_decimal_places) }} {{ formatAmountBySymbol(transaction.amount*-1, transaction.currency_symbol, transaction.currency_decimal_places) }}
@@ -163,6 +166,7 @@
({{ formatAmountBySymbol(transaction.foreign_amount, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }}) ({{ formatAmountBySymbol(transaction.foreign_amount, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }})
{% endif %} {% endif %}
{% endif %} {% endif %}
{# reconciliation #}
{% elseif transaction.transaction_type_type == 'Reconciliation' %} {% elseif transaction.transaction_type_type == 'Reconciliation' %}
{% if transaction.source_account_type == 'Reconciliation account' %} {% if transaction.source_account_type == 'Reconciliation account' %}
{{ formatAmountBySymbol(transaction.amount*-1, transaction.currency_symbol, transaction.currency_decimal_places) }} {{ formatAmountBySymbol(transaction.amount*-1, transaction.currency_symbol, transaction.currency_decimal_places) }}
@@ -175,6 +179,22 @@
({{ formatAmountBySymbol(transaction.foreign_amount, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }}) ({{ formatAmountBySymbol(transaction.foreign_amount, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }})
{% endif %} {% endif %}
{% endif %} {% endif %}
{# liability credit #}
{% elseif transaction.transaction_type_type == 'Liability credit' %}
{% if transaction.source_account_type == 'Liability credit' %}
{{ formatAmountBySymbol(transaction.amount, transaction.currency_symbol, transaction.currency_decimal_places) }}
{% if null != transaction.foreign_amount %}
({{ formatAmountBySymbol(transaction.foreign_amount, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }})
{% endif %}
{% else %}
{{ formatAmountBySymbol(transaction.amount*-1, transaction.currency_symbol, transaction.currency_decimal_places) }}
{% if null != transaction.foreign_amount %}
({{ formatAmountBySymbol(transaction.foreign_amount*-1, transaction.foreign_currency_symbol, transaction.foreign_currency_decimal_places) }})
{% endif %}
{% endif %}
{# THE REST #}
{% else %} {% else %}
{{ formatAmountBySymbol(transaction.amount, transaction.currency_symbol, transaction.currency_decimal_places) }} {{ formatAmountBySymbol(transaction.amount, transaction.currency_symbol, transaction.currency_decimal_places) }}
{% if null != transaction.foreign_amount %} {% if null != transaction.foreign_amount %}