Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Fix call to old object.
James Cole4a3f62df896f
diff --git a/app/Services/Internal/Update/JournalUpdateService.php b/app/Services/Internal/Update/JournalUpdateService.phpindex c1c722b618..a814c4c553 100644--- a/app/Services/Internal/Update/JournalUpdateService.php+++ b/app/Services/Internal/Update/JournalUpdateService.php@@ -48,7 +48,6 @@ use FireflyIII\Services\Internal\Support\JournalServiceTrait;use FireflyIII\Support\Facades\FireflyConfig;use FireflyIII\Support\Facades\Preferences;use FireflyIII\Support\Facades\Steam;-use FireflyIII\Support\NullArrayObject;use FireflyIII\Validation\AccountValidator;use Illuminate\Support\Facades\Log;@@ -61,41 +60,43 @@ class JournalUpdateService{use JournalServiceTrait;- private BillRepositoryInterface $billRepository;- private CurrencyRepositoryInterface $currencyRepository;+ private BillRepositoryInterface $billRepository;+ private CurrencyRepositoryInterface $currencyRepository;private TransactionGroupRepositoryInterface $transactionGroupRepository;- private array $data;- private ?Account $destinationAccount = null;- private ?Transaction $destinationTransaction = null;- private array $metaDate = [- 'interest_date',- 'book_date',- 'process_date',- 'due_date',- 'payment_date',- 'invoice_date',- '_internal_previous_date',- ];- private array $metaString = [- 'sepa_cc',- 'sepa_ct_op',- 'sepa_ct_id',- 'sepa_db',- 'sepa_country',- 'sepa_ep',- 'sepa_ci',- 'sepa_batch_id',- 'recurrence_id',- 'internal_reference',- 'bunq_payment_id',- 'external_id',- 'external_url',- ];- private ?Account $sourceAccount = null;- private ?Transaction $sourceTransaction = null;- private ?TransactionGroup $transactionGroup = null;- private ?TransactionJournal $transactionJournal = null;- private string $startCompareHash = '';+ private array $data;+ private ?Account $destinationAccount = null;+ private ?Transaction $destinationTransaction = null;+ private array $metaDate+ = [+ 'interest_date',+ 'book_date',+ 'process_date',+ 'due_date',+ 'payment_date',+ 'invoice_date',+ '_internal_previous_date',+ ];+ private array $metaString+ = [+ 'sepa_cc',+ 'sepa_ct_op',+ 'sepa_ct_id',+ 'sepa_db',+ 'sepa_country',+ 'sepa_ep',+ 'sepa_ci',+ 'sepa_batch_id',+ 'recurrence_id',+ 'internal_reference',+ 'bunq_payment_id',+ 'external_id',+ 'external_url',+ ];+ private ?Account $sourceAccount = null;+ private ?Transaction $sourceTransaction = null;+ private ?TransactionGroup $transactionGroup = null;+ private ?TransactionJournal $transactionJournal = null;+ private string $startCompareHash = '';/*** JournalUpdateService constructor.@@ -118,7 +119,7 @@ class JournalUpdateServicepublic 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);@@ -189,8 +190,8 @@ class JournalUpdateServiceprivate 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'])) {@@ -205,11 +206,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)));// TODO type overrule the account validator may have a different opinion on the transaction type.@@ -220,7 +221,7 @@ class JournalUpdateServiceprivate 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@@ -241,8 +242,7 @@ class JournalUpdateService->transactions()->with(['account'])->where('amount', '<', 0)- ->first()- ;+ ->first();$this->sourceTransaction = $result;}Log::debug(sprintf('getSourceTransaction: %s', $this->sourceTransaction->amount));@@ -270,8 +270,8 @@ class JournalUpdateServiceprivate 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.');@@ -281,16 +281,16 @@ 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();- $result = $validator->validateDestination(['id' => $destId, 'name' => $destName]);+ $result = $validator->validateDestination(['id' => $destId, 'name' => $destName]);Log::debug(sprintf('hasValidDestinationAccount(%d, "%s") will return %s', $destId, $destName, var_export($result, true)));// TODO typeOverrule: the account validator may have another opinion on the transaction type.@@ -319,8 +319,7 @@ class JournalUpdateService$result = $this->transactionJournal->transactions()->where('amount', '>', 0)- ->first()- ;+ ->first();$this->destinationTransaction = $result;}@@ -338,8 +337,8 @@ class JournalUpdateServicereturn $this->getOriginalSourceAccount();}- $sourceInfo = [- 'id' => (int) ($this->data['source_id'] ?? null),+ $sourceInfo = [+ 'id' => (int)($this->data['source_id'] ?? null),'name' => $this->data['source_name'] ?? null,'iban' => $this->data['source_iban'] ?? null,'number' => $this->data['source_number'] ?? null,@@ -366,8 +365,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) {@@ -380,7 +379,7 @@ class JournalUpdateService$origSourceTransaction->account()->associate($source);$origSourceTransaction->save();- $destTransaction = $this->getDestinationTransaction();+ $destTransaction = $this->getDestinationTransaction();$destTransaction->account()->associate($destination);$destTransaction->save();@@ -402,8 +401,8 @@ class JournalUpdateServicereturn $this->getOriginalDestinationAccount();}- $destInfo = [- 'id' => (int) ($this->data['destination_id'] ?? null),+ $destInfo = [+ 'id' => (int)($this->data['destination_id'] ?? null),'name' => $this->data['destination_name'] ?? null,'iban' => $this->data['destination_iban'] ?? null,'number' => $this->data['destination_number'] ?? null,@@ -431,13 +430,13 @@ 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.',- $this->transactionJournal->id,- $this->transactionJournal->transactionType->type,- $type- ));+ 'Trying to change journal #%d from a %s to a %s.',+ $this->transactionJournal->id,+ $this->transactionJournal->transactionType->type,+ $type+ ));/** @var TransactionTypeFactory $typeFactory */$typeFactory = app(TransactionTypeFactory::class);@@ -462,8 +461,8 @@ class JournalUpdateService{$type = $this->transactionJournal->transactionType->type;if ((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);- $billName = (string) ($this->data['bill_name'] ?? '');+ $billId = (int)($this->data['bill_id'] ?? 0);+ $billName = (string)($this->data['bill_name'] ?? '');$bill = $this->billRepository->findBill($billId, $billName);$this->transactionJournal->bill_id = $bill?->id;Log::debug('Updated bill ID');@@ -475,8 +474,8 @@ class JournalUpdateService*/private function updateField(string $fieldName): void{- if (array_key_exists($fieldName, $this->data) && '' !== (string) $this->data[$fieldName]) {- $value = $this->data[$fieldName];+ if (array_key_exists($fieldName, $this->data) && '' !== (string)$this->data[$fieldName]) {+ $value = $this->data[$fieldName];if ('date' === $fieldName) {if (!$value instanceof Carbon) {@@ -496,8 +495,8 @@ class JournalUpdateServiceLog::debug(sprintf('Old date: %s, new date: %s', $this->transactionJournal->date->toW3cString(), $value->toW3cString()));/** @var TransactionJournalMetaFactory $factory */- $factory = app(TransactionJournalMetaFactory::class);- $set = ['journal' => $this->transactionJournal, 'name' => '_internal_previous_date', 'data' => null];+ $factory = app(TransactionJournalMetaFactory::class);+ $set = ['journal' => $this->transactionJournal, 'name' => '_internal_previous_date', 'data' => null];if ($res) {Log::debug('Transaction is set to be AFTER its current date. Save also the "_internal_previous_date"-field.');$set['data'] = clone $this->transactionJournal->date;@@ -528,7 +527,7 @@ class JournalUpdateServiceif ($this->hasFields(['category_id', 'category_name'])) {Log::debug('Will update category.');- $this->storeCategory($this->transactionJournal, new NullArrayObject($this->data));+ $this->storeCategory($this->transactionJournal, $this->data);}}@@ -537,7 +536,7 @@ class JournalUpdateService// update budgetif ($this->hasFields(['budget_id', 'budget_name'])) {Log::debug('Will update budget.');- $this->storeBudget($this->transactionJournal, new NullArrayObject($this->data));+ $this->storeBudget($this->transactionJournal, $this->data);}// is transfer? remove budgetif (TransactionTypeEnum::TRANSFER->value === $this->transactionJournal->transactionType->type) {@@ -565,7 +564,7 @@ class JournalUpdateService{// update notes.if ($this->hasFields(['notes'])) {- $notes = '' === (string) $this->data['notes'] ? null : $this->data['notes'];+ $notes = '' === (string)$this->data['notes'] ? null : $this->data['notes'];$this->storeNotes($this->transactionJournal, $notes);}}@@ -595,7 +594,7 @@ class JournalUpdateServiceif ($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 = ['journal' => $this->transactionJournal, 'name' => $field, 'data' => $value];+ $set = ['journal' => $this->transactionJournal, 'name' => $field, 'data' => $value];$factory->updateOrCreate($set);}}@@ -609,17 +608,17 @@ class JournalUpdateServiceforeach ($this->metaDate as $field) {if ($this->hasFields([$field])) {try {- $value = '' === (string) $this->data[$field] ? null : new Carbon($this->data[$field]);- } catch (InvalidDateException|InvalidFormatException $e) { // @phpstan-ignore-line+ $value = '' === (string)$this->data[$field] ? null : new Carbon($this->data[$field]);+ } catch (InvalidDateException | InvalidFormatException $e) { // @phpstan-ignore-lineLog::debug(sprintf('%s is not a valid date value: %s', $this->data[$field], $e->getMessage()));return;}Log::debug(sprintf('Field "%s" is present ("%s"), try to update it.', $field, $value));- $set = ['journal' => $this->transactionJournal, 'name' => $field, 'data' => $value];+ $set = ['journal' => $this->transactionJournal, 'name' => $field, 'data' => $value];$factory->updateOrCreate($set);// also set date with timezone.- $set = ['journal' => $this->transactionJournal, 'name' => sprintf('%s_tz', $field), 'data' => $value?->format('e')];+ $set = ['journal' => $this->transactionJournal, 'name' => sprintf('%s_tz', $field), 'data' => $value?->format('e')];$factory->updateOrCreate($set);}}@@ -631,19 +630,19 @@ class JournalUpdateServiceif (!$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.@@ -659,7 +658,7 @@ class JournalUpdateServicereturn;}- $value = $this->data['amount'] ?? '';+ $value = $this->data['amount'] ?? '';Log::debug(sprintf('[a] Amount is now "%s"', $value));try {@@ -670,9 +669,9 @@ class JournalUpdateServicereturn;}Log::debug(sprintf('[b] Amount is now "%s"', $value));- $origSourceTransaction = $this->getSourceTransaction();- $destTransaction = $this->getDestinationTransaction();- $originalSourceAmount = $origSourceTransaction->amount;+ $origSourceTransaction = $this->getSourceTransaction();+ $destTransaction = $this->getDestinationTransaction();+ $originalSourceAmount = $origSourceTransaction->amount;// $originalDestAmount = $destTransaction->amount;$origSourceTransaction->amount = Steam::negative($amount);$origSourceTransaction->balance_dirty = true;@@ -686,7 +685,7 @@ class JournalUpdateService$this->destinationTransaction->refresh();Log::debug(sprintf('Updated amount to "%s"', $amount));- $group = $this->transactionGroup;+ $group = $this->transactionGroup;if (null === $group) {$group = $this->transactionJournal?->transactionGroup;}@@ -699,13 +698,13 @@ class JournalUpdateServicereturn;}Log::debug('Amount was changed, needs audit log entry.');- $transfer = TransactionTypeEnum::TRANSFER->value === $this->transactionJournal->transactionType->type;+ $transfer = TransactionTypeEnum::TRANSFER->value === $this->transactionJournal->transactionType->type;// $withdrawal = TransactionTypeEnum::WITHDRAWAL->value === $this->transactionJournal->transactionType->type;- $deposit = TransactionTypeEnum::DEPOSIT->value === $this->transactionJournal->transactionType->type;- $makePositive = $transfer || $deposit ? true : false;- $recordCurrency = $origSourceTransaction->transactionCurrency;- $originalSourceAmount = $makePositive ? Steam::positive($originalSourceAmount) : Steam::negative($originalSourceAmount);- $value = $makePositive ? Steam::positive($value) : Steam::negative($value);+ $deposit = TransactionTypeEnum::DEPOSIT->value === $this->transactionJournal->transactionType->type;+ $makePositive = $transfer || $deposit ? true : false;+ $recordCurrency = $origSourceTransaction->transactionCurrency;+ $originalSourceAmount = $makePositive ? Steam::positive($originalSourceAmount) : Steam::negative($originalSourceAmount);+ $value = $makePositive ? Steam::positive($value) : Steam::negative($value);// should not return in NULL but seems to do.event(@@ -718,7 +717,7 @@ class JournalUpdateService'decimal_places' => $recordCurrency->decimal_places,'amount' => $originalSourceAmount,],- ['currency_symbol' => $recordCurrency->symbol, 'decimal_places' => $recordCurrency->decimal_places, 'amount' => $value]+ ['currency_symbol' => $recordCurrency->symbol, 'decimal_places' => $recordCurrency->decimal_places, 'amount' => $value]));}@@ -738,9 +737,9 @@ class JournalUpdateService$originalSourceAmount = $source->foreign_amount;// find currency in data array- $newForeignId = $this->data['foreign_currency_id'] ?? null;- $newForeignCode = $this->data['foreign_currency_code'] ?? null;- $foreignCurrency = $this->currencyRepository->findCurrencyNull($newForeignId, $newForeignCode) ?? $foreignCurrency;+ $newForeignId = $this->data['foreign_currency_id'] ?? null;+ $newForeignCode = $this->data['foreign_currency_code'] ?? null;+ $foreignCurrency = $this->currencyRepository->findCurrencyNull($newForeignId, $newForeignCode) ?? $foreignCurrency;// not the same as normal currencyif (null !== $foreignCurrency && $foreignCurrency->id === $this->transactionJournal->transaction_currency_id) {@@ -758,9 +757,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');@@ -785,7 +784,7 @@ class JournalUpdateService// add audit log entry.Log::debug(sprintf('Updated foreign amount to "%s"', $foreignAmount));- $group = $this->transactionGroup;+ $group = $this->transactionGroup;if (null === $group) {$group = $this->transactionJournal?->transactionGroup;}@@ -798,13 +797,13 @@ class JournalUpdateServicereturn;}Log::debug('Amount was changed, needs audit log entry.');- $transfer = TransactionTypeEnum::TRANSFER->value === $this->transactionJournal->transactionType->type;+ $transfer = TransactionTypeEnum::TRANSFER->value === $this->transactionJournal->transactionType->type;// $withdrawal = TransactionTypeEnum::WITHDRAWAL->value === $this->transactionJournal->transactionType->type;- $deposit = TransactionTypeEnum::DEPOSIT->value === $this->transactionJournal->transactionType->type;- $makePositive = $transfer || $deposit ? true : false;- $recordCurrency = $source->foreignCurrency;- $originalSourceAmount = $makePositive ? Steam::positive($originalSourceAmount) : Steam::negative($originalSourceAmount);- $value = $makePositive ? Steam::positive($foreignAmount) : Steam::negative($foreignAmount);+ $deposit = TransactionTypeEnum::DEPOSIT->value === $this->transactionJournal->transactionType->type;+ $makePositive = $transfer || $deposit ? true : false;+ $recordCurrency = $source->foreignCurrency;+ $originalSourceAmount = $makePositive ? Steam::positive($originalSourceAmount) : Steam::negative($originalSourceAmount);+ $value = $makePositive ? Steam::positive($foreignAmount) : Steam::negative($foreignAmount);// should not return in NULL but seems to do.event(@@ -817,7 +816,7 @@ class JournalUpdateService'decimal_places' => $recordCurrency->decimal_places,'amount' => $originalSourceAmount,],- ['currency_symbol' => $recordCurrency->symbol, 'decimal_places' => $recordCurrency->decimal_places, 'amount' => $value]+ ['currency_symbol' => $recordCurrency->symbol, 'decimal_places' => $recordCurrency->decimal_places, 'amount' => $value]));}@@ -826,8 +825,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));@@ -843,18 +842,16 @@ class JournalUpdateServiceprivate function isBetweenAssetAndLiability(): bool{/** @var null|Transaction $sourceTransaction */- $sourceTransaction = $this->transactionJournal+ $sourceTransaction = $this->transactionJournal->transactions()->where('amount', '<', 0)- ->first()- ;+ ->first();/** @var null|Transaction $destinationTransaction */$destinationTransaction = $this->transactionJournal->transactions()->where('amount', '>', 0)- ->first()- ;+ ->first();if (null === $sourceTransaction || null === $destinationTransaction) {Log::warning('Either transaction is false, stop.');@@ -866,15 +863,15 @@ class JournalUpdateServicereturn 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 assetif (in_array($source->accountType->type, $sourceTypes, true) && AccountTypeEnum::ASSET->value === $destination->accountType->type) {