James Cole 2022-03-19 07:34:09 +01:00
parent e3ecfdfac6
commit 55a6cc5cd4
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
3 changed files with 74 additions and 23 deletions

View File

@ -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(

View File

@ -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',
];

View File

@ -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);