-- CreateTable
CREATE TABLE `clients` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,
    `status` ENUM('ACTIVE', 'BLOCKED', 'INACTIVE', 'DECEASED', 'REVISION', 'PRE_REGISTER') NOT NULL DEFAULT 'ACTIVE',
    `name` VARCHAR(191) NOT NULL,
    `email` VARCHAR(191) NULL,
    `phone` VARCHAR(191) NULL,
    `mobile` VARCHAR(191) NULL,
    `is_mobile_whatsapp` BOOLEAN NOT NULL DEFAULT false,
    `mobile_alternate` VARCHAR(191) NULL,
    `is_mobile_alternate_whatsapp` BOOLEAN NOT NULL DEFAULT false,
    `birth_date` DATE NULL,
    `gender` ENUM('MALE', 'FEMALE', 'NOT_INFORMED') NOT NULL DEFAULT 'NOT_INFORMED',
    `nationality` VARCHAR(191) NULL,
    `place_of_birth` VARCHAR(191) NULL,
    `cpf` VARCHAR(191) NULL,
    `rg` VARCHAR(191) NULL,
    `rg_emiter` VARCHAR(191) NULL,
    `marital_status` ENUM('MARRIED', 'SINGLE', 'DIVORCED', 'STABLE_UNION', 'WIDOWED') NULL,
    `marital_regimes` VARCHAR(191) NULL,
    `spouse_name` VARCHAR(191) NULL,
    `registry_office_signature` VARCHAR(191) NULL,
    `consultant` INTEGER NULL,
    `address` VARCHAR(191) NULL,
    `complement` VARCHAR(191) NULL,
    `neighborhood` VARCHAR(191) NULL,
    `zip_code` VARCHAR(191) NULL,
    `state` INTEGER NULL,
    `city` INTEGER NULL,
    `bank` INTEGER NULL,
    `agency_number` VARCHAR(191) NULL,
    `account_number` VARCHAR(191) NULL,
    `operation_number` VARCHAR(191) NULL,
    `account_type` ENUM('CHECKING', 'SAVINGS') NULL,
    `account_ownership_type` ENUM('INDIVIDUAL', 'JOINT') NULL,
    `account_owner_name` VARCHAR(191) NULL,
    `account_owner_cpf` VARCHAR(191) NULL,
    `account_co_owner_name` VARCHAR(191) NULL,
    `account_co_owner_cpf` VARCHAR(191) NULL,
    `pix_key` VARCHAR(191) NULL,
    `pix_type` VARCHAR(191) NULL,
    `comments` TEXT NULL,
    `consider_contract_fees` BOOLEAN NOT NULL DEFAULT false,

    UNIQUE INDEX `clients_email_key`(`email`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `professional_details` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `council_register_number` VARCHAR(191) NULL,
    `occupation` INTEGER NULL,
    `expertise` INTEGER NULL,
    `training_title` INTEGER NULL,
    `professional_council` INTEGER NULL,
    `state` INTEGER NULL,
    `client` INTEGER NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `states` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `cities` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `state` INTEGER NOT NULL,
    `name` VARCHAR(191) NOT NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `providers` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `cnpj` VARCHAR(191) NULL,
    `email` VARCHAR(191) NULL,
    `phone` VARCHAR(191) NULL,
    `mobile` VARCHAR(191) NULL,
    `municipal_registration` VARCHAR(191) NULL,
    `state_registration` VARCHAR(191) NULL,
    `address` VARCHAR(191) NULL,
    `complement` VARCHAR(191) NULL,
    `neighborhood` VARCHAR(191) NULL,
    `zip_code` VARCHAR(191) NULL,
    `state` INTEGER NULL,
    `city` INTEGER NULL,
    `comments` TEXT NULL,
    `provider_type` INTEGER NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `provider_types` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `description` VARCHAR(191) NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `service_takers` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `corporate_name` VARCHAR(191) NULL,
    `cnpj` VARCHAR(191) NULL,
    `email` VARCHAR(191) NULL,
    `phone` VARCHAR(191) NULL,
    `mobile` VARCHAR(191) NULL,
    `municipal_registration` VARCHAR(191) NULL,
    `state_registration` VARCHAR(191) NULL,
    `address` VARCHAR(191) NULL,
    `complement` VARCHAR(191) NULL,
    `neighborhood` VARCHAR(191) NULL,
    `zip_code` VARCHAR(191) NULL,
    `state` INTEGER NULL,
    `city` INTEGER NULL,
    `ir` DECIMAL(65, 30) NULL,
    `ir_retains` BOOLEAN NULL DEFAULT false,
    `cofins` DECIMAL(65, 30) NULL,
    `cofins_retains` BOOLEAN NULL DEFAULT false,
    `pis` DECIMAL(65, 30) NULL,
    `pis_retains` BOOLEAN NULL DEFAULT false,
    `csll` DECIMAL(65, 30) NULL,
    `csll_retains` BOOLEAN NULL DEFAULT false,
    `iss` DECIMAL(65, 30) NULL,
    `iss_retains` BOOLEAN NULL DEFAULT false,
    `comments` TEXT NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `contacts` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,
    `sector` VARCHAR(191) NULL,
    `name` VARCHAR(191) NOT NULL,
    `phone` VARCHAR(191) NULL,
    `mobile` VARCHAR(191) NULL,
    `email` VARCHAR(191) NULL,
    `description` VARCHAR(191) NULL,
    `service_taker` INTEGER NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `customer_work_places` (
    `work_place` INTEGER NOT NULL,
    `client` INTEGER NOT NULL,
    `sector` INTEGER NOT NULL,

    PRIMARY KEY (`work_place`, `sector`, `client`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `sectors` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `contact_person` VARCHAR(191) NULL,
    `phone` VARCHAR(191) NULL,
    `email` VARCHAR(191) NULL,
    `service_taker` INTEGER NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `professional_councils` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `description` VARCHAR(191) NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `company_council_details` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `professional_council` INTEGER NOT NULL,
    `company` INTEGER NULL,
    `technical_manager` VARCHAR(191) NULL,
    `register_date` DATE NULL,
    `expiration_date` DATE NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `occupations` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `description` VARCHAR(191) NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `expertises` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `description` TEXT NULL,
    `occupation` INTEGER NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `training_titles` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `description` VARCHAR(191) NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `client_tags` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `color` VARCHAR(191) NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `users` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(191) NOT NULL,
    `password` VARCHAR(191) NOT NULL,
    `status` ENUM('ACTIVE', 'INACTIVE') NOT NULL DEFAULT 'ACTIVE',
    `is_consultant` BOOLEAN NOT NULL DEFAULT false,
    `name` VARCHAR(191) NOT NULL,
    `email` VARCHAR(191) NOT NULL,
    `phone` VARCHAR(191) NULL,
    `mobile` VARCHAR(191) NULL,

    UNIQUE INDEX `users_username_key`(`username`),
    UNIQUE INDEX `users_email_key`(`email`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `invoices` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `status` ENUM('WAITING_CHECK', 'APPROVED', 'PAID', 'CANCELED', 'PENDING', 'ARCHIVED') NOT NULL DEFAULT 'WAITING_CHECK',
    `service_taker` INTEGER NULL,
    `company` INTEGER NULL,
    `number` VARCHAR(191) NOT NULL,
    `gross_value` DECIMAL(20, 2) NOT NULL,
    `net_value` DECIMAL(20, 2) NOT NULL,
    `description` TEXT NULL,
    `issue_date` DATE NOT NULL,
    `release_date` DATE NULL,
    `sector` INTEGER NULL,
    `ir` DECIMAL NOT NULL,
    `cofins` DECIMAL NOT NULL,
    `pis` DECIMAL NOT NULL,
    `csll` DECIMAL NOT NULL,
    `iss` DECIMAL NOT NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `invoice_lines` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `payment_date` DATE NULL,
    `client` INTEGER NOT NULL,
    `invoice` INTEGER NOT NULL,
    `gross_value` DECIMAL(20, 2) NOT NULL,
    `paid_value` DECIMAL(20, 2) NULL,
    `status` ENUM('WAITING_CHECK', 'APPROVED', 'PAID', 'CANCELLED') NOT NULL DEFAULT 'WAITING_CHECK',
    `info` JSON NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `invoice_line_fees` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `description` VARCHAR(191) NOT NULL,
    `value` DECIMAL(20, 2) NOT NULL,
    `invoice_line_id` INTEGER NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `companies` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,
    `status` ENUM('ACTIVE', 'CLOSED', 'CONTROL') NOT NULL DEFAULT 'ACTIVE',
    `name` VARCHAR(191) NOT NULL,
    `administrative_partner` VARCHAR(191) NULL,
    `cnpj` VARCHAR(191) NULL,
    `email` VARCHAR(191) NULL,
    `phone` VARCHAR(191) NULL,
    `municipal_registration` VARCHAR(191) NULL,
    `contract` INTEGER NULL,
    `accounting_company` INTEGER NULL,
    `send_emails` BOOLEAN NOT NULL DEFAULT false,
    `address` VARCHAR(191) NULL,
    `complement` VARCHAR(191) NULL,
    `neighborhood` VARCHAR(191) NULL,
    `zip_code` VARCHAR(191) NULL,
    `state` INTEGER NULL,
    `city` INTEGER NULL,
    `bank` INTEGER NULL,
    `agency_number` VARCHAR(191) NULL,
    `account_number` VARCHAR(191) NULL,
    `pix` VARCHAR(191) NULL,
    `pix_type` ENUM('CPF_CNPJ', 'TELEFONE', 'AGENCIA_CONTA', 'EMAIL', 'ALEATORIA') NULL,
    `comments` TEXT NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `banks` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `code` VARCHAR(191) NULL,
    `name` VARCHAR(191) NOT NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `contracts` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `description` VARCHAR(191) NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `fees` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `code` VARCHAR(191) NULL,
    `name` VARCHAR(191) NOT NULL,
    `description` VARCHAR(191) NULL,
    `fee_type` ENUM('MONTHLY', 'PERCENTAGE', 'REAL_VALUE') NOT NULL,
    `amount` DECIMAL(20, 2) NOT NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `logs` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `description` VARCHAR(191) NOT NULL,
    `user` INTEGER NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `balances` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `type` ENUM('ADMINISTRATIVE_FEE', 'TAXES', 'BANK_FEE', 'PAYMENT_APPROVED', 'TRANSFER', 'REVERSAL', 'ISOLATED') NOT NULL,
    `value` DECIMAL(20, 2) NOT NULL,
    `description` VARCHAR(191) NOT NULL,
    `date` DATE NOT NULL,
    `client` INTEGER NULL,
    `company` INTEGER NULL,
    `invoice` INTEGER NULL,
    `invoice_line` INTEGER NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `_ClientToClientTags` (
    `A` INTEGER NOT NULL,
    `B` INTEGER NOT NULL,

    UNIQUE INDEX `_ClientToClientTags_AB_unique`(`A`, `B`),
    INDEX `_ClientToClientTags_B_index`(`B`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `_ClientToCompany` (
    `A` INTEGER NOT NULL,
    `B` INTEGER NOT NULL,

    UNIQUE INDEX `_ClientToCompany_AB_unique`(`A`, `B`),
    INDEX `_ClientToCompany_B_index`(`B`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `_ClientToFee` (
    `A` INTEGER NOT NULL,
    `B` INTEGER NOT NULL,

    UNIQUE INDEX `_ClientToFee_AB_unique`(`A`, `B`),
    INDEX `_ClientToFee_B_index`(`B`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `_ContractToFee` (
    `A` INTEGER NOT NULL,
    `B` INTEGER NOT NULL,

    UNIQUE INDEX `_ContractToFee_AB_unique`(`A`, `B`),
    INDEX `_ContractToFee_B_index`(`B`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `clients` ADD CONSTRAINT `clients_consultant_fkey` FOREIGN KEY (`consultant`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `clients` ADD CONSTRAINT `clients_state_fkey` FOREIGN KEY (`state`) REFERENCES `states`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `clients` ADD CONSTRAINT `clients_city_fkey` FOREIGN KEY (`city`) REFERENCES `cities`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `clients` ADD CONSTRAINT `clients_bank_fkey` FOREIGN KEY (`bank`) REFERENCES `banks`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `professional_details` ADD CONSTRAINT `professional_details_occupation_fkey` FOREIGN KEY (`occupation`) REFERENCES `occupations`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `professional_details` ADD CONSTRAINT `professional_details_expertise_fkey` FOREIGN KEY (`expertise`) REFERENCES `expertises`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `professional_details` ADD CONSTRAINT `professional_details_training_title_fkey` FOREIGN KEY (`training_title`) REFERENCES `training_titles`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `professional_details` ADD CONSTRAINT `professional_details_professional_council_fkey` FOREIGN KEY (`professional_council`) REFERENCES `professional_councils`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `professional_details` ADD CONSTRAINT `professional_details_state_fkey` FOREIGN KEY (`state`) REFERENCES `states`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `professional_details` ADD CONSTRAINT `professional_details_client_fkey` FOREIGN KEY (`client`) REFERENCES `clients`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cities` ADD CONSTRAINT `cities_state_fkey` FOREIGN KEY (`state`) REFERENCES `states`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `providers` ADD CONSTRAINT `providers_provider_type_fkey` FOREIGN KEY (`provider_type`) REFERENCES `provider_types`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `providers` ADD CONSTRAINT `providers_city_fkey` FOREIGN KEY (`city`) REFERENCES `cities`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `providers` ADD CONSTRAINT `providers_state_fkey` FOREIGN KEY (`state`) REFERENCES `states`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `service_takers` ADD CONSTRAINT `service_takers_state_fkey` FOREIGN KEY (`state`) REFERENCES `states`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `service_takers` ADD CONSTRAINT `service_takers_city_fkey` FOREIGN KEY (`city`) REFERENCES `cities`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `contacts` ADD CONSTRAINT `contacts_service_taker_fkey` FOREIGN KEY (`service_taker`) REFERENCES `service_takers`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `customer_work_places` ADD CONSTRAINT `customer_work_places_sector_fkey` FOREIGN KEY (`sector`) REFERENCES `sectors`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `customer_work_places` ADD CONSTRAINT `customer_work_places_client_fkey` FOREIGN KEY (`client`) REFERENCES `clients`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `customer_work_places` ADD CONSTRAINT `customer_work_places_work_place_fkey` FOREIGN KEY (`work_place`) REFERENCES `service_takers`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `sectors` ADD CONSTRAINT `sectors_service_taker_fkey` FOREIGN KEY (`service_taker`) REFERENCES `service_takers`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `company_council_details` ADD CONSTRAINT `company_council_details_company_fkey` FOREIGN KEY (`company`) REFERENCES `companies`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `company_council_details` ADD CONSTRAINT `company_council_details_professional_council_fkey` FOREIGN KEY (`professional_council`) REFERENCES `professional_councils`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `expertises` ADD CONSTRAINT `expertises_occupation_fkey` FOREIGN KEY (`occupation`) REFERENCES `occupations`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `invoices` ADD CONSTRAINT `invoices_service_taker_fkey` FOREIGN KEY (`service_taker`) REFERENCES `service_takers`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `invoices` ADD CONSTRAINT `invoices_company_fkey` FOREIGN KEY (`company`) REFERENCES `companies`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `invoices` ADD CONSTRAINT `invoices_sector_fkey` FOREIGN KEY (`sector`) REFERENCES `sectors`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `invoice_lines` ADD CONSTRAINT `invoice_lines_client_fkey` FOREIGN KEY (`client`) REFERENCES `clients`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `invoice_lines` ADD CONSTRAINT `invoice_lines_invoice_fkey` FOREIGN KEY (`invoice`) REFERENCES `invoices`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `invoice_line_fees` ADD CONSTRAINT `invoice_line_fees_invoice_line_id_fkey` FOREIGN KEY (`invoice_line_id`) REFERENCES `invoice_lines`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `companies` ADD CONSTRAINT `companies_accounting_company_fkey` FOREIGN KEY (`accounting_company`) REFERENCES `service_takers`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `companies` ADD CONSTRAINT `companies_contract_fkey` FOREIGN KEY (`contract`) REFERENCES `contracts`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `companies` ADD CONSTRAINT `companies_bank_fkey` FOREIGN KEY (`bank`) REFERENCES `banks`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `companies` ADD CONSTRAINT `companies_state_fkey` FOREIGN KEY (`state`) REFERENCES `states`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `companies` ADD CONSTRAINT `companies_city_fkey` FOREIGN KEY (`city`) REFERENCES `cities`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `logs` ADD CONSTRAINT `logs_user_fkey` FOREIGN KEY (`user`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `balances` ADD CONSTRAINT `balances_client_fkey` FOREIGN KEY (`client`) REFERENCES `clients`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `balances` ADD CONSTRAINT `balances_company_fkey` FOREIGN KEY (`company`) REFERENCES `companies`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `balances` ADD CONSTRAINT `balances_invoice_line_fkey` FOREIGN KEY (`invoice_line`) REFERENCES `invoice_lines`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `balances` ADD CONSTRAINT `balances_invoice_fkey` FOREIGN KEY (`invoice`) REFERENCES `invoices`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `_ClientToClientTags` ADD CONSTRAINT `_ClientToClientTags_A_fkey` FOREIGN KEY (`A`) REFERENCES `clients`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `_ClientToClientTags` ADD CONSTRAINT `_ClientToClientTags_B_fkey` FOREIGN KEY (`B`) REFERENCES `client_tags`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `_ClientToCompany` ADD CONSTRAINT `_ClientToCompany_A_fkey` FOREIGN KEY (`A`) REFERENCES `clients`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `_ClientToCompany` ADD CONSTRAINT `_ClientToCompany_B_fkey` FOREIGN KEY (`B`) REFERENCES `companies`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `_ClientToFee` ADD CONSTRAINT `_ClientToFee_A_fkey` FOREIGN KEY (`A`) REFERENCES `clients`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `_ClientToFee` ADD CONSTRAINT `_ClientToFee_B_fkey` FOREIGN KEY (`B`) REFERENCES `fees`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `_ContractToFee` ADD CONSTRAINT `_ContractToFee_A_fkey` FOREIGN KEY (`A`) REFERENCES `contracts`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `_ContractToFee` ADD CONSTRAINT `_ContractToFee_B_fkey` FOREIGN KEY (`B`) REFERENCES `fees`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
