# Fastener QC & Certificate Management System

Hệ thống quản lý chất lượng và xuất chứng chỉ (COA/COC) cho ngành ốc vít — bu lông, đai ốc, lông đền, ty ren.

## Thành phần chính

| File / Module | Vai trò |
|---|---|
| `fastener_server.py` | HTTP server (port 8899) — serve prototype, proxy tới globalfastener.com, cache spec |
| `db.py` | SQLite storage backend — `load_json_file` / `save_json_file` atomic, WAL mode, ACID |
| `cert_manager.py` + `cert_manager.html` | Quản lý & phát hành chứng chỉ COA/COC |
| `visual_template.py` | Visual Template Builder cho mẫu chứng chỉ |
| `template_engine.py` | Render template (docx/xlsx) với field mapping |
| `scrape_fastener.py` | Thu thập thông số chuẩn từ globalfastener.com |
| `generate_dashboard.py` + `dashboard.html` | Dashboard KPI / tiến độ đơn hàng |
| `build_html.py` | Build các trang HTML tĩnh (flowchart, qc_standards, blueprint…) |

## Storage

Dự án dùng **SQLite** (file `database.db`, WAL mode) để lưu dữ liệu transactional — safe với nhiều user đồng thời, atomic writes, crash-safe.

Các file `*.json` trong repo là **seed data**. Lần chạy đầu tiên, `db.py` tự import chúng vào SQLite. Các lần sau, tất cả I/O đọc/ghi qua SQLite.

`database.db` **không được commit** — mỗi deploy build từ seed JSON.

## Dữ liệu tham chiếu

- `fastener_specs.json` — thông số tiêu chuẩn (DIN/ISO/ASTM…)
- `products.json` — danh mục sản phẩm
- `field_mapping.json` — ánh xạ field template ↔ spec
- `template_fields.json` — định nghĩa field cho visual template
- `packing.json` / `dictionary.json` — dữ liệu phụ trợ

## Chạy

```bash
# Khởi động server chính
python fastener_server.py
# hoặc:
START_CERTMANAGER.bat
```

Mở: http://localhost:8899/prototype.html

## Requirements

- Python 3.10+
- `requests`, `beautifulsoup4`, `openpyxl`, `python-docx`, `reportlab`

## Ghi chú

Dữ liệu nhạy cảm (`customers.json`, `cert_history.json`, `certificates/`, `visual_templates/`, `uploads/`, file test) đã được loại khỏi git — xem `.gitignore`.
