sophhub 0.4.46 → 0.4.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/skills/sophnet-invoice-assistant/skill.json +21 -0
- package/skills/sophnet-invoice-assistant/src/SKILL.md +186 -0
- package/skills/sophnet-invoice-assistant/src/pyproject.toml +8 -0
- package/skills/sophnet-invoice-assistant/src/references/api-contract.md +73 -0
- package/skills/sophnet-invoice-assistant/src/references/conversation-examples.md +16 -0
- package/skills/sophnet-invoice-assistant/src/references/error-cases.md +16 -0
- package/skills/sophnet-invoice-assistant/src/scripts/__init__.py +1 -0
- package/skills/sophnet-invoice-assistant/src/scripts/auth_context.py +74 -0
- package/skills/sophnet-invoice-assistant/src/scripts/cli.py +136 -0
- package/skills/sophnet-invoice-assistant/src/scripts/download_flow.py +89 -0
- package/skills/sophnet-invoice-assistant/src/scripts/dto.py +46 -0
- package/skills/sophnet-invoice-assistant/src/scripts/invoice_client.py +164 -0
- package/skills/sophnet-invoice-assistant/src/scripts/output.py +63 -0
- package/skills/sophnet-invoice-assistant/src/scripts/prepare_flow.py +58 -0
- package/skills/sophnet-invoice-assistant/src/scripts/risk_rules.py +95 -0
- package/skills/sophnet-invoice-assistant/src/scripts/status_flow.py +69 -0
- package/skills/sophnet-invoice-assistant/src/scripts/submit_flow.py +472 -0
- package/skills/sophnet-invoice-assistant/src/scripts/template_flow.py +196 -0
- package/skills/sophnet-invoice-assistant/src/scripts/update_flow.py +223 -0
package/package.json
CHANGED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sophnet-invoice-assistant",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"types": [
|
|
5
|
+
"store"
|
|
6
|
+
],
|
|
7
|
+
"displayName": "Sophnet发票助手",
|
|
8
|
+
"description": "帮助已登录 Sophnet 的个人用户在 OpenClaw 中收集开票信息、提交发票申请、查询发票状态和下载发票文件。",
|
|
9
|
+
"changelog": [
|
|
10
|
+
{
|
|
11
|
+
"version": "1.0.0",
|
|
12
|
+
"date": "2026-06-26",
|
|
13
|
+
"changes": [
|
|
14
|
+
"初始化个人发票助手 skill 骨架",
|
|
15
|
+
"补充设计文档、脚本入口与参考文档"
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
],
|
|
19
|
+
"createdAt": "2026-06-26",
|
|
20
|
+
"updatedAt": "2026-06-26"
|
|
21
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: sophnet-invoice-assistant
|
|
3
|
+
description: Use when an OpenClaw user wants to manage personal Sophnet invoice templates, apply for an invoice, update an invoice application, check invoice status, or download an issued invoice file while already logged in to Sophnet.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Sophnet Invoice Assistant
|
|
7
|
+
|
|
8
|
+
用于帮助已登录 Sophnet 的用户完成发票中心相关操作,包括:
|
|
9
|
+
|
|
10
|
+
- 准备发票上下文
|
|
11
|
+
- 提交发票申请
|
|
12
|
+
- 修改发票申请
|
|
13
|
+
- 查询发票申请状态
|
|
14
|
+
- 下载发票文件
|
|
15
|
+
- 管理发票模板
|
|
16
|
+
|
|
17
|
+
## Supported actions
|
|
18
|
+
|
|
19
|
+
- `prepare`
|
|
20
|
+
- `submit`
|
|
21
|
+
- `update`
|
|
22
|
+
- `status`
|
|
23
|
+
- `download`
|
|
24
|
+
- `template_list`
|
|
25
|
+
- `template_save`
|
|
26
|
+
- `template_delete`
|
|
27
|
+
|
|
28
|
+
## Auth requirements
|
|
29
|
+
|
|
30
|
+
脚本会按以下优先级自动读取 Sophnet 登录态:
|
|
31
|
+
|
|
32
|
+
- `SOPHNET_ACCESS_TOKEN`
|
|
33
|
+
- `SOPHNET_COOKIE`
|
|
34
|
+
- `OPENCLAW_JWT_PATH` 指向的 `jwt.json` 中的 `web_jwt`
|
|
35
|
+
- 默认位置的 `jwt.json`
|
|
36
|
+
|
|
37
|
+
支持的 API Base URL 环境变量:
|
|
38
|
+
|
|
39
|
+
- `SOPHNET_API_BASE_URL`
|
|
40
|
+
- `SOPHNET_BASE_URL`
|
|
41
|
+
|
|
42
|
+
## CLI usage
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
cd "<this-skill-directory>"
|
|
46
|
+
uv run {baseDir}/scripts/cli.py --action prepare --user-request "帮我准备发票信息"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
cd "<this-skill-directory>"
|
|
51
|
+
uv run {baseDir}/scripts/cli.py \
|
|
52
|
+
--action submit \
|
|
53
|
+
--user-request "帮我给这两笔充值开票并聚合开一张" \
|
|
54
|
+
--buyer-type ENTERPRISE \
|
|
55
|
+
--invoice-type SPECIAL \
|
|
56
|
+
--title "杭州某某科技有限公司" \
|
|
57
|
+
--tax-no "9133XXXX" \
|
|
58
|
+
--address-phone "杭州xx路 0571-xxxx" \
|
|
59
|
+
--bank-account "招商银行杭州分行 6222xxxx" \
|
|
60
|
+
--recharge-trans-nos "RCHG001,RCHG002" \
|
|
61
|
+
--issuing-mode AGGREGATED
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
cd "<this-skill-directory>"
|
|
66
|
+
uv run {baseDir}/scripts/cli.py \
|
|
67
|
+
--action update \
|
|
68
|
+
--application-no INV202606250001 \
|
|
69
|
+
--recharge-trans-nos "RCHG001,RCHG002" \
|
|
70
|
+
--issuing-mode SEPARATE \
|
|
71
|
+
--user-request "把这张发票申请改成分开开发票"
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Parameters
|
|
75
|
+
|
|
76
|
+
- `--action`: `prepare|submit|update|status|download|template_list|template_save|template_delete`
|
|
77
|
+
- `--user-request`: 用户原始请求
|
|
78
|
+
- `--org-id`: 可选,已知个人组织 ID
|
|
79
|
+
- `--application-no`: 可选,查询/下载/修改指定申请时使用
|
|
80
|
+
- `--template-id`: 可选,删除模板时使用
|
|
81
|
+
- `--confirm`: `true|false`
|
|
82
|
+
- `--buyer-type`: `INDIVIDUAL|ENTERPRISE`
|
|
83
|
+
- `--title`: 发票抬头
|
|
84
|
+
- `--tax-no`: 企业税号
|
|
85
|
+
- `--address-phone`: 企业地址电话
|
|
86
|
+
- `--bank-account`: 企业开户行及账号
|
|
87
|
+
- `--invoice-type`: `NORMAL|SPECIAL`
|
|
88
|
+
- `--recharge-trans-nos`: 逗号分隔的充值流水号
|
|
89
|
+
- `--issuing-mode`: `AGGREGATED|SEPARATE`
|
|
90
|
+
- `--save-as-template`: `true|false`
|
|
91
|
+
- `--template-name`: 模板名称
|
|
92
|
+
- `--use-latest-template`: `true|false`
|
|
93
|
+
|
|
94
|
+
## Current business rules
|
|
95
|
+
|
|
96
|
+
### Recharge eligibility
|
|
97
|
+
|
|
98
|
+
- `2026-06-24` 之前的充值记录不支持自助开发票
|
|
99
|
+
- skill 会在 `prepare` 中显式返回:
|
|
100
|
+
- `AVAILABLE_RECHARGE_RECORDS`
|
|
101
|
+
- `INELIGIBLE_RECHARGE_RECORDS`
|
|
102
|
+
- `INVOICE_ELIGIBLE_START_DATE=2026-06-24`
|
|
103
|
+
- 如果用户选择了不可开票记录,`submit` / `update` 会直接失败并提示重新选择
|
|
104
|
+
|
|
105
|
+
### Multiple recharge records
|
|
106
|
+
|
|
107
|
+
- 当一次申请关联多条 `rechargeTransNos` 时,必须指定 `issuingMode`
|
|
108
|
+
- 可选值:
|
|
109
|
+
- `AGGREGATED`: 多条充值记录聚合开票
|
|
110
|
+
- `SEPARATE`: 每条充值记录分别开票
|
|
111
|
+
- 如果用户选了多条充值记录但没有给出 `issuingMode`,skill 会返回 `STATUS=need_confirmation`
|
|
112
|
+
|
|
113
|
+
## Current behavior
|
|
114
|
+
|
|
115
|
+
### `prepare`
|
|
116
|
+
|
|
117
|
+
- 自动定位当前个人组织
|
|
118
|
+
- 拉取发票模板
|
|
119
|
+
- 拉取可开票充值记录
|
|
120
|
+
- 拉取最近发票申请
|
|
121
|
+
- 额外返回不可开票记录数量与起始日期限制
|
|
122
|
+
|
|
123
|
+
### `submit`
|
|
124
|
+
|
|
125
|
+
- 调用真实 Sophnet 发票申请接口
|
|
126
|
+
- 自动解析用户选择的模板、开票信息和充值流水
|
|
127
|
+
- 多条充值流水时要求 `issuingMode`
|
|
128
|
+
- 自动过滤 `2026-06-24` 之前的充值记录
|
|
129
|
+
- 当前默认会将本次开票信息保存为模板
|
|
130
|
+
|
|
131
|
+
### `update`
|
|
132
|
+
|
|
133
|
+
- 调用真实 Sophnet 发票申请修改接口
|
|
134
|
+
- 支持修改抬头、税号、地址电话、银行账号、发票类型、充值流水、开票方式
|
|
135
|
+
- 多条充值流水时要求 `issuingMode`
|
|
136
|
+
- 默认先返回确认摘要,再由 OpenClaw 做二次确认
|
|
137
|
+
|
|
138
|
+
### `status`
|
|
139
|
+
|
|
140
|
+
- 查询最近申请或指定申请状态
|
|
141
|
+
|
|
142
|
+
### `download`
|
|
143
|
+
|
|
144
|
+
- 下载已开具发票的文件信息
|
|
145
|
+
|
|
146
|
+
### `template_list`
|
|
147
|
+
|
|
148
|
+
- 查询模板列表,可按模板名称过滤
|
|
149
|
+
|
|
150
|
+
### `template_save`
|
|
151
|
+
|
|
152
|
+
- 新增模板或按同名覆盖模板
|
|
153
|
+
|
|
154
|
+
### `template_delete`
|
|
155
|
+
|
|
156
|
+
- 按 `templateId` 或 `templateName` 删除模板
|
|
157
|
+
|
|
158
|
+
## Output format
|
|
159
|
+
|
|
160
|
+
```text
|
|
161
|
+
STATUS=succeeded|failed|need_confirmation|not_implemented
|
|
162
|
+
ACTION=prepare|submit|update|status|download|template_list|template_save|template_delete
|
|
163
|
+
MESSAGE=...
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
常见附加字段:
|
|
167
|
+
|
|
168
|
+
- `ORG_ID`
|
|
169
|
+
- `APPLICATION_NO`
|
|
170
|
+
- `APPLICATION_STATUS`
|
|
171
|
+
- `TITLE`
|
|
172
|
+
- `AMOUNT`
|
|
173
|
+
- `ISSUING_MODE`
|
|
174
|
+
- `TEMPLATE_ID`
|
|
175
|
+
- `TEMPLATE_NAME`
|
|
176
|
+
- `INVOICE_ELIGIBLE_START_DATE`
|
|
177
|
+
- `AVAILABLE_ISSUING_MODES`
|
|
178
|
+
- `INELIGIBLE_RECHARGE_RECORD_COUNT`
|
|
179
|
+
- `ERROR_CODE`
|
|
180
|
+
- `ERROR_MESSAGE`
|
|
181
|
+
|
|
182
|
+
## Notes
|
|
183
|
+
|
|
184
|
+
- 当前 skill 面向个人组织发票中心流程
|
|
185
|
+
- 所有操作依赖真实 Sophnet API
|
|
186
|
+
- 不要在输出中打印完整 token、cookie 或其他敏感凭证
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# API Contract Notes
|
|
2
|
+
|
|
3
|
+
本文档记录 `sophnet-invoice-assistant` 当前对接的 Sophnet 发票中心接口与关键约束。
|
|
4
|
+
|
|
5
|
+
## Current endpoints
|
|
6
|
+
|
|
7
|
+
- `GET /orgs/joined-org`
|
|
8
|
+
- `GET /orgs/{orgId}/invoices/templates`
|
|
9
|
+
- `POST /orgs/{orgId}/invoices/templates`
|
|
10
|
+
- `DELETE /orgs/{orgId}/invoices/templates/{templateId}`
|
|
11
|
+
- `GET /orgs/{orgId}/invoices/recharge-records`
|
|
12
|
+
- `POST /orgs/{orgId}/invoices/applications`
|
|
13
|
+
- `PUT /orgs/{orgId}/invoices/applications/{applicationNo}`
|
|
14
|
+
- `GET /orgs/{orgId}/invoices/applications`
|
|
15
|
+
- `GET /orgs/{orgId}/invoices/applications/{applicationNo}/download`
|
|
16
|
+
|
|
17
|
+
## Request payload notes
|
|
18
|
+
|
|
19
|
+
创建和修改发票申请都使用以下核心字段:
|
|
20
|
+
|
|
21
|
+
- `buyerType`
|
|
22
|
+
- `title`
|
|
23
|
+
- `taxNo`
|
|
24
|
+
- `addressPhone`
|
|
25
|
+
- `bankAccount`
|
|
26
|
+
- `invoiceType`
|
|
27
|
+
- `rechargeTransNos`
|
|
28
|
+
- `issuingMode`
|
|
29
|
+
|
|
30
|
+
模板保存额外使用:
|
|
31
|
+
|
|
32
|
+
- `saveAsTemplate`
|
|
33
|
+
- `templateName`
|
|
34
|
+
|
|
35
|
+
## Important business rules
|
|
36
|
+
|
|
37
|
+
### Multiple recharge records
|
|
38
|
+
|
|
39
|
+
当 `rechargeTransNos` 数量大于 1 时,后端要求必须传 `issuingMode`:
|
|
40
|
+
|
|
41
|
+
- `AGGREGATED`
|
|
42
|
+
- `SEPARATE`
|
|
43
|
+
|
|
44
|
+
后端校验位置:
|
|
45
|
+
|
|
46
|
+
- `InvoiceApplicationValidator.validateIssuingMode(...)`
|
|
47
|
+
|
|
48
|
+
### Recharge eligibility start date
|
|
49
|
+
|
|
50
|
+
`2026-06-24` 之前的充值记录不支持自助开发票。
|
|
51
|
+
|
|
52
|
+
后端规则常量位置:
|
|
53
|
+
|
|
54
|
+
- `InvoiceEligibilityRules.ELIGIBLE_RECHARGE_START_DATE`
|
|
55
|
+
- `InvoiceEligibilityRules.ELIGIBLE_RECHARGE_START_DATE_TEXT`
|
|
56
|
+
|
|
57
|
+
前端同步常量位置:
|
|
58
|
+
|
|
59
|
+
- `sophnet/web/src/views/organization/invoices/utils.ts`
|
|
60
|
+
- `INVOICE_ELIGIBLE_START_DATE = "2026-06-24"`
|
|
61
|
+
|
|
62
|
+
## Suggested client wrappers
|
|
63
|
+
|
|
64
|
+
- `get_current_personal_org()`
|
|
65
|
+
- `list_invoice_templates(org_id)`
|
|
66
|
+
- `save_invoice_template(org_id, payload)`
|
|
67
|
+
- `delete_invoice_template(org_id, template_id)`
|
|
68
|
+
- `list_invoiceable_recharge_records(org_id, params)`
|
|
69
|
+
- `create_invoice_application(org_id, payload)`
|
|
70
|
+
- `update_invoice_application(org_id, application_no, payload)`
|
|
71
|
+
- `list_invoice_applications(org_id, params)`
|
|
72
|
+
- `get_invoice_application(org_id, application_no)`
|
|
73
|
+
- `download_invoice_file(org_id, application_no)`
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Error Cases
|
|
2
|
+
|
|
3
|
+
## 登录态缺失
|
|
4
|
+
|
|
5
|
+
- `AUTH_CONTEXT_MISSING`
|
|
6
|
+
- 提示:当前 Sophnet 登录态缺失,请重新登录后再试。
|
|
7
|
+
|
|
8
|
+
## 个人组织缺失
|
|
9
|
+
|
|
10
|
+
- `PERSONAL_ORG_NOT_FOUND`
|
|
11
|
+
- 提示:当前账号下未找到可用的个人组织,暂时无法代你提交发票申请。
|
|
12
|
+
|
|
13
|
+
## 真实接口未接入
|
|
14
|
+
|
|
15
|
+
- `API_NOT_IMPLEMENTED`
|
|
16
|
+
- 提示:当前 skill 仅完成骨架搭建,尚未接入真实 Sophnet 发票接口。
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""sophnet-invoice-assistant skill scripts package."""
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""Read Sophnet auth context from runtime environment."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from dto import AuthContext
|
|
10
|
+
|
|
11
|
+
DEFAULT_JWT_PATHS = (
|
|
12
|
+
"/home/node/.openclaw/jwt.json",
|
|
13
|
+
"~/.openclaw/jwt.json",
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _normalize_api_base_url(raw_value):
|
|
18
|
+
value = (raw_value or "").strip()
|
|
19
|
+
if not value:
|
|
20
|
+
return "https://www.sophnet.com/api"
|
|
21
|
+
value = value.rstrip("/")
|
|
22
|
+
if value.endswith("/api"):
|
|
23
|
+
return value
|
|
24
|
+
return value + "/api"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _normalize_access_token(raw_value):
|
|
28
|
+
value = (raw_value or "").strip()
|
|
29
|
+
if not value:
|
|
30
|
+
return None
|
|
31
|
+
if value.lower().startswith("bearer "):
|
|
32
|
+
value = value[7:].strip()
|
|
33
|
+
return value or None
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _candidate_jwt_paths():
|
|
37
|
+
custom_path = (os.environ.get("OPENCLAW_JWT_PATH") or "").strip()
|
|
38
|
+
if custom_path:
|
|
39
|
+
yield Path(custom_path).expanduser()
|
|
40
|
+
|
|
41
|
+
for raw_path in DEFAULT_JWT_PATHS:
|
|
42
|
+
yield Path(raw_path).expanduser()
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _load_access_token_from_jwt():
|
|
46
|
+
for path in _candidate_jwt_paths():
|
|
47
|
+
try:
|
|
48
|
+
with path.open("r", encoding="utf-8") as fp:
|
|
49
|
+
payload = json.load(fp)
|
|
50
|
+
except (OSError, ValueError):
|
|
51
|
+
continue
|
|
52
|
+
if not isinstance(payload, dict):
|
|
53
|
+
continue
|
|
54
|
+
token = _normalize_access_token(payload.get("web_jwt"))
|
|
55
|
+
if token:
|
|
56
|
+
return token
|
|
57
|
+
return None
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _resolve_access_token():
|
|
61
|
+
explicit_token = _normalize_access_token(os.environ.get("SOPHNET_ACCESS_TOKEN"))
|
|
62
|
+
if explicit_token:
|
|
63
|
+
return explicit_token
|
|
64
|
+
return _load_access_token_from_jwt()
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def load_auth_context():
|
|
68
|
+
return AuthContext(
|
|
69
|
+
api_base_url=_normalize_api_base_url(
|
|
70
|
+
os.environ.get("SOPHNET_API_BASE_URL") or os.environ.get("SOPHNET_BASE_URL")
|
|
71
|
+
),
|
|
72
|
+
access_token=_resolve_access_token(),
|
|
73
|
+
cookie=os.environ.get("SOPHNET_COOKIE"),
|
|
74
|
+
)
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""CLI entry for sophnet-invoice-assistant."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
|
|
8
|
+
from auth_context import load_auth_context
|
|
9
|
+
from download_flow import run_download
|
|
10
|
+
from dto import ActionRequest
|
|
11
|
+
from invoice_client import InvoiceClient
|
|
12
|
+
from output import emit_result, failure
|
|
13
|
+
from prepare_flow import run_prepare
|
|
14
|
+
from status_flow import run_status
|
|
15
|
+
from submit_flow import run_submit
|
|
16
|
+
from template_flow import run_template_delete, run_template_list, run_template_save
|
|
17
|
+
from update_flow import run_update
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def parse_args():
|
|
21
|
+
parser = argparse.ArgumentParser(description="Sophnet personal invoice assistant skeleton")
|
|
22
|
+
parser.add_argument(
|
|
23
|
+
"--action",
|
|
24
|
+
required=True,
|
|
25
|
+
choices=["prepare", "submit", "update", "status", "download", "template_list", "template_save", "template_delete"],
|
|
26
|
+
help="Action to perform",
|
|
27
|
+
)
|
|
28
|
+
parser.add_argument(
|
|
29
|
+
"--user-request",
|
|
30
|
+
required=True,
|
|
31
|
+
help="Original user request text",
|
|
32
|
+
)
|
|
33
|
+
parser.add_argument(
|
|
34
|
+
"--org-id",
|
|
35
|
+
help="Known personal org id",
|
|
36
|
+
)
|
|
37
|
+
parser.add_argument(
|
|
38
|
+
"--application-no",
|
|
39
|
+
help="Known invoice application number",
|
|
40
|
+
)
|
|
41
|
+
parser.add_argument(
|
|
42
|
+
"--template-id",
|
|
43
|
+
help="Known invoice template id",
|
|
44
|
+
)
|
|
45
|
+
parser.add_argument(
|
|
46
|
+
"--confirm",
|
|
47
|
+
default="false",
|
|
48
|
+
choices=["true", "false"],
|
|
49
|
+
help="Whether user already confirmed the submit action",
|
|
50
|
+
)
|
|
51
|
+
parser.add_argument("--buyer-type", choices=["INDIVIDUAL", "ENTERPRISE"])
|
|
52
|
+
parser.add_argument("--title")
|
|
53
|
+
parser.add_argument("--tax-no")
|
|
54
|
+
parser.add_argument("--address-phone")
|
|
55
|
+
parser.add_argument("--bank-account")
|
|
56
|
+
parser.add_argument("--invoice-type", choices=["NORMAL", "SPECIAL"])
|
|
57
|
+
parser.add_argument(
|
|
58
|
+
"--recharge-trans-nos",
|
|
59
|
+
help="Comma-separated recharge transNo list",
|
|
60
|
+
)
|
|
61
|
+
parser.add_argument(
|
|
62
|
+
"--issuing-mode",
|
|
63
|
+
choices=["AGGREGATED", "SEPARATE"],
|
|
64
|
+
help="Required when multiple recharge records are selected",
|
|
65
|
+
)
|
|
66
|
+
parser.add_argument(
|
|
67
|
+
"--save-as-template",
|
|
68
|
+
default="false",
|
|
69
|
+
choices=["true", "false"],
|
|
70
|
+
)
|
|
71
|
+
parser.add_argument("--template-name")
|
|
72
|
+
parser.add_argument(
|
|
73
|
+
"--use-latest-template",
|
|
74
|
+
default="false",
|
|
75
|
+
choices=["true", "false"],
|
|
76
|
+
)
|
|
77
|
+
return parser.parse_args()
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def build_request(args):
|
|
81
|
+
return ActionRequest(
|
|
82
|
+
action=args.action,
|
|
83
|
+
user_request=args.user_request,
|
|
84
|
+
org_id=args.org_id,
|
|
85
|
+
application_no=args.application_no,
|
|
86
|
+
template_id=args.template_id,
|
|
87
|
+
confirm=args.confirm == "true",
|
|
88
|
+
buyer_type=args.buyer_type,
|
|
89
|
+
title=args.title,
|
|
90
|
+
tax_no=args.tax_no,
|
|
91
|
+
address_phone=args.address_phone,
|
|
92
|
+
bank_account=args.bank_account,
|
|
93
|
+
invoice_type=args.invoice_type,
|
|
94
|
+
recharge_trans_nos=args.recharge_trans_nos,
|
|
95
|
+
issuing_mode=args.issuing_mode,
|
|
96
|
+
save_as_template=args.save_as_template == "true",
|
|
97
|
+
template_name=args.template_name,
|
|
98
|
+
use_latest_template=args.use_latest_template == "true",
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def dispatch(request, auth_context, client):
|
|
103
|
+
if request.action == "prepare":
|
|
104
|
+
return run_prepare(request, auth_context, client)
|
|
105
|
+
if request.action == "submit":
|
|
106
|
+
return run_submit(request, auth_context, client)
|
|
107
|
+
if request.action == "status":
|
|
108
|
+
return run_status(request, auth_context, client)
|
|
109
|
+
if request.action == "download":
|
|
110
|
+
return run_download(request, auth_context, client)
|
|
111
|
+
if request.action == "update":
|
|
112
|
+
return run_update(request, auth_context, client)
|
|
113
|
+
if request.action == "template_list":
|
|
114
|
+
return run_template_list(request, auth_context, client)
|
|
115
|
+
if request.action == "template_save":
|
|
116
|
+
return run_template_save(request, auth_context, client)
|
|
117
|
+
if request.action == "template_delete":
|
|
118
|
+
return run_template_delete(request, auth_context, client)
|
|
119
|
+
return failure(
|
|
120
|
+
action=request.action,
|
|
121
|
+
error_code="UNKNOWN_ACTION",
|
|
122
|
+
error_message="Unknown action: {0}".format(request.action),
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def main():
|
|
127
|
+
args = parse_args()
|
|
128
|
+
request = build_request(args)
|
|
129
|
+
auth_context = load_auth_context()
|
|
130
|
+
client = InvoiceClient(auth_context)
|
|
131
|
+
result = dispatch(request, auth_context, client)
|
|
132
|
+
emit_result(result)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
if __name__ == "__main__":
|
|
136
|
+
main()
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""Issued invoice download skeleton."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from invoice_client import InvoiceClientError
|
|
6
|
+
from output import build_result, failure, need_confirmation
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _candidate_summary(items):
|
|
10
|
+
values = []
|
|
11
|
+
for item in items[:5]:
|
|
12
|
+
values.append(
|
|
13
|
+
"{0}({1})".format(
|
|
14
|
+
item.get("applicationNo") or "",
|
|
15
|
+
item.get("title") or item.get("status") or "unknown",
|
|
16
|
+
)
|
|
17
|
+
)
|
|
18
|
+
return "Multiple issued invoices were found. Specify an application number before downloading: {0}".format(", ".join(values))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def run_download(request, auth_context, client):
|
|
22
|
+
if not auth_context.is_authenticated:
|
|
23
|
+
return failure(
|
|
24
|
+
action=request.action,
|
|
25
|
+
error_code="AUTH_CONTEXT_MISSING",
|
|
26
|
+
error_message="Current Sophnet auth context is missing. Please log in again.",
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
try:
|
|
30
|
+
org = client.get_current_personal_org(request.org_id)
|
|
31
|
+
target_application_no = request.application_no
|
|
32
|
+
if not target_application_no:
|
|
33
|
+
page = client.list_invoice_applications(
|
|
34
|
+
org.get("id"),
|
|
35
|
+
params={"pageNum": 1, "pageSize": 10, "status": "ISSUED"},
|
|
36
|
+
)
|
|
37
|
+
items = page.get("list") or [] if isinstance(page, dict) else []
|
|
38
|
+
if not items:
|
|
39
|
+
return failure(
|
|
40
|
+
action=request.action,
|
|
41
|
+
error_code="NO_ISSUED_APPLICATION_FOUND",
|
|
42
|
+
error_message="No issued invoice is currently available for download under the personal org.",
|
|
43
|
+
extra={"ORG_ID": str(org.get("id"))},
|
|
44
|
+
)
|
|
45
|
+
if len(items) > 1:
|
|
46
|
+
return need_confirmation(
|
|
47
|
+
action=request.action,
|
|
48
|
+
summary=_candidate_summary(items),
|
|
49
|
+
message="Multiple downloadable invoices were found. Please specify an application number.",
|
|
50
|
+
extra={
|
|
51
|
+
"ORG_ID": str(org.get("id")),
|
|
52
|
+
"CANDIDATE_APPLICATION_NOS": ",".join(
|
|
53
|
+
[str(item.get("applicationNo") or "") for item in items[:10]]
|
|
54
|
+
),
|
|
55
|
+
},
|
|
56
|
+
)
|
|
57
|
+
target_application_no = items[0].get("applicationNo")
|
|
58
|
+
|
|
59
|
+
if not target_application_no:
|
|
60
|
+
return failure(
|
|
61
|
+
action=request.action,
|
|
62
|
+
error_code="APPLICATION_NO_REQUIRED",
|
|
63
|
+
error_message="The invoice application number is required before downloading.",
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
file_info = client.download_invoice_file(org.get("id"), target_application_no)
|
|
67
|
+
except InvoiceClientError as exc:
|
|
68
|
+
return failure(
|
|
69
|
+
action=request.action,
|
|
70
|
+
error_code=exc.code,
|
|
71
|
+
error_message=exc.message,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
return build_result(
|
|
75
|
+
status="succeeded",
|
|
76
|
+
action=request.action,
|
|
77
|
+
message="Invoice download details were retrieved successfully.",
|
|
78
|
+
extra={
|
|
79
|
+
"ORG_ID": str(org.get("id")),
|
|
80
|
+
"ORG_NAME": org.get("name") or "",
|
|
81
|
+
"APPLICATION_NO": file_info.get("applicationNo") or str(target_application_no),
|
|
82
|
+
"FILE_NAME": file_info.get("fileName") or "",
|
|
83
|
+
"CONTENT_TYPE": file_info.get("contentType") or "",
|
|
84
|
+
"FILE_SIZE": str(file_info.get("fileSize") or ""),
|
|
85
|
+
"PREVIEW_URL": file_info.get("previewUrl") or "",
|
|
86
|
+
"DOWNLOAD_URL": file_info.get("downloadUrl") or "",
|
|
87
|
+
"EXPIRES_IN": str(file_info.get("expiresIn") or ""),
|
|
88
|
+
},
|
|
89
|
+
)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""Data transfer objects for the invoice assistant skeleton."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Dict, Optional
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class ActionRequest(object):
|
|
11
|
+
action: str
|
|
12
|
+
user_request: str
|
|
13
|
+
org_id: Optional[str] = None
|
|
14
|
+
application_no: Optional[str] = None
|
|
15
|
+
template_id: Optional[str] = None
|
|
16
|
+
confirm: bool = False
|
|
17
|
+
buyer_type: Optional[str] = None
|
|
18
|
+
title: Optional[str] = None
|
|
19
|
+
tax_no: Optional[str] = None
|
|
20
|
+
address_phone: Optional[str] = None
|
|
21
|
+
bank_account: Optional[str] = None
|
|
22
|
+
invoice_type: Optional[str] = None
|
|
23
|
+
recharge_trans_nos: Optional[str] = None
|
|
24
|
+
issuing_mode: Optional[str] = None
|
|
25
|
+
save_as_template: bool = False
|
|
26
|
+
template_name: Optional[str] = None
|
|
27
|
+
use_latest_template: bool = False
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class AuthContext(object):
|
|
32
|
+
api_base_url: str
|
|
33
|
+
access_token: Optional[str]
|
|
34
|
+
cookie: Optional[str]
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def is_authenticated(self) -> bool:
|
|
38
|
+
return bool(self.access_token or self.cookie)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass
|
|
42
|
+
class ActionResult(object):
|
|
43
|
+
status: str
|
|
44
|
+
action: str
|
|
45
|
+
message: str
|
|
46
|
+
extra: Optional[Dict[str, str]] = None
|