# 竹地雞營運平台 Level 5.1

# ERD Blueprint 資料庫架構總圖

---

# 核心設計原則

## 1. 事件驅動

重要異動都要留下事件紀錄。

例如：

* 訂單修改
* 出貨狀態變更
* 庫存異動
* 支出同步
* 收款
* 養殖紀錄

---

## 2. 訂單與出貨分離

Order Session 是客戶需求。

Shipment 是實際出貨。

---

## 3. 生產與訂單分離

生產由屠宰批次開始。

訂單只影響出貨與需求分析，不直接控制生產。

---

## 4. 庫存是交會中心

生產入庫。

出貨扣庫。

盤點調整。

報廢扣庫。

---

# A. 客戶與訂單資料表

## customers

客戶主檔

### 欄位

* id
* customer_name
* customer_type
* phone
* address
* tax_id
* payment_terms
* default_delivery_method
* status

---

## line_messages

LINE 原始訊息

### 欄位

* id
* line_user_id
* group_id
* customer_id
* raw_message
* receive_time
* parse_status
* created_at

---

## message_parse

AI 解析結果

### 欄位

* id
* message_id
* customer_id
* parsed_json
* confidence
* reviewed_by
* reviewed_at
* status

---

## order_session

訂單會話

### 欄位

* id
* session_no
* customer_id
* delivery_date
* status
* source_type
* created_at
* updated_at

---

## order_session_item

訂單會話商品

### 欄位

* id
* session_id
* product_id
* qty
* unit
* remark

---

## order_event

訂單事件紀錄

### 欄位

* id
* session_id
* event_type
* old_value
* new_value
* operator_type
* operator_id
* created_at

---

# B. Shipment 出貨資料表

## shipment

出貨批次 / 出貨單

### 欄位

* id
* shipment_no
* customer_id
* delivery_date
* delivery_method
* shipment_type
* status
* remark
* created_at
* updated_at

---

## shipment_item

出貨商品明細

### 欄位

* id
* shipment_id
* product_id
* qty
* picked_qty
* packed_qty
* unit
* remark

---

## shipment_event

出貨事件

### 欄位

* id
* shipment_id
* event_type
* old_status
* new_status
* operator_id
* created_at

---

# C. Barcode 掃碼資料表

## barcode_scan

掃碼紀錄

### 欄位

* id
* shipment_id
* package_id
* barcode
* product_id
* qty
* scan_status
* scanned_by
* scanned_at

---

# D. Package 裝箱資料表

## package

箱子主檔

### 欄位

* id
* package_no
* shipment_id
* weight
* temperature_type
* status
* created_at

---

## package_item

箱內商品

### 欄位

* id
* package_id
* product_id
* qty
* unit

---

# E. Logistics 物流資料表

## shipment_logistics

物流資料

### 欄位

* id
* shipment_id
* carrier
* tracking_no
* freight_amount
* expense_status
* expense_id
* created_at
* updated_at

---

## delivery_route

自送路線

### 欄位

* id
* route_date
* driver_id
* status
* created_at

---

## delivery_stop

配送站點

### 欄位

* id
* route_id
* shipment_id
* customer_id
* stop_order
* address
* status
* arrived_at
* completed_at

---

## delivery_event

配送事件

### 欄位

* id
* stop_id
* event_type
* remark
* operator_id
* created_at

---

# F. Payment 與應收資料表

## accounts_receivable

應收帳款

### 欄位

* id
* customer_id
* shipment_id
* amount
* due_date
* status
* created_at

---

## payment

收款紀錄

### 欄位

* id
* customer_id
* shipment_id
* amount
* payment_method
* received_by
* received_at
* remark

---

# G. Expenses 支出資料表

## expenses

支出主檔

### 欄位

* id
* expense_date
* category
* sub_category
* amount
* source_type
* source_id
* status
* remark
* created_at
* updated_at

---

## expense_event

支出事件

### 欄位

* id
* expense_id
* event_type
* old_value
* new_value
* operator_id
* created_at

---

# H. Product 商品主檔

## products

商品主檔

### 欄位

* id
* product_name
* product_type
* category_id
* unit
* barcode_prefix
* status

---

## product_categories

商品分類

### 欄位

* id
* category_name
* sort_order
* status

---

# I. Inventory 庫存資料表

## inventory

庫存主檔

### 欄位

* id
* product_id
* location
* qty
* safety_qty
* updated_at

---

## inventory_event

庫存事件

### 欄位

* id
* product_id
* location
* event_type
* qty
* source_type
* source_id
* created_at

---

# J. Production 生產資料表

## slaughter_plan

屠宰計畫

### 欄位

* id
* plan_date
* breed
* planned_qty
* remark
* status

---

## slaughter_batch

屠宰批次

### 欄位

