Skip to content

Shared Rules — Ecommerce Inventory Operations

1. Thuật ngữ chuẩn

Thuật ngữNghĩa trong code hiện tạiGhi chú
Inventory requestinventory_requestIntent layer cho checktransfer
Inventory documentinventory_documentExecution layer cho import/export
Shipping noteshipping_noteChuyển hàng thực nhận, không đồng nghĩa với inventory document
Product supplyingproduct_supplyingLedger movement tổng, không phải snapshot
Lot snapshotproduct_lot_no_stockTồn theo product + branch + sku + lot_number
SKU snapshotproduct_sku_stockAggregate snapshot theo SKU
Order transferorder_kind = order_transferHost order sinh ra sau transfer request hoàn tất
Internal orderorder_kind = order_material hoặc host layer nội bộKhông đồng nhất hoàn toàn với inventory request

2. Boundary Matrix

BoundaryEngine chínhMục đích
Product / warehouse masterproduct, warehouse, branch_productDanh mục và phạm vi bán/tồn
Check / transfer requestinventory_requestGhi nhận intent và approval runtime
Import / export executioninventory_documentPhiếu thực thi tạo movement
Ship / receive vật lýshipping_noteGiao nhận thực tế giữa kho
Movement ledgerproduct_supplyingGhi nhận dòng nhập/xuất/hold/release/dispose
Snapshot vận hànhproduct_lot_no_stock, product_sku_stockĐọc tồn hiện tại
Snapshot báo cáo*_mv materialized viewsDashboard / reporting

3. Status Matrix

3.1 Inventory request

FamilyStatus chính
Checkinventory_request_check_new, inventory_request_check_balanced, inventory_request_check_canceled
Transferinventory_request_transfer_new, inventory_request_transfer_completed, inventory_request_transfer_canceled

3.2 Inventory document

StatusÝ nghĩa
inventory_newPhiếu mới
inventory_releasedPhiếu đã release, side effects chạy
inventory_canceledPhiếu bị hủy

3.3 Shipping note

StatusÝ nghĩa
DRAFTDraft note
APPROVEDĐã duyệt note
DELIVERYĐang giao / đã ship
RECEIVEDĐã nhận
CANCELHủy

4. Invariants quan trọng

SR-001: Ledger thật là product_supplying

  • product_lot_no_stockproduct_sku_stock đều là derived read model.
  • Mọi phân tích movement phải bắt đầu từ product_supplying.

SR-002: inventory_request không phải document

  • request chỉ là intent/approval container,
  • execution thật thường bắt đầu khi event update-status chạy,
  • phiếu thực thi cụ thể nằm ở inventory_document.

SR-003: shipping_note là boundary riêng

  • không dùng inventory_document_id,
  • movement bám theo shipping_note_code,
  • có state model riêng cho ship/receive/cancel.

SR-004: Snapshot vận hành và reporting snapshot là hai tầng khác nhau

  • product_lot_no_stock / product_sku_stock: read model vận hành,
  • product_sku_stock_mv / product_sku_stock_product_mv: materialized view cho dashboard/report.

SR-005: Một transfer hoàn chỉnh đi qua nhiều lớp

inventory_request_transfer -> order_transfer -> export inventory_document -> shipping_note -> import inventory_document -> order_transfer_completed

5. Boundary Checklist

Khi phân tích bug hoặc thay đổi ở vùng kho, luôn hỏi:

  1. Case này đang ở request layer, document layer hay shipping layer?
  2. Dữ liệu đang đọc từ ledger, snapshot vận hành, hay materialized view report?
  3. Đây là import/export trực tiếp, check balanced, hay transfer có order/shipping đi kèm?
  4. Rule này nằm ở FE validation, Hasura mutate, backend event hay scheduler?
  5. Permission đang do route/moduleId, runtime filter, hay Hasura role quyết định?

6. Rủi ro / Findings

IDFinding
SR-F01Từ “inventory” trong UI/route đang bao nhiều lớp dữ liệu khác nhau, nên rất dễ mô tả sai boundary.
SR-F02Import/export/check/transfer/internal/shipping đang chia sẻ một phần engine nhưng không chia sẻ vocabulary rõ ràng.
SR-F03Nếu không tách rõ ledgersnapshot, QA rất dễ viết test sai nguồn dữ liệu kỳ vọng.