James Cole
2024-11-24 15:23:17 +01:00
parent 25c1ca2f5d
commit 91a2a1afc3
2 changed files with 49 additions and 47 deletions

View File

@@ -132,14 +132,14 @@ class JournalUpdateService
public function update(): void public function update(): void
{ {
app('log')->debug(sprintf('Now in %s', __METHOD__)); Log::debug(sprintf('Now in %s', __METHOD__));
app('log')->debug(sprintf('Now in JournalUpdateService for journal #%d.', $this->transactionJournal->id)); Log::debug(sprintf('Now in JournalUpdateService for journal #%d.', $this->transactionJournal->id));
$this->data['reconciled'] = array_key_exists('reconciled', $this->data) ? $this->data['reconciled'] : null; $this->data['reconciled'] = array_key_exists('reconciled', $this->data) ? $this->data['reconciled'] : null;
// can we update account data using the new type? // can we update account data using the new type?
if ($this->hasValidAccounts()) { if ($this->hasValidAccounts()) {
app('log')->info('Account info is valid, now update.'); Log::info('Account info is valid, now update.');
// update accounts: // update accounts:
$this->updateAccounts(); $this->updateAccounts();
@@ -175,14 +175,15 @@ class JournalUpdateService
private function hasValidAccounts(): bool private function hasValidAccounts(): bool
{ {
Log::debug('Now in hasValidAccounts().');
return $this->hasValidSourceAccount() && $this->hasValidDestinationAccount(); return $this->hasValidSourceAccount() && $this->hasValidDestinationAccount();
} }
private function hasValidSourceAccount(): bool private function hasValidSourceAccount(): bool
{ {
app('log')->debug('Now in hasValidSourceAccount().');
$sourceId = $this->data['source_id'] ?? null; $sourceId = $this->data['source_id'] ?? null;
$sourceName = $this->data['source_name'] ?? null; $sourceName = $this->data['source_name'] ?? null;
Log::debug(sprintf('Now in hasValidSourceAccount("%s","%s").', $sourceId, $sourceName));
if (!$this->hasFields(['source_id', 'source_name'])) { if (!$this->hasFields(['source_id', 'source_name'])) {
$origSourceAccount = $this->getOriginalSourceAccount(); $origSourceAccount = $this->getOriginalSourceAccount();
@@ -192,7 +193,7 @@ class JournalUpdateService
// make new account validator. // make new account validator.
$expectedType = $this->getExpectedType(); $expectedType = $this->getExpectedType();
app('log')->debug(sprintf('(a) Expected type (new or unchanged) is %s', $expectedType)); Log::debug(sprintf('(a) Expected type (new or unchanged) is %s', $expectedType));
// make a new validator. // make a new validator.
/** @var AccountValidator $validator */ /** @var AccountValidator $validator */
@@ -200,8 +201,8 @@ class JournalUpdateService
$validator->setTransactionType($expectedType); $validator->setTransactionType($expectedType);
$validator->setUser($this->transactionJournal->user); $validator->setUser($this->transactionJournal->user);
$result = $validator->validateSource(['id' => $sourceId]); $result = $validator->validateSource(['id' => $sourceId, 'name' => $sourceName]);
app('log')->debug( Log::debug(
sprintf('hasValidSourceAccount(%d, "%s") will return %s', $sourceId, $sourceName, var_export($result, true)) sprintf('hasValidSourceAccount(%d, "%s") will return %s', $sourceId, $sourceName, var_export($result, true))
); );
@@ -241,7 +242,7 @@ class JournalUpdateService
0 0
)->first(); )->first();
} }
app('log')->debug(sprintf('getSourceTransaction: %s', $this->sourceTransaction->amount)); Log::debug(sprintf('getSourceTransaction: %s', $this->sourceTransaction->amount));
return $this->sourceTransaction; return $this->sourceTransaction;
} }
@@ -255,7 +256,7 @@ class JournalUpdateService
*/ */
private function getExpectedType(): string private function getExpectedType(): string
{ {
app('log')->debug('Now in getExpectedType()'); Log::debug('Now in getExpectedType()');
if ($this->hasFields(['type'])) { if ($this->hasFields(['type'])) {
return ucfirst('opening-balance' === $this->data['type'] ? 'opening balance' : $this->data['type']); return ucfirst('opening-balance' === $this->data['type'] ? 'opening balance' : $this->data['type']);
} }
@@ -265,12 +266,12 @@ class JournalUpdateService
private function hasValidDestinationAccount(): bool private function hasValidDestinationAccount(): bool
{ {
app('log')->debug('Now in hasValidDestinationAccount().'); Log::debug('Now in hasValidDestinationAccount().');
$destId = $this->data['destination_id'] ?? null; $destId = $this->data['destination_id'] ?? null;
$destName = $this->data['destination_name'] ?? null; $destName = $this->data['destination_name'] ?? null;
if (!$this->hasFields(['destination_id', 'destination_name'])) { if (!$this->hasFields(['destination_id', 'destination_name'])) {
app('log')->debug('No destination info submitted, grab the original data.'); Log::debug('No destination info submitted, grab the original data.');
$destination = $this->getOriginalDestinationAccount(); $destination = $this->getOriginalDestinationAccount();
$destId = $destination->id; $destId = $destination->id;
$destName = $destination->name; $destName = $destination->name;
@@ -278,7 +279,7 @@ class JournalUpdateService
// make new account validator. // make new account validator.
$expectedType = $this->getExpectedType(); $expectedType = $this->getExpectedType();
app('log')->debug(sprintf('(b) Expected type (new or unchanged) is %s', $expectedType)); Log::debug(sprintf('(b) Expected type (new or unchanged) is %s', $expectedType));
// make a new validator. // make a new validator.
/** @var AccountValidator $validator */ /** @var AccountValidator $validator */
@@ -287,7 +288,7 @@ class JournalUpdateService
$validator->setUser($this->transactionJournal->user); $validator->setUser($this->transactionJournal->user);
$validator->source = $this->getValidSourceAccount(); $validator->source = $this->getValidSourceAccount();
$result = $validator->validateDestination(['id' => $destId, 'name' => $destName]); $result = $validator->validateDestination(['id' => $destId, 'name' => $destName]);
app('log')->debug( Log::debug(
sprintf( sprintf(
'hasValidDestinationAccount(%d, "%s") will return %s', 'hasValidDestinationAccount(%d, "%s") will return %s',
$destId, $destId,
@@ -329,7 +330,7 @@ class JournalUpdateService
*/ */
private function getValidSourceAccount(): Account private function getValidSourceAccount(): Account
{ {
app('log')->debug('Now in getValidSourceAccount().'); Log::debug('Now in getValidSourceAccount().');
if (!$this->hasFields(['source_id', 'source_name'])) { if (!$this->hasFields(['source_id', 'source_name'])) {
return $this->getOriginalSourceAccount(); return $this->getOriginalSourceAccount();
@@ -348,12 +349,12 @@ class JournalUpdateService
try { try {
$result = $this->getAccount($expectedType, 'source', $sourceInfo); $result = $this->getAccount($expectedType, 'source', $sourceInfo);
} catch (FireflyException $e) { } catch (FireflyException $e) {
app('log')->error(sprintf('Cant get the valid source account: %s', $e->getMessage())); Log::error(sprintf('Cant get the valid source account: %s', $e->getMessage()));
$result = $this->getOriginalSourceAccount(); $result = $this->getOriginalSourceAccount();
} }
app('log')->debug(sprintf('getValidSourceAccount() will return #%d ("%s")', $result->id, $result->name)); Log::debug(sprintf('getValidSourceAccount() will return #%d ("%s")', $result->id, $result->name));
return $result; return $result;
} }
@@ -368,7 +369,7 @@ class JournalUpdateService
// cowardly refuse to update if both accounts are the same. // cowardly refuse to update if both accounts are the same.
if ($source->id === $destination->id) { if ($source->id === $destination->id) {
app('log')->error(sprintf('Source + dest accounts are equal (%d, "%s")', $source->id, $source->name)); Log::error(sprintf('Source + dest accounts are equal (%d, "%s")', $source->id, $source->name));
return; return;
} }
@@ -384,8 +385,8 @@ class JournalUpdateService
// refresh transactions. // refresh transactions.
$this->sourceTransaction->refresh(); $this->sourceTransaction->refresh();
$this->destinationTransaction->refresh(); $this->destinationTransaction->refresh();
app('log')->debug(sprintf('Will set source to #%d ("%s")', $source->id, $source->name)); Log::debug(sprintf('Will set source to #%d ("%s")', $source->id, $source->name));
app('log')->debug(sprintf('Will set dest to #%d ("%s")', $destination->id, $destination->name)); Log::debug(sprintf('Will set dest to #%d ("%s")', $destination->id, $destination->name));
} }
/** /**
@@ -393,7 +394,7 @@ class JournalUpdateService
*/ */
private function getValidDestinationAccount(): Account private function getValidDestinationAccount(): Account
{ {
app('log')->debug('Now in getValidDestinationAccount().'); Log::debug('Now in getValidDestinationAccount().');
if (!$this->hasFields(['destination_id', 'destination_name'])) { if (!$this->hasFields(['destination_id', 'destination_name'])) {
return $this->getOriginalDestinationAccount(); return $this->getOriginalDestinationAccount();
@@ -409,12 +410,12 @@ class JournalUpdateService
// make new account validator. // make new account validator.
$expectedType = $this->getExpectedType(); $expectedType = $this->getExpectedType();
app('log')->debug(sprintf('(c) Expected type (new or unchanged) is %s', $expectedType)); Log::debug(sprintf('(c) Expected type (new or unchanged) is %s', $expectedType));
try { try {
$result = $this->getAccount($expectedType, 'destination', $destInfo); $result = $this->getAccount($expectedType, 'destination', $destInfo);
} catch (FireflyException $e) { } catch (FireflyException $e) {
app('log')->error(sprintf('getValidDestinationAccount() threw unexpected error: %s', $e->getMessage())); Log::error(sprintf('getValidDestinationAccount() threw unexpected error: %s', $e->getMessage()));
$result = $this->getOriginalDestinationAccount(); $result = $this->getOriginalDestinationAccount();
} }
@@ -426,10 +427,10 @@ class JournalUpdateService
*/ */
private function updateType(): void private function updateType(): void
{ {
app('log')->debug('Now in updateType()'); Log::debug('Now in updateType()');
if ($this->hasFields(['type'])) { 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'];
app('log')->debug( Log::debug(
sprintf( sprintf(
'Trying to change journal #%d from a %s to a %s.', 'Trying to change journal #%d from a %s to a %s.',
$this->transactionJournal->id, $this->transactionJournal->id,
@@ -442,7 +443,7 @@ class JournalUpdateService
$typeFactory = app(TransactionTypeFactory::class); $typeFactory = app(TransactionTypeFactory::class);
$result = $typeFactory->find($this->data['type']); $result = $typeFactory->find($this->data['type']);
if (null !== $result) { if (null !== $result) {
app('log')->debug('Changed transaction type!'); Log::debug('Changed transaction type!');
$this->transactionJournal->transaction_type_id = $result->id; $this->transactionJournal->transaction_type_id = $result->id;
$this->transactionJournal->save(); $this->transactionJournal->save();
@@ -451,7 +452,7 @@ class JournalUpdateService
return; return;
} }
app('log')->debug('No type field present.'); Log::debug('No type field present.');
} }
/** /**
@@ -470,7 +471,7 @@ class JournalUpdateService
$billName = (string) ($this->data['bill_name'] ?? ''); $billName = (string) ($this->data['bill_name'] ?? '');
$bill = $this->billRepository->findBill($billId, $billName); $bill = $this->billRepository->findBill($billId, $billName);
$this->transactionJournal->bill_id = $bill?->id; $this->transactionJournal->bill_id = $bill?->id;
app('log')->debug('Updated bill ID'); Log::debug('Updated bill ID');
} }
} }
@@ -494,7 +495,7 @@ class JournalUpdateService
} }
// do some parsing. // do some parsing.
app('log')->debug(sprintf('Create date value from string "%s".', $value)); Log::debug(sprintf('Create date value from string "%s".', $value));
$this->transactionJournal->date_tz = $value->format('e'); $this->transactionJournal->date_tz = $value->format('e');
} }
event( event(
@@ -508,7 +509,7 @@ class JournalUpdateService
); );
$this->transactionJournal->{$fieldName} = $value; // @phpstan-ignore-line $this->transactionJournal->{$fieldName} = $value; // @phpstan-ignore-line
app('log')->debug(sprintf('Updated %s', $fieldName)); Log::debug(sprintf('Updated %s', $fieldName));
} }
} }
@@ -516,7 +517,7 @@ class JournalUpdateService
{ {
// update category // update category
if ($this->hasFields(['category_id', 'category_name'])) { if ($this->hasFields(['category_id', 'category_name'])) {
app('log')->debug('Will update category.'); Log::debug('Will update category.');
$this->storeCategory($this->transactionJournal, new NullArrayObject($this->data)); $this->storeCategory($this->transactionJournal, new NullArrayObject($this->data));
} }
@@ -526,7 +527,7 @@ class JournalUpdateService
{ {
// update budget // update budget
if ($this->hasFields(['budget_id', 'budget_name'])) { if ($this->hasFields(['budget_id', 'budget_name'])) {
app('log')->debug('Will update budget.'); Log::debug('Will update budget.');
$this->storeBudget($this->transactionJournal, new NullArrayObject($this->data)); $this->storeBudget($this->transactionJournal, new NullArrayObject($this->data));
} }
// is transfer? remove budget // is transfer? remove budget
@@ -538,7 +539,7 @@ class JournalUpdateService
private function updateTags(): void private function updateTags(): void
{ {
if ($this->hasFields(['tags'])) { if ($this->hasFields(['tags'])) {
app('log')->debug('Will update tags.'); Log::debug('Will update tags.');
$tags = $this->data['tags'] ?? null; $tags = $this->data['tags'] ?? null;
$this->storeTags($this->transactionJournal, $tags); $this->storeTags($this->transactionJournal, $tags);
} }
@@ -565,13 +566,13 @@ class JournalUpdateService
// update meta fields. // update meta fields.
// first string // first string
if ($this->hasFields($this->metaString)) { if ($this->hasFields($this->metaString)) {
app('log')->debug('Meta string fields are present.'); Log::debug('Meta string fields are present.');
$this->updateMetaFields(); $this->updateMetaFields();
} }
// then date fields. // then date fields.
if ($this->hasFields($this->metaDate)) { if ($this->hasFields($this->metaDate)) {
app('log')->debug('Meta date fields are present.'); Log::debug('Meta date fields are present.');
$this->updateMetaDateFields(); $this->updateMetaDateFields();
} }
} }
@@ -584,7 +585,7 @@ class JournalUpdateService
foreach ($this->metaString as $field) { foreach ($this->metaString as $field) {
if ($this->hasFields([$field])) { if ($this->hasFields([$field])) {
$value = '' === $this->data[$field] ? null : $this->data[$field]; $value = '' === $this->data[$field] ? null : $this->data[$field];
app('log')->debug(sprintf('Field "%s" is present ("%s"), try to update it.', $field, $value)); Log::debug(sprintf('Field "%s" is present ("%s"), try to update it.', $field, $value));
$set = [ $set = [
'journal' => $this->transactionJournal, 'journal' => $this->transactionJournal,
'name' => $field, 'name' => $field,
@@ -605,11 +606,11 @@ class JournalUpdateService
try { try {
$value = '' === (string) $this->data[$field] ? null : new Carbon($this->data[$field]); $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
app('log')->debug(sprintf('%s is not a valid date value: %s', $this->data[$field], $e->getMessage())); Log::debug(sprintf('%s is not a valid date value: %s', $this->data[$field], $e->getMessage()));
return; return;
} }
app('log')->debug(sprintf('Field "%s" is present ("%s"), try to update it.', $field, $value)); Log::debug(sprintf('Field "%s" is present ("%s"), try to update it.', $field, $value));
$set = [ $set = [
'journal' => $this->transactionJournal, 'journal' => $this->transactionJournal,
'name' => $field, 'name' => $field,
@@ -651,23 +652,23 @@ class JournalUpdateService
// refresh transactions. // refresh transactions.
$this->sourceTransaction->refresh(); $this->sourceTransaction->refresh();
$this->destinationTransaction->refresh(); $this->destinationTransaction->refresh();
app('log')->debug(sprintf('Updated currency to #%d (%s)', $currency->id, $currency->code)); Log::debug(sprintf('Updated currency to #%d (%s)', $currency->id, $currency->code));
} }
private function updateAmount(): void private function updateAmount(): void
{ {
app('log')->debug(sprintf('Now in %s', __METHOD__)); Log::debug(sprintf('Now in %s', __METHOD__));
if (!$this->hasFields(['amount'])) { if (!$this->hasFields(['amount'])) {
return; return;
} }
$value = $this->data['amount'] ?? ''; $value = $this->data['amount'] ?? '';
app('log')->debug(sprintf('Amount is now "%s"', $value)); Log::debug(sprintf('Amount is now "%s"', $value));
try { try {
$amount = $this->getAmount($value); $amount = $this->getAmount($value);
} catch (FireflyException $e) { } catch (FireflyException $e) {
app('log')->debug(sprintf('getAmount("%s") returns error: %s', $value, $e->getMessage())); Log::debug(sprintf('getAmount("%s") returns error: %s', $value, $e->getMessage()));
return; return;
} }
@@ -683,7 +684,7 @@ class JournalUpdateService
// refresh transactions. // refresh transactions.
$this->sourceTransaction->refresh(); $this->sourceTransaction->refresh();
$this->destinationTransaction->refresh(); $this->destinationTransaction->refresh();
app('log')->debug(sprintf('Updated amount to "%s"', $amount)); Log::debug(sprintf('Updated amount to "%s"', $amount));
} }
private function updateForeignAmount(): void private function updateForeignAmount(): void
@@ -707,7 +708,7 @@ class JournalUpdateService
// not the same as normal currency // not the same as normal currency
if (null !== $foreignCurrency && $foreignCurrency->id === $this->transactionJournal->transaction_currency_id) { if (null !== $foreignCurrency && $foreignCurrency->id === $this->transactionJournal->transaction_currency_id) {
app('log')->error(sprintf('Foreign currency is equal to normal currency (%s)', $foreignCurrency->code)); Log::error(sprintf('Foreign currency is equal to normal currency (%s)', $foreignCurrency->code));
return; return;
} }
@@ -736,7 +737,7 @@ class JournalUpdateService
$dest->save(); $dest->save();
app('log')->debug( Log::debug(
sprintf( sprintf(
'Update foreign info to %s (#%d) %s', 'Update foreign info to %s (#%d) %s',
$foreignCurrency->code, $foreignCurrency->code,
@@ -759,9 +760,9 @@ class JournalUpdateService
$dest->foreign_currency_id = null; $dest->foreign_currency_id = null;
$dest->foreign_amount = null; $dest->foreign_amount = null;
$dest->save(); $dest->save();
app('log')->debug(sprintf('Foreign amount is "%s" so remove foreign amount info.', $amount)); Log::debug(sprintf('Foreign amount is "%s" so remove foreign amount info.', $amount));
} }
app('log')->info('Not enough info to update foreign currency info.'); Log::info('Not enough info to update foreign currency info.');
// refresh transactions. // refresh transactions.
$this->sourceTransaction->refresh(); $this->sourceTransaction->refresh();

View File

@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Validation; namespace FireflyIII\Validation;
use FireflyIII\Enums\AccountTypeEnum;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
@@ -238,7 +239,7 @@ class AccountValidator
protected function canCreateType(string $accountType): bool protected function canCreateType(string $accountType): bool
{ {
$canCreate = [AccountType::EXPENSE, AccountType::REVENUE, AccountType::INITIAL_BALANCE, AccountType::LIABILITY_CREDIT]; $canCreate = [AccountTypeEnum::EXPENSE->value, AccountTypeEnum::REVENUE->value, AccountTypeEnum::INITIAL_BALANCE->value, AccountTypeEnum::LIABILITY_CREDIT->value];
if (in_array($accountType, $canCreate, true)) { if (in_array($accountType, $canCreate, true)) {
return true; return true;
} }