# Turnkey notes: 奧銳有限公司 (統編 00015555, 繞送代碼 PA006753)

Written 2026-10-06 while preparing the 財政部 電子發票 Turnkey 上線前自行檢測作業 (Ver 4.8.1) 書面檢核, 檢測單號 6, B2B交換, MIG 4.1.
Contains no passwords or keys on purpose. Items marked **(unverified)** were inferred, not tested.

## 1. What this installation is

- Turnkey **3.2.1**, `execute-environment` = `T` (test). SFTP host in logs: `tsftp.einvoice.nat.gov.tw:2222` (test). Production would be `sftp.einvoice.nat.gov.tw`.
- Test platform: `https://wwwtest.einvoice.nat.gov.tw`. Web API host (test): `tgw.einvoice.nat.gov.tw:443`.
- Connectivity checked from the server with `nc`: 443 and 2222 both reachable.
- Runs as `turnkey.service` (headless). `ReceiveFile` polls the SFTP server about once a minute (2,880 connects/day).
- The Turnkey GUI is a separate process. Its 排程設定 screen showing "已停止 / 排程已暫停" does not mean the service is stopped. Do not press 啟動 / 立即上傳 / 立即下載 in the GUI.
- Config: `/invoice/EINVTurnkey/einvUserConfig.xml`. Database tables: `schedule_config`, `task_config`, `sign_config`, `from_config`, `to_config`, `turnkey_transport_config`, `turnkey_user_profile`, `turnkey_message_log`, `turnkey_message_log_detail`, `turnkey_sysevent_log`.
- Certificate: `sign_config` id `AULY`, type PFX, `cert/AULY_00015555_20251119194418.pfx`. Public cert `DE11026756F218E7B05D4156CF15CA8C.cer`, issued by 工商憑證管理中心, valid 2025-11-23 to 2030-11-23. Whether the .pfx contains this same certificate was not checked.
- 傳送帳號 `00015555`, 送方 `00015555` / routing `PA006753` / sign_id `AULY`, user `ADMIN`.
- `task_config` has 15 rows: B2B EXCHANGE and B2P MESSAGE (發票配號訊息) with six tasks each; B2S STORAGE with Pack, SendFile, UpCast only. Some configured folders do not exist on disk (e.g. UpCast/B2PMESSAGE/E0401/SRC), which causes -001 / -002 events.

## 2. Status codes (訊息紀錄) and event codes (事件紀錄)

- Status: **P** processing, **G** Turnkey 判讀資料已上傳 (G rows appear after C rows, they are not "sent unconfirmed"), **C** 確認 (platform received and validated OK), **E** error (fix and re-upload), **I** interrupted.
- Event codes seen: -001 / -002 (missing folder), -011 (營業人接收方式未設定, see section 4), -017 (SFTP connect timeout, e.g. 2026-10-02 23:30:25, host inferred as the test SFTP), -022 (ProcessResult 回傳錯誤), -998.
- ProcessResult codes seen: **E0301** (憑證 not registered on the platform), **E0107**, **E0229**, **E0206** (see section 4).

## 3. What went wrong in the first tests and why (the story behind the rejected 單 3, 4, 5)

The 書面檢核 asked for evidence on 字軌, 重號, 漏上傳, 異常處理, Turnkey 確認 (status C) and 大平台 發票查詢.

1. 2026-09-23 early hours (00:52 to 11:51): first messages came back **E0301**. The signing certificate was not registered on the platform. Fix: the certificate was exported (10:50) and registered on the 大平台 營業人憑證資料登錄作業 as a **附屬憑證**. At 11:51 there were still 7 x E0301.
2. 11:54 to 11:55: six -011 rows, "營業人接收方式未設定" (A0101, A0102, A0201, A0202, B0101, B0102). The B2B 交換 接收方式 must be set on the platform (營業人功能選單 > 接收方式及授權 > 新增 > B2B交換). No screenshot of that page was captured.
3. 11:57 to 11:58: E0107 / E0229 / E0206 caused by placeholder numbers (TK1000000x, GWTK000001) used in tests. Re-uploaded with real range LP50936600 onward at 12:36.
4. After that all real messages reached **C** (09/23 and 09/24). Invoices LP50936600 to LP50936613 show correct states in 大平台 發票查詢 (15 rows).
5. Lesson: always screenshot the 大平台 setup pages (接收方式, 憑證 list) right after setting them, not afterwards.

