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