wulf-pulse/migrations/051_create_qbo_tables.sql
lorentz b98c67482a feat: QuickBooks Online integration
- Add QBO OAuth2 client with token refresh (lib/services/qbo-client.ts)
- Add QBO sync service for invoices, payments, deposits, purchases, journal entries, reports (lib/services/qbo-sync-service.ts)
- Add QBO types (lib/types/qbo.ts)
- Add API routes: /api/qbo/auth, /api/qbo/sync, /api/qbo/disconnect
- Add /admin/qbo status and sync management page
- Add legal pages: /legal/eula, /legal/privacy (Intuit app assessment)
- Add QBO nav link under Admin
- Fix reports: remove invalid summarize_column_by, add accounting_method from Preferences API, add showrows=all&showcols=all
- Add CashFlow report type alongside P&L and BalanceSheet
- Add NoReportData check to skip empty report months
- Add intuit_tid capture in error messages
- Add redirect: follow for cluster routing
- Migration 051: qbo_tokens, qbo_invoices, qbo_payments, qbo_deposits, qbo_transactions, qbo_reports tables

Also includes earlier work:
- Ping flap suppression pipeline step
- Ticket digest reports with LLM analysis
- Zabbix WAN monitor and gap analysis
- Kiosk is_deleted filter fixes
- Datto RMM ping target enrichment
- Entity sync soft-delete detection
2026-03-17 07:39:55 -04:00

125 lines
5.2 KiB
SQL

-- QBO OAuth2 tokens (single row for Wulf Consulting's QBO)
CREATE TABLE IF NOT EXISTS qbo_tokens (
id SERIAL PRIMARY KEY,
realm_id TEXT NOT NULL UNIQUE,
access_token TEXT NOT NULL,
refresh_token TEXT NOT NULL,
access_token_expires_at TIMESTAMPTZ NOT NULL,
refresh_token_expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Invoices
CREATE TABLE IF NOT EXISTS qbo_invoices (
id TEXT PRIMARY KEY, -- QBO Id
realm_id TEXT NOT NULL,
doc_number TEXT,
txn_date DATE,
due_date DATE,
customer_ref_id TEXT,
customer_ref_name TEXT,
email_address TEXT,
total_amt NUMERIC(12,2),
balance NUMERIC(12,2),
status TEXT, -- Open, Paid, Voided, etc.
currency_code TEXT DEFAULT 'USD',
line_items JSONB,
linked_txns JSONB,
sync_token TEXT,
qbo_created_at TIMESTAMPTZ,
qbo_updated_at TIMESTAMPTZ,
synced_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_qbo_invoices_txn_date ON qbo_invoices(txn_date);
CREATE INDEX IF NOT EXISTS idx_qbo_invoices_customer ON qbo_invoices(customer_ref_id);
CREATE INDEX IF NOT EXISTS idx_qbo_invoices_status ON qbo_invoices(status);
-- Payments
CREATE TABLE IF NOT EXISTS qbo_payments (
id TEXT PRIMARY KEY, -- QBO Id
realm_id TEXT NOT NULL,
txn_date DATE,
customer_ref_id TEXT,
customer_ref_name TEXT,
total_amt NUMERIC(12,2),
unapplied_amt NUMERIC(12,2),
currency_code TEXT DEFAULT 'USD',
payment_method_ref TEXT,
deposit_account_ref TEXT,
linked_txns JSONB,
sync_token TEXT,
qbo_created_at TIMESTAMPTZ,
qbo_updated_at TIMESTAMPTZ,
synced_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_qbo_payments_txn_date ON qbo_payments(txn_date);
CREATE INDEX IF NOT EXISTS idx_qbo_payments_customer ON qbo_payments(customer_ref_id);
-- Deposits
CREATE TABLE IF NOT EXISTS qbo_deposits (
id TEXT PRIMARY KEY, -- QBO Id
realm_id TEXT NOT NULL,
txn_date DATE,
deposit_to_account_ref_id TEXT,
deposit_to_account_ref_name TEXT,
total_amt NUMERIC(12,2),
line_items JSONB,
sync_token TEXT,
qbo_created_at TIMESTAMPTZ,
qbo_updated_at TIMESTAMPTZ,
synced_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_qbo_deposits_txn_date ON qbo_deposits(txn_date);
-- General ledger transactions (purchases, expenses, journal entries, etc.)
CREATE TABLE IF NOT EXISTS qbo_transactions (
id TEXT NOT NULL,
txn_type TEXT NOT NULL, -- Purchase, Expense, JournalEntry, Transfer, etc.
realm_id TEXT NOT NULL,
txn_date DATE,
doc_number TEXT,
entity_ref_id TEXT, -- Vendor/Customer ref
entity_ref_name TEXT,
entity_type TEXT, -- Vendor, Customer
account_ref_id TEXT,
account_ref_name TEXT,
total_amt NUMERIC(12,2),
currency_code TEXT DEFAULT 'USD',
private_note TEXT,
line_items JSONB,
sync_token TEXT,
qbo_created_at TIMESTAMPTZ,
qbo_updated_at TIMESTAMPTZ,
synced_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
PRIMARY KEY (id, txn_type)
);
CREATE INDEX IF NOT EXISTS idx_qbo_transactions_txn_date ON qbo_transactions(txn_date);
CREATE INDEX IF NOT EXISTS idx_qbo_transactions_type ON qbo_transactions(txn_type);
CREATE INDEX IF NOT EXISTS idx_qbo_transactions_entity ON qbo_transactions(entity_ref_id);
-- Financial reports: P&L and Balance Sheet stored as JSONB per period
CREATE TABLE IF NOT EXISTS qbo_reports (
id SERIAL PRIMARY KEY,
realm_id TEXT NOT NULL,
report_type TEXT NOT NULL, -- ProfitAndLoss, BalanceSheet
period_start DATE NOT NULL,
period_end DATE NOT NULL,
summarize_by TEXT DEFAULT 'Month',
report_data JSONB NOT NULL, -- Full report response
synced_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE (realm_id, report_type, period_start, period_end)
);
CREATE INDEX IF NOT EXISTS idx_qbo_reports_type_period ON qbo_reports(report_type, period_start);
COMMENT ON TABLE qbo_tokens IS 'QuickBooks Online OAuth2 tokens for Wulf Consulting';
COMMENT ON TABLE qbo_invoices IS 'QBO invoices synced from QuickBooks Online';
COMMENT ON TABLE qbo_payments IS 'QBO customer payments synced from QuickBooks Online';
COMMENT ON TABLE qbo_deposits IS 'QBO bank deposits synced from QuickBooks Online';
COMMENT ON TABLE qbo_transactions IS 'QBO general transactions (purchases, expenses, journals) synced from QuickBooks Online';
COMMENT ON TABLE qbo_reports IS 'QBO financial reports (P&L, Balance Sheet) stored per period';