arbritage/supabase_schema.sql
2026-01-26 11:20:25 -03:00

126 lines
3.9 KiB
SQL

-- Enable UUID extension
create extension if not exists "uuid-ossp";
-- 1. USERS & PROFILES (Managed by Supabase Auth, but we can have a public profile)
create table if not exists public.profiles (
id uuid references auth.users not null primary key,
email text,
full_name text,
role text default 'Buyer',
created_at timestamp with time zone default timezone('utc'::text, now()) not null
);
-- 2. INVENTORY (Products)
create table if not exists public.inventory (
id uuid default uuid_generate_v4() primary key,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
name text not null,
sku text,
ean text,
quantity integer default 0,
avg_cost_brl numeric default 0,
market_price_brl numeric default 0,
last_supplier text,
user_id uuid references auth.users
);
-- 3. SUPPLIERS
create table if not exists public.suppliers (
id uuid default uuid_generate_v4() primary key,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
name text not null,
contact text,
rating integer default 0,
last_purchase timestamp with time zone,
user_id uuid references auth.users
);
-- 4. CUSTOMERS
create table if not exists public.customers (
id uuid default uuid_generate_v4() primary key,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
name text not null,
email text,
phone text,
city text,
status text default 'Active', -- 'Active', 'Inactive', 'Prospect'
total_purchased numeric default 0,
user_id uuid references auth.users
);
-- 5. ORDERS
create table if not exists public.orders (
id uuid default uuid_generate_v4() primary key,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
date timestamp with time zone,
status text default 'Pending', -- 'Pending', 'Paid', 'Received', 'Cancelled'
total_usd numeric default 0,
total_brl numeric default 0,
total_cost_with_overhead numeric default 0,
estimated_profit numeric default 0,
items jsonb, -- Stores the JSON array of items
supplier_name text,
user_id uuid references auth.users
);
-- 6. TRANSACTIONS (Financial)
create table if not exists public.transactions (
id uuid default uuid_generate_v4() primary key,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
date timestamp with time zone,
type text, -- 'Income', 'Expense'
category text,
amount numeric default 0,
description text,
user_id uuid references auth.users
);
-- 7. SETTINGS (Global Config)
create table if not exists public.settings (
id uuid default uuid_generate_v4() primary key,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
updated_at timestamp with time zone default timezone('utc'::text, now()),
-- Company Info
company_name text,
cnpj text,
ie text,
-- Financial Defaults
default_overhead numeric default 20,
default_exchange numeric default 5.65,
-- Integration Tokens
brazil_api_token text,
melhor_envio_token text,
bling_token text,
tiny_token text,
gemini_key text,
-- Fiscal / NFe
certificate_password text, -- CAUTION: Storing plain text/base64? Ideally encrypted.
nfe_serie text default '1',
nfe_number text,
nfe_environment text default 'homologacao',
-- Email SMTP
smtp_host text,
smtp_port text,
smtp_user text,
smtp_pass text,
-- Automation
auto_sync_sales boolean default true,
auto_sync_stock boolean default true,
user_id uuid references auth.users
);
-- RLS POLICIES (Optional but recommended)
alter table public.inventory enable row level security;
alter table public.suppliers enable row level security;
alter table public.customers enable row level security;
alter table public.orders enable row level security;
alter table public.transactions enable row level security;
alter table public.settings enable row level security;
-- (Policies would need to be added to allow read/write for authenticated users)