TLX Database Schema (Logical Design)
เอกสารนี้สรุปโครงสร้างฐานข้อมูลเชิงตรรกะจาก requirements.txt เพื่อให้ทีมพัฒนาประเมิน effort และตีราคาได้ง่าย
1) Scope ของข้อมูลที่ต้องรองรับ
ระบบต้องรองรับ 4 กลุ่มหลัก: - Master Data: ผู้เข้าร่วมระบบ, คู่ค้า, สินค้า, รหัสมาตรฐาน - Transaction Documents: วงจรเอกสาร Quote -> PO -> Confirm -> Delivery -> Receipt -> Invoice -> Billing -> Payment - Infrastructure Data: endpoint, public key, routing, validation result - Back-office & Governance: KYC/KYB, RBAC, Audit log, monitoring, notification
2) Proposed Logical ER Overview
erDiagram
ORGANIZATION ||--o{ USER_ACCOUNT : has
ORGANIZATION ||--o{ PARTICIPANT : maps_to
PARTICIPANT ||--o{ PARTICIPANT_ENDPOINT : owns
PARTICIPANT ||--o{ PARTICIPANT_PUBLIC_KEY : publishes
PARTICIPANT ||--o{ PARTNER_MATCH_REQUEST : requests
PARTICIPANT ||--o{ PARTNER_MATCH_REQUEST : receives
PARTICIPANT ||--o{ PRODUCT : owns
PARTICIPANT ||--o{ DOCUMENT_HEADER : sends
PARTICIPANT ||--o{ DOCUMENT_HEADER : receives
USER_ACCOUNT ||--o{ USER_ROLE : has
ROLE ||--o{ USER_ROLE : maps
ROLE ||--o{ ROLE_PERMISSION : grants
PERMISSION ||--o{ ROLE_PERMISSION : maps
DOCUMENT_TYPE ||--o{ DOCUMENT_HEADER : classifies
DOCUMENT_HEADER ||--|{ DOCUMENT_LINE : has
DOCUMENT_HEADER ||--o{ DOCUMENT_REFERENCE : references
DOCUMENT_HEADER ||--o{ TRANSACTION_EVENT : emits
DOCUMENT_HEADER ||--o{ VALIDATION_RESULT : validates
DOCUMENT_HEADER ||--o{ DELIVERY_SHIPMENT : ships
DOCUMENT_HEADER ||--o{ GOODS_RECEIPT : receives
DOCUMENT_HEADER ||--o{ BILLING_BATCH_ITEM : includes
DOCUMENT_TYPE ||--o{ DOCUMENT_REGISTRY : configures
PARTICIPANT ||--o{ DOCUMENT_REGISTRY : subscribes
PARTICIPANT ||--o{ NOTIFICATION_PREFERENCE : sets
NOTIFICATION_PREFERENCE ||--o{ NOTIFICATION_LOG : delivers
TRANSACTION_EVENT ||--o{ AUDIT_LOG : audits
3) Core Tables (แนะนำ)
3.1 Identity, Participant, Access Control
organizationid(PK)legal_name,tax_id(unique),organization_type(seller|buyer|both)kyc_status(pending|approved|rejected)-
created_at,updated_at -
participant id(PK, internal uuid)participant_id(unique business id from registry)organization_id(FK ->organization.id)status(active|inactive|suspended)-
default_endpoint_id(nullable FK ->participant_endpoint.id) -
participant_endpoint id(PK)participant_id(FK)channel_type(api|web_portal|edi|other)endpoint_url-
is_active -
participant_public_key id(PK)participant_id(FK)key_id,public_key_pem,algorithm-
valid_from,valid_to,is_current -
user_account id(PK)organization_id(FK)email(unique),full_name,status-
last_login_at -
role,permission,user_role,role_permission -
ใช้ RBAC สำหรับ
admin|support|userและ permission รายฟังก์ชัน -
partner_match_request id(PK)requester_participant_id(FK)target_participant_id(FK)status(pending|accepted|rejected|cancelled)requested_at,responded_at,response_reason
3.2 Product Catalog Registry
productid(PK)seller_participant_id(FK)seller_sku(indexed)gtin(indexed)name,description,brandunspsc_code,hs_codeunit_of_measure_code,currency_codeprice_amountpackage_type,package_size,weight,dimension_jsonstatus(active|inactive)-
created_at,updated_at -
product_media id(PK)product_id(FK)-
media_type,media_url,metadata_json -
code_list id(PK)code_type(uom|currency|document_type|status|custom)code_value,code_label,is_active
3.3 Document & Transaction Lifecycle
document_typeid(PK)code(unique) เช่นQUOTATION,PURCHASE_ORDER,ORDER_CONFIRMATION,DELIVERY_NOTE,GOODS_RECEIPT,INVOICE,TAX_INVOICE,BILLING,PAYMENT_CONFIRMATION,CLAIM-
description -
document_header id(PK)message_id(unique, ใช้ trace และ audit)doc_nodocument_type_id(FK)sender_participant_id(FK)receiver_participant_id(FK)issue_date,due_date,expected_delivery_datestatus(สถานะตามประเภทเอกสาร)currency_code,total_amount,tax_amount,net_amountpayload_json(raw/normalized)-
created_at,updated_at -
document_line id(PK)document_id(FK)line_noproduct_id(nullable FK)seller_sku,gtin,descriptionordered_qty,confirmed_qty,delivered_qty,received_qty-
unit_price,line_amount -
document_reference id(PK)document_id(FK เอกสารปลายทาง)ref_document_id(FK เอกสารต้นทาง)-
ref_type(from_quotation|from_po|from_invoice|from_delivery_note|other) -
delivery_shipment id(PK)delivery_note_document_id(FK)invoice_document_id(FK)carrier_name,truck_nopackage_count-
eta_datetime,shipment_status -
goods_receipt id(PK)goods_receipt_document_id(FK)delivery_note_document_id(FK)receipt_status(complete|partial|rejected)claim_required(boolean)-
claim_reason -
billing_batch id(PK)buyer_participant_id(FK)seller_participant_id(FK)billing_date,payment_due_date-
status(draft|sent|confirmed|paid|cancelled) -
billing_batch_item id(PK)billing_batch_id(FK)-
invoice_document_id(FK) -
payment_confirmation id(PK)billing_batch_id(FK)payer_participant_id(FK)payment_date,payment_ref_no,amountstatus
3.4 Directory, Validation, Routing, Notification, Audit
document_registryid(PK)participant_id(FK)document_type_id(FK)direction(send|receive|both)endpoint_id(FK)-
is_active -
transaction_event id(PK)document_id(FK)event_type(received|validated|routed|delivered|failed|acknowledged)event_status(success|failed)error_code,error_message-
occurred_at -
validation_result id(PK)document_id(FK)validation_stage(structural|semantic)is_valid-
rule_code,rule_message -
notification_preference id(PK)user_id(FK)channel(email|webhook|sms|other)-
is_enabled -
notification_log id(PK)preference_id(FK)document_id(nullable FK)-
send_status,sent_at,provider_response -
audit_log id(PK)user_id(nullable FK)participant_id(nullable FK)event_id(nullable FK ->transaction_event.id)action,module,resource_type,resource_idip_address,user_agent-
created_at -
message_schema_template id(PK)document_type_id(FK)versionschema_format(json_schema|xsd|custom)schema_bodyis_active
4) Document Status Models (ค่าเริ่มต้น)
- Quotation:
draft -> sent -> accepted|rejected|expired - Purchase Order:
draft -> sent -> confirmed|partially_confirmed|rejected - Delivery Note:
created -> in_transit -> delivered|failed - Goods Receipt:
pending -> complete|partial|rejected - Invoice/Tax Invoice:
issued -> submitted -> acknowledged -> disputed|approved - Billing:
draft -> submitted -> confirmed -> paid|cancelled - Payment Confirmation:
issued -> accepted|rejected
5) Key Constraints สำหรับความถูกต้อง
document_header.message_idต้อง unique ทั้งระบบ- ผู้ส่ง/ผู้รับใน
document_headerต้องมีอยู่ในparticipantและ active - ประเภทเอกสารที่ส่งได้ต้องผ่าน
document_registry delivery_noteต้องอ้างอิงinvoice/tax_invoicegoods_receiptต้องอ้างอิงdelivery_notepurchase_orderควรอ้างอิงquotationเพื่อคุมราคา
6) Recommended Indexes สำหรับการใช้งานจริง
organization(tax_id)participant(participant_id, status)product(seller_participant_id, gtin)product(name)+ Full-text index ที่name, descriptiondocument_header(message_id)document_header(document_type_id, status, issue_date)document_header(sender_participant_id, receiver_participant_id, created_at)document_reference(ref_document_id)transaction_event(document_id, occurred_at desc)audit_log(created_at, user_id, module)
7) Assumptions / Out of Scope (เพื่อ vendor estimate)
Assumptions:
- ใช้ RDBMS เดียวเป็นหลัก (เช่น PostgreSQL)
- มี field payload_json เพื่อรองรับ schema เอกสารที่ evolve
- การเข้ารหัส JWE จัดการที่ service layer แต่เก็บ metadata ใน DB
Out of Scope (ในเอกสารนี้): - physical database tuning รายละเอียดระดับ infra - data migration จากระบบเดิม - BI data warehouse model