# 寄請款信

月底了，你請 Agent 把這個月開的、還沒寄過的請款單一次寄給客戶。

需要的授權項目：`bills:read`（找請款單）、`bills:send`（寄請款信）。

## 你對 Agent 說

> 把這個月開的、還沒寄過的請款單都寄給客戶，寄之前先跟我說會寄給誰，我說好你再寄。

## Agent 的呼叫

### bill_search

找這個月建立、還沒寄過請款信、還沒付的請款單。

參數：

```json
{
  "status": "UNPAID",
  "emailed": false,
  "created_from": "2026-11-01",
  "created_to": "2026-11-30",
  "limit": 100
}
```

回傳（只列了幾個欄位）：

```json
{
  "ok": true,
  "data": {
    "items": [
      { "number": "BL-000125", "buyer_name": "晨光咖啡有限公司", "currency": "TWD", "total": "8400", "balance": "8400", "due_date": "2026-12-01" },
      { "number": "BL-000126", "buyer_name": "山嵐民宿", "currency": "TWD", "total": "15000", "balance": "15000", "due_date": "2026-12-03" },
      { "number": "BL-000127", "buyer_name": "小麥烘焙坊", "currency": "TWD", "total": "4200", "balance": "4200", "due_date": "2026-12-05" }
    ],
    "next_cursor": null
  }
}
```

### bill_send

先試算，看每張會不會寄、寄給誰，拿確認碼。

參數：

```json
{
  "bills": ["BL-000125", "BL-000126", "BL-000127"],
  "message": "您好，附上本月的請款單，謝謝。",
  "dry_run": true
}
```

回傳：

```json
{
  "ok": true,
  "data": {
    "dry_run": true,
    "bills": [
      { "bill_id": "c1d2e3f4-0000-4000-8000-000000000125", "bill_number": "BL-000125", "will_send": true, "to": ["wang@example.com"], "cc": [], "reason": null },
      { "bill_id": "c1d2e3f4-0000-4000-8000-000000000126", "bill_number": "BL-000126", "will_send": true, "to": ["lin@example.com"], "cc": ["acct@example.com"], "reason": null },
      { "bill_id": "c1d2e3f4-0000-4000-8000-000000000127", "bill_number": "BL-000127", "will_send": false, "to": [], "cc": [], "reason": "CUSTOMER_EMAIL_MISSING" }
    ],
    "email_count": 2,
    "approval_required": false,
    "approval_reasons": [],
    "confirmation_required": true,
    "confirmation_code": "（一串字，每次試算不一樣）",
    "confirmation_expires_at": "2026-11-16T14:11:12+08:00"
  }
}
```

Agent 把結果告訴你：兩張會寄，BL-000125 寄給 wang@example.com，BL-000126 寄給 lin@example.com、副本 acct@example.com，BL-000127 的訂單沒有買方的 Email，不會寄；然後問你要不要寄。你說好之後，Agent 帶著試算回的 `confirmation_code`，正式寄出：

### bill_send

參數：

```json
{
  "bills": ["BL-000125", "BL-000126", "BL-000127"],
  "message": "您好，附上本月的請款單，謝謝。",
  "confirmation_code": "（試算回的確認碼）",
  "idempotency_key": "send-2026-11-batch-01"
}
```

回傳（只列了幾個欄位）：

```json
{
  "ok": true,
  "data": {
    "queued": [
      { "kind": "BILL", "status": "QUEUED", "to": ["wang@example.com"], "bills": [{ "number": "BL-000125" }] },
      { "kind": "BILL", "status": "QUEUED", "to": ["lin@example.com"], "cc": ["acct@example.com"], "bills": [{ "number": "BL-000126" }] }
    ],
    "skipped": [{ "bill_id": "c1d2e3f4-0000-4000-8000-000000000127", "bill_number": "BL-000127", "code": "CUSTOMER_EMAIL_MISSING" }]
  }
}
```

一次帶好幾張時，每張各自檢查：買方沒有主要 Email（`CUSTOMER_EMAIL_MISSING`）、已取消（`BILL_STATE_INVALID`）、已經有一封在排隊的會跳過，其他的照寄。只帶一張時，這些情況會直接回錯誤。

確認碼 10 分鐘內有效、只能用一次，只有 Agent 要用，你不用看、也不用輸入。超過 10 分鐘，或試算之後收件人、要寄的請款單變了，Agent 會重新試算、再問你一次。

## 結果

- BL-000125、BL-000126 各寄出一封請款信，主旨是「<你的公司名稱> 請款單 BL-000125」，附請款單 PDF 和「查看請款單並付款」的連結。
- 請款單明細的寄送紀錄、「寄信紀錄」頁都看得到這兩封；寄不出去的會自動重試，最後失敗的可以重寄。
- BL-000127 要先在網站上幫訂單補買方的 Email，再請 Agent 寄。

寄請款信不用核可，但沒有確認碼不會寄。用平台信箱寄的請款信會算進寄信額度（先扣本月免費的，再扣加購的）。額度不夠時整批都不寄，回 `PLAN_QUOTA_EXCEEDED`，見[寄件設定](/docs/getting-started/email)。
