170 lines
7.1 KiB
SQL
170 lines
7.1 KiB
SQL
-- Зеркало QuickBooks Online (модуль quickbooks_bookkeeper, пока загружает сервис qbo_auth: POST /sync).
|
||
-- Таблицы создаются по предложению docs/proposed/modules/quickbooks_bookkeeper.md (п. 4.2); состав колонок ждёт согласования владельца.
|
||
-- Ключ везде (realm_id, id): sandbox и production не смешиваются. Полная запись объекта QuickBooks - в raw (jsonb), чтобы ничего не терять при смене состава колонок.
|
||
-- Зеркало только читается из QuickBooks; изменения в QuickBooks делаются отдельным путём (после подтверждения человека), эти таблицы вручную не правятся.
|
||
|
||
create table if not exists qbo_account (
|
||
realm_id text not null,
|
||
id text not null,
|
||
acct_num text, -- код счёта (например 3.5.)
|
||
name text not null,
|
||
fully_qualified_name text, -- с родителями: "13. Hard Cost:13.17. Plumbing:..."
|
||
account_type text,
|
||
account_sub_type text,
|
||
classification text, -- Asset / Liability / Equity / Revenue / Expense
|
||
parent_id text,
|
||
active boolean not null default true,
|
||
current_balance numeric(18,2),
|
||
sync_token text,
|
||
meta_updated timestamptz,
|
||
raw jsonb not null,
|
||
synced_at timestamptz not null default now(),
|
||
primary key (realm_id, id)
|
||
);
|
||
|
||
create table if not exists qbo_vendor (
|
||
realm_id text not null,
|
||
id text not null,
|
||
display_name text not null,
|
||
company_name text,
|
||
email text,
|
||
phone text,
|
||
active boolean not null default true,
|
||
balance numeric(18,2),
|
||
sync_token text,
|
||
meta_updated timestamptz,
|
||
raw jsonb not null,
|
||
synced_at timestamptz not null default now(),
|
||
primary key (realm_id, id)
|
||
);
|
||
|
||
-- Клиенты и проекты: проект в QuickBooks - дочерний Customer (is_job = true, parent_id - клиент).
|
||
create table if not exists qbo_customer (
|
||
realm_id text not null,
|
||
id text not null,
|
||
display_name text not null,
|
||
fully_qualified_name text,
|
||
parent_id text,
|
||
is_job boolean not null default false,
|
||
active boolean not null default true,
|
||
balance numeric(18,2),
|
||
sync_token text,
|
||
meta_updated timestamptz,
|
||
raw jsonb not null,
|
||
synced_at timestamptz not null default now(),
|
||
primary key (realm_id, id)
|
||
);
|
||
|
||
-- Расходы (банк, карта, чек): Purchase.
|
||
create table if not exists qbo_purchase (
|
||
realm_id text not null,
|
||
id text not null,
|
||
txn_date date,
|
||
payment_type text, -- Cash / Check / CreditCard
|
||
account_id text, -- банковский или карточный счёт
|
||
account_name text,
|
||
entity_id text, -- вендор
|
||
entity_name text,
|
||
total_amt numeric(18,2),
|
||
doc_number text,
|
||
private_note text, -- сюда автомат пишет идентификатор предложения (идемпотентность, отметка источника)
|
||
sync_token text,
|
||
meta_created timestamptz,
|
||
meta_updated timestamptz,
|
||
raw jsonb not null,
|
||
synced_at timestamptz not null default now(),
|
||
primary key (realm_id, id)
|
||
);
|
||
create index if not exists qbo_purchase_date_idx on qbo_purchase (realm_id, txn_date);
|
||
|
||
create table if not exists qbo_purchase_line (
|
||
realm_id text not null,
|
||
purchase_id text not null,
|
||
line_no int not null,
|
||
amount numeric(18,2),
|
||
description text,
|
||
account_id text, -- счёт учёта строки (категория)
|
||
account_name text,
|
||
customer_id text, -- проект / клиент строки
|
||
customer_name text,
|
||
class_id text,
|
||
billable_status text,
|
||
primary key (realm_id, purchase_id, line_no)
|
||
);
|
||
create index if not exists qbo_purchase_line_account_idx on qbo_purchase_line (realm_id, account_id);
|
||
create index if not exists qbo_purchase_line_customer_idx on qbo_purchase_line (realm_id, customer_id);
|
||
|
||
-- Счета вендоров.
|
||
create table if not exists qbo_bill (
|
||
realm_id text not null,
|
||
id text not null,
|
||
txn_date date,
|
||
due_date date,
|
||
vendor_id text,
|
||
vendor_name text,
|
||
total_amt numeric(18,2),
|
||
balance numeric(18,2),
|
||
doc_number text,
|
||
private_note text,
|
||
sync_token text,
|
||
meta_created timestamptz,
|
||
meta_updated timestamptz,
|
||
raw jsonb not null,
|
||
synced_at timestamptz not null default now(),
|
||
primary key (realm_id, id)
|
||
);
|
||
|
||
create table if not exists qbo_bill_line (
|
||
realm_id text not null,
|
||
bill_id text not null,
|
||
line_no int not null,
|
||
amount numeric(18,2),
|
||
description text,
|
||
account_id text,
|
||
account_name text,
|
||
customer_id text,
|
||
customer_name text,
|
||
class_id text,
|
||
billable_status text,
|
||
primary key (realm_id, bill_id, line_no)
|
||
);
|
||
create index if not exists qbo_bill_line_customer_idx on qbo_bill_line (realm_id, customer_id);
|
||
|
||
-- Вложения (инвойсы, чеки) и к чему они привязаны: нужны для правила «к клиентскому расходу обязателен документ».
|
||
create table if not exists qbo_attachable (
|
||
realm_id text not null,
|
||
id text not null,
|
||
file_name text,
|
||
content_type text,
|
||
size bigint,
|
||
note text,
|
||
refs jsonb not null default '[]'::jsonb, -- [{"type":"Purchase","id":"144"}, ...]
|
||
sync_token text,
|
||
meta_created timestamptz,
|
||
raw jsonb not null,
|
||
synced_at timestamptz not null default now(),
|
||
primary key (realm_id, id)
|
||
);
|
||
|
||
-- Журнал загрузок зеркала.
|
||
create table if not exists qbo_sync_run (
|
||
id bigserial primary key,
|
||
realm_id text,
|
||
environment text,
|
||
started_at timestamptz not null default now(),
|
||
finished_at timestamptz,
|
||
status text not null default 'running' check (status in ('running', 'ok', 'error')),
|
||
counts jsonb not null default '{}'::jsonb, -- {"Account": 89, "Purchase": 35, ...}
|
||
error text
|
||
);
|
||
|
||
alter table qbo_account enable row level security;
|
||
alter table qbo_vendor enable row level security;
|
||
alter table qbo_customer enable row level security;
|
||
alter table qbo_purchase enable row level security;
|
||
alter table qbo_purchase_line enable row level security;
|
||
alter table qbo_bill enable row level security;
|
||
alter table qbo_bill_line enable row level security;
|
||
alter table qbo_attachable enable row level security;
|
||
alter table qbo_sync_run enable row level security;
|