From 3cfaa5aa03814f577903411c68b89b43cd1b988f Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 16 Jan 2023 06:39:16 +0100 Subject: [PATCH 1/2] Fix https://github.com/firefly-iii/firefly-iii/issues/6869 --- .../V1/Requests/Models/Account/StoreRequest.php | 2 -- .../V1/Requests/Models/Account/UpdateRequest.php | 16 +--------------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/app/Api/V1/Requests/Models/Account/StoreRequest.php b/app/Api/V1/Requests/Models/Account/StoreRequest.php index e758863e66..a1bfff913d 100644 --- a/app/Api/V1/Requests/Models/Account/StoreRequest.php +++ b/app/Api/V1/Requests/Models/Account/StoreRequest.php @@ -83,8 +83,6 @@ class StoreRequest extends FormRequest $data = $this->appendLocationData($data, null); if ('liability' === $data['account_type_name'] || 'liabilities' === $data['account_type_name']) { - $data['opening_balance'] = app('steam')->negative($this->convertString('liability_amount')); - $data['opening_balance_date'] = $this->getCarbonDate('liability_start_date'); $data['account_type_name'] = $this->convertString('liability_type'); $data['liability_direction'] = $this->convertString('liability_direction'); $data['account_type_id'] = null; diff --git a/app/Api/V1/Requests/Models/Account/UpdateRequest.php b/app/Api/V1/Requests/Models/Account/UpdateRequest.php index 1bacf98792..aabec9fc15 100644 --- a/app/Api/V1/Requests/Models/Account/UpdateRequest.php +++ b/app/Api/V1/Requests/Models/Account/UpdateRequest.php @@ -76,22 +76,8 @@ class UpdateRequest extends FormRequest 'liability_amount' => ['liability_amount', 'convertString'], 'liability_start_date' => ['liability_start_date', 'date'], ]; - /** @var Account $account */ - $account = $this->route()->parameter('account'); $data = $this->getAllData($fields); - $data = $this->appendLocationData($data, null); - $valid = config('firefly.valid_liabilities'); - if (array_key_exists('liability_amount', $data) && in_array($account->accountType->type, $valid, true)) { - $data['opening_balance'] = app('steam')->negative($data['liability_amount']); - Log::debug(sprintf('Opening balance for liability is "%s".', $data['opening_balance'])); - } - - if (array_key_exists('liability_start_date', $data) && in_array($account->accountType->type, $valid, true)) { - $data['opening_balance_date'] = $data['liability_start_date']; - Log::debug(sprintf('Opening balance date for liability is "%s".', $data['opening_balance_date'])); - } - - return $data; + return $this->appendLocationData($data, null); } /** From a753fde2f1b3feb4c0e20275368bdae41b66e24c Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 16 Jan 2023 06:54:57 +0100 Subject: [PATCH 2/2] Fix #6870 --- .../Internal/Destroy/CategoryDestroyService.php | 3 +++ .../Internal/Support/RecurringTransactionTrait.php | 2 +- .../Internal/Update/RecurrenceUpdateService.php | 13 +++++++++---- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/Services/Internal/Destroy/CategoryDestroyService.php b/app/Services/Internal/Destroy/CategoryDestroyService.php index ee00647357..9c7f91e377 100644 --- a/app/Services/Internal/Destroy/CategoryDestroyService.php +++ b/app/Services/Internal/Destroy/CategoryDestroyService.php @@ -46,5 +46,8 @@ class CategoryDestroyService // also delete all relations between categories and transactions: DB::table('category_transaction')->where('category_id', (int)$category->id)->delete(); + + // delete references to category from recurring transactions. + DB::table('rt_meta')->where('name', 'category_id')->where('value', $category->id)->delete(); } } diff --git a/app/Services/Internal/Support/RecurringTransactionTrait.php b/app/Services/Internal/Support/RecurringTransactionTrait.php index bb7157aa5e..815f13a7f5 100644 --- a/app/Services/Internal/Support/RecurringTransactionTrait.php +++ b/app/Services/Internal/Support/RecurringTransactionTrait.php @@ -294,7 +294,7 @@ trait RecurringTransactionTrait return; } - + $transaction->recurrenceTransactionMeta()->where('name', 'category_name')->delete(); $meta = $transaction->recurrenceTransactionMeta()->where('name', 'category_id')->first(); if (null === $meta) { $meta = new RecurrenceTransactionMeta(); diff --git a/app/Services/Internal/Update/RecurrenceUpdateService.php b/app/Services/Internal/Update/RecurrenceUpdateService.php index dfce5f715f..950a1c4e3f 100644 --- a/app/Services/Internal/Update/RecurrenceUpdateService.php +++ b/app/Services/Internal/Update/RecurrenceUpdateService.php @@ -224,9 +224,10 @@ class RecurrenceUpdateService */ private function updateTransactions(Recurrence $recurrence, array $transactions): void { + Log::debug('Now in updateTransactions()'); $originalCount = $recurrence->recurrenceTransactions()->count(); if (0 === count($transactions)) { - // wont drop transactions, rather avoid. + // won't drop transactions, rather avoid. return; } // user added or removed repetitions, delete all and recreate: @@ -240,12 +241,13 @@ class RecurrenceUpdateService $currencyFactory = app(TransactionCurrencyFactory::class); // loop all and try to match them: if ($originalCount === count($transactions)) { - Log::debug('Loop and find'); + Log::debug(sprintf('Count is equal (%d), update transactions.', $originalCount)); foreach ($transactions as $current) { $match = $this->matchTransaction($recurrence, $current); if (null === $match) { throw new FireflyException('Cannot match recurring transaction to existing transaction. Not sure what to do. Break.'); } + // complex loop to find currency: $currency = null; $foreignCurrency = null; if (array_key_exists('currency_id', $current) || array_key_exists('currency_code', $current)) { @@ -267,7 +269,7 @@ class RecurrenceUpdateService $current['foreign_currency_id'] = (int)$foreignCurrency->id; } - // update fields + // update fields that are part of the recurring transaction itself. $fields = [ 'source_id' => 'source_id', 'destination_id' => 'destination_id', @@ -293,11 +295,13 @@ class RecurrenceUpdateService // reset category if name is set but empty: // can be removed when v1 is retired. if (array_key_exists('category_name', $current) && '' === (string)$current['category_name']) { + Log::debug('Category name is submitted but is empty. Set category to be empty.'); $current['category_name'] = null; $current['category_id'] = 0; } if (array_key_exists('category_id', $current)) { + Log::debug(sprintf('Category ID is submitted, set category to be %d.', (int)$current['category_id'])); $this->setCategory($match, (int)$current['category_id']); } @@ -319,9 +323,10 @@ class RecurrenceUpdateService */ private function matchTransaction(Recurrence $recurrence, array $data): ?RecurrenceTransaction { + Log::debug('Now in matchTransaction()'); $originalCount = $recurrence->recurrenceTransactions()->count(); if (1 === $originalCount) { - Log::debug('Return the first one'); + Log::debug('Return the first one.'); return $recurrence->recurrenceTransactions()->first(); }