Validation rules for update client

This commit is contained in:
David Bomba 2019-10-08 22:14:23 +11:00
parent ec0f49e88a
commit faa5a05ac1
6 changed files with 64 additions and 12 deletions

View File

@ -199,7 +199,6 @@ class CompanySettings extends BaseSettings
'datetime_format_id' => 'string',
'military_time' => 'bool',
'language_id' => 'string',
'precision' => 'int',
'show_currency_code' => 'bool',
'payment_terms' => 'int',
'custom_label1' => 'string',
@ -267,6 +266,16 @@ class CompanySettings extends BaseSettings
'company_gateways' => 'string',
];
/**
* Array of variables which
* cannot be modified client side
*/
public static $protected = [
'credit_number_counter',
'invoice_number_counter',
'quote_number_counter',
];
/**
* Cast object values and return entire class
* prevents missing properties from not being returned
@ -290,6 +299,7 @@ class CompanySettings extends BaseSettings
$data = (object)get_class_vars(CompanySettings::class);
unset($data->casts);
unset($data->protected);
$data->timezone_id = (string)config('ninja.i18n.timezone_id');
$data->language_id = (string)config('ninja.i18n.language_id');

View File

@ -19,6 +19,8 @@ use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Illuminate\Support\Arr;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
class Handler extends ExceptionHandler
{
@ -81,7 +83,7 @@ class Handler extends ExceptionHandler
}
else if($exception instanceof AuthorizationException)
{
return response()->json(['message'=>'You are not authorized to view or perform this action',401]);
return response()->json(['message'=>'You are not authorized to view or perform this action'],401);
}
else if ($exception instanceof \Illuminate\Session\TokenMismatchException)
{
@ -92,6 +94,12 @@ class Handler extends ExceptionHandler
'message' => ctrans('texts.token_expired'),
'message-type' => 'danger']);
}
else if ($exception instanceof NotFoundHttpException) {
return response()->json(['message'=>'Route does not exist'],404);
}
else if($exception instanceof MethodNotAllowedHttpException){
return response()->json(['message'=>'Method not support for this route'],404);
}
return parent::render($request, $exception);

View File

@ -40,6 +40,10 @@ class UpdateClientRequest extends Request
$rules['currency_id'] = 'integer|nullable';
$rules['country_id'] = 'integer|nullable';
$rules['shipping_country_id'] = 'integer|nullable';
//$rules['id_number'] = 'unique:clients,id_number,,id,company_id,' . auth()->user()->company()->id;
$rules['id_number'] = 'unique:clients,id_number,' . $this->id . ',id,company_id,' . $this->company_id;
// $rules['settings'] = 'json';
$contacts = request('contacts');
@ -66,5 +70,13 @@ class UpdateClientRequest extends Request
];
}
public function sanitize()
{
$input = $this->all();
// $this->replace($input);
return $this->all();
}
}

View File

@ -211,31 +211,51 @@ class Client extends BaseModel
*/
public function getSetting($setting)
{
//check client level first
/*Client Settings*/
if($this->settings && (property_exists($this->settings, $setting) !== false) && (isset($this->settings->{$setting}) !== false) ){
/*need to catch empt string here*/
/*need to catch empty string here*/
if(is_string($this->settings->{$setting}) && (iconv_strlen($this->settings->{$setting}) >=1)){
return $this->settings->{$setting};
}
}
//check group level (if a group is assigned)
/*Group Settings*/
if($this->group_settings && (property_exists($this->group_settings->settings, $setting) !== false) && (isset($this->group_settings->settings->{$setting}) !== false)){
return $this->group_settings->settings->{$setting};
}
//check company level
/*Company Settings*/
if((property_exists($this->company->settings, $setting) != false ) && (isset($this->company->settings->{$setting}) !== false) ){
return $this->company->settings->{$setting};
}
throw new \Exception("Settings corrupted", 1);
}
public function getSettingEntity($setting)
{
/*Client Settings*/
if($this->settings && (property_exists($this->settings, $setting) !== false) && (isset($this->settings->{$setting}) !== false) ){
/*need to catch empty string here*/
if(is_string($this->settings->{$setting}) && (iconv_strlen($this->settings->{$setting}) >=1)){
return $this;
}
}
/*Group Settings*/
if($this->group_settings && (property_exists($this->group_settings->settings, $setting) !== false) && (isset($this->group_settings->settings->{$setting}) !== false)){
return $this->group_settings;
}
/*Company Settings*/
if((property_exists($this->company->settings, $setting) != false ) && (isset($this->company->settings->{$setting}) !== false) ){
return $this->company;
}
throw new \Exception("Could not find a settings object", 1);
}
public function documents()

View File

@ -65,7 +65,8 @@ class ClientRepository extends BaseRepository
$client->save();
$client->id_number = $this->getNextClientNumber($client); //todo write tests for this and make sure that custom client numbers also works as expected from here
// if($client->id_number == "")
// $client->id_number = $this->getNextClientNumber($client); //todo write tests for this and make sure that custom client numbers also works as expected from here
$client->save();

View File

@ -153,10 +153,11 @@ trait GeneratesCounter
$this->resetCounters($client);
$counter = $client->getSetting('client_number_counter' );
$setting_entity = $client->getSettingEntity('client_number_counter');
$client_number = $this->checkEntityNumber(Client::class, $client, $counter, $client->getSetting('counter_padding'), $client->getSetting('client_number_prefix'), $client->getSetting('client_number_pattern'));
$this->incrementCounter($client->company, 'client_number_counter');
$this->incrementCounter($setting_entity, 'client_number_counter');
return $client_number;
}