* id
* batch_no
* slaughter_date
* breed
* qty
* total_weight
* status

---

## slaughter_output

屠宰產出

### 欄位

* id
* batch_id
* product_id
* qty
* unit
* weight

---

## production_log

加工紀錄

### 欄位

* id
* batch_id
* workstation
* product_id
* qty
* unit
* operator_id
* created_at

---

# K. Farm 養殖資料表

## houses

雞舍

### 欄位

* id
* house_name
* sort_order
* status

---

## zones

分區

### 欄位

* id
* house_id
* zone_name
* sort_order
* status

---

## flocks

批次 / 進雞

### 欄位

* id
* zone_id
* flock_no
* start_date
* start_age
* initial_qty
* current_qty
* status

---

## farm_logs

養殖紀錄

### 欄位

* id
* flock_id
* zone_id
* log_type
* log_date
* qty
* content
* operator_id
* created_at

---

# L. Packaging 包材資料表

## packaging_items

包材主檔

### 欄位

* id
* item_name
* unit
* safety_qty
* status

---

## packaging_inventory

包材庫存

### 欄位

* id
* item_id
* qty
* location
* updated_at

---

## packaging_usage

包材耗用

### 欄位

* id
* item_id
* source_type
* source_id
* qty
* used_at

---

# M. HR 人資資料表

## employees

員工資料

### 欄位

* id
* employee_name
* employee_type
* phone
* status
* start_date
* end_date

---

## attendance

出勤紀錄

### 欄位

* id
* employee_id
* work_date
* clock_in
* clock_out
* status

---

## overtime

加班紀錄

### 欄位

* id
* employee_id
* work_date
* overtime_type
* hours
* days
* amount

---

## salary

薪資紀錄

### 欄位

* id
* employee_id
* salary_month
* base_salary
* overtime_amount
* holiday_overtime_amount
* lodging_fee
* deduction
* net_salary
* status

---

# N. Work Center 工作中心資料表

## tasks

工作任務

### 欄位

* id
* task_type
* title
* description
* source_type
* source_id
* assigned_to
* priority
* due_date
* status
* created_at
* completed_at

---

# O. Notification 通知資料表

## notifications

通知

### 欄位

* id
* notification_type
* title
* message
* source_type
* source_id
* receiver_id
* level
* status
* created_at
* read_at

---

# P. 官網資料表

## website_orders

官網訂單

### 欄位

* id
* order_no
* member_id
* customer_id
* amount
* payment_status
* shipment_status
* created_at

---

## members

官網會員

### 欄位

* id
* member_name
* phone
* email
* address
* status

---

# 主要關聯

## LINE 到訂單

line_messages

→ message_parse

→ order_session

→ order_session_item

---

## 訂單到出貨

order_session

→ shipment

→ shipment_item

---

## 出貨到裝箱

shipment

→ package

→ package_item

---

## 出貨到物流

shipment

→ shipment_logistics

---

## 自送配送

shipment

→ delivery_stop

→ delivery_route

---

## 出貨到應收

shipment

→ accounts_receivable

---

## 自送收款

delivery_stop

→ payment

→ accounts_receivable

---

## 黑貓支出

shipment_logistics

→ expenses

---

## 生產到庫存

slaughter_batch

→ slaughter_output

→ production_log

→ inventory_event

→ inventory

---

## 出貨扣庫存

shipment_item

→ inventory_event

→ inventory

---

## 養殖到屠宰

flocks

→ farm_logs

→ slaughter_batch

---

## 工作與通知

notifications

→ tasks

---

# 黑貓物流支出關聯

Shipment 設定黑貓配送

↓

建立 shipment_logistics

↓

輸入黑貓單號與運費

↓

按同步支出

↓

建立 expenses

↓

shipment_logistics.expense_id = expenses.id

↓

財務可修改確認

---

# 自送配送收款關聯

Shipment 設定自送

↓

建立 delivery_route

↓

建立 delivery_stop

↓

司機送達

↓

司機收款

↓

建立 payment

↓

更新 accounts_receivable

---

# 庫存事件原則

所有庫存變動都必須寫入 inventory_event。

例如：

* 生產入庫
* 出貨扣庫
* 盤點調整
* 報廢
* 退貨入庫

inventory 只保存目前庫存量。

---

# 支出事件原則

所有自動帶入的支出都可以在財務中心修改。

修改時寫入 expense_event。

---

# 最小 MVP 資料表

第一階段可先做：

* customers
* products
* line_messages
* message_parse
* order_session
* order_session_item
* order_event
* shipment
* shipment_item
* shipment_logistics
* package
* package_item
* barcode_scan
* accounts_receivable
* payment
* expenses
* inventory
* inventory_event
* houses
* zones
* flocks
* farm_logs
* employees
* tasks
* notifications
