diff --git a/app/Console/Commands/TypeCheck.php b/app/Console/Commands/TypeCheck.php new file mode 100644 index 000000000000..6665cf5d8678 --- /dev/null +++ b/app/Console/Commands/TypeCheck.php @@ -0,0 +1,162 @@ +option('all')) + { + if (! config('ninja.db.multi_db_enabled')) { + $this->checkAll(); + } else { + + foreach (MultiDB::$dbs as $db) + { + MultiDB::setDB($db); + + $this->checkAll(); + } + + MultiDB::setDB($current_db); + + } + + } + + if($this->option('client_id')) + { + $client = MultiDB::findAndSetDbByClientId($this->option('client_id')); + + if($client) + $this->checkClient($client); + else + $this->logMessage(date('Y-m-d h:i:s').' Could not find this client'); + + } + + if($this->option('company_id')) + { + $company = MultiDB::findAndSetDbByCompanyId($this->option('company_id')); + + + if($company) + $this->checkCompany($company); + else + $this->logMessage(date('Y-m-d h:i:s').' Could not find this company'); + } + + } + + + private function checkClient($client) + { + $this->logMessage(date('Y-m-d h:i:s').' Checking Client => ' . $client->present()->name(). " " . $client->id); + + + $entity_settings = $this->checkSettingType($client->settings); + $entity_settings->md5 = md5(time()); + $client->settings = $entity_settings; + $client->save(); + + } + + private function checkCompany($company) + { + $this->logMessage(date('Y-m-d h:i:s').' Checking Company => ' . $company->present()->name(). " " . $company->id); + + $company->saveSettings($company->settings, $company); + + } + + private function checkAll() + { + + $this->logMessage(date('Y-m-d h:i:s').' Checking all clients and companies.'); + + Client::cursor()->each( function ($client) { + + $entity_settings = $this->checkSettingType($client->settings); + $entity_settings->md5 = md5(time()); + $client->settings = $entity_settings; + $client->save(); + + }); + + Company::cursor()->each( function ($company) { + + $company->saveSettings($company->settings, $company); + + }); + + + } + + private function logMessage($str) + { + $str = date('Y-m-d h:i:s').' '.$str; + $this->info($str); + $this->log .= $str."\n"; + } +} + diff --git a/app/Http/Requests/Client/StoreClientRequest.php b/app/Http/Requests/Client/StoreClientRequest.php index 06693fff25f9..b641c4c025ac 100644 --- a/app/Http/Requests/Client/StoreClientRequest.php +++ b/app/Http/Requests/Client/StoreClientRequest.php @@ -36,7 +36,8 @@ class StoreClientRequest extends Request } public function rules() - {nlog($this->input); + { + if ($this->input('documents') && is_array($this->input('documents'))) { $documents = count($this->input('documents')); diff --git a/app/Libraries/MultiDB.php b/app/Libraries/MultiDB.php index 7250e9f81fae..eee061cb5902 100644 --- a/app/Libraries/MultiDB.php +++ b/app/Libraries/MultiDB.php @@ -284,6 +284,22 @@ class MultiDB return false; } + public static function findAndSetDbByCompanyId($company_id) :?Company + { + $current_db = config('database.default'); + + foreach (self::$dbs as $db) { + if ($company = Company::on($db)->where('id', $company_id)->first()) { + self::setDb($db); + return $company; + } + } + + self::setDB($current_db); + + return false; + } + public static function findAndSetDbByAccountKey($account_key) :bool { $current_db = config('database.default'); @@ -332,6 +348,22 @@ class MultiDB return false; } + public static function findAndSetDbByClientId($client_id) :?Client + { + $current_db = config('database.default'); + + foreach (self::$dbs as $db) { + if ($client = Client::on($db)->where('id', $client_id)->first()) { + self::setDb($db); + return $client; + } + } + + self::setDB($current_db); + + return false; + } + public static function findAndSetDbByDomain($query_array) { diff --git a/app/Utils/Traits/ClientGroupSettingsSaver.php b/app/Utils/Traits/ClientGroupSettingsSaver.php index 663b17394a2f..0957c70e9dfe 100644 --- a/app/Utils/Traits/ClientGroupSettingsSaver.php +++ b/app/Utils/Traits/ClientGroupSettingsSaver.php @@ -176,7 +176,9 @@ trait ClientGroupSettingsSaver if (! property_exists($settings, $key)) { continue; } elseif ($this->checkAttribute($value, $settings->{$key})) { - if (substr($key, -3) == '_id') { + if (substr($key, -3) == '_id'|| + ($key == 'payment_terms' && property_exists($settings, 'payment_terms') && strlen($settings->{$key}) >= 1) || + ($key == 'valid_until' && property_exists($settings, 'valid_until') && strlen($settings->{$key}) >= 1)) { settype($settings->{$key}, 'string'); } else { settype($settings->{$key}, $value);