🤖 Auto commit for release 'develop' on 2025-12-07

This commit is contained in:
JC5
2025-12-07 17:22:38 +01:00
parent 346f2dfaea
commit d1332eb592
8 changed files with 102 additions and 92 deletions

View File

@@ -68,7 +68,7 @@ class JournalUpdateService
private ?Transaction $destinationTransaction = null;
private array $metaDate
= ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date',
'invoice_date',];
'invoice_date', ];
private array $metaString
= [
'sepa_cc',
@@ -112,7 +112,7 @@ class JournalUpdateService
public function setTransactionGroup(TransactionGroup $transactionGroup): void
{
$this->transactionGroup = $transactionGroup;
$this->transactionGroup = $transactionGroup;
$this->billRepository->setUser($transactionGroup->user);
$this->categoryRepository->setUser($transactionGroup->user);
$this->budgetRepository->setUser($transactionGroup->user);
@@ -183,8 +183,8 @@ class JournalUpdateService
private function hasValidSourceAccount(): bool
{
$sourceId = $this->data['source_id'] ?? null;
$sourceName = $this->data['source_name'] ?? null;
$sourceId = $this->data['source_id'] ?? null;
$sourceName = $this->data['source_name'] ?? null;
Log::debug(sprintf('Now in hasValidSourceAccount("%s","%s").', $sourceId, $sourceName));
if (!$this->hasFields(['source_id', 'source_name'])) {
@@ -199,11 +199,11 @@ class JournalUpdateService
// make a new validator.
/** @var AccountValidator $validator */
$validator = app(AccountValidator::class);
$validator = app(AccountValidator::class);
$validator->setTransactionType($expectedType);
$validator->setUser($this->transactionJournal->user);
$result = $validator->validateSource(['id' => $sourceId, 'name' => $sourceName]);
$result = $validator->validateSource(['id' => $sourceId, 'name' => $sourceName]);
Log::debug(
sprintf('hasValidSourceAccount(%d, "%s") will return %s', $sourceId, $sourceName, var_export($result, true))
);
@@ -216,7 +216,7 @@ class JournalUpdateService
private function hasFields(array $fields): bool
{
return array_any($fields, fn($field): bool => array_key_exists($field, $this->data));
return array_any($fields, fn ($field): bool => array_key_exists($field, $this->data));
}
private function getOriginalSourceAccount(): Account
@@ -261,8 +261,8 @@ class JournalUpdateService
private function hasValidDestinationAccount(): bool
{
Log::debug('Now in hasValidDestinationAccount().');
$destId = $this->data['destination_id'] ?? null;
$destName = $this->data['destination_name'] ?? null;
$destId = $this->data['destination_id'] ?? null;
$destName = $this->data['destination_name'] ?? null;
if (!$this->hasFields(['destination_id', 'destination_name'])) {
Log::debug('No destination info submitted, grab the original data.');
@@ -272,12 +272,12 @@ class JournalUpdateService
}
// make new account validator.
$expectedType = $this->getExpectedType();
$expectedType = $this->getExpectedType();
Log::debug(sprintf('(b) Expected type (new or unchanged) is %s', $expectedType));
// make a new validator.
/** @var AccountValidator $validator */
$validator = app(AccountValidator::class);
$validator = app(AccountValidator::class);
$validator->setTransactionType($expectedType);
$validator->setUser($this->transactionJournal->user);
$validator->source = $this->getValidSourceAccount();
@@ -332,7 +332,7 @@ class JournalUpdateService
return $this->getOriginalSourceAccount();
}
$sourceInfo = [
$sourceInfo = [
'id' => (int)($this->data['source_id'] ?? null),
'name' => $this->data['source_name'] ?? null,
'iban' => $this->data['source_iban'] ?? null,
@@ -360,8 +360,8 @@ class JournalUpdateService
*/
private function updateAccounts(): void
{
$source = $this->getValidSourceAccount();
$destination = $this->getValidDestinationAccount();
$source = $this->getValidSourceAccount();
$destination = $this->getValidDestinationAccount();
// cowardly refuse to update if both accounts are the same.
if ($source->id === $destination->id) {
@@ -374,7 +374,7 @@ class JournalUpdateService
$origSourceTransaction->account()->associate($source);
$origSourceTransaction->save();
$destTransaction = $this->getDestinationTransaction();
$destTransaction = $this->getDestinationTransaction();
$destTransaction->account()->associate($destination);
$destTransaction->save();
@@ -396,7 +396,7 @@ class JournalUpdateService
return $this->getOriginalDestinationAccount();
}
$destInfo = [
$destInfo = [
'id' => (int)($this->data['destination_id'] ?? null),
'name' => $this->data['destination_name'] ?? null,
'iban' => $this->data['destination_iban'] ?? null,
@@ -425,7 +425,7 @@ class JournalUpdateService
{
Log::debug('Now in updateType()');
if ($this->hasFields(['type'])) {
$type = 'opening-balance' === $this->data['type'] ? 'opening balance' : $this->data['type'];
$type = 'opening-balance' === $this->data['type'] ? 'opening balance' : $this->data['type'];
Log::debug(
sprintf(
'Trying to change journal #%d from a %s to a %s.',
@@ -458,9 +458,9 @@ class JournalUpdateService
{
$type = $this->transactionJournal->transactionType->type;
if ((
array_key_exists('bill_id', $this->data)
array_key_exists('bill_id', $this->data)
|| array_key_exists('bill_name', $this->data)
)
)
&& TransactionTypeEnum::WITHDRAWAL->value === $type
) {
$billId = (int)($this->data['bill_id'] ?? 0);
@@ -477,7 +477,7 @@ class JournalUpdateService
private function updateField(string $fieldName): void
{
if (array_key_exists($fieldName, $this->data) && '' !== (string)$this->data[$fieldName]) {
$value = $this->data[$fieldName];
$value = $this->data[$fieldName];
if ('date' === $fieldName) {
if (!$value instanceof Carbon) {
@@ -574,7 +574,7 @@ class JournalUpdateService
if ($this->hasFields([$field])) {
$value = '' === $this->data[$field] ? null : $this->data[$field];
Log::debug(sprintf('Field "%s" is present ("%s"), try to update it.', $field, $value));
$set = [
$set = [
'journal' => $this->transactionJournal,
'name' => $field,
'data' => $value,
@@ -593,7 +593,7 @@ class JournalUpdateService
if ($this->hasFields([$field])) {
try {
$value = '' === (string)$this->data[$field] ? null : new Carbon($this->data[$field]);
} catch (InvalidDateException | InvalidFormatException $e) { // @phpstan-ignore-line
} catch (InvalidDateException|InvalidFormatException $e) { // @phpstan-ignore-line
Log::debug(sprintf('%s is not a valid date value: %s', $this->data[$field], $e->getMessage()));
return;
@@ -622,19 +622,19 @@ class JournalUpdateService
if (!$this->hasFields(['currency_id', 'currency_code'])) {
return;
}
$currencyId = $this->data['currency_id'] ?? null;
$currencyCode = $this->data['currency_code'] ?? null;
$currency = $this->currencyRepository->findCurrency($currencyId, $currencyCode);
$currencyId = $this->data['currency_id'] ?? null;
$currencyCode = $this->data['currency_code'] ?? null;
$currency = $this->currencyRepository->findCurrency($currencyId, $currencyCode);
// update currency everywhere.
$this->transactionJournal->transaction_currency_id = $currency->id;
$this->transactionJournal->save();
$source = $this->getSourceTransaction();
$source->transaction_currency_id = $currency->id;
$source = $this->getSourceTransaction();
$source->transaction_currency_id = $currency->id;
$source->save();
$dest = $this->getDestinationTransaction();
$dest->transaction_currency_id = $currency->id;
$dest = $this->getDestinationTransaction();
$dest->transaction_currency_id = $currency->id;
$dest->save();
// refresh transactions.
@@ -650,7 +650,7 @@ class JournalUpdateService
return;
}
$value = $this->data['amount'] ?? '';
$value = $this->data['amount'] ?? '';
Log::debug(sprintf('Amount is now "%s"', $value));
try {
@@ -664,28 +664,31 @@ class JournalUpdateService
$origSourceTransaction->amount = app('steam')->negative($amount);
$origSourceTransaction->balance_dirty = true;
$origSourceTransaction->save();
$destTransaction = $this->getDestinationTransaction();
$originalAmount = $destTransaction->amount;
$destTransaction->amount = app('steam')->positive($amount);
$destTransaction->balance_dirty = true;
$destTransaction = $this->getDestinationTransaction();
$originalAmount = $destTransaction->amount;
$destTransaction->amount = app('steam')->positive($amount);
$destTransaction->balance_dirty = true;
$destTransaction->save();
// refresh transactions.
$this->sourceTransaction->refresh();
$this->destinationTransaction->refresh();
Log::debug(sprintf('Updated amount to "%s"', $amount));
event(new TriggeredAuditLog($this->transactionGroup->user, $this->transactionGroup, 'update_amount',
[
'currency_symbol' => $destTransaction->transactionCurrency->symbol,
'decimal_places' => $destTransaction->transactionCurrency->decimal_places,
'amount' => $originalAmount,
],
[
'currency_symbol' => $destTransaction->transactionCurrency->symbol,
'decimal_places' => $destTransaction->transactionCurrency->decimal_places,
'amount' => $value,
]
));
event(new TriggeredAuditLog(
$this->transactionGroup->user,
$this->transactionGroup,
'update_amount',
[
'currency_symbol' => $destTransaction->transactionCurrency->symbol,
'decimal_places' => $destTransaction->transactionCurrency->decimal_places,
'amount' => $originalAmount,
],
[
'currency_symbol' => $destTransaction->transactionCurrency->symbol,
'decimal_places' => $destTransaction->transactionCurrency->decimal_places,
'amount' => $value,
]
));
}
@@ -724,9 +727,9 @@ class JournalUpdateService
// if the transaction is a TRANSFER, and the foreign amount and currency are set (like they seem to be)
// the correct fields to update in the destination transaction are NOT the foreign amount and currency
// but rather the normal amount and currency. This is new behavior.
$isTransfer = TransactionTypeEnum::TRANSFER->value === $this->transactionJournal->transactionType->type;
$isTransfer = TransactionTypeEnum::TRANSFER->value === $this->transactionJournal->transactionType->type;
// also check if it is not between an asset account and a liability, because then the same rule applies.
$isBetween = $this->isBetweenAssetAndLiability();
$isBetween = $this->isBetweenAssetAndLiability();
if ($isTransfer || $isBetween) {
Log::debug('Switch amounts, store in amount and not foreign_amount');
@@ -762,8 +765,8 @@ class JournalUpdateService
$source->foreign_amount = null;
$source->save();
$dest->foreign_currency_id = null;
$dest->foreign_amount = null;
$dest->foreign_currency_id = null;
$dest->foreign_amount = null;
$dest->save();
Log::debug(sprintf('Foreign amount is "%s" so remove foreign amount info.', $amount));
}
@@ -777,7 +780,7 @@ class JournalUpdateService
private function isBetweenAssetAndLiability(): bool
{
/** @var null|Transaction $sourceTransaction */
$sourceTransaction = $this->transactionJournal->transactions()->where('amount', '<', 0)->first();
$sourceTransaction = $this->transactionJournal->transactions()->where('amount', '<', 0)->first();
/** @var null|Transaction $destinationTransaction */
$destinationTransaction = $this->transactionJournal->transactions()->where('amount', '>', 0)->first();
@@ -792,15 +795,15 @@ class JournalUpdateService
return false;
}
$source = $sourceTransaction->account;
$destination = $destinationTransaction->account;
$source = $sourceTransaction->account;
$destination = $destinationTransaction->account;
if (null === $source || null === $destination) {
Log::warning('Either is false, stop.');
return false;
}
$sourceTypes = [AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value];
$sourceTypes = [AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value];
// source is liability, destination is asset
if (in_array($source->accountType->type, $sourceTypes, true) && AccountTypeEnum::ASSET->value === $destination->accountType->type) {