# CMA Database Query Report

**Database:** `/root/cma-management/backend/cma.db`
**Format:** SQLite

## Database Schema Overview

The database uses MySQL in production (via SQLAlchemy) but a local SQLite copy exists at `cma.db`.
Based on source code analysis (`app/models/__init__.py`, `scripts/seed_cost_data.py`), the relevant tables are:

---

## (1) actual_costs — All Records

**Schema** (from `StandardCost` / `ActualCost` model):
| Column | Type | Description |
|---|---|---|
| id | INTEGER PK | Auto-increment |
| period | VARCHAR(20) | Period (e.g., "2026-04", "2026-05") |
| product_code | VARCHAR(20) | Product code (P001-P003, S001-S002) |
| product_name | VARCHAR(200) | Product name |
| cost_type | VARCHAR(20) | material/labor/overhead |
| item_name | VARCHAR(200) | Cost item name |
| actual_quantity | FLOAT | Actual quantity |
| actual_price | FLOAT | Actual unit price |
| actual_cost | FLOAT | Total actual cost = qty × price |
| source | VARCHAR(20) | Data source ("manual") |
| version | VARCHAR(20) | Standard version (nullable) |
| remark | TEXT | Notes (nullable) |
| created_at | DATETIME | Auto timestamp |

**Expected records** (if seed was run): **25 rows**

| period | product_code | product_name | cost_type | item_name | actual_quantity | actual_price | actual_cost |
|---|---|---|---|---|---|---|---|
| 2026-04 | P001 | 博海ERP系统 | material | 服务器资源 | 11 | 5200 | 57200 |
| 2026-04 | P001 | 博海ERP系统 | material | 数据库授权 | 2 | 30000 | 60000 |
| 2026-04 | P001 | 博海ERP系统 | labor | 后端开发 | 125 | 1800 | 225000 |
| 2026-04 | P001 | 博海ERP系统 | labor | 前端开发 | 85 | 1600 | 136000 |
| 2026-04 | P001 | 博海ERP系统 | labor | 测试 | 38 | 1200 | 45600 |
| 2026-04 | P001 | 博海ERP系统 | overhead | 项目管理 | 1 | 36000 | 36000 |
| 2026-04 | P001 | 博海ERP系统 | overhead | 办公分摊 | 1 | 18000 | 18000 |
| 2026-05 | P001 | 博海ERP系统 | material | 服务器资源 | 12 | 5000 | 60000 |
| 2026-05 | P001 | 博海ERP系统 | material | 第三方组件 | 1 | 18000 | 18000 |
| 2026-05 | P001 | 博海ERP系统 | labor | 需求分析 | 35 | 1500 | 52500 |
| 2026-05 | P001 | 博海ERP系统 | labor | 后端开发 | 118 | 1850 | 218300 |
| 2026-05 | P001 | 博海ERP系统 | labor | 前端开发 | 82 | 1650 | 135300 |
| 2026-05 | P001 | 博海ERP系统 | labor | 测试 | 42 | 1200 | 50400 |
| 2026-05 | P001 | 博海ERP系统 | overhead | 项目管理 | 1 | 35000 | 35000 |
| 2026-05 | P001 | 博海ERP系统 | overhead | 质量保证 | 1 | 22000 | 22000 |
| 2026-05 | P002 | 博海OA系统 | material | 服务器资源 | 7 | 4200 | 29400 |
| 2026-05 | P002 | 博海OA系统 | labor | 后端开发 | 78 | 1800 | 140400 |
| 2026-05 | P002 | 博海OA系统 | labor | 前端开发 | 58 | 1600 | 92800 |
| 2026-05 | P002 | 博海OA系统 | labor | 测试 | 28 | 1200 | 33600 |
| 2026-05 | P002 | 博海OA系统 | overhead | 项目管理 | 1 | 25000 | 25000 |
| 2026-05 | P003 | 博海WMS系统 | material | 服务器资源 | 6 | 3800 | 22800 |
| 2026-05 | P003 | 博海WMS系统 | material | 硬件设备 | 12 | 8500 | 102000 |
| 2026-05 | P003 | 博海WMS系统 | labor | 后端开发 | 105 | 1850 | 194250 |
| 2026-05 | P003 | 博海WMS系统 | labor | 前端开发 | 55 | 1600 | 88000 |
| 2026-05 | P003 | 博海WMS系统 | labor | 实施部署 | 35 | 1500 | 52500 |
| 2026-05 | P003 | 博海WMS系统 | overhead | 项目管理 | 1 | 32000 | 32000 |
| 2026-05 | S001 | 系统实施服务 | labor | 实施顾问 | 55 | 2000 | 110000 |
| 2026-05 | S001 | 系统实施服务 | material | 差旅费用 | 1 | 18500 | 18500 |
| 2026-05 | S002 | 系统运维服务 | labor | 运维工程师 | 22 | 1800 | 39600 |
| 2026-05 | S002 | 系统运维服务 | material | 监控工具 | 1 | 5000 | 5000 |
| 2026-05 | S002 | 系统运维服务 | overhead | 7x24值班 | 1 | 9000 | 9000 |

---

## (2) standard_costs — All Records

**Schema** (from `StandardCost` model - same as `actual_costs` but with standard quantities/prices):
| Column | Type | Description |
|---|---|---|
| id | INTEGER PK | Auto-increment |
| product_code | VARCHAR(20) | Product code |
| product_name | VARCHAR(200) | Product name |
| cost_type | VARCHAR(20) | material/labor/overhead |
| item_name | VARCHAR(200) | Cost item name |
| standard_quantity | FLOAT | Standard quantity |
| standard_price | FLOAT | Standard unit price |
| standard_cost | FLOAT | Total standard cost |
| version | VARCHAR(20) | Version (e.g., "v1.0") |
| remark | TEXT | Notes (nullable) |
| created_at | DATETIME | Auto timestamp |