## 4. SummaryResult: how to read and reconcile it

- Files: `/invoice/EINVTurnkey/ReceiveFile/B2BEXCHANGE/BAK/SummaryResult/`. Decode with: `base64 -d FILE | openssl cms -verify -inform DER -noverify`.
- Per day and per group (存證 = file names containing `-00000000-0-`, 交換 = the others) there are numbered files (`-1`, `-2`, ...) listing messages, plus a `-Final.SummaryResult` holding the total.
- **Final = sum of the per-message Total counts, i.e. documents (rows), not messages.** Each message has Good / Failed counts. Counting distinct uuids gives the wrong answer (an earlier match of 17 = 5+12 was a coincidence).
- Status **E** rows are generally **not** in SummaryResult.
- E0402 (空白未使用字軌) counts 0 in SummaryResult (Total 0) but Turnkey still shows a C row for it.
- Reconciliation (run 2026-10-06 13:18, `scripts/summary_compare.py`):

| Data date | Group | SummaryResult | Σ Good | Σ Failed | Turnkey C rows | Turnkey E rows (in SR) | Diff | E rows not in any SR |
|---|---|---|---|---|---|---|---|---|
| 09-23 | 存證 | 17 | 10 | 7 | 10 | 7 | 0 | 14 |
| 09-23 | 交換 | 8 | 8 | 0 | 8 | 0 | 0 | 18 |
| 09-24 | 存證 | 0 | 0 | 0 | 1 | 0 | -1 (the E0402 row) | 0 |
| 09-24 | 交換 | 13 | 13 | 0 | 13 | 0 | 0 | 0 |

- A day without traffic produces no SummaryResult file. Files for data day D appear around 01:00 on D+1.

## 5. Settings changed on 2026-10-06 (all via the Turnkey GUI)

| Time | Change |
|---|---|
| ~13:43 | 通知設定: mail server `mail.wang.si`, **port 465** with account and password. Saved (儲存成功). |
| 14:01 | 系統環境設定 > 系統設定 > **資料保留天數 30 to 90** (`data-keep-days`). Verified in `einvUserConfig.xml`. |
| ~14:07 | 排程設定: new job **O:清檔任務**, type FIX, **03:00 daily**, weekdays 1 to 7, enabled. In the database it is the task **HouseKeeping**. |
| 14:08:46 | `sudo systemctl restart turnkey.service` (once, to load all of the above). No errors afterwards, ReceiveFile polling resumed. |

Details learned:

