Skip to content

Data Flow

Quick Reference — dữ liệu đi từ trái sang phải, mỗi mũi tên = 1 service call + 1 audit row (nếu có side-effect).

1. End-to-end pipeline

mermaid
flowchart LR
    subgraph Intake[Intake Layer]
      RAW[Raw orders<br/>CSV/Excel/manual]
      ADJ[KAM adjustments<br/>Excel/form]
    end

    subgraph Storage[Supabase]
      HO[historical_orders]
      FA[forecast_adjustments]
      CFG[config_values]
      FR[forecast_results]
      AL[audit_log]
    end

    subgraph Engine[Engine Layer]
      AGG[historical-aggregator]
      GEN[forecast/generator]
      WLU[wlu/calculator]
    end

    subgraph Output[Output / UI]
      OVR[Forecast Overview]
      REV[Review queue]
      WH[Warehouse report]
      CUS[Customer report]
    end

    RAW --> HO
    ADJ --> FA
    HO --> AGG
    AGG --> GEN
    CFG --> GEN
    GEN --> FR
    FR --> WLU
    CFG --> WLU
    WLU --> WH
    FR --> OVR
    FA --> REV
    REV --> FR
    FA -.audit.-> AL
    FR -.audit.-> AL
    CFG -.audit.-> AL

Text mô tả: Đầu vào gồm historical_orders (90 ngày) và forecast_adjustments (KAM gửi). Engine historical-aggregator → forecast/generator đọc lịch sử + config → ghi forecast_results. WLU calculator đọc forecast_results + config → output báo cáo workload. Mọi thay đổi state, qty, config đều ghi audit_log.

2. Bảng trạng thái dữ liệu

Giai đoạnBảngTần suất ghiAi ghi
Import lịch sửhistorical_ordershàng tuần / theo đợtAdmin / Ops
KAM gửiforecast_adjustmentshàng ngàyKAM
Engine nightlyforecast_results02:00 mỗi đêmSystem (cron / RPC)
Planner duyệtforecast_results.audited_qty + statehàng ngàyPlanner
Auditaudit_logmỗi actionSystem (trigger / service)
Config changeconfig_valuesthỉnh thoảngAdmin

3. Forecast nightly job

mermaid
sequenceDiagram
    participant Cron as Cron 02:00
    participant Svc as forecastService
    participant Eng as engine/forecast
    participant DB as Supabase

    Cron->>Svc: trigger compute()
    Svc->>DB: read historical_orders (90d)
    Svc->>DB: read config_values (monthly, event)
    Svc->>DB: read customer_warehouse (active pairs)
    loop mỗi (kho, khách, ngày)
        Svc->>Eng: aggregate(historical) → baseline
        Svc->>Eng: generate(baseline, monthly, event) → forecast_qty
    end
    Svc->>DB: upsert forecast_results
    Svc->>DB: insert audit_log (action=NIGHTLY_COMPUTE)

4. Adjustment merge vào forecast_result

Khi Planner ACCEPTED 1 adjustment:

mermaid
sequenceDiagram
    participant UI as ForecastReview
    participant Svc as reviewService
    participant DB

    UI->>Svc: accept(adjustment_id, audited_qty, note)
    Svc->>DB: update adjustment.state=ACCEPTED
    Svc->>DB: update forecast_result.audited_qty
    Svc->>DB: insert audit_log (action=STATE_CHANGE)
    Svc->>DB: insert audit_log (action=QTY_EDIT)
    DB-->>Svc: ok
    Svc-->>UI: toast success

5. Read paths (báo cáo)

TrangServiceQuery chính
/planner/forecastforecastService.list()forecast_results join customer, warehouse
/planner/forecast/reviewreviewService.queue()forecast_adjustments WHERE state IN (NEW,SEEN)
/planner/forecast/warehousereportService.byWarehouse()aggregate forecast_results × wlu_config
/planner/forecast/customer/:idreportService.byCustomer()filter forecast_results by customer

6. Điểm ghi audit (tổng kết)

ActionAi triggerGhi audit khi nào
IMPORT_HISTORICALAdminservice xử lý xong file
SUBMIT_ADJUSTMENTKAMsau insert thành công
STATE_CHANGEAM / Plannermỗi lần đổi state
QTY_EDITPlannerkhi sửa audited_qty
CONFIG_CHANGEAdminsau update config_values
NIGHTLY_COMPUTESystemđầu + cuối job

Tiếp: Deployment · API Reference