CREATE TABLE IF NOT EXISTS pessoas (id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,grupo_matriz_id BIGINT UNSIGNED NOT NULL,codigo VARCHAR(20) NOT NULL,tipo_pessoa VARCHAR(10) NOT NULL,cpf_cnpj VARCHAR(14) NULL,nome_razao VARCHAR(160) NOT NULL,nome_fantasia VARCHAR(160) NULL,rg VARCHAR(20) NULL,inscricao_estadual VARCHAR(20) NULL,inscricao_municipal VARCHAR(20) NULL,nascimento_abertura DATE NULL,estrangeiro BOOLEAN NOT NULL DEFAULT FALSE,observacoes TEXT NULL,ativo BOOLEAN NOT NULL DEFAULT TRUE,criado_em TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,atualizado_em TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,excluido_em TIMESTAMP NULL,CONSTRAINT fk_pessoas_grupo FOREIGN KEY(grupo_matriz_id) REFERENCES empresas(id),UNIQUE KEY uq_pessoas_grupo_codigo(grupo_matriz_id,codigo),UNIQUE KEY uq_pessoas_grupo_documento(grupo_matriz_id,cpf_cnpj),INDEX idx_pessoas_grupo_nome(grupo_matriz_id,nome_razao)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS pessoa_empresas (pessoa_id BIGINT UNSIGNED NOT NULL,empresa_id BIGINT UNSIGNED NOT NULL,ativo BOOLEAN NOT NULL DEFAULT TRUE,criado_em TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,PRIMARY KEY(pessoa_id,empresa_id),CONSTRAINT fk_pessoa_empresas_pessoa FOREIGN KEY(pessoa_id) REFERENCES pessoas(id),CONSTRAINT fk_pessoa_empresas_empresa FOREIGN KEY(empresa_id) REFERENCES empresas(id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS pessoa_enderecos (id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,pessoa_id BIGINT UNSIGNED NOT NULL,tipo VARCHAR(20) NOT NULL DEFAULT 'principal',identificacao VARCHAR(80) NULL,cep VARCHAR(8) NULL,logradouro VARCHAR(160) NULL,numero VARCHAR(20) NULL,complemento VARCHAR(80) NULL,bairro VARCHAR(80) NULL,municipio VARCHAR(100) NULL,codigo_ibge VARCHAR(7) NULL,uf CHAR(2) NULL,pais VARCHAR(80) NOT NULL DEFAULT 'Brasil',principal BOOLEAN NOT NULL DEFAULT FALSE,CONSTRAINT fk_pessoa_enderecos_pessoa FOREIGN KEY(pessoa_id) REFERENCES pessoas(id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS pessoa_contatos (id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,pessoa_id BIGINT UNSIGNED NOT NULL,tipo VARCHAR(20) NOT NULL,nome VARCHAR(120) NULL,departamento VARCHAR(80) NULL,valor VARCHAR(160) NOT NULL,principal BOOLEAN NOT NULL DEFAULT FALSE,CONSTRAINT fk_pessoa_contatos_pessoa FOREIGN KEY(pessoa_id) REFERENCES pessoas(id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS pessoa_clientes (id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,pessoa_id BIGINT UNSIGNED NOT NULL,empresa_id BIGINT UNSIGNED NOT NULL,grupo_pessoa_id BIGINT UNSIGNED NULL,tabela_preco_id BIGINT UNSIGNED NULL,limite_credito DECIMAL(15,2) NOT NULL DEFAULT 0,desconto_maximo DECIMAL(7,2) NOT NULL DEFAULT 0,consumidor_final BOOLEAN NOT NULL DEFAULT TRUE,indicador_ie VARCHAR(20) NOT NULL DEFAULT 'nao_contribuinte',permite_venda_prazo BOOLEAN NOT NULL DEFAULT FALSE,permite_vencidos BOOLEAN NOT NULL DEFAULT FALSE,bloqueado BOOLEAN NOT NULL DEFAULT FALSE,motivo_bloqueio VARCHAR(500) NULL,ativo BOOLEAN NOT NULL DEFAULT TRUE,criado_em TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,atualizado_em TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,excluido_em TIMESTAMP NULL,CONSTRAINT fk_pessoa_clientes_pessoa FOREIGN KEY(pessoa_id) REFERENCES pessoas(id),CONSTRAINT fk_pessoa_clientes_empresa FOREIGN KEY(empresa_id) REFERENCES empresas(id),CONSTRAINT fk_pessoa_clientes_grupo FOREIGN KEY(grupo_pessoa_id) REFERENCES grupos_pessoa(id),CONSTRAINT fk_pessoa_clientes_tabela FOREIGN KEY(tabela_preco_id) REFERENCES tabelas_preco(id),UNIQUE KEY uq_pessoa_clientes(pessoa_id,empresa_id),INDEX idx_pessoa_clientes_empresa(empresa_id,ativo)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
