Files
agenciapsilmno/migrations/payment_settings.sql
2026-03-12 08:58:36 -03:00

72 lines
2.5 KiB
PL/PgSQL

-- migrations/payment_settings.sql
-- Tabela de configurações de formas de pagamento por terapeuta/owner
-- Execute no Supabase SQL Editor
CREATE TABLE IF NOT EXISTS payment_settings (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
owner_id uuid NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
tenant_id uuid REFERENCES tenants(id) ON DELETE CASCADE,
-- Pix
pix_ativo boolean NOT NULL DEFAULT false,
pix_tipo text NOT NULL DEFAULT 'cpf', -- cpf | cnpj | email | celular | aleatoria
pix_chave text NOT NULL DEFAULT '',
pix_nome_titular text NOT NULL DEFAULT '',
-- Depósito / TED
deposito_ativo boolean NOT NULL DEFAULT false,
deposito_banco text NOT NULL DEFAULT '',
deposito_agencia text NOT NULL DEFAULT '',
deposito_conta text NOT NULL DEFAULT '',
deposito_tipo_conta text NOT NULL DEFAULT 'corrente', -- corrente | poupanca
deposito_titular text NOT NULL DEFAULT '',
deposito_cpf_cnpj text NOT NULL DEFAULT '',
-- Dinheiro (espécie)
dinheiro_ativo boolean NOT NULL DEFAULT false,
-- Cartão (maquininha presencial)
cartao_ativo boolean NOT NULL DEFAULT false,
cartao_instrucao text NOT NULL DEFAULT '',
-- Plano de saúde / Convênio
convenio_ativo boolean NOT NULL DEFAULT false,
convenio_lista text NOT NULL DEFAULT '', -- texto livre com convênios aceitos
-- Observações gerais exibidas ao paciente
observacoes_pagamento text NOT NULL DEFAULT '',
created_at timestamptz DEFAULT now(),
updated_at timestamptz DEFAULT now(),
CONSTRAINT payment_settings_owner_id_key UNIQUE (owner_id)
);
-- Índice por tenant
CREATE INDEX IF NOT EXISTS payment_settings_tenant_id_idx ON payment_settings(tenant_id);
-- RLS
ALTER TABLE payment_settings ENABLE ROW LEVEL SECURITY;
-- Owner lê e escreve os próprios dados
DROP POLICY IF EXISTS "payment_settings: owner full access" ON payment_settings;
CREATE POLICY "payment_settings: owner full access"
ON payment_settings
FOR ALL
USING (owner_id = auth.uid())
WITH CHECK (owner_id = auth.uid());
-- updated_at automático
CREATE OR REPLACE FUNCTION update_payment_settings_updated_at()
RETURNS TRIGGER LANGUAGE plpgsql AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$;
DROP TRIGGER IF EXISTS trg_payment_settings_updated_at ON payment_settings;
CREATE TRIGGER trg_payment_settings_updated_at
BEFORE UPDATE ON payment_settings
FOR EACH ROW EXECUTE FUNCTION update_payment_settings_updated_at();