FinOpenPOS
Fiscal Module

Database Schema (Fiscal)

The fiscal module adds 4 tables to the PGLite database using Drizzle ORM, all multi-tenant and keyed by user_uid, covering fiscal settings, invoices, invoice items, and invoice events.

Overview

The fiscal module adds 4 tables to the PGLite database (Drizzle ORM). All tables are multi-tenant, keyed by user_uid. The schema follows the convention: monetary values in cents (integer), names in snake_case.

File: apps/web/src/lib/db/schema.ts

Tables

fiscalSettings

One row per user. Stores company info, certificate, SEFAZ configuration, and default tax codes.

ColumnTypeDescription
idserial PKAuto-increment ID
user_uidtext UNIQUEUser identifier (multi-tenancy key)
Company
company_nametextRazao Social
trade_nametextNome Fantasia (optional)
tax_idtextCNPJ (14 digits)
state_tax_idtextInscricao Estadual (IE)
tax_regimetextCRT: "1" (Simples), "2" (excess), "3" (Normal)
Address
state_codetextUF (e.g. "SP")
city_codetextIBGE city code
city_nametextCity name
streettextLogradouro
street_numbertextNumber
districttextBairro
zip_codetextCEP (8 digits)
address_complementtextComplemento (optional)
SEFAZ
environmenttext"1" (production) or "2" (homologation)
nfe_seriesintegerNF-e series (default 1)
nfce_seriesintegerNFC-e series (default 1)
next_nfe_numberintegerNext NF-e number counter
next_nfce_numberintegerNext NFC-e number counter
NFC-e Security
csc_idtextCSC identifier (for QR code)
csc_tokentextCSC secret token
Certificate
certificate_pfxbyteaRaw PFX binary (nullable)
certificate_passwordtextPFX password (nullable)
certificate_valid_untiltimestampCertificate expiry date
Defaults
default_ncmtextDefault NCM code for products
default_cfoptextDefault CFOP (e.g. "5102")
default_icms_csttextDefault ICMS CST (e.g. "00")
default_pis_csttextDefault PIS CST (e.g. "99")
default_cofins_csttextDefault COFINS CST (e.g. "99")

invoices

One row per issued invoice. Tracks the complete lifecycle from pending to authorized/cancelled.

ColumnTypeDescription
idserial PK
user_uidtextMulti-tenancy key
order_idinteger FKReference to orders table
modelinteger55 (NF-e) or 65 (NFC-e)
seriesintegerSeries number
numberintegerInvoice sequential number
access_keytext44-digit chave de acesso
operation_naturetextNatureza da operacao (e.g. "Venda")
operation_typeinteger0 (entrada) or 1 (saida)
statustextpending, authorized, rejected, cancelled, denied, contingency, voided
environmentinteger1 or 2
XML Storage
request_xmltextSigned XML sent to SEFAZ
response_xmltextRaw SEFAZ response
protocol_xmltextnfeProc (NFe + protNFe)
SEFAZ Response
protocol_numbertextnProt from SEFAZ
status_codeintegercStat (100=authorized, etc.)
status_messagetextxMotivo
Timestamps
issued_attimestampWhen XML was generated
authorized_attimestampWhen SEFAZ authorized
Contingency
is_contingencybooleanWas issued in contingency mode
contingency_typetextsvc-an, svc-rs, offline
contingency_attimestampWhen contingency was activated
contingency_reasontextReason for contingency
Recipient
recipient_tax_idtextCPF/CNPJ of recipient
recipient_nametextRecipient name
total_amountintegerTotal in cents

invoiceItems

One row per item in an invoice.

ColumnTypeDescription
idserial PK
invoice_idinteger FKReference to invoices
product_idinteger FKReference to products (optional)
item_numberintegernItem (1-based position)
product_codetextcProd
descriptiontextxProd
ncmtextNCM code (8 digits)
cfoptextCFOP code (4 digits)
unit_of_measuretextuCom (e.g. "UN", "KG")
quantityintegerx1000 (1500 = 1.500)
unit_priceintegerCents (1050 = R$10.50)
total_priceintegerCents
icms_csttextICMS CST code
icms_rateintegerx100 (1800 = 18.00%)
icms_amountintegerCents
pis_csttextPIS CST code
cofins_csttextCOFINS CST code

invoiceEvents

Audit log for invoice lifecycle events (cancellation, voiding, etc.).

ColumnTypeDescription
idserial PK
invoice_idinteger FKReference to invoices
event_typetext"cancellation", "voiding", etc.
sequenceintegernSeqEvento
protocol_numbertextnProt from SEFAZ response
status_codeintegercStat
reasontextxJust
request_xmltextSigned event XML
response_xmltextSEFAZ event response
created_attimestampEvent timestamp

cities (Reference)

IBGE city codes, seeded from the IBGE API (~5570 rows).

ColumnTypeDescription
idinteger PKIBGE city code
nametextCity name
state_codetextUF (e.g. "SP")

products (Extended)

The existing products table was extended with optional fiscal fields:

ColumnTypeDescription
ncmtextNCM code (nullable, uses default from settings)
cfoptextCFOP code (nullable)
icms_csttextICMS CST (nullable)
pis_csttextPIS CST (nullable)
cofins_csttextCOFINS CST (nullable)
unit_of_measuretextUnit (nullable, default "UN")

Seeding (apps/web/src/lib/db/seed.ts)

The cities table is seeded from the IBGE API:

https://servicodados.ibge.gov.br/api/v1/localidades/municipios

This runs automatically on bun run dev (via drizzle-kit push + seed).

On this page