diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index 373706326519..c0db18de99d8 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -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'); diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 15087776d02f..46e989e4222b 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -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); diff --git a/app/Http/Requests/Client/UpdateClientRequest.php b/app/Http/Requests/Client/UpdateClientRequest.php index f862696dada3..618f7c7521c3 100644 --- a/app/Http/Requests/Client/UpdateClientRequest.php +++ b/app/Http/Requests/Client/UpdateClientRequest.php @@ -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(); + } } \ No newline at end of file diff --git a/app/Models/Client.php b/app/Models/Client.php index bdeb84357546..8940eb9f1e6c 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -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() diff --git a/app/Repositories/ClientRepository.php b/app/Repositories/ClientRepository.php index 38baf89bb3cf..ea6a58f80a68 100644 --- a/app/Repositories/ClientRepository.php +++ b/app/Repositories/ClientRepository.php @@ -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(); diff --git a/app/Utils/Traits/GeneratesCounter.php b/app/Utils/Traits/GeneratesCounter.php index 6ba75277e00d..d1d0fba7e86b 100644 --- a/app/Utils/Traits/GeneratesCounter.php +++ b/app/Utils/Traits/GeneratesCounter.php @@ -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; }