connection = new Mysql(); } /** Carrega os campos de uma tabela */ public function Describe() { /** Consulta SQL */ $this->sql = "describe companies"; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ $this->field = $this->stmt->fetchAll(\PDO::FETCH_OBJ); /** Declara o objeto */ $resultDescribe = new \stdClass(); $Field = ''; /** Lista os campos da tabela para objetos */ foreach($this->field as $UsersKey => $Result){ /** Pega o nome do Field/Campo */ $Field = $Result->Field; /** Carrega os objetos como null */ $resultDescribe->$Field = null; } /** Retorna os campos declarados como vazios */ return $resultDescribe; } /** Lista os registros do banco de dados com limitação */ public function Get(int $companyId) { /** Parametros de entrada */ $this->companyId = $companyId; /** Consulta SQL */ $this->sql = 'select cp.*, c.name as city_name from companies cp left join cities c on c.city_id = cp.city_id where cp.company_id = :company_id'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam(':company_id', $this->companyId); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject(); } /** Lista todos os egistros do banco com ou sem paginação*/ public function All(? int $start, ? int $max, ?int $typeSearch, ?string $search) { /** Parametros de entrada */ $this->start = $start; $this->max = $max; $this->typeSearch = $typeSearch; $this->search = !empty($search) ? '%'.$search.'%' : ''; /** Verifico se há paginação */ if($this->max){ $this->limit = " limit $this->start, $this->max"; } /** Consulta SQL */ $this->sql = 'select * from companies c '; /** Verifica se há pesquisa */ if($this->typeSearch && $this->search){ /** Verifica o tipo de pesquisa */ if($this->typeSearch == 1){ //CNS $this->sql .= 'where c.cns like :search'; }else if($this->typeSearch == 2){ //Pesquisa por Nome Fantasia $this->sql .= 'where c.name_fantasy like :search'; }else if($this->typeSearch == 3){ //Pesquisa por responsável $this->sql .= 'where c.responsible like :search'; }else if($this->typeSearch == 4){ //Pesquisa por e-mail $this->sql .= 'where c.email like :search'; }else if($this->typeSearch == 5){ //Pesquisa por estado $this->sql .= 'where (select count(s.state_id) from states s where uf like :search and s.state_id = c.state_id) > 0'; } } $this->sql .= $this->limit; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Verifica se há pesquisa */ if($this->typeSearch && $this->search){ /** Preencho os parâmetros do SQL */ $this->stmt->bindParam(':search', $this->search); } /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchAll(\PDO::FETCH_OBJ); } /** Conta a quantidades de registros */ public function Count(?int $typeSearch, ?string $search) { $this->typeSearch = $typeSearch; $this->search = !empty($search) ? '%'.$search.'%' : ''; /** Consulta SQL */ $this->sql = 'select count(c.company_id) as qtde from companies c '; /** Verifica se há pesquisa */ if($this->typeSearch && $this->search){ /** Verifica o tipo de pesquisa */ if($this->typeSearch == 1){ //CNS $this->sql .= 'where c.cns like :search'; }else if($this->typeSearch == 2){ //Pesquisa por Nome Fantasia $this->sql .= 'where c.name_fantasy like :search'; }else if($this->typeSearch == 3){ //Pesquisa por responsável $this->sql .= 'where c.responsible like :search'; }else if($this->typeSearch == 4){ //Pesquisa por e-mail $this->sql .= 'where c.email like :search'; }else if($this->typeSearch == 5){ //Pesquisa por estado $this->sql .= 'where (select count(s.state_id) from states s where uf like :search and s.state_id = c.state_id) > 0'; } } /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Verifica se há pesquisa */ if($this->typeSearch && $this->search){ /** Preencho os parâmetros do SQL */ $this->stmt->bindParam(':search', $this->search); } /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject(); } /** Insere um novo registro no banco */ public function Save($companyId, $nameBusiness, $nameFantasy, $cnpj, $cep, $adress, $number, $complement, $district, $cityId, $stateId, $active, $type, $responsible, $email, $reference, $responsibleDocument, $contractDate, $computers, $servers, $userId) { /** Parametros */ $this->companyId = $companyId; $this->nameBusiness = $nameBusiness; $this->nameFantasy = $nameFantasy; $this->cnpj = $cnpj; $this->cep = $cep; $this->adress = $adress; $this->number = $number; $this->complement = $complement; $this->district = $district; $this->cityId = $cityId; $this->stateId = $stateId; $this->active = $active; $this->type = $type; $this->responsible = $responsible; $this->email = $email; $this->reference = $reference; $this->responsibleDocument = $responsibleDocument; $this->contractDate = $contractDate; $this->computers = $computers; $this->servers = $servers; $this->userId = $userId; /** Verifica se o ID do registro foi informado */ if($this->companyId > 0){ /** Consulta SQL */ $this->sql = 'update companies set name_business = :name_business, name_fantasy = :name_fantasy, cnpj = :cnpj, cep = :cep, adress = :adress, number = :number, complement = :complement, district = :district, city_id = :city_id, state_id = :state_id, active = :active where company_id = :company_id'; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('company_id', $this->companyId); $this->stmt->bindParam('name_business', $this->nameBusiness); $this->stmt->bindParam('name_fantasy', $this->nameFantasy); $this->stmt->bindParam('cnpj', $this->cnpj); $this->stmt->bindParam('cep', $this->cep); $this->stmt->bindParam('adress', $this->adress); $this->stmt->bindParam('number', $this->number); $this->stmt->bindParam('complement', $this->complement); $this->stmt->bindParam('district', $this->district); $this->stmt->bindParam('city_id', $this->cityId); $this->stmt->bindParam('state_id', $this->stateId); $this->stmt->bindParam('active', $this->active); }else{//Se o ID não foi informado, grava-se um novo registro /** Consulta SQL */ $this->sql = 'insert into companies(name_business, name_fantasy, cnpj, cep, adress, number, complement, district, city_id, state_id, active, type, responsible, email, referene responsible_document, contract_date, computers, servers, user_id ) values (:name_business, :name_fantasy, :cnpj, :cep, :adress, :number, :complement, :district, :city_id, :state_id, :active, :type, :responsible, :email, :referene, :responsible_document, :contract_date, :computers, :servers, :user_id)'; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('user_id', $this->userId);/** Informa o usuário responsável pelo nova empresa cadastrada */ $this->stmt->bindParam('name_business', $this->nameBusiness); $this->stmt->bindParam('name_fantasy', $this->nameFantasy); $this->stmt->bindParam('cnpj', $this->cnpj); $this->stmt->bindParam('cep', $this->cep); $this->stmt->bindParam('adress', $this->adress); $this->stmt->bindParam('number', $this->number); $this->stmt->bindParam('complement', $this->complement); $this->stmt->bindParam('district', $this->district); $this->stmt->bindParam('city_id', $this->cityId); $this->stmt->bindParam('state_id', $this->stateId); $this->stmt->bindParam('active', $this->active); $this->stmt->bindParam('type', $this->type); $this->stmt->bindParam('responsible', $this->responsible); $this->stmt->bindParam('email', $this->email); $this->stmt->bindParam('referene', $this->referene); $this->stmt->bindParam('responsible_document', $this->responsible_document); $this->stmt->bindParam('contract_date', $this->contract_date); $this->stmt->bindParam('computers', $this->computers); $this->stmt->bindParam('servers', $this->servers); } /** Executo o SQL */ return $this->stmt->execute(); } /** Deleta um determinado registro no banco de dados */ function Delete(int $companyId) { /** Parametros de entrada */ $this->companyId = $companyId; /** Consulta SQL */ $this->sql = 'delete from companies where company_id = :company_id'; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('company_id', $this->companyId); /** Executo o SQL */ return $this->stmt->execute(); } /** Fecha uma conexão aberta anteriormente com o banco de dados */ function __destruct() { $this->connection = null; } }