From 63984f1c37dc8ba937361fadb877ebf7fe388fbc Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 17 May 2023 07:02:08 +0200 Subject: [PATCH] Fix #7505 --- .../Requests/Models/Account/UpdateRequest.php | 4 +- .../Models/PiggyBank/UpdateRequest.php | 4 +- .../Models/Recurrence/StoreRequest.php | 4 +- .../Models/Recurrence/UpdateRequest.php | 4 +- app/Models/PiggyBank.php | 2 +- .../Account/AccountRepository.php | 2 +- .../Internal/Support/AccountServiceTrait.php | 3 ++ .../Internal/Update/AccountUpdateService.php | 2 +- app/Support/Request/ConvertsDataTypes.php | 39 +++++++++++++++++++ app/Transformers/AccountTransformer.php | 10 ++++- app/Transformers/PiggyBankTransformer.php | 5 +-- app/Transformers/RecurrenceTransformer.php | 6 +-- app/Transformers/TagTransformer.php | 2 +- config/firefly.php | 2 +- 14 files changed, 68 insertions(+), 21 deletions(-) diff --git a/app/Api/V1/Requests/Models/Account/UpdateRequest.php b/app/Api/V1/Requests/Models/Account/UpdateRequest.php index a9c27e6485..ac3ecd8171 100644 --- a/app/Api/V1/Requests/Models/Account/UpdateRequest.php +++ b/app/Api/V1/Requests/Models/Account/UpdateRequest.php @@ -63,9 +63,9 @@ class UpdateRequest extends FormRequest 'account_role' => ['account_role', 'convertString'], 'liability_type' => ['liability_type', 'convertString'], 'opening_balance' => ['opening_balance', 'convertString'], - 'opening_balance_date' => ['opening_balance_date', 'date'], + 'opening_balance_date' => ['opening_balance_date', 'convertDateTime'], 'cc_type' => ['credit_card_type', 'convertString'], - 'cc_monthly_payment_date' => ['monthly_payment_date', 'convertString'], + 'cc_monthly_payment_date' => ['monthly_payment_date', 'convertDateTime'], 'notes' => ['notes', 'stringWithNewlines'], 'interest' => ['interest', 'convertString'], 'interest_period' => ['interest_period', 'convertString'], diff --git a/app/Api/V1/Requests/Models/PiggyBank/UpdateRequest.php b/app/Api/V1/Requests/Models/PiggyBank/UpdateRequest.php index fdbd8e8e47..e12390f600 100644 --- a/app/Api/V1/Requests/Models/PiggyBank/UpdateRequest.php +++ b/app/Api/V1/Requests/Models/PiggyBank/UpdateRequest.php @@ -52,8 +52,8 @@ class UpdateRequest extends FormRequest 'account_id' => ['account_id', 'convertInteger'], 'targetamount' => ['target_amount', 'convertString'], 'current_amount' => ['current_amount', 'convertString'], - 'startdate' => ['start_date', 'date'], - 'targetdate' => ['target_date', 'convertString'], + 'startdate' => ['start_date', 'convertDateTime'], + 'targetdate' => ['target_date', 'convertDateTime'], 'notes' => ['notes', 'stringWithNewlines'], 'order' => ['order', 'convertInteger'], 'object_group_title' => ['object_group_title', 'convertString'], diff --git a/app/Api/V1/Requests/Models/Recurrence/StoreRequest.php b/app/Api/V1/Requests/Models/Recurrence/StoreRequest.php index 9b34a07ad9..fa33102464 100644 --- a/app/Api/V1/Requests/Models/Recurrence/StoreRequest.php +++ b/app/Api/V1/Requests/Models/Recurrence/StoreRequest.php @@ -57,8 +57,8 @@ class StoreRequest extends FormRequest 'type' => ['type', 'convertString'], 'title' => ['title', 'convertString'], 'description' => ['description', 'convertString'], - 'first_date' => ['first_date', 'date'], - 'repeat_until' => ['repeat_until', 'date'], + 'first_date' => ['first_date', 'convertDateTime'], + 'repeat_until' => ['repeat_until', 'convertDateTime'], 'nr_of_repetitions' => ['nr_of_repetitions', 'convertInteger'], 'apply_rules' => ['apply_rules', 'boolean'], 'active' => ['active', 'boolean'], diff --git a/app/Api/V1/Requests/Models/Recurrence/UpdateRequest.php b/app/Api/V1/Requests/Models/Recurrence/UpdateRequest.php index 0cd1464794..a922abef1d 100644 --- a/app/Api/V1/Requests/Models/Recurrence/UpdateRequest.php +++ b/app/Api/V1/Requests/Models/Recurrence/UpdateRequest.php @@ -58,8 +58,8 @@ class UpdateRequest extends FormRequest $fields = [ 'title' => ['title', 'convertString'], 'description' => ['description', 'convertString'], - 'first_date' => ['first_date', 'date'], - 'repeat_until' => ['repeat_until', 'date'], + 'first_date' => ['first_date', 'convertDateTime'], + 'repeat_until' => ['repeat_until', 'convertDateTime'], 'nr_of_repetitions' => ['nr_of_repetitions', 'convertInteger'], 'apply_rules' => ['apply_rules', 'boolean'], 'active' => ['active', 'boolean'], diff --git a/app/Models/PiggyBank.php b/app/Models/PiggyBank.php index af01280562..ee60f7d9ba 100644 --- a/app/Models/PiggyBank.php +++ b/app/Models/PiggyBank.php @@ -86,7 +86,7 @@ class PiggyBank extends Model use SoftDeletes; /** - * The attributes that should be casted to native types. + * The attributes that should be cast to native types. * * @var array */ diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index ed47edd520..49056a0c20 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -394,7 +394,7 @@ class AccountRepository implements AccountRepositoryInterface return null; } - return $journal->date->format('Y-m-d'); + return $journal->date->format('Y-m-d H:i:s'); } /** diff --git a/app/Services/Internal/Support/AccountServiceTrait.php b/app/Services/Internal/Support/AccountServiceTrait.php index 244dd60c98..d29ceac998 100644 --- a/app/Services/Internal/Support/AccountServiceTrait.php +++ b/app/Services/Internal/Support/AccountServiceTrait.php @@ -154,6 +154,9 @@ trait AccountServiceTrait if (is_bool($data[$field]) && true === $data[$field]) { $data[$field] = 1; } + if($data[$field] instanceof Carbon) { + $data[$field] = $data[$field]->toAtomString(); + } $factory->crud($account, $field, (string)$data[$field]); } diff --git a/app/Services/Internal/Update/AccountUpdateService.php b/app/Services/Internal/Update/AccountUpdateService.php index 020cffdbee..f1a0abb389 100644 --- a/app/Services/Internal/Update/AccountUpdateService.php +++ b/app/Services/Internal/Update/AccountUpdateService.php @@ -296,6 +296,7 @@ class AccountUpdateService $type = $account->accountType; if (in_array($type->type, $this->canHaveOpeningBalance, true)) { // check if is submitted as empty, that makes it valid: + if ($this->validOBData($data) && !$this->isEmptyOBData($data)) { $openingBalance = $data['opening_balance']; $openingBalanceDate = $data['opening_balance_date']; @@ -306,7 +307,6 @@ class AccountUpdateService $openingBalance ); } - $this->updateOBGroupV2($account, $openingBalance, $openingBalanceDate); } diff --git a/app/Support/Request/ConvertsDataTypes.php b/app/Support/Request/ConvertsDataTypes.php index 67f0417db8..cb207d6377 100644 --- a/app/Support/Request/ConvertsDataTypes.php +++ b/app/Support/Request/ConvertsDataTypes.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\Support\Request; use Carbon\Carbon; +use Carbon\Exceptions\InvalidDateException; use Carbon\Exceptions\InvalidFormatException; use Illuminate\Support\Facades\Log; @@ -255,6 +256,44 @@ trait ConvertsDataTypes return $carbon; } + protected function convertDateTime(?string $string): ?Carbon + { + $value = $this->get($string); + if (null === $value) { + return null; + } + if ('' === $value) { + return null; + } + if (10 === strlen($value)) { + // probably a date format. + try { + $carbon = Carbon::createFromFormat('Y-m-d', $value); + } catch (InvalidDateException $e) { + Log::error(sprintf('[1] "%s" is not a valid date: %s', $value, $e->getMessage())); + return null; + } catch (InvalidFormatException $e) { + Log::error(sprintf('[2] "%s" is of an invalid format: %s', $value, $e->getMessage())); + + return null; + } + return $carbon; + } + // is an atom string, I hope? + try { + $carbon = Carbon::parse($value); + } catch (InvalidDateException $e) { + Log::error(sprintf('[3] "%s" is not a valid date or time: %s', $value, $e->getMessage())); + + return null; + } catch (InvalidFormatException $e) { + Log::error(sprintf('[4] "%s" is of an invalid format: %s', $value, $e->getMessage())); + + return null; + } + return $carbon; + } + /** * Returns all data in the request, or omits the field if not set, * according to the config from the request. This is the way. diff --git a/app/Transformers/AccountTransformer.php b/app/Transformers/AccountTransformer.php index e38431e09a..63225907a6 100644 --- a/app/Transformers/AccountTransformer.php +++ b/app/Transformers/AccountTransformer.php @@ -209,7 +209,13 @@ class AccountTransformer extends AbstractTransformer $monthlyPaymentDate = $this->repository->getMetaValue($account, 'cc_monthly_payment_date'); } if (null !== $monthlyPaymentDate) { - $monthlyPaymentDate = Carbon::createFromFormat('!Y-m-d', $monthlyPaymentDate, config('app.timezone'))->toAtomString(); + // try classic date: + if(10 === strlen($monthlyPaymentDate)) { + $monthlyPaymentDate = Carbon::createFromFormat('!Y-m-d', $monthlyPaymentDate, config('app.timezone'))->toAtomString(); + } + if(10 !== strlen($monthlyPaymentDate)) { + $monthlyPaymentDate = Carbon::parse($monthlyPaymentDate, config('app.timezone'))->toAtomString(); + } } return [$creditCardType, $monthlyPaymentDate]; @@ -233,7 +239,7 @@ class AccountTransformer extends AbstractTransformer $openingBalanceDate = $this->repository->getOpeningBalanceDate($account); } if (null !== $openingBalanceDate) { - $openingBalanceDate = Carbon::createFromFormat('!Y-m-d', $openingBalanceDate, config('app.timezone'))->toAtomString(); + $openingBalanceDate = Carbon::createFromFormat('Y-m-d H:i:s', $openingBalanceDate, config('app.timezone'))->toAtomString(); } return [$openingBalance, $openingBalanceDate]; diff --git a/app/Transformers/PiggyBankTransformer.php b/app/Transformers/PiggyBankTransformer.php index 1ddb5412bc..e50df975c6 100644 --- a/app/Transformers/PiggyBankTransformer.php +++ b/app/Transformers/PiggyBankTransformer.php @@ -103,9 +103,8 @@ class PiggyBankTransformer extends AbstractTransformer $leftToSave = app('steam')->bcround($leftToSave, $currency->decimal_places); $savePerMonth = app('steam')->bcround($this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places); } - $startDate = $piggyBank->startdate?->toAtomString(); - $targetDate = $piggyBank->targetdate?->toAtomString(); - + $startDate = $piggyBank->startdate?->format('Y-m-d'); + $targetDate = $piggyBank->targetdate?->format('Y-m-d'); return [ 'id' => (string)$piggyBank->id, diff --git a/app/Transformers/RecurrenceTransformer.php b/app/Transformers/RecurrenceTransformer.php index 5869664f0c..b1b98ac2ed 100644 --- a/app/Transformers/RecurrenceTransformer.php +++ b/app/Transformers/RecurrenceTransformer.php @@ -92,9 +92,9 @@ class RecurrenceTransformer extends AbstractTransformer 'type' => $shortType, 'title' => $recurrence->title, 'description' => $recurrence->description, - 'first_date' => $recurrence->first_date->toAtomString(), - 'latest_date' => $recurrence->latest_date?->toAtomString(), - 'repeat_until' => $recurrence->repeat_until?->toAtomString(), + 'first_date' => $recurrence->first_date->format('Y-m-d'), + 'latest_date' => $recurrence->latest_date?->format('Y-m-d'), + 'repeat_until' => $recurrence->repeat_until?->format('Y-m-d'), 'apply_rules' => $recurrence->apply_rules, 'active' => $recurrence->active, 'nr_of_repetitions' => $reps, diff --git a/app/Transformers/TagTransformer.php b/app/Transformers/TagTransformer.php index ca650b4463..169a8ac206 100644 --- a/app/Transformers/TagTransformer.php +++ b/app/Transformers/TagTransformer.php @@ -42,7 +42,7 @@ class TagTransformer extends AbstractTransformer */ public function transform(Tag $tag): array { - $date = $tag->date?->toAtomString(); + $date = $tag->date?->format('Y-m-d'); /** @var Location $location */ $location = $tag->locations()->first(); $latitude = null; diff --git a/config/firefly.php b/config/firefly.php index 5821785b01..4a972d64a6 100644 --- a/config/firefly.php +++ b/config/firefly.php @@ -108,7 +108,7 @@ return [ 'handle_debts' => true, ], 'version' => '6.0.10', - 'api_version' => '2.0.1', + 'api_version' => '2.0.2', 'db_version' => 19, // generic settings