Skip to content

Cơ sở dữ liệu ​

Cơ sở dữ liệu D1 (SQLite edge) với 6 bảng chính, 8 indexes, và quan hệ foreign key cho hệ thống hóa đơn điện tử.

Tóm tắt

Haravan Invoice sử dụng Cloudflare D1 — SQLite chạy ở edge — với 6 bảng: invoices, audit_logs, merchant_config, idempotency_keys, customers. Schema được quản lý qua file schema.sql và migrations.

Tổng quan schema ​

Hình 1: Entity Relationship Diagram

Bảng: invoices ​

Bảng chính lưu trữ thông tin hóa đơn điện tử.

ColumnTypeConstraintMô tả
idTEXTPRIMARY KEYUUID của hóa đơn
haravan_idTEXTUNIQUE NOT NULLID từ hệ thống Haravan
tvan_idTEXTID từ T-VAN provider
statusTEXTNOT NULL DEFAULT 'draft'Trạng thái hóa đơn
issue_dateTEXTNgày phát hành (ISO 8601)
buyer_nameTEXTNOT NULL DEFAULT ''Tên người mua
buyer_mstTEXTMã số thuế người mua
buyer_addressTEXTĐịa chỉ người mua
buyer_emailTEXTEmail người mua
buyer_phoneTEXTSĐT người mua
seller_nameTEXTNOT NULL DEFAULT ''Tên người bán
seller_mstTEXTNOT NULL DEFAULT ''MST người bán
seller_addressTEXTĐịa chỉ người bán
seller_emailTEXTEmail người bán
itemsTEXTNOT NULL DEFAULT '[]'JSON array line items
subtotalINTEGERNOT NULL DEFAULT 0Tiền trước thuế (VNĐ)
tax_amountINTEGERNOT NULL DEFAULT 0Tiền thuế
discountINTEGERNOT NULL DEFAULT 0Giảm giá
totalINTEGERNOT NULL DEFAULT 0Tổng cộng
tax_rateREALDEFAULT 0.1Thuế suất (0, 0.05, 0.08, 0.1)
payment_methodTEXTDEFAULT 'transfer'Phương thức thanh toán
channelTEXTDEFAULT 'admin'Kênh bán
order_idTEXTID đơn hàng gốc
replaced_byTEXTFK → invoices(id)HĐ thay thế
replacesTEXTFK → invoices(id)HĐ bị thay thế
adjusted_byTEXTFK → invoices(id)HĐ điều chỉnh
adjustsTEXTFK → invoices(id)HĐ bị điều chỉnh
metadataTEXTDEFAULT '{}'JSON metadata
created_atTEXTDEFAULT now()Thời gian tạo
updated_atTEXTDEFAULT now()Thời gian cập nhật
versionINTEGERDEFAULT 1Version number

Status values ​

StatusMô tả
draftHóa đơn nháp
pendingĐang chờ xử lý
issuedĐã phát hành
cqt_acceptedCơ quan thuế đã chấp nhận
cqt_rejectedCơ quan thuế từ chối
adjustedĐã điều chỉnh
replacedĐã thay thế

Channel values ​

ChannelMô tả
adminTạo từ Haravan Admin
posTạo từ POS
webTạo từ Website
autoTự động phát hành

Payment method values ​

MethodMô tả
cashTiền mặt
transferChuyển khoản
cardThẻ
codThu hộ
otherKhác

Bảng: audit_logs ​

Nhật ký thao tác trên hóa đơn, phục vụ compliance và audit trail.

ColumnTypeConstraintMô tả
idTEXTPRIMARY KEYUUID log entry
invoice_idTEXTNOT NULL, FKReference đến invoices
actionTEXTNOT NULLThao tác (create, issue, replace, adjust)
actorTEXTDEFAULT 'system'Người thực hiện
detailsTEXTDEFAULT '{}'JSON chi tiết
created_atTEXTDEFAULT now()Thời gian

Bảng: merchant_config ​

Cấu hình riêng cho từng merchant.

ColumnTypeConstraintMô tả
merchant_idTEXTPRIMARY KEYID merchant
auto_issue_on_paidINTEGERDEFAULT 0Tự động phát hành khi đơn paid
default_tax_rateREALDEFAULT 0.1Thuế suất mặc định
seller_nameTEXTTên người bán
seller_mstTEXTMST người bán
seller_addressTEXTĐịa chỉ người bán
tvan_providerTEXTDEFAULT 'mock'TVAN provider (mock/hilo/viettel/misa)
mau_soTEXTDEFAULT '01GTKT0/001'Mẫu số hóa đơn
ky_hieuTEXTDEFAULT 'AA/20E'Ký hiệu hóa đơn
created_atTEXTDEFAULT now()Thời gian tạo
updated_atTEXTDEFAULT now()Thời gian cập nhật

Bảng: idempotency_keys ​

Chống duplicate request khi retry.

ColumnTypeConstraintMô tả
keyTEXTPRIMARY KEYIdempotency key từ header
merchant_idTEXTNOT NULLID merchant
responseTEXTJSON response đã cache
created_atTEXTDEFAULT now()Thời gian tạo
expires_atTEXTThời gian hết hạn

Bảng: customers ​

Danh sách khách hàng mua hàng.

ColumnTypeConstraintMô tả
idTEXTPRIMARY KEYUUID khách hàng
nameTEXTNOT NULLTên khách hàng
mstTEXTMã số thuế
addressTEXTĐịa chỉ
emailTEXTEmail
phoneTEXTSĐT
created_atTEXTDEFAULT now()Thời gian tạo

Indexes ​

IndexTableColumn(s)Mục đích
idx_invoices_statusinvoicesstatusFilter theo status
idx_invoices_haravaninvoicesharavan_idLookup by Haravan ID
idx_invoices_buyer_mstinvoicesbuyer_mstSearch theo MST
idx_invoices_issue_dateinvoicesissue_dateFilter theo ngày
idx_invoices_createdinvoicescreated_atSort theo thời gian
idx_audit_invoiceaudit_logsinvoice_idLookup audit by invoice
idx_audit_createdaudit_logscreated_atSort audit logs
idx_customers_mstcustomersmstSearch customer by MST

Migrations ​

Schema được quản lý qua file schema.sql tại apps/api/schema.sql:

bash
# Apply migrations
cd apps/api && pnpm db:migrate

# Seed dev data
pnpm db:seed

Data Flow ​

Hình 2: Data flow giữa các bảng

Liên kết liên quan ​

Phát hành theo giấy phép MIT.