From 55a6cc5cd46a735698264575ebb14357db529de6 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 19 Mar 2022 07:34:09 +0100 Subject: [PATCH] Fix https://github.com/firefly-iii/firefly-iii/issues/5852 --- app/Factory/TransactionCurrencyFactory.php | 9 +++ app/Http/Requests/CurrencyFormRequest.php | 6 +- app/Validation/FireflyValidator.php | 82 ++++++++++++++++------ 3 files changed, 74 insertions(+), 23 deletions(-) diff --git a/app/Factory/TransactionCurrencyFactory.php b/app/Factory/TransactionCurrencyFactory.php index 4d6779a911..555f9e025f 100644 --- a/app/Factory/TransactionCurrencyFactory.php +++ b/app/Factory/TransactionCurrencyFactory.php @@ -43,6 +43,15 @@ class TransactionCurrencyFactory */ public function create(array $data): TransactionCurrency { + // if the code already exists (deleted) + // force delete it and then create the transaction: + $count = TransactionCurrency::withTrashed()->whereCode($data['code'])->count(); + if(1 === $count) { + $old = TransactionCurrency::withTrashed()->whereCode($data['code'])->first(); + $old->forceDelete(); + Log::warning(sprintf('Force deleted old currency with ID #%d and code "%s".', $old->id, $data['code'])); + } + try { /** @var TransactionCurrency $result */ $result = TransactionCurrency::create( diff --git a/app/Http/Requests/CurrencyFormRequest.php b/app/Http/Requests/CurrencyFormRequest.php index a73fcc91c9..ce85fe0f43 100644 --- a/app/Http/Requests/CurrencyFormRequest.php +++ b/app/Http/Requests/CurrencyFormRequest.php @@ -59,9 +59,9 @@ class CurrencyFormRequest extends FormRequest { // fixed $rules = [ - 'name' => 'required|max:48|min:1|unique:transaction_currencies,name', - 'code' => 'required|min:3|max:51|unique:transaction_currencies,code', - 'symbol' => 'required|min:1|max:51|unique:transaction_currencies,symbol', + 'name' => 'required|max:48|min:1|uniqueCurrencyName', + 'code' => 'required|min:3|max:51|uniqueCurrencyCode', + 'symbol' => 'required|min:1|max:51|uniqueCurrencySymbol', 'decimal_places' => 'required|min:0|max:12|numeric', 'enabled' => 'in:0,1', ]; diff --git a/app/Validation/FireflyValidator.php b/app/Validation/FireflyValidator.php index 69f6b11762..18e77c7301 100644 --- a/app/Validation/FireflyValidator.php +++ b/app/Validation/FireflyValidator.php @@ -53,6 +53,48 @@ use function is_string; */ class FireflyValidator extends Validator { + + /** + * @param $attribute + * @param $value + * @return bool + */ + public function validateUniqueCurrencyName($attribute, $value): bool + { + return $this->validateUniqueCurrency('name', (string) $attribute, (string) $value); + } + + /** + * @param $attribute + * @param $value + * @return bool + */ + public function validateUniqueCurrencyCode($attribute, $value): bool + { + return $this->validateUniqueCurrency('code', (string) $attribute, (string) $value); + } + + /** + * @param $attribute + * @param $value + * @return bool + */ + public function validateUniqueCurrencySymbol($attribute, $value): bool + { + return $this->validateUniqueCurrency('symbol', (string) $attribute, (string) $value); + } + + /** + * @param $attribute + * @param $value + * @return bool + */ + public function validateUniqueCurrency(string $field, string $attribute, string $value): bool + { + return 0 === DB::table('transaction_currencies')->where($field, $value)->whereNull('deleted_at')->count(); + } + + /** * @param mixed $attribute * @param mixed $value @@ -84,7 +126,7 @@ class FireflyValidator extends Validator { $field = $parameters[1] ?? 'id'; - if (0 === (int)$value) { + if (0 === (int) $value) { return true; } $count = DB::table($parameters[0])->where('user_id', auth()->user()->id)->where($field, $value)->count(); @@ -202,7 +244,7 @@ class FireflyValidator extends Validator return false; } - return 1 === (int)$checksum; + return 1 === (int) $checksum; } /** @@ -217,7 +259,7 @@ class FireflyValidator extends Validator /** @var mixed $compare */ $compare = $parameters[0] ?? '0'; - return bccomp((string)$value, (string)$compare) < 0; + return bccomp((string) $value, (string) $compare) < 0; } /** @@ -232,7 +274,7 @@ class FireflyValidator extends Validator /** @var mixed $compare */ $compare = $parameters[0] ?? '0'; - return bccomp((string)$value, (string)$compare) > 0; + return bccomp((string) $value, (string) $compare) > 0; } /** @@ -246,7 +288,7 @@ class FireflyValidator extends Validator { $field = $parameters[1] ?? 'id'; - if (0 === (int)$value) { + if (0 === (int) $value) { return true; } $count = DB::table($parameters[0])->where($field, $value)->count(); @@ -266,7 +308,7 @@ class FireflyValidator extends Validator // first, get the index from this string: $value = $value ?? ''; $parts = explode('.', $attribute); - $index = (int)($parts[1] ?? '0'); + $index = (int) ($parts[1] ?? '0'); // get the name of the trigger from the data array: $actionType = $this->data['actions'][$index]['type'] ?? 'invalid'; @@ -330,7 +372,7 @@ class FireflyValidator extends Validator { // first, get the index from this string: $parts = explode('.', $attribute); - $index = (int)($parts[1] ?? '0'); + $index = (int) ($parts[1] ?? '0'); // get the name of the trigger from the data array: $triggerType = $this->data['triggers'][$index]['type'] ?? 'invalid'; @@ -358,7 +400,7 @@ class FireflyValidator extends Validator // check if it's an existing account. if (in_array($triggerType, ['destination_account_id', 'source_account_id'])) { - return is_numeric($value) && (int)$value > 0; + return is_numeric($value) && (int) $value > 0; } // check transaction type. @@ -396,7 +438,7 @@ class FireflyValidator extends Validator { $verify = false; if (array_key_exists('verify_password', $this->data)) { - $verify = 1 === (int)$this->data['verify_password']; + $verify = 1 === (int) $this->data['verify_password']; } if ($verify) { /** @var Verifier $service */ @@ -433,7 +475,7 @@ class FireflyValidator extends Validator } $parameterId = $parameters[0] ?? null; if (null !== $parameterId) { - return $this->validateByParameterId((int)$parameterId, $value); + return $this->validateByParameterId((int) $parameterId, $value); } if (array_key_exists('id', $this->data)) { return $this->validateByAccountId($value); @@ -483,7 +525,7 @@ class FireflyValidator extends Validator } $accountTypes = AccountType::whereIn('type', $search)->get(); - $ignore = (int)($parameters[0] ?? 0.0); + $ignore = (int) ($parameters[0] ?? 0.0); $accountTypeIds = $accountTypes->pluck('id')->toArray(); /** @var Collection $set */ $set = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)->get(); @@ -506,7 +548,7 @@ class FireflyValidator extends Validator private function validateByAccountTypeId($value, $parameters): bool { $type = AccountType::find($this->data['account_type_id'])->first(); - $ignore = (int)($parameters[0] ?? 0.0); + $ignore = (int) ($parameters[0] ?? 0.0); /** @var Collection $set */ $set = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get(); @@ -580,9 +622,9 @@ class FireflyValidator extends Validator */ public function validateUniqueAccountNumberForUser($attribute, $value, $parameters): bool { - $accountId = (int)($this->data['id'] ?? 0.0); + $accountId = (int) ($this->data['id'] ?? 0.0); if (0 === $accountId) { - $accountId = (int)($parameters[0] ?? 0.0); + $accountId = (int) ($parameters[0] ?? 0.0); } $query = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id') @@ -615,7 +657,7 @@ class FireflyValidator extends Validator */ public function validateUniqueExistingWebhook($value, $parameters, $something): bool { - $existingId = (int)($something[0] ?? 0); + $existingId = (int) ($something[0] ?? 0); $trigger = 0; $response = 0; $delivery = 0; @@ -671,15 +713,15 @@ class FireflyValidator extends Validator public function validateUniqueObjectForUser($attribute, $value, $parameters): bool { [$table, $field] = $parameters; - $exclude = (int)($parameters[2] ?? 0.0); + $exclude = (int) ($parameters[2] ?? 0.0); /* * If other data (in $this->getData()) contains * ID field, set that field to be the $exclude. */ $data = $this->getData(); - if (!array_key_exists(2, $parameters) && array_key_exists('id', $data) && (int)$data['id'] > 0) { - $exclude = (int)$data['id']; + if (!array_key_exists(2, $parameters) && array_key_exists('id', $data) && (int) $data['id'] > 0) { + $exclude = (int) $data['id']; } // get entries from table $set = DB::table($table)->where('user_id', auth()->user()->id)->whereNull('deleted_at') @@ -711,7 +753,7 @@ class FireflyValidator extends Validator ->where('object_groups.user_id', auth()->user()->id) ->where('object_groups.title', $value); if (null !== $exclude) { - $query->where('object_groups.id', '!=', (int)$exclude); + $query->where('object_groups.id', '!=', (int) $exclude); } return 0 === $query->count(); @@ -732,7 +774,7 @@ class FireflyValidator extends Validator $query = DB::table('piggy_banks')->whereNull('piggy_banks.deleted_at') ->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', auth()->user()->id); if (null !== $exclude) { - $query->where('piggy_banks.id', '!=', (int)$exclude); + $query->where('piggy_banks.id', '!=', (int) $exclude); } $query->where('piggy_banks.name', $value);