- **Mail:** `mail.wang.si` speaks implicit TLS on 465 (certificate verifies). 587 is refused. Turnkey's main notification code uses an SSL socket factory with trust-all, so 465 is the right port. The "(TLS port: 587)" text is only a label. The separate monitor-process mail (`mail-on-monitor-err`) uses STARTTLS only and would not work with this server. There is no "send test mail" button. Events that send mail: service stopped, DB connection lost, system events (-0xx rows in the notification cycle window), monitor-log write failure, a message not at status C after 6 days. Whether a real mail was ever delivered is **unverified**: `Turnkey_mail.log` was empty before the change. An SMTP test with Python `smtplib` against 465 worked.
- **清檔 (HouseKeeping):** the scheduled job reads `data-keep-days`, deletes 訊息紀錄 (`turnkey_message_log`, `turnkey_message_log_detail`), 事件紀錄 (`turnkey_sysevent_log`) and physical files older than N days (cutoff is 00:00 today minus N days), all three together, no way to choose. Physical files: folders from `task_config` (BAK, ERR, BAK/ProcessResult, BAK/SummaryResult, ERR/...), recursive, then empty directories removed. Because of a skip rule that probably never matches **(unverified)**, SummaryResult files in BAK are deleted too after N days. With N=90 the 09/23 and 09/24 evidence files are kept until about late December 2026.
- **The 清檔設定 screen (工具/AuxCollect) is a manual run only.** Its number is not saved, and pressing 執行 deletes immediately. Do not use it. The persistent value lives on **系統環境設定** (傳輸設定 > 系統環境設定 > 系統設定 tab > 資料保留天數, then 確定).
- First scheduled 清檔 run: **2026-10-07 03:00**. Check `Turnkey.log` / `turnkey_sysevent_log` afterwards (the code logs delete counts).
- Startup log line seen: `send ban status true: A1 20261005 to 20261030 Turnkey_v3.2.2 版更新通知`. Meaning **(unverified)**: a notice about Turnkey 3.2.2, with a date range to 2026-10-30. Worth reading what "send ban" implies before 10-30.

## 6. ERP-side checks (test only)

- `scripts/erp_bridge_checks.py`: daily checks (track number, duplicate, missing upload, error handling, SummaryResult reconcile). Cron runs `all` at 23:00; it now checks **yesterday's** data date and treats an idle day (no messages, no file) as OK (exit 0). Exit 1 = anomaly, exit 2 = cannot run.
- `scripts/summary_compare.py`: read-only comparison table (section 4).
- Cron needs `ERP_DSN` set (now in the crontab line). Optional improvement: set `EINV_ALERT_LOG` in the crontab line, otherwise alerts go to `~/alert.log`.
- These run against a **scratch Postgres** (`dbname=turnkey user=turnkey host=/tmp`, created 2026-10-06, backup `backup/erp_test_20261006.sql`), not the real ERP. The "no activity" decision uses the scratch table, so with real traffic outside it, the idle check could wrongly say OK.
- `erp_bridge/syncer.py` marks FAILED / CANCEL_FAILED with the constant code E9999 for four message types (it does not read the real error code; improvement: pass the real code).
- The Supabase-based external ERP bridge is **not installed or configured** (no `.env`). Out of scope for the 書面檢核.
- Backups of edited scripts: `*.bak-20261006`.

## 7. The submission (書面檢核)

- Built from the official template `5440.odt`, answer fields only. Final file: `電子發票Turnkey上線前自行檢測作業_4.8.1_v3.odt`.
- All STEP 1 (firewall: SFTP 2222, Web API 443), STEP 2 (大平台設定, registered on both platforms per the owner: "registered in March this year") and STEP 3 (Turnkey settings, items 1 to 10) are ticked.
- 現行發票數量: **150 per week, 600 per month**.
- Upload: 書面文件 tab, 暫存, then 送出. Avoid platform maintenance windows: 10:00–10:30, 12:00–13:00, 14:00–14:30, 16:00–16:30, and after 17:30 (as shown on the platform that day).

## 8. Open items

- **SMTP password was exposed in a chat transcript.** Change it at the mail provider and re-enter it in Turnkey 通知設定. (Not confirmed done as of the last message.)
- Tighten file permissions (**unverified need**): private key `.key` is mode 744 and `AULY_softcert.pem` is 666. Suggested: `chmod 600` on the `.key` and `.pfx`, 644 on the `.pem`.
- Confirm whether the .pfx contains the same certificate as the registered `.cer`.
- 【接收方式及授權】 platform page was never screenshotted.
- Replace the scratch Postgres with a permanent one and connect the real ERP when ready (the real `turnkey_message_log` uses `message_dts`, the scratch one `sent_at`).
- Production go-live: the environment is still `T`. Before production, switch the environment on 系統環境設定 and re-check sections 1 and 5.
- After the first 03:00 run: confirm the 清檔 job actually ran and that the evidence files are still present.
