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
{
app('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 %s', __METHOD__));
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;
// can we update account data using the new type?
if ($this->hasValidAccounts()) {
app('log')->info('Account info is valid, now update.');
Log::info('Account info is valid, now update.');
// update accounts:
$this->updateAccounts();
@@ -175,14 +175,15 @@ class JournalUpdateService
private function hasValidAccounts(): bool
{
Log::debug('Now in hasValidAccounts().');
return $this->hasValidSourceAccount() && $this->hasValidDestinationAccount();
}
private function hasValidSourceAccount(): bool
{
app('log')->debug('Now in hasValidSourceAccount().');
$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'])) {
$origSourceAccount = $this->getOriginalSourceAccount();
@@ -192,7 +193,7 @@ class JournalUpdateService
// make new account validator.
$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.
/** @var AccountValidator $validator */
@@ -200,8 +201,8 @@ class JournalUpdateService
$validator->setTransactionType($expectedType);
$validator->setUser($this->transactionJournal->user);
$result = $validator->validateSource(['id' => $sourceId]);
app('log')->debug(
$result = $validator->validateSource(['id' => $sourceId, 'name' => $sourceName]);
Log::debug(
sprintf('hasValidSourceAccount(%d, "%s") will return %s', $sourceId, $sourceName, var_export($result, true))
);
@@ -241,7 +242,7 @@ class JournalUpdateService
0
)->first();
}
app('log')->debug(sprintf('getSourceTransaction: %s', $this->sourceTransaction->amount));
Log::debug(sprintf('getSourceTransaction: %s', $this->sourceTransaction->amount));
return $this->sourceTransaction;
}
@@ -255,7 +256,7 @@ class JournalUpdateService
*/
private function getExpectedType(): string
{
app('log')->debug('Now in getExpectedType()');
Log::debug('Now in getExpectedType()');
if ($this->hasFields(['type'])) {
return ucfirst('opening-balance' === $this->data['type'] ? 'opening balance' : $this->data['type']);
}
@@ -265,12 +266,12 @@ class JournalUpdateService
private function hasValidDestinationAccount(): bool
{
app('log')->debug('Now in hasValidDestinationAccount().');
Log::debug('Now in hasValidDestinationAccount().');
$destId = $this->data['destination_id'] ?? null;
$destName = $this->data['destination_name'] ?? null;
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();
$destId = $destination->id;
$destName = $destination->name;
@@ -278,7 +279,7 @@ class JournalUpdateService
// make new account validator.
$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.
/** @var AccountValidator $validator */
@@ -287,7 +288,7 @@ class JournalUpdateService
$validator->setUser($this->transactionJournal->user);
$validator->source = $this->getValidSourceAccount();
$result = $validator->validateDestination(['id' => $destId, 'name' => $destName]);
app('log')->debug(
Log::debug(
sprintf(
'hasValidDestinationAccount(%d, "%s") will return %s',
$destId,
@@ -329,7 +330,7 @@ class JournalUpdateService
*/
private function getValidSourceAccount(): Account
{
app('log')->debug('Now in getValidSourceAccount().');
Log::debug('Now in getValidSourceAccount().');
if (!$this->hasFields(['source_id', 'source_name'])) {
return $this->getOriginalSourceAccount();
@@ -348,12 +349,12 @@ class JournalUpdateService
try {
$result = $this->getAccount($expectedType, 'source', $sourceInfo);
} 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();
}
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;
}
@@ -368,7 +369,7 @@ class JournalUpdateService
// cowardly refuse to update if both accounts are the same.
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;
}
@@ -384,8 +385,8 @@ class JournalUpdateService
// refresh transactions.
$this->sourceTransaction->refresh();
$this->destinationTransaction->refresh();
app('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 source to #%d ("%s")', $source->id, $source->name));
Log::debug(sprintf('Will set dest to #%d ("%s")', $destination->id, $destination->name));
}
/**
@@ -393,7 +394,7 @@ class JournalUpdateService
*/
private function getValidDestinationAccount(): Account
{
app('log')->debug('Now in getValidDestinationAccount().');
Log::debug('Now in getValidDestinationAccount().');
if (!$this->hasFields(['destination_id', 'destination_name'])) {
return $this->getOriginalDestinationAccount();
@@ -409,12 +410,12 @@ class JournalUpdateService
// make new account validator.
$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 {
$result = $this->getAccount($expectedType, 'destination', $destInfo);
} 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();
}
@@ -426,10 +427,10 @@ class JournalUpdateService
*/
private function updateType(): void
{
app('log')->debug('Now in updateType()');
Log::debug('Now in updateType()');
if ($this->hasFields(['type'])) {
$type = 'opening-balance' === $this->data['type'] ? 'opening balance' : $this->data['type'];
app('log')->debug(
Log::debug(
sprintf(
'Trying to change journal #%d from a %s to a %s.',
$this->transactionJournal->id,
@@ -442,7 +443,7 @@ class JournalUpdateService
$typeFactory = app(TransactionTypeFactory::class);
$result = $typeFactory->find($this->data['type']);
if (null !== $result) {
app('log')->debug('Changed transaction type!');
Log::debug('Changed transaction type!');
$this->transactionJournal->transaction_type_id = $result->id;
$this->transactionJournal->save();
@@ -451,7 +452,7 @@ class JournalUpdateService
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'] ?? '');
$bill = $this->billRepository->findBill($billId, $billName);
$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.
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');
}
event(
@@ -508,7 +509,7 @@ class JournalUpdateService
);
$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
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));
}
@@ -526,7 +527,7 @@ class JournalUpdateService
{
// update budget
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));
}
// is transfer? remove budget
@@ -538,7 +539,7 @@ class JournalUpdateService
private function updateTags(): void
{
if ($this->hasFields(['tags'])) {
app('log')->debug('Will update tags.');
Log::debug('Will update tags.');
$tags = $this->data['tags'] ?? null;
$this->storeTags($this->transactionJournal, $tags);
}
@@ -565,13 +566,13 @@ class JournalUpdateService
// update meta fields.
// first string
if ($this->hasFields($this->metaString)) {
app('log')->debug('Meta string fields are present.');
Log::debug('Meta string fields are present.');
$this->updateMetaFields();
}
// then date fields.
if ($this->hasFields($this->metaDate)) {
app('log')->debug('Meta date fields are present.');
Log::debug('Meta date fields are present.');
$this->updateMetaDateFields();
}
}
@@ -584,7 +585,7 @@ class JournalUpdateService
foreach ($this->metaString as $field) {
if ($this->hasFields([$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 = [
'journal' => $this->transactionJournal,
'name' => $field,
@@ -605,11 +606,11 @@ class JournalUpdateService
try {
$value = '' === (string) $this->data[$field] ? null : new Carbon($this->data[$field]);
} 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;
}
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 = [
'journal' => $this->transactionJournal,
'name' => $field,
@@ -651,23 +652,23 @@ class JournalUpdateService
// refresh transactions.
$this->sourceTransaction->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
{
app('log')->debug(sprintf('Now in %s', __METHOD__));
Log::debug(sprintf('Now in %s', __METHOD__));
if (!$this->hasFields(['amount'])) {
return;
}
$value = $this->data['amount'] ?? '';
app('log')->debug(sprintf('Amount is now "%s"', $value));
Log::debug(sprintf('Amount is now "%s"', $value));
try {
$amount = $this->getAmount($value);
} 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;
}
@@ -683,7 +684,7 @@ class JournalUpdateService
// refresh transactions.
$this->sourceTransaction->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
@@ -707,7 +708,7 @@ class JournalUpdateService
// not the same as normal currency
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;
}
@@ -736,7 +737,7 @@ class JournalUpdateService
$dest->save();
app('log')->debug(
Log::debug(
sprintf(
'Update foreign info to %s (#%d) %s',
$foreignCurrency->code,
@@ -759,9 +760,9 @@ class JournalUpdateService
$dest->foreign_currency_id = null;
$dest->foreign_amount = null;
$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.
$this->sourceTransaction->refresh();