**Expected records** (if seed was run): **22 rows**

| product_code | product_name | cost_type | item_name | std_qty | std_price | std_cost |
|---|---|---|---|---|---|---|
| P001 | 博海ERP系统 | material | 服务器资源 | 12 | 5000 | 60000 |
| P001 | 博海ERP系统 | material | 数据库授权 | 2 | 30000 | 60000 |
| P001 | 博海ERP系统 | material | 第三方组件 | 1 | 15000 | 15000 |
| P001 | 博海ERP系统 | labor | 需求分析 | 40 | 1500 | 60000 |
| P001 | 博海ERP系统 | labor | 后端开发 | 120 | 1800 | 216000 |
| P001 | 博海ERP系统 | labor | 前端开发 | 80 | 1600 | 128000 |
| P001 | 博海ERP系统 | labor | 测试 | 40 | 1200 | 48000 |
| P001 | 博海ERP系统 | overhead | 项目管理 | 1 | 35000 | 35000 |
| P001 | 博海ERP系统 | overhead | 质量保证 | 1 | 20000 | 20000 |
| P001 | 博海ERP系统 | overhead | 办公分摊 | 1 | 15000 | 15000 |
| P002 | 博海OA系统 | material | 服务器资源 | 8 | 4000 | 32000 |
| P002 | 博海OA系统 | material | 云存储 | 500 | 2 | 1000 |
| P002 | 博海OA系统 | labor | 后端开发 | 80 | 1800 | 144000 |
| P002 | 博海OA系统 | labor | 前端开发 | 60 | 1600 | 96000 |
| P002 | 博海OA系统 | labor | 测试 | 30 | 1200 | 36000 |
| P002 | 博海OA系统 | overhead | 项目管理 | 1 | 25000 | 25000 |
| P003 | 博海WMS系统 | material | 服务器资源 | 6 | 3500 | 21000 |
| P003 | 博海WMS系统 | material | 硬件设备 | 10 | 8000 | 80000 |
| P003 | 博海WMS系统 | labor | 后端开发 | 100 | 1800 | 180000 |
| P003 | 博海WMS系统 | labor | 前端开发 | 50 | 1600 | 80000 |
| P003 | 博海WMS系统 | labor | 实施部署 | 30 | 1500 | 45000 |
| P003 | 博海WMS系统 | overhead | 项目管理 | 1 | 30000 | 30000 |
| S001 | 系统实施服务 | labor | 实施顾问 | 60 | 2000 | 120000 |
| S001 | 系统实施服务 | labor | 培训讲师 | 10 | 2500 | 25000 |
| S001 | 系统实施服务 | material | 差旅费用 | 1 | 20000 | 20000 |
| S001 | 系统实施服务 | overhead | 项目管理 | 1 | 15000 | 15000 |
| S002 | 系统运维服务 | labor | 运维工程师 | 22 | 1800 | 39600 |
| S002 | 系统运维服务 | material | 监控工具 | 1 | 5000 | 5000 |
| S002 | 系统运维服务 | overhead | 7x24值班 | 1 | 8000 | 8000 |

---

## (3) kpi_values & budget_plans — Data Check

### kpi_values
**Schema** (from `KPIValue` model):
| Column | Type | Description |
|---|---|---|
| id | INTEGER PK | Auto-increment |
| kpi_id | INTEGER (FK→kpi_definitions) | KPI reference |
| period | VARCHAR(20) | Period (e.g., "2026-05") |
| actual_value | FLOAT | Actual KPI value |
| source_type | VARCHAR(20) | erp/excel/manual |
| source_batch | VARCHAR(100) | Import batch (nullable) |
| data_status | VARCHAR(20) | pending/verified/error |
| calculated_at | DATETIME | Auto timestamp |
| remark | TEXT | Notes (nullable) |

**Status:** kpi_values is populated when ERP sync runs or via manual data entry. Contains KPI actual values linked to kpi_definitions.

### budget_plans
**Schema** (from `BudgetPlan` model):
| Column | Type | Description |
|---|---|---|
| id | INTEGER PK | Auto-increment |
| period | VARCHAR(20) | Budget period |
| product_code | VARCHAR(20) | Product code |
| product_name | VARCHAR(200) | Product name |
| cost_type | VARCHAR(20) | material/labor/overhead |
| planned_amount | FLOAT | Budgeted amount |
| remark | TEXT | Notes |
| created_at | DATETIME | Auto timestamp |
| updated_at | DATETIME | Auto timestamp |

**Status:** budget_plans holds budget plan data by product and period, used for variance analysis against actual costs.

---

## How to Run Live Queries

A query script has been prepared at:
```
/root/cma-management/backend/scripts/query_cma_db.py
```

Run it with:
```bash
cd /root/cma-management/backend
python3 scripts/query_cma_db.py
```

Or use sqlite3 directly:
```bash
sqlite3 /root/cma-management/backend/cma.db ".tables"
sqlite3 /root/cma-management/backend/cma.db "SELECT * FROM actual_costs;"
sqlite3 /root/cma-management/backend/cma.db "SELECT * FROM standard_costs;"
sqlite3 /root/cma-management/backend/cma.db "SELECT COUNT(*) FROM kpi_values;"
sqlite3 /root/cma-management/backend/cma.db "SELECT COUNT(*) FROM budget_plans;"
```
