cetustek 0.8.0 → 0.10.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +66 -0
- data/README.md +70 -15
- data/lib/cetustek/cancel_allowance.rb +17 -39
- data/lib/cetustek/cancel_invoice.rb +20 -44
- data/lib/cetustek/create_allowance.rb +24 -61
- data/lib/cetustek/create_invoice.rb +4 -2
- data/lib/cetustek/errors.rb +24 -0
- data/lib/cetustek/models/allowance_data.rb +47 -4
- data/lib/cetustek/models/invoice_data.rb +75 -8
- data/lib/cetustek/queries.rb +64 -56
- data/lib/cetustek/query_invoice_by_order_id.rb +11 -12
- data/lib/cetustek/services/invoice_xml_builder.rb +38 -78
- data/lib/cetustek/services/response_handler.rb +9 -21
- data/lib/cetustek/soap.rb +29 -0
- data/lib/cetustek/version.rb +1 -1
- data/lib/cetustek/xml.rb +72 -0
- data/lib/cetustek.rb +3 -1
- metadata +3 -2
- data/lib/cetustek/services/invoice_service.rb +0 -28
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 90bc8844eac9b3eb305d59f5550be7bf6e552b0fffafe74b0e71e6afc31c55ef
|
|
4
|
+
data.tar.gz: a9337fd9f37f80a3ec6532a319c4eac87ce1db79a475d7e237e3f2b59567a850
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 57e5c7e15f119c182c8d0f38ef0c98ce40c682bc0262ed7043a09ed8c379a2603ae88d581bef8bcdc9e0f0a95cc5be85e6a6dc502f7c883bb7f6e74c9ebd8fa3
|
|
7
|
+
data.tar.gz: c267be5d85ea54c917d932453a910bfcca464b4e5441ad4f2c98aec3e4b2f567712e65617e6250186ebd6164ec39f4157ad12014cfce6445847afe7c8db1b926
|
data/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,72 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.10.0] - 2026-08-07
|
|
9
|
+
|
|
10
|
+
延續 0.9.0 的 code review:統一驗證力度與查詢介面,讓四個查詢類別與
|
|
11
|
+
`CancelInvoice`/`CancelAllowance` 的行為一致。
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
- `QueryInvoice.find`、`QueryInvoiceByOrderId.find`、`QueryInvoiceNumberByOrderId.find`:
|
|
15
|
+
比照既有的 `QueryAllowance.find`,把 Savon 回應解析成 snake_case 的 Hash(`QueryInvoice`
|
|
16
|
+
與 `QueryInvoiceByOrderId` 共用 Table 13 格式,含 `:seller`/`:buyer`/`:details`),
|
|
17
|
+
`nil` 表示空回覆或規格說的 `"nodata"`,非 XML 的代碼字串一律拋 `Cetustek::ResultError`
|
|
18
|
+
- `CancelInvoice` 補上 `invoice_number`/`invoice_year` 必填檢查(Table 9 皆為 Y),
|
|
19
|
+
原本只驗證 `remark`,另兩個欄位空白時要等伺服器回 `M:?` 才會發現
|
|
20
|
+
|
|
21
|
+
### Changed
|
|
22
|
+
- 四個查詢類別的 XML 解析共用邏輯(`text_of`/`snake_case`)搬到 `Cetustek::Xml.parse_fields`,
|
|
23
|
+
不再各自留一份幾乎相同的 private method
|
|
24
|
+
- `QueryInvoice`/`QueryInvoiceNumberByOrderId`/`QueryAllowance` 改用 `Soap#soap_call`,
|
|
25
|
+
不再手動組 `source`/`rentid`,與 `QueryInvoiceByOrderId`/`CancelInvoice` 等類別一致
|
|
26
|
+
- `QueryInvoice.parse`/`QueryAllowance.parse` 共用的「空值/nodata 判斷、非 XML 代碼丟
|
|
27
|
+
`ResultError`、解析成 Ox root」邏輯收成 `Cetustek::Xml.parse_response`
|
|
28
|
+
- `QueryInvoice.party` 改名 `parse_party`,跟同一個類別的 `parse` 放在一起看更清楚
|
|
29
|
+
|
|
30
|
+
### Fixed
|
|
31
|
+
- **Breaking:** `QueryInvoiceNumberByOrderId.find` 原本會把非發票號碼、非 `"nodata"`
|
|
32
|
+
的回覆(如 `Invalid`)直接當發票號碼回傳;現在會比對 Table 9 的字軌+8碼數字格式,
|
|
33
|
+
格式不符者一律丟 `Cetustek::ResultError`,跟其他 `Query*.find` 一致
|
|
34
|
+
|
|
35
|
+
## [0.9.0] - 2026-08-05
|
|
36
|
+
|
|
37
|
+
比對規格 AVM-26-03 做的 code review 修正:0.8.0 有幾條規則寫得比規格寬或比規格嚴,
|
|
38
|
+
另外把四份重複的 SOAP/XML 樣板收成共用模組。
|
|
39
|
+
|
|
40
|
+
### Fixed
|
|
41
|
+
- **Breaking:** `carrier_id2` 只在無顯碼隱碼區分的載具(手機條碼 `3J0002`、自然人憑證
|
|
42
|
+
`CQ0001`)自動鏡射顯碼。會員載具(如鯨躍發票卡 `EJ0011`)兩碼本來就不同,0.8.0 會把顯碼
|
|
43
|
+
當隱碼送出;現在未填 `carrier_id2` 會直接丟 `ArgumentError`
|
|
44
|
+
- `donate_mark: 0` 不再強制 `carrier_type`:規格註明「使用鯨躍發票卡,電子郵件必填,
|
|
45
|
+
載具類別可為空或填 EJ0011」,原本的檢查讓這個情境無法送出。必填改為 `buyer_email`
|
|
46
|
+
與 `carrier_id1`/`carrier_id2`
|
|
47
|
+
- `QueryAllowance.find` 移除規格沒有的 `'nodata'` 哨兵值;非 XML 的回覆(代碼字串)
|
|
48
|
+
改為丟 `ResultError`,只有真的空回覆才回傳 `nil`
|
|
49
|
+
- 修掉註解裡捏造的規格出處(`spec V4.16`)與 TaxType 9 的「限收銀機類型發票」限制,
|
|
50
|
+
Table 1 只寫「9:混合(應稅、零稅率與免稅)」
|
|
51
|
+
|
|
52
|
+
### Added
|
|
53
|
+
- `Cetustek::CarrierType`:`MOBILE_BARCODE` / `CITIZEN_CERT` / `CETUSTEK_CARD` 常數
|
|
54
|
+
- `AllowanceData` 依 Table 15 驗證必填(折讓單號、折讓日期、發票號碼、發票年份、
|
|
55
|
+
折讓原因、至少一筆明細)與 `round_num` 0-7、`reason` 20 字。原本 `allowance_date`
|
|
56
|
+
為 nil 會在組 XML 時炸成 `NoMethodError`
|
|
57
|
+
- `InvoiceItem` 依 Table 2/16 驗證 `code`/`name`/`quantity`/`unit_price` 必填(`unit` 選填)
|
|
58
|
+
- `CancelAllowance` 驗證 Table 18 的兩個必填欄位與 20 字上限
|
|
59
|
+
- `InvoiceData` 補上規格寫死的條件與範圍:`zero_reason` 限 `tax_type` 2/5、
|
|
60
|
+
`mail_send` 限 `donate_mark: 0`、`round_num` 0-7、`remark` 200 字
|
|
61
|
+
|
|
62
|
+
### Changed
|
|
63
|
+
- **Breaking:** `CancelInvoice` 的 `remark:`(作廢原因)改為必填且限 20 字 —— Table 9 是
|
|
64
|
+
必填欄位,預設 `'退貨'` 等於幫呼叫端編造理由
|
|
65
|
+
- **Breaking:** 移除 `Services::InvoiceService`,它只剩一次 Savon 呼叫;`CreateInvoice`
|
|
66
|
+
直接送出
|
|
67
|
+
- `ResponseHandler` 失敗時丟 `ResultError` 本身,與折讓路徑一致。
|
|
68
|
+
`ResponseHandler::InvalidResponseError` 改為 `ResultError` 的別名常數(deprecated),
|
|
69
|
+
既有 `rescue InvalidResponseError` 仍然攔得到
|
|
70
|
+
- 新增 `Cetustek::Xml` 與 `Cetustek::Soap`,收掉五份 Savon client 與四份 `raw_tag` 複製;
|
|
71
|
+
Table 7/10/17/19 共用的代碼列集中在 `ResultCode::COMMON` 與 `ResultCode::DETAILS`。
|
|
72
|
+
`Cetustek::Queries` 成為 `Cetustek::Soap` 的別名(deprecated)
|
|
73
|
+
|
|
8
74
|
## [0.8.0] - 2026-08-04
|
|
9
75
|
|
|
10
76
|
### Added
|
data/README.md
CHANGED
|
@@ -66,8 +66,8 @@ invoice_data = Cetustek::Models::InvoiceData.new(
|
|
|
66
66
|
buyer_name: invoice.name,
|
|
67
67
|
buyer_email: invoice.email,
|
|
68
68
|
donate_mark: Cetustek::DonateMark::CARRIER,
|
|
69
|
-
carrier_type:
|
|
70
|
-
carrier_id: invoice.barcode, # CarrierId2
|
|
69
|
+
carrier_type: Cetustek::CarrierType::MOBILE_BARCODE, # 3J0002
|
|
70
|
+
carrier_id: invoice.barcode, # 手機條碼無顯隱碼之分,CarrierId2 自動填相同值
|
|
71
71
|
payment_type: Cetustek::PayWay::ATM,
|
|
72
72
|
items: invoice.items.map { |item|
|
|
73
73
|
Cetustek::Models::InvoiceItem.new(
|
|
@@ -96,9 +96,28 @@ Persisting the result is the caller's job — this gem writes to no database.
|
|
|
96
96
|
so a guaranteed rejection never leaves your process:
|
|
97
97
|
|
|
98
98
|
- `order_id`, `order_date` (a `Date`/`Time`), `items`, `donate_mark` and `payment_type` are required
|
|
99
|
-
-
|
|
99
|
+
- every item needs `code`, `name`, `quantity` and `unit_price` (`unit` is optional)
|
|
100
|
+
- `donate_mark: 0` (載具) requires `buyer_email`, `carrier_id1` and `carrier_id2`
|
|
100
101
|
- `donate_mark: 1` (捐贈) requires a 3–7 digit `npo_ban`
|
|
101
102
|
- `tax_type: 4` (特種稅率) requires an explicit `tax_rate` and `invoice_type: '08'`
|
|
103
|
+
- `zero_reason` only with `tax_type` 2 or 5; `mail_send` only with `donate_mark: 0`
|
|
104
|
+
- `round_num` 0–7, `remark` at most 200 characters
|
|
105
|
+
|
|
106
|
+
### Carriers (載具)
|
|
107
|
+
|
|
108
|
+
`Cetustek::CarrierType` names the codes the spec spells out:
|
|
109
|
+
|
|
110
|
+
| Constant | Code | Meaning |
|
|
111
|
+
|----------|------|---------|
|
|
112
|
+
| `MOBILE_BARCODE` | `3J0002` | 手機條碼 |
|
|
113
|
+
| `CITIZEN_CERT` | `CQ0001` | 自然人憑證條碼 |
|
|
114
|
+
| `CETUSTEK_CARD` | `EJ0011` | 鯨躍發票卡 |
|
|
115
|
+
|
|
116
|
+
手機條碼與自然人憑證「無顯碼隱碼區分」, so passing `carrier_id` (or `carrier_id1`)
|
|
117
|
+
alone is enough and `CarrierId2` is filled with the same value. Member carriers
|
|
118
|
+
do carry two different codes, so both `carrier_id1` and `carrier_id2` must be
|
|
119
|
+
given — the gem will not guess a 隱碼 it cannot know. 鯨躍發票卡 may be requested
|
|
120
|
+
with a blank `carrier_type`, as the spec allows.
|
|
102
121
|
|
|
103
122
|
Anything needing an external lookup (是否為有效手機條碼、捐贈碼是否存在) is left
|
|
104
123
|
to the caller — see `Cetustek::PhoneBarcode` below.
|
|
@@ -119,7 +138,7 @@ general invoice type of `07`. Use `Cetustek::TaxType` to switch modes:
|
|
|
119
138
|
| `TAX_FREE` | 3 | 免稅 |
|
|
120
139
|
| `SPECIAL` | 4 | 應稅(特種稅率) — set `tax_rate`, use `invoice_type: '08'` |
|
|
121
140
|
| `ZERO_RATE_CUSTOMS` | 5 | 零稅率(經海關出口) |
|
|
122
|
-
| `MIXED` | 9 | 混合(
|
|
141
|
+
| `MIXED` | 9 | 混合(應稅/零稅率/免稅) |
|
|
123
142
|
|
|
124
143
|
#### Zero-rate invoice (零稅率)
|
|
125
144
|
|
|
@@ -185,13 +204,18 @@ Cetustek::Models::InvoiceItem.new(code: 'DISCOUNT', name: '折抵', quantity: 1,
|
|
|
185
204
|
### Cancel an Invoice (作廢發票確認)
|
|
186
205
|
|
|
187
206
|
```ruby
|
|
188
|
-
|
|
189
|
-
Cetustek::CancelInvoice.new('AB12345678', 2024, remark: '
|
|
207
|
+
# remark 是 Table 9 的必填作廢原因,最多 20 字,沒有預設值
|
|
208
|
+
Cetustek::CancelInvoice.new('AB12345678', 2024, remark: '退貨').execute # => "C0"
|
|
190
209
|
|
|
191
210
|
# 超過申報期間才需要專案作廢核准文號 (否則會收到 C3)
|
|
192
|
-
Cetustek::CancelInvoice.new('AB12345678', 2024,
|
|
211
|
+
Cetustek::CancelInvoice.new('AB12345678', 2024, remark: '明細錯誤',
|
|
212
|
+
return_tax_document_number: '65327645').execute
|
|
193
213
|
```
|
|
194
214
|
|
|
215
|
+
`invoice_number` and `invoice_year` are Table 9's other two `必填` fields, and
|
|
216
|
+
raise `ArgumentError` if left blank — the same guaranteed-rejection guarantee
|
|
217
|
+
`InvoiceData`/`AllowanceData` give for their own required fields.
|
|
218
|
+
|
|
195
219
|
Uploading is not the end of it: the cancellation still has to be confirmed
|
|
196
220
|
manually on the 鯨躍 platform before the invoice counts as void. Any code other
|
|
197
221
|
than `"C0"` raises `Cetustek::ResultError` (`C5 - 該發票已經作廢過`, …), and
|
|
@@ -199,10 +223,35 @@ marking your own record as canceled is the caller's job.
|
|
|
199
223
|
|
|
200
224
|
### Query invoices
|
|
201
225
|
|
|
226
|
+
Each query class has both a raw `.query` (the Savon response, for callers who
|
|
227
|
+
want the untouched SOAP object) and a `.find` that parses the same response
|
|
228
|
+
into a plain Ruby value, mirroring `QueryAllowance` below:
|
|
229
|
+
|
|
202
230
|
```ruby
|
|
203
|
-
Cetustek::QueryInvoiceByOrderId.query(order_id) #
|
|
204
|
-
Cetustek::
|
|
205
|
-
Cetustek::
|
|
231
|
+
Cetustek::QueryInvoiceByOrderId.query(order_id) # raw Savon response
|
|
232
|
+
Cetustek::QueryInvoiceByOrderId.find(order_id) # parsed Hash, or nil
|
|
233
|
+
Cetustek::QueryInvoice.query(invoice_number, invoice_year) # raw Savon response
|
|
234
|
+
Cetustek::QueryInvoice.find(invoice_number, invoice_year) # parsed Hash, or nil
|
|
235
|
+
Cetustek::QueryInvoiceNumberByOrderId.query(order_id) # raw Savon response
|
|
236
|
+
Cetustek::QueryInvoiceNumberByOrderId.find(order_id) # bare invoice number String, or nil
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
`.find` returns `nil` when the platform answers with nothing at all or with
|
|
240
|
+
the documented `"nodata"`; a non-XML answer (a result code) is raised as
|
|
241
|
+
`Cetustek::ResultError`, same as `QueryAllowance.find`.
|
|
242
|
+
|
|
243
|
+
`QueryInvoice.find`/`QueryInvoiceByOrderId.find` (both share Table 13's XML
|
|
244
|
+
shape) return the fields as snake_case symbols, with `:seller`/`:buyer`
|
|
245
|
+
sub-hashes and the line items under `:details`:
|
|
246
|
+
|
|
247
|
+
```ruby
|
|
248
|
+
{ order_id: '44556655', invoice_number: 'AA00000027', invoice_date: '2011/05/19',
|
|
249
|
+
invoice_time: '19:10:49', invoice_status: '開立', donate_mark: '0',
|
|
250
|
+
sales_amount: '120', tax_amount: '6', total_amount: '126', ctk_url: nil,
|
|
251
|
+
seller: { identifier: '53118823', name: '鯨躍科技有限公司', ... },
|
|
252
|
+
buyer: { identifier: '55669988', name: '漢客料理店', ... },
|
|
253
|
+
details: [{ product_code: 'AA783457', description: '筆記本(綠色)', quantity: '1',
|
|
254
|
+
unit: nil, unit_price: '60', amount: '60', sequence_number: '1' }] }
|
|
206
255
|
```
|
|
207
256
|
|
|
208
257
|
### Tax-inclusive vs tax-exclusive prices (`hastax`)
|
|
@@ -223,7 +272,7 @@ Cetustek::Models::InvoiceData.new(hastax: 0, items: [...])
|
|
|
223
272
|
| `remark` | `Remark` | 備註,200 字 |
|
|
224
273
|
| `zero_reason` | `ZeroReason` | 零稅率原因;未填時平台預設 `72`(TaxType 2)或 `71`(TaxType 5) |
|
|
225
274
|
| `round_num` | `RoundNum` | 金額計算位數,未填預設 4 |
|
|
226
|
-
| `mail_send` | `MailSend` | `0`(預設)由加值中心寄送通知,`1`
|
|
275
|
+
| `mail_send` | `MailSend` | `0`(預設)由加值中心寄送通知,`1` 自行處理;限 `donate_mark: 0` |
|
|
227
276
|
| `rtn_msg` | `RtnMsg` | 預設 `'Json'`;傳 `nil` 退回只回傳 15 碼字串的舊模式 |
|
|
228
277
|
|
|
229
278
|
Fields with a platform-side default (`ZeroReason`, `RoundNum`, `MailSend`,
|
|
@@ -248,13 +297,19 @@ allowance = Cetustek::Models::AllowanceData.new(
|
|
|
248
297
|
)
|
|
249
298
|
Cetustek::CreateAllowance.new(allowance).execute # => "A0" on success
|
|
250
299
|
Cetustek::CancelAllowance.new('AA20240216000001', '明細錯誤').execute # => "C0" on success
|
|
251
|
-
Cetustek::QueryAllowance.find('AA20240216000001') # parsed Hash
|
|
300
|
+
Cetustek::QueryAllowance.find('AA20240216000001') # parsed Hash
|
|
252
301
|
Cetustek::QueryAllowance.query('AA20240216000001') # raw Savon response
|
|
253
302
|
```
|
|
254
303
|
|
|
255
|
-
`
|
|
256
|
-
|
|
257
|
-
|
|
304
|
+
`AllowanceData.new` raises `ArgumentError` for the Table 15 rules: `allowance_number`,
|
|
305
|
+
`allowance_date` (a `Date`/`Time`), `invoice_number`, `invoice_year`, `reason` (20 字)
|
|
306
|
+
and at least one item are required, and `round_num` must be 0–7. `tax_type` only
|
|
307
|
+
accepts `1` 應稅, `2` 零稅率 or `3` 免稅 — the invoice-only values (`4`, `5`, `9`)
|
|
308
|
+
raise. `unit_price` is **tax-inclusive** (there is no `hastax` on allowances).
|
|
309
|
+
`CancelAllowance` takes the two Table 18 fields, both required, 作廢原因 up to 20 字.
|
|
310
|
+
|
|
311
|
+
`QueryAllowance.find` returns `nil` only when the platform answers with nothing at
|
|
312
|
+
all; a non-XML answer is a result code and is raised as `ResultError`.
|
|
258
313
|
|
|
259
314
|
Any other result code raises `Cetustek::ResultError`, whose `#code` is the raw
|
|
260
315
|
code and whose message includes the documented reason (e.g. `A2 - 所有折讓金額加總
|
|
@@ -1,66 +1,44 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'ox'
|
|
4
|
-
require 'cgi'
|
|
5
|
-
|
|
6
3
|
module Cetustek
|
|
7
4
|
# 2.10 CancelAllowance 作廢折讓單. Returns "C0" on success, raises ResultError otherwise.
|
|
8
5
|
class CancelAllowance
|
|
6
|
+
include Soap
|
|
7
|
+
|
|
9
8
|
SUCCESS_CODE = 'C0'
|
|
9
|
+
MAX_REASON_LENGTH = 20
|
|
10
10
|
|
|
11
11
|
# Spec AVM-26-03 Table 19.
|
|
12
|
-
RESULT_MESSAGES =
|
|
13
|
-
'M:' => '欄位未填或格式錯誤',
|
|
14
|
-
'M1' => 'XML 格式錯誤',
|
|
12
|
+
RESULT_MESSAGES = ResultCode::COMMON.merge(
|
|
15
13
|
'C1' => '上傳失敗',
|
|
16
14
|
'C2' => '折讓單已申報,無法作廢',
|
|
17
15
|
'C3' => '折讓單號不存在或須為已確認後的折讓單',
|
|
18
16
|
'C4' => '該作廢折讓單已經上傳',
|
|
19
|
-
'C5' => '折讓單已過作廢期限,無法作廢'
|
|
20
|
-
|
|
21
|
-
}.freeze
|
|
17
|
+
'C5' => '折讓單已過作廢期限,無法作廢'
|
|
18
|
+
).freeze
|
|
22
19
|
|
|
20
|
+
# Table 18 has only these two fields, both 必填.
|
|
23
21
|
def initialize(allowance_number, reason)
|
|
22
|
+
raise ArgumentError, 'allowance_number is required' if allowance_number.to_s.strip.empty?
|
|
23
|
+
raise ArgumentError, 'reason (作廢原因) is required' if reason.to_s.strip.empty?
|
|
24
|
+
raise ArgumentError, "reason must not exceed #{MAX_REASON_LENGTH} characters" if reason.length > MAX_REASON_LENGTH
|
|
25
|
+
|
|
24
26
|
@allowance_number = allowance_number
|
|
25
27
|
@reason = reason
|
|
26
28
|
end
|
|
27
29
|
|
|
28
30
|
def execute
|
|
29
|
-
|
|
30
|
-
ResultCode.check!(
|
|
31
|
-
RESULT_MESSAGES, success: SUCCESS_CODE)
|
|
31
|
+
response = soap_call(:cancel_allowance, allowancexml: generate_xml)
|
|
32
|
+
ResultCode.check!(soap_return(response, :cancel_allowance), RESULT_MESSAGES, success: SUCCESS_CODE)
|
|
32
33
|
end
|
|
33
34
|
|
|
34
35
|
private
|
|
35
36
|
|
|
36
|
-
def perform
|
|
37
|
-
client = Savon.client(wsdl: Cetustek.config.url, open_timeout: 300, read_timeout: 300)
|
|
38
|
-
@response = client.call(:cancel_allowance, message: {
|
|
39
|
-
allowancexml: generate_xml,
|
|
40
|
-
source: Cetustek.config.site_id + Cetustek.config.password,
|
|
41
|
-
rentid: Cetustek.config.username
|
|
42
|
-
})
|
|
43
|
-
end
|
|
44
|
-
|
|
45
37
|
def generate_xml
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
doc << instruct
|
|
51
|
-
|
|
52
|
-
allowance = Ox::Element.new('Allowance')
|
|
53
|
-
allowance[:XSDVersion] = '2.8'
|
|
54
|
-
doc << allowance
|
|
55
|
-
|
|
56
|
-
allowance << raw_tag('AllowanceNumber', @allowance_number)
|
|
57
|
-
allowance << raw_tag('Reason', @reason)
|
|
58
|
-
|
|
59
|
-
Ox.dump(doc).force_encoding('UTF-8')
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def raw_tag(name, value)
|
|
63
|
-
Ox::Raw.new("<#{name}>#{CGI.escapeHTML(value.to_s)}</#{name}>")
|
|
38
|
+
Xml.document('Allowance') do |allowance|
|
|
39
|
+
Xml.append(allowance, 'AllowanceNumber', @allowance_number)
|
|
40
|
+
Xml.append(allowance, 'Reason', @reason)
|
|
41
|
+
end
|
|
64
42
|
end
|
|
65
43
|
end
|
|
66
44
|
end
|
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'ox'
|
|
4
|
-
require 'cgi'
|
|
5
|
-
|
|
6
3
|
module Cetustek
|
|
7
4
|
# 2.2 CancelInvoice 作廢發票確認. Returns "C0" on success, raises ResultError
|
|
8
5
|
# otherwise. Uploading is not the end of it — the cancellation still has to be
|
|
9
6
|
# confirmed manually on the 鯨躍 platform before the invoice counts as void.
|
|
10
7
|
class CancelInvoice
|
|
8
|
+
include Soap
|
|
9
|
+
|
|
11
10
|
SUCCESS_CODE = 'C0'
|
|
11
|
+
MAX_REMARK_LENGTH = 20
|
|
12
12
|
|
|
13
13
|
# Spec AVM-26-03 Table 10.
|
|
14
|
-
RESULT_MESSAGES =
|
|
15
|
-
'M:' => '欄位未填或格式錯誤',
|
|
16
|
-
'M0' => 'XML 格式錯誤',
|
|
17
|
-
'M1' => 'XML 格式錯誤',
|
|
14
|
+
RESULT_MESSAGES = ResultCode::COMMON.merge(
|
|
18
15
|
'C1' => '資料庫發生錯誤',
|
|
19
16
|
'C2' => '資料有誤',
|
|
20
17
|
'C3' => '該發票已過申報期間,需填入核准作廢文號 (return_tax_document_number)',
|
|
21
18
|
'C4' => '無此發票號碼可以作廢',
|
|
22
19
|
'C5' => '該發票已經作廢過',
|
|
23
|
-
'C6' => '作廢資訊已經傳送過'
|
|
24
|
-
|
|
25
|
-
}.freeze
|
|
20
|
+
'C6' => '作廢資訊已經傳送過'
|
|
21
|
+
).freeze
|
|
26
22
|
|
|
27
|
-
|
|
23
|
+
# remark 是 Table 9 的必填作廢原因,沒有預設值可用:理由是業務決定的。
|
|
24
|
+
def initialize(invoice_number, invoice_year, remark:, return_tax_document_number: nil)
|
|
25
|
+
raise ArgumentError, 'invoice_number is required' if invoice_number.to_s.strip.empty?
|
|
26
|
+
raise ArgumentError, 'invoice_year is required' if invoice_year.to_s.strip.empty?
|
|
28
27
|
raise ArgumentError, 'remark (作廢原因) is required' if remark.to_s.strip.empty?
|
|
28
|
+
raise ArgumentError, "remark must not exceed #{MAX_REMARK_LENGTH} characters" if remark.length > MAX_REMARK_LENGTH
|
|
29
29
|
|
|
30
30
|
@invoice_number = invoice_number
|
|
31
31
|
@invoice_year = invoice_year
|
|
@@ -34,44 +34,20 @@ module Cetustek
|
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
def execute
|
|
37
|
-
|
|
38
|
-
ResultCode.check!(
|
|
39
|
-
RESULT_MESSAGES, success: SUCCESS_CODE)
|
|
37
|
+
response = soap_call(:cancel_invoice, invoicexml: generate_xml)
|
|
38
|
+
ResultCode.check!(soap_return(response, :cancel_invoice), RESULT_MESSAGES, success: SUCCESS_CODE)
|
|
40
39
|
end
|
|
41
40
|
|
|
42
41
|
private
|
|
43
42
|
|
|
44
|
-
def perform
|
|
45
|
-
client = Savon.client(wsdl: Cetustek.config.url, open_timeout: 300, read_timeout: 300)
|
|
46
|
-
@response = client.call(:cancel_invoice, message: {
|
|
47
|
-
invoicexml: generate_xml,
|
|
48
|
-
source: Cetustek.config.site_id + Cetustek.config.password,
|
|
49
|
-
rentid: Cetustek.config.username
|
|
50
|
-
})
|
|
51
|
-
end
|
|
52
|
-
|
|
53
43
|
def generate_xml
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
invoice[:XSDVersion] = '2.8'
|
|
62
|
-
doc << invoice
|
|
63
|
-
|
|
64
|
-
invoice << raw_tag('InvoiceNumber', @invoice_number)
|
|
65
|
-
invoice << raw_tag('InvoiceYear', @invoice_year)
|
|
66
|
-
# 專案作廢核准文號:只在超過申報期間作廢時才需要 (Table 10 的 C3)。
|
|
67
|
-
invoice << raw_tag('ReturnTaxDocumentNumber', @return_tax_document_number) if @return_tax_document_number
|
|
68
|
-
invoice << raw_tag('Remark', @remark)
|
|
69
|
-
|
|
70
|
-
Ox.dump(doc).force_encoding('UTF-8')
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
def raw_tag(name, value)
|
|
74
|
-
Ox::Raw.new("<#{name}>#{CGI.escapeHTML(value.to_s)}</#{name}>")
|
|
44
|
+
Xml.document('Invoice') do |invoice|
|
|
45
|
+
Xml.append(invoice, 'InvoiceNumber', @invoice_number)
|
|
46
|
+
Xml.append(invoice, 'InvoiceYear', @invoice_year)
|
|
47
|
+
# 專案作廢核准文號:只在超過申報期間作廢時才需要 (Table 10 的 C3)。
|
|
48
|
+
Xml.append(invoice, 'ReturnTaxDocumentNumber', @return_tax_document_number, skip_nil: true)
|
|
49
|
+
Xml.append(invoice, 'Remark', @remark)
|
|
50
|
+
end
|
|
75
51
|
end
|
|
76
52
|
end
|
|
77
53
|
end
|
|
@@ -1,34 +1,22 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'ox'
|
|
4
|
-
require 'cgi'
|
|
5
|
-
|
|
6
3
|
module Cetustek
|
|
7
4
|
# 2.9 CreateAllowance 開立折讓單. Returns "A0" on success, raises ResultError otherwise.
|
|
8
5
|
class CreateAllowance
|
|
6
|
+
include Soap
|
|
7
|
+
|
|
9
8
|
SUCCESS_CODE = 'A0'
|
|
10
9
|
|
|
11
10
|
# Spec AVM-26-03 Table 17.
|
|
12
|
-
RESULT_MESSAGES =
|
|
13
|
-
'M:' => '欄位未填或格式錯誤',
|
|
14
|
-
'M1' => 'XML 格式錯誤',
|
|
15
|
-
'D0' => '沒有產品明細',
|
|
16
|
-
'D0_' => '產品編號格式錯誤',
|
|
17
|
-
'D1_' => '品名未填或格式錯誤',
|
|
18
|
-
'D2_' => '數量未填或格式錯誤',
|
|
19
|
-
'D3_' => '單價未填或格式錯誤',
|
|
20
|
-
'D4_' => '單位格式錯誤',
|
|
21
|
-
'D5_' => '數量*單價,其小計整數位大於 13 位',
|
|
22
|
-
'D999' => '明細筆數最多 9999 筆',
|
|
11
|
+
RESULT_MESSAGES = ResultCode::COMMON.merge(ResultCode::DETAILS).merge(
|
|
23
12
|
'A1' => '上傳失敗',
|
|
24
13
|
'A2' => '所有折讓金額加總不能大於原發票金額',
|
|
25
14
|
'A3' => '發票號碼不存在',
|
|
26
15
|
'A4' => '發票號碼已經被作廢',
|
|
27
16
|
'A5' => '折讓單已經上傳',
|
|
28
17
|
'A6' => '折讓總金額須大於零',
|
|
29
|
-
'A7' => '折讓日期應大於原發票開立日期'
|
|
30
|
-
|
|
31
|
-
}.freeze
|
|
18
|
+
'A7' => '折讓日期應大於原發票開立日期'
|
|
19
|
+
).freeze
|
|
32
20
|
|
|
33
21
|
# check_allowance 只剩 0(已確認的折讓單):規格 §2.9 已刪除 1,
|
|
34
22
|
# 114/01/01 起上傳的折讓單皆為已確認。
|
|
@@ -40,64 +28,39 @@ module Cetustek
|
|
|
40
28
|
end
|
|
41
29
|
|
|
42
30
|
def execute
|
|
43
|
-
|
|
44
|
-
ResultCode.check!(
|
|
45
|
-
RESULT_MESSAGES, success: SUCCESS_CODE)
|
|
31
|
+
response = soap_call(:create_allowance, allowancexml: generate_xml, checkallowance: @check_allowance)
|
|
32
|
+
ResultCode.check!(soap_return(response, :create_allowance), RESULT_MESSAGES, success: SUCCESS_CODE)
|
|
46
33
|
end
|
|
47
34
|
|
|
48
35
|
private
|
|
49
36
|
|
|
50
|
-
def perform
|
|
51
|
-
client = Savon.client(wsdl: Cetustek.config.url, open_timeout: 300, read_timeout: 300)
|
|
52
|
-
@response = client.call(:create_allowance, message: {
|
|
53
|
-
allowancexml: generate_xml,
|
|
54
|
-
checkallowance: @check_allowance,
|
|
55
|
-
source: Cetustek.config.site_id + Cetustek.config.password,
|
|
56
|
-
rentid: Cetustek.config.username
|
|
57
|
-
})
|
|
58
|
-
end
|
|
59
|
-
|
|
60
37
|
def generate_xml
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
allowance << raw_tag('InvoiceNumber', @data.invoice_number)
|
|
74
|
-
allowance << raw_tag('InvoiceYear', @data.invoice_year)
|
|
75
|
-
allowance << raw_tag('BuyerAddress', @data.buyer_address)
|
|
76
|
-
allowance << raw_tag('BuyerEmailAddress', @data.buyer_email)
|
|
77
|
-
allowance << raw_tag('TaxType', @data.tax_type)
|
|
78
|
-
allowance << raw_tag('Reason', @data.reason)
|
|
79
|
-
allowance << raw_tag('RoundNum', @data.round_num) unless @data.round_num.nil?
|
|
80
|
-
allowance << build_details
|
|
81
|
-
|
|
82
|
-
Ox.dump(doc).force_encoding('UTF-8')
|
|
38
|
+
Xml.document('Allowance') do |allowance|
|
|
39
|
+
Xml.append(allowance, 'AllowanceNumber', @data.allowance_number)
|
|
40
|
+
Xml.append(allowance, 'AllowanceDate', @data.allowance_date.strftime('%Y/%m/%d'))
|
|
41
|
+
Xml.append(allowance, 'InvoiceNumber', @data.invoice_number)
|
|
42
|
+
Xml.append(allowance, 'InvoiceYear', @data.invoice_year)
|
|
43
|
+
Xml.append(allowance, 'BuyerAddress', @data.buyer_address)
|
|
44
|
+
Xml.append(allowance, 'BuyerEmailAddress', @data.buyer_email)
|
|
45
|
+
Xml.append(allowance, 'TaxType', @data.tax_type)
|
|
46
|
+
Xml.append(allowance, 'Reason', @data.reason)
|
|
47
|
+
Xml.append(allowance, 'RoundNum', @data.round_num, skip_nil: true)
|
|
48
|
+
allowance << build_details
|
|
49
|
+
end
|
|
83
50
|
end
|
|
84
51
|
|
|
85
52
|
def build_details
|
|
86
53
|
details = Ox::Element.new('Details')
|
|
87
54
|
@data.items.each do |item|
|
|
88
55
|
product = Ox::Element.new('ProductItem')
|
|
89
|
-
product
|
|
90
|
-
product
|
|
91
|
-
product
|
|
92
|
-
product
|
|
93
|
-
product
|
|
56
|
+
Xml.append(product, 'ProductionCode', item.code)
|
|
57
|
+
Xml.append(product, 'Description', item.name)
|
|
58
|
+
Xml.append(product, 'Quantity', item.quantity)
|
|
59
|
+
Xml.append(product, 'Unit', item.unit)
|
|
60
|
+
Xml.append(product, 'UnitPrice', item.unit_price)
|
|
94
61
|
details << product
|
|
95
62
|
end
|
|
96
63
|
details
|
|
97
64
|
end
|
|
98
|
-
|
|
99
|
-
def raw_tag(name, value)
|
|
100
|
-
Ox::Raw.new("<#{name}>#{CGI.escapeHTML(value.to_s)}</#{name}>")
|
|
101
|
-
end
|
|
102
65
|
end
|
|
103
66
|
end
|
|
@@ -2,20 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative 'models/invoice_data'
|
|
4
4
|
require_relative 'services/invoice_xml_builder'
|
|
5
|
-
require_relative 'services/invoice_service'
|
|
6
5
|
require_relative 'services/response_handler'
|
|
7
6
|
|
|
8
7
|
module Cetustek
|
|
9
8
|
# 2.1 CreateInvoiceV3 開立發票. Returns the issued invoice on success and
|
|
10
9
|
# raises ResultError otherwise. Persisting the result is the caller's job.
|
|
11
10
|
class CreateInvoice
|
|
11
|
+
include Soap
|
|
12
|
+
|
|
12
13
|
def initialize(invoice_data)
|
|
13
14
|
@invoice_data = invoice_data
|
|
14
15
|
end
|
|
15
16
|
|
|
16
17
|
def execute
|
|
17
18
|
xml = Services::InvoiceXmlBuilder.new(@invoice_data).build
|
|
18
|
-
|
|
19
|
+
# hastax: 0 = 明細單價未稅, 1 = 含稅; comes from the order, not a constant.
|
|
20
|
+
response = soap_call(:create_invoice_v3, invoicexml: xml, hastax: @invoice_data.hastax)
|
|
19
21
|
Services::ResponseHandler.new(response, @invoice_data, xml).process
|
|
20
22
|
end
|
|
21
23
|
end
|
data/lib/cetustek/errors.rb
CHANGED
|
@@ -14,6 +14,26 @@ module Cetustek
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
module ResultCode
|
|
17
|
+
# Rows that are identical across the spec's result tables (7/10/17/19).
|
|
18
|
+
COMMON = {
|
|
19
|
+
'M:' => '欄位未填或格式錯誤',
|
|
20
|
+
'M0' => 'XML 格式錯誤',
|
|
21
|
+
'M1' => 'XML 格式錯誤',
|
|
22
|
+
'Invalid' => '無效 IP,請通知系統商'
|
|
23
|
+
}.freeze
|
|
24
|
+
|
|
25
|
+
# Detail-line rows, identical between Table 7 (發票) and Table 17 (折讓).
|
|
26
|
+
DETAILS = {
|
|
27
|
+
'D0' => '沒有產品明細',
|
|
28
|
+
'D0_' => '產品編號格式錯誤',
|
|
29
|
+
'D1_' => '品名未填或格式錯誤',
|
|
30
|
+
'D2_' => '數量未填或格式錯誤',
|
|
31
|
+
'D3_' => '單價未填或格式錯誤',
|
|
32
|
+
'D4_' => '單位格式錯誤',
|
|
33
|
+
'D5_' => '數量*單價,其小計整數位大於 13 位',
|
|
34
|
+
'D999' => '明細筆數最多 9999 筆'
|
|
35
|
+
}.freeze
|
|
36
|
+
|
|
17
37
|
# Codes may carry a suffix naming the offending field or detail row
|
|
18
38
|
# ("M:AllowanceDate", "D2_3"), so an exact miss falls back to the table key
|
|
19
39
|
# without the suffix.
|
|
@@ -25,6 +45,10 @@ module Cetustek
|
|
|
25
45
|
def self.check!(code, messages, success:)
|
|
26
46
|
return code if code == success
|
|
27
47
|
|
|
48
|
+
raise!(code, messages)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.raise!(code, messages)
|
|
28
52
|
raise ResultError.new(code, describe(code, messages))
|
|
29
53
|
end
|
|
30
54
|
end
|