einvoice 2.0.0.alpha2 → 2.0.0.beta1
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/.github/workflows/build.yml +1 -1
- data/.github/workflows/cron.yml +33 -0
- data/CHANGELOG.md +20 -0
- data/README.md +150 -84
- data/README.zh-TW.md +140 -81
- data/einvoice.gemspec +3 -2
- data/lib/einvoice/amego/payload.rb +4 -10
- data/lib/einvoice/amego/provider.rb +17 -12
- data/lib/einvoice/ecpay/client.rb +4 -3
- data/lib/einvoice/ecpay/payload.rb +9 -9
- data/lib/einvoice/ecpay/provider.rb +26 -17
- data/lib/einvoice/ezpay/payload.rb +3 -3
- data/lib/einvoice/ezpay/provider.rb +31 -13
- data/lib/einvoice/input.rb +66 -13
- data/lib/einvoice/mock_provider.rb +1 -1
- data/lib/einvoice/number.rb +40 -0
- data/lib/einvoice/tax_split.rb +27 -39
- data/lib/einvoice/tradevan/default_api/payload.rb +3 -3
- data/lib/einvoice/tradevan/default_api/provider.rb +11 -24
- data/lib/einvoice/types.rb +48 -6
- data/lib/einvoice/version.rb +1 -1
- metadata +4 -3
|
@@ -31,6 +31,8 @@ module Einvoice
|
|
|
31
31
|
|
|
32
32
|
# ECPay ItemTaxType → unified 課稅別, for reading a queried invoice back.
|
|
33
33
|
TAX_CODE_TO_TYPE = Payload::TAX_TYPE.invert.freeze
|
|
34
|
+
# IIS_Tax_Type in a GetIssue result. 9 is mixed.
|
|
35
|
+
INVOICE_TAX_TYPE = TAX_CODE_TO_TYPE.merge(Payload::MIXED_TAX_CODE => TaxType::MIXED).freeze
|
|
34
36
|
# The only rate ECPay applies to a taxable line.
|
|
35
37
|
TAXABLE_RATE = Rational(5, 100)
|
|
36
38
|
|
|
@@ -82,7 +84,7 @@ module Einvoice
|
|
|
82
84
|
order_id: parsed.order_id,
|
|
83
85
|
total_amount: parsed.amount.total_amount,
|
|
84
86
|
buyer_ubn: parsed.buyer&.ubn,
|
|
85
|
-
tax_type:
|
|
87
|
+
tax_type: parsed.resolved_tax_type,
|
|
86
88
|
tax_rate: parsed.tax_rate,
|
|
87
89
|
status: InvoiceStatus::ISSUED,
|
|
88
90
|
raw: result
|
|
@@ -209,18 +211,20 @@ module Einvoice
|
|
|
209
211
|
|
|
210
212
|
# ECPay fixes the rate by TaxType. A taxable line is always 5%.
|
|
211
213
|
def assert_rate_supported!(parsed)
|
|
212
|
-
taxable = parsed.
|
|
214
|
+
taxable = parsed.item_tax_types.include?(TaxType::TAXABLE)
|
|
213
215
|
return unless taxable && parsed.tax_rate != TAXABLE_RATE
|
|
214
216
|
|
|
215
217
|
raise UnsupportedError.new("ECPay taxes a taxable invoice at 0.05 only, got #{parsed.tax_rate.to_f}",
|
|
216
218
|
provider: name)
|
|
217
219
|
end
|
|
218
220
|
|
|
221
|
+
# GetIssue always sends both totals [doc 7923] [live], so a missing one is
|
|
222
|
+
# an error. So is a value that is not a whole number.
|
|
219
223
|
def build_query_result(result, reference)
|
|
220
|
-
total = result
|
|
221
|
-
tax = result
|
|
222
|
-
|
|
223
|
-
|
|
224
|
+
total = amount!(result, "IIS_Sales_Amount")
|
|
225
|
+
tax = amount!(result, "IIS_Tax_Amount")
|
|
226
|
+
tax_type = INVOICE_TAX_TYPE[result["IIS_Tax_Type"].to_s]
|
|
227
|
+
items = TaxType.fill_items(build_items(result["Items"]), tax_type)
|
|
224
228
|
|
|
225
229
|
QueryInvoiceResult.new(
|
|
226
230
|
invoice_number: result["IIS_Number"] || reference.number.to_s,
|
|
@@ -239,8 +243,9 @@ module Einvoice
|
|
|
239
243
|
end
|
|
240
244
|
|
|
241
245
|
# ECPay fixes the rate of a taxable, zero-rated or tax-free invoice. A
|
|
242
|
-
#
|
|
243
|
-
#
|
|
246
|
+
# mixed-tax invoice always has a taxable line [doc 7896]. A special-tax
|
|
247
|
+
# invoice has its own rate, which GetIssue returns. A rate that is not a
|
|
248
|
+
# MIG rate reads as nil, so an allowance has to give one.
|
|
244
249
|
def query_rate(tax_type, reported)
|
|
245
250
|
case tax_type
|
|
246
251
|
when TaxType::TAXABLE, TaxType::MIXED then TAXABLE_RATE
|
|
@@ -269,9 +274,9 @@ module Einvoice
|
|
|
269
274
|
Array(rows).map do |row|
|
|
270
275
|
InvoiceItem.new(
|
|
271
276
|
description: row["ItemName"].to_s,
|
|
272
|
-
quantity:
|
|
273
|
-
unit_price:
|
|
274
|
-
amount:
|
|
277
|
+
quantity: item_number(row, "ItemCount"),
|
|
278
|
+
unit_price: item_number(row, "ItemPrice"),
|
|
279
|
+
amount: item_number(row, "ItemAmount"),
|
|
275
280
|
unit: presence(row["ItemWord"]),
|
|
276
281
|
tax_type: TAX_CODE_TO_TYPE[row["ItemTaxType"].to_s],
|
|
277
282
|
remark: presence(row["ItemRemark"])
|
|
@@ -287,17 +292,21 @@ module Einvoice
|
|
|
287
292
|
return nil if result["IIS_Issue_Status"].to_s == "0"
|
|
288
293
|
return InvoiceStatus::VOIDED if result["IIS_Invalid_Status"].to_s == "1"
|
|
289
294
|
|
|
290
|
-
sales = result
|
|
291
|
-
remaining = result
|
|
295
|
+
sales = amount!(result, "IIS_Sales_Amount")
|
|
296
|
+
remaining = amount!(result, "IIS_Remain_Allowance_Amt")
|
|
292
297
|
return InvoiceStatus::ALLOWANCE if sales.positive? && remaining < sales
|
|
293
298
|
|
|
294
299
|
InvoiceStatus::ISSUED
|
|
295
300
|
end
|
|
296
301
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
302
|
+
def amount!(result, field)
|
|
303
|
+
Number.response_integer!(result[field], field: field, provider: name, raw: result)
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
# ECPay returns item numbers as JSON numbers, such as 2.0. A missing one
|
|
307
|
+
# is nil, not 0.
|
|
308
|
+
def item_number(row, field)
|
|
309
|
+
Number.response_plain(row[field], field: "Items #{field}", provider: name, raw: row)
|
|
301
310
|
end
|
|
302
311
|
|
|
303
312
|
def presence(value, placeholder: nil)
|
|
@@ -37,11 +37,11 @@ module Einvoice
|
|
|
37
37
|
def issue(parsed)
|
|
38
38
|
b2b = parsed.resolved_category == InvoiceCategory::B2B
|
|
39
39
|
options = string_keys(parsed.provider_options)
|
|
40
|
-
types = parsed.
|
|
40
|
+
types = parsed.item_tax_types
|
|
41
41
|
mixed = types.uniq.size > 1
|
|
42
42
|
check_issue!(parsed, b2b, types, mixed)
|
|
43
43
|
|
|
44
|
-
breakdown = parsed.tax_breakdown
|
|
44
|
+
breakdown = parsed.tax_breakdown
|
|
45
45
|
|
|
46
46
|
fields = {
|
|
47
47
|
"MerchantOrderNo" => parsed.order_id.to_s,
|
|
@@ -77,7 +77,7 @@ module Einvoice
|
|
|
77
77
|
def allowance(parsed, original)
|
|
78
78
|
options = string_keys(parsed.provider_options)
|
|
79
79
|
check_rate!(parsed.tax_rate)
|
|
80
|
-
breakdown = parsed.tax_breakdown
|
|
80
|
+
breakdown = parsed.tax_breakdown
|
|
81
81
|
refuse_special!(breakdown.lines.map(&:tax_type))
|
|
82
82
|
|
|
83
83
|
fields = { "InvoiceNo" => parsed.invoice.number.to_s, "MerchantOrderNo" => original[:order_id].to_s }
|
|
@@ -80,9 +80,9 @@ module Einvoice
|
|
|
80
80
|
invoice_datetime: taipei_time(result["CreateTime"]),
|
|
81
81
|
random_code: result["RandomNum"].to_s,
|
|
82
82
|
order_id: result["MerchantOrderNo"].to_s,
|
|
83
|
-
total_amount: result
|
|
83
|
+
total_amount: amount(result, "TotalAmt"),
|
|
84
84
|
buyer_ubn: parsed.buyer&.ubn,
|
|
85
|
-
tax_type:
|
|
85
|
+
tax_type: parsed.resolved_tax_type,
|
|
86
86
|
tax_rate: parsed.tax_rate,
|
|
87
87
|
status: InvoiceStatus::ISSUED,
|
|
88
88
|
raw: result
|
|
@@ -108,7 +108,7 @@ module Einvoice
|
|
|
108
108
|
allowance_number: result["AllowanceNo"].to_s,
|
|
109
109
|
invoice_number: result.fetch("InvoiceNumber", parsed.invoice.number).to_s,
|
|
110
110
|
allowance_date: Taipei.today,
|
|
111
|
-
total_amount: result
|
|
111
|
+
total_amount: amount(result, "AllowanceAmt"),
|
|
112
112
|
raw: result
|
|
113
113
|
)
|
|
114
114
|
end
|
|
@@ -165,19 +165,20 @@ module Einvoice
|
|
|
165
165
|
end
|
|
166
166
|
|
|
167
167
|
def build_query_result(result)
|
|
168
|
+
tax_type = TAX_TYPE[result["TaxType"].to_s]
|
|
169
|
+
items = TaxType.fill_items(item_detail(result["ItemDetail"]).map { |row| build_item(row) }, tax_type)
|
|
168
170
|
QueryInvoiceResult.new(
|
|
169
171
|
invoice_number: result["InvoiceNumber"].to_s,
|
|
170
172
|
invoice_datetime: taipei_time(result["CreateTime"]),
|
|
171
173
|
random_code: result["RandomNum"].to_s,
|
|
172
174
|
order_id: presence(result["MerchantOrderNo"]),
|
|
173
175
|
status: STATUS[result["InvoiceStatus"].to_s],
|
|
174
|
-
amount:
|
|
175
|
-
total_amount: result["TotalAmt"].to_i),
|
|
176
|
+
amount: query_amount(result),
|
|
176
177
|
buyer: Buyer.new(name: presence(result["BuyerName"]), ubn: presence(result["BuyerUBN"]),
|
|
177
178
|
email: presence(result["BuyerEmail"]), address: presence(result["BuyerAddress"]),
|
|
178
179
|
phone: presence(result["BuyerPhone"])),
|
|
179
|
-
items:
|
|
180
|
-
tax_type:
|
|
180
|
+
items: items,
|
|
181
|
+
tax_type: tax_type,
|
|
181
182
|
tax_rate: query_rate(result["TaxRate"]),
|
|
182
183
|
raw: result
|
|
183
184
|
)
|
|
@@ -206,18 +207,35 @@ module Einvoice
|
|
|
206
207
|
def build_item(row)
|
|
207
208
|
InvoiceItem.new(
|
|
208
209
|
description: row["ItemName"].to_s,
|
|
209
|
-
quantity:
|
|
210
|
-
unit_price:
|
|
211
|
-
amount:
|
|
210
|
+
quantity: item_number(row, "ItemCount"),
|
|
211
|
+
unit_price: item_number(row, "ItemPrice"),
|
|
212
|
+
amount: item_number(row, "ItemAmount"),
|
|
212
213
|
unit: presence(row["ItemWord"]),
|
|
213
214
|
tax_type: Payload::TAX_TYPE.invert[row["ItemTaxType"].to_s],
|
|
214
215
|
remark: nil
|
|
215
216
|
)
|
|
216
217
|
end
|
|
217
218
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
219
|
+
# A total as a whole number. Nobody has run the adapter against ezPay,
|
|
220
|
+
# so no live response shows if a total can be missing. A missing one
|
|
221
|
+
# reads as 0, as before. A value that is not a whole number is an error.
|
|
222
|
+
def amount(result, field)
|
|
223
|
+
Number.response_integer!(result[field], field: field, provider: name, raw: result, if_missing: 0)
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# ezPay reads the three totals apart, so they can disagree. A result
|
|
227
|
+
# where sales and tax do not add up to the total is not a MIG invoice.
|
|
228
|
+
def query_amount(result)
|
|
229
|
+
sales, tax, total = %w[Amt TaxAmt TotalAmt].map { |field| amount(result, field) }
|
|
230
|
+
return AmountSummary.new(sales_amount: sales, tax_amount: tax, total_amount: total) if sales + tax == total
|
|
231
|
+
|
|
232
|
+
raise ProviderError.new("the ezpay response has Amt #{sales} and TaxAmt #{tax}, " \
|
|
233
|
+
"which do not add up to TotalAmt #{total}", provider: name, raw: result)
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# A missing item number is nil, not 0.
|
|
237
|
+
def item_number(row, field)
|
|
238
|
+
Number.response_plain(row[field], field: "ItemDetail #{field}", provider: name, raw: row)
|
|
221
239
|
end
|
|
222
240
|
|
|
223
241
|
def presence(value)
|
data/lib/einvoice/input.rb
CHANGED
|
@@ -43,7 +43,12 @@ module Einvoice
|
|
|
43
43
|
# The rates the MIG allows [mig 表 4-18].
|
|
44
44
|
TAX_RATES = [0, Rational(1, 100), Rational(2, 100), Rational(5, 100), Rational(15, 100),
|
|
45
45
|
Rational(25, 100)].freeze
|
|
46
|
-
|
|
46
|
+
# The general rate (一般稅額) is 5% to 10% [營業稅法第 10 條], and 0.05 is
|
|
47
|
+
# the only MIG rate in that range. The other MIG rates are for special tax
|
|
48
|
+
# (特種稅額) [第 11 至 13 條]. If the general rate changes, change it here.
|
|
49
|
+
# Each adapter keeps its own check, because a provider may stay at 5%.
|
|
50
|
+
TAXABLE_RATES = [Rational(5, 100)].freeze
|
|
51
|
+
STANDARD_TAX_RATE = TAXABLE_RATES.first
|
|
47
52
|
# A reference or an allowance can describe a mixed-tax invoice.
|
|
48
53
|
REFERENCE_TAX_TYPES = (TaxType::ALL + [TaxType::MIXED]).freeze
|
|
49
54
|
|
|
@@ -58,6 +63,8 @@ module Einvoice
|
|
|
58
63
|
category = h[:category] && enum!(h[:category], InvoiceCategory::ALL, :category, provider)
|
|
59
64
|
check_category!(category, buyer, provider)
|
|
60
65
|
check_consumer_name!(category, buyer, provider)
|
|
66
|
+
tax_rate = tax_rate!(h[:tax_rate], items, tax_type, provider)
|
|
67
|
+
check_tax_rate!(tax_rate, items, tax_type, provider)
|
|
61
68
|
parsed = IssueInvoiceInput.new(
|
|
62
69
|
order_id: fetch!(h, :order_id, provider),
|
|
63
70
|
buyer: buyer,
|
|
@@ -65,7 +72,7 @@ module Einvoice
|
|
|
65
72
|
amount: amount,
|
|
66
73
|
tax_type: tax_type,
|
|
67
74
|
price_mode: enum!(h[:price_mode], PriceMode::ALL, :price_mode, provider),
|
|
68
|
-
tax_rate: tax_rate
|
|
75
|
+
tax_rate: tax_rate,
|
|
69
76
|
category: category,
|
|
70
77
|
carrier: build_carrier(h[:carrier], provider),
|
|
71
78
|
donation: build_donation(h[:donation], provider),
|
|
@@ -106,10 +113,13 @@ module Einvoice
|
|
|
106
113
|
fail!("tax_type is required, or invoice.tax_type in the reference", provider) if tax_type.nil?
|
|
107
114
|
|
|
108
115
|
items = build_items(h[:items], provider)
|
|
116
|
+
check_item_tax_types!(items, tax_type, provider)
|
|
109
117
|
check_not_negative!(items, provider)
|
|
118
|
+
check_whole_amounts!(items, provider)
|
|
110
119
|
tax_rate = h[:tax_rate].nil? ? invoice.tax_rate : mig_rate!(h[:tax_rate], "tax_rate", provider)
|
|
111
|
-
tax_rate ||= 0r if items.none? { |
|
|
120
|
+
tax_rate ||= 0r if TaxType.per_item(items, tax_type).none? { |type| TaxType::TAXED.include?(type) }
|
|
112
121
|
fail!("tax_rate is required, or invoice.tax_rate in the reference", provider) if tax_rate.nil?
|
|
122
|
+
check_tax_rate!(tax_rate, items, tax_type, provider)
|
|
113
123
|
|
|
114
124
|
parsed = AllowanceInput.new(
|
|
115
125
|
invoice: invoice,
|
|
@@ -174,7 +184,7 @@ module Einvoice
|
|
|
174
184
|
# Either way the tax is round(taxable sales × tax_rate), so the same input
|
|
175
185
|
# gets the same tax at every provider.
|
|
176
186
|
def check_issue_amount!(input, provider)
|
|
177
|
-
breakdown = input.tax_breakdown
|
|
187
|
+
breakdown = input.tax_breakdown
|
|
178
188
|
amount = input.amount
|
|
179
189
|
check_total!(amount, breakdown, provider)
|
|
180
190
|
if input.resolved_category == InvoiceCategory::B2B
|
|
@@ -189,7 +199,7 @@ module Einvoice
|
|
|
189
199
|
# the same rules that check a given one: F0401 for an invoice, G0401 for an
|
|
190
200
|
# allowance.
|
|
191
201
|
def issue_amount(input, provider)
|
|
192
|
-
breakdown = input.tax_breakdown
|
|
202
|
+
breakdown = input.tax_breakdown
|
|
193
203
|
total = breakdown.total_amount
|
|
194
204
|
tax = input.resolved_category == InvoiceCategory::B2B ? breakdown.tax_amount : 0
|
|
195
205
|
totals = { sales_amount: total - tax, tax_amount: tax, total_amount: total }
|
|
@@ -202,14 +212,14 @@ module Einvoice
|
|
|
202
212
|
end
|
|
203
213
|
|
|
204
214
|
def allowance_amount(input, provider)
|
|
205
|
-
breakdown = input.tax_breakdown
|
|
215
|
+
breakdown = input.tax_breakdown
|
|
206
216
|
AmountSummary.new(sales_amount: breakdown.total_amount - breakdown.tax_amount,
|
|
207
217
|
tax_amount: breakdown.tax_amount, total_amount: breakdown.total_amount)
|
|
208
218
|
end
|
|
209
219
|
|
|
210
220
|
# An allowance follows G0401: sales_amount excludes tax.
|
|
211
221
|
def check_allowance_amount!(input, provider)
|
|
212
|
-
breakdown = input.tax_breakdown
|
|
222
|
+
breakdown = input.tax_breakdown
|
|
213
223
|
check_total!(input.amount, breakdown, provider)
|
|
214
224
|
check_tax!(input.amount, breakdown, input.tax_rate, provider)
|
|
215
225
|
end
|
|
@@ -361,6 +371,28 @@ module Einvoice
|
|
|
361
371
|
|
|
362
372
|
# --- checks ----------------------------------------------------------------
|
|
363
373
|
|
|
374
|
+
# Only an allowance can have a MIXED tax type, from the reference of a
|
|
375
|
+
# mixed-tax invoice. MIXED is not a type of a line, so each item then needs
|
|
376
|
+
# its own.
|
|
377
|
+
def check_item_tax_types!(items, tax_type, provider)
|
|
378
|
+
return unless tax_type == TaxType::MIXED
|
|
379
|
+
|
|
380
|
+
index = items.index { |item| item.tax_type.nil? }
|
|
381
|
+
fail!("items[#{index}].tax_type is required when the invoice has mixed tax", provider) if index
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
# G0401 wants each item amount without tax in whole TWD [mig 表 18-5]. A
|
|
385
|
+
# line tax is whole TWD too, so the amount the caller gives has to be whole,
|
|
386
|
+
# with or without tax. Quantity and unit price may still have decimals.
|
|
387
|
+
def check_whole_amounts!(items, provider)
|
|
388
|
+
items.each_with_index do |item, index|
|
|
389
|
+
next if Number.decimal!(item.amount, field: "items[#{index}].amount", provider: provider).denominator == 1
|
|
390
|
+
|
|
391
|
+
fail!("items[#{index}].amount must be a whole number on an allowance (MIG G0401), got #{item.amount}",
|
|
392
|
+
provider)
|
|
393
|
+
end
|
|
394
|
+
end
|
|
395
|
+
|
|
364
396
|
# MIG 4.1 does not allow a negative amount on an allowance. An invoice may
|
|
365
397
|
# have a negative discount line, so build_items does not check this.
|
|
366
398
|
def check_not_negative!(items, provider)
|
|
@@ -404,12 +436,28 @@ module Einvoice
|
|
|
404
436
|
def tax_rate!(value, items, tax_type, provider)
|
|
405
437
|
return mig_rate!(value, "tax_rate", provider) unless value.nil?
|
|
406
438
|
|
|
407
|
-
types = items
|
|
439
|
+
types = TaxType.per_item(items, tax_type)
|
|
408
440
|
return nil if types.include?(TaxType::SPECIAL)
|
|
409
441
|
|
|
410
442
|
types.include?(TaxType::TAXABLE) ? STANDARD_TAX_RATE : 0r
|
|
411
443
|
end
|
|
412
444
|
|
|
445
|
+
# A taxed line needs a rate above 0. At rate 0 it would file taxable sales
|
|
446
|
+
# as untaxed. A taxable line takes the general rate only. Lines that are
|
|
447
|
+
# not taxed do not use the rate.
|
|
448
|
+
def check_tax_rate!(rate, items, tax_type, provider)
|
|
449
|
+
types = TaxType.per_item(items, tax_type)
|
|
450
|
+
taxed = types.find { |type| TaxType::TAXED.include?(type) }
|
|
451
|
+
return if taxed.nil?
|
|
452
|
+
|
|
453
|
+
fail!("tax_rate is required for a #{taxed} line", provider) if rate.nil?
|
|
454
|
+
fail!("tax_rate must be above 0 when a line is taxable", provider) if rate.zero?
|
|
455
|
+
return unless types.include?(TaxType::TAXABLE) && !TAXABLE_RATES.include?(rate)
|
|
456
|
+
|
|
457
|
+
fail!("a taxable line takes the general rate #{TAXABLE_RATES.map { |r| Number.plain(r) }.join(', ')}, " \
|
|
458
|
+
"got #{Number.plain(rate)}. The other rates are for special tax", provider)
|
|
459
|
+
end
|
|
460
|
+
|
|
413
461
|
def mig_rate!(value, field, provider)
|
|
414
462
|
rate = Number.decimal!(value, field: field, provider: provider)
|
|
415
463
|
unless TAX_RATES.include?(rate)
|
|
@@ -428,13 +476,18 @@ module Einvoice
|
|
|
428
476
|
code
|
|
429
477
|
end
|
|
430
478
|
|
|
431
|
-
# The MIG requires CustomsClearanceMark on a
|
|
432
|
-
#
|
|
433
|
-
#
|
|
479
|
+
# The MIG requires CustomsClearanceMark and ZeroTaxRateReason on a
|
|
480
|
+
# zero-rated invoice [mig 表 15-3]. ECPay can take the reason from its back
|
|
481
|
+
# office, but the reason goes to the MOF in the end, so the caller gives it.
|
|
434
482
|
def check_zero_rated!(parsed, provider)
|
|
435
|
-
return unless parsed.zero_rated?
|
|
483
|
+
return unless parsed.zero_rated?
|
|
484
|
+
|
|
485
|
+
if parsed.customs_clearance_mark.nil?
|
|
486
|
+
fail!("a zero-rated invoice needs customs_clearance_mark (1 or 2)", provider)
|
|
487
|
+
end
|
|
488
|
+
return unless parsed.zero_tax_rate_reason.nil?
|
|
436
489
|
|
|
437
|
-
fail!("a zero-rated invoice needs
|
|
490
|
+
fail!("a zero-rated invoice needs zero_tax_rate_reason (71 to 79)", provider)
|
|
438
491
|
end
|
|
439
492
|
|
|
440
493
|
def reason!(reason, provider)
|
|
@@ -50,7 +50,7 @@ module Einvoice
|
|
|
50
50
|
parsed = Input.issue(input, provider: name)
|
|
51
51
|
assert_currency_supported!(parsed.currency)
|
|
52
52
|
assert_supports!(Capability::B2B) if parsed.resolved_category == InvoiceCategory::B2B
|
|
53
|
-
tax_type =
|
|
53
|
+
tax_type = parsed.resolved_tax_type
|
|
54
54
|
assert_supports!(Capability::MIXED_TAX) if tax_type == TaxType::MIXED
|
|
55
55
|
fail!(ConflictError, "Order id already issued", Reason::DUPLICATE_ORDER) if @by_order_id.key?(parsed.order_id)
|
|
56
56
|
|
data/lib/einvoice/number.rb
CHANGED
|
@@ -6,6 +6,9 @@ module Einvoice
|
|
|
6
6
|
module Number
|
|
7
7
|
DECIMAL_PLACES = 7
|
|
8
8
|
DECIMAL = /\A-?\d+(\.\d+)?\z/
|
|
9
|
+
# The default of +if_missing+: the value is required.
|
|
10
|
+
REQUIRED = Object.new.freeze
|
|
11
|
+
private_constant :REQUIRED
|
|
9
12
|
|
|
10
13
|
module_function
|
|
11
14
|
|
|
@@ -44,6 +47,43 @@ module Einvoice
|
|
|
44
47
|
value.denominator == 1 ? value.to_i : value.to_f
|
|
45
48
|
end
|
|
46
49
|
|
|
50
|
+
# --- provider responses ----------------------------------------------------
|
|
51
|
+
#
|
|
52
|
+
# A number in a provider's response goes through the same parser as the
|
|
53
|
+
# input. It is the provider's data, not the caller's, so a value that is not
|
|
54
|
+
# a number raises ProviderError, with the response in +raw+. Nothing reads
|
|
55
|
+
# such a value as 0, and nothing cuts off its decimals.
|
|
56
|
+
|
|
57
|
+
# A Rational, or nil when the provider sent no value.
|
|
58
|
+
def response(value, field:, provider:, raw:)
|
|
59
|
+
return nil if value.nil? || (value.is_a?(String) && value.strip.empty?)
|
|
60
|
+
|
|
61
|
+
rational = to_rational(value)
|
|
62
|
+
return rational unless rational.nil?
|
|
63
|
+
|
|
64
|
+
raise ProviderError.new("the #{provider} response has a #{field} that is not a number: #{value.inspect}",
|
|
65
|
+
provider: provider, raw: raw)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# #response as an Integer or a Float (#plain), or nil when it is missing.
|
|
69
|
+
# For the numbers of an item, which may have decimals.
|
|
70
|
+
def response_plain(value, field:, provider:, raw:)
|
|
71
|
+
rational = response(value, field: field, provider: provider, raw: raw)
|
|
72
|
+
rational && plain(rational)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# A whole number, such as a total. The provider has to send it, unless
|
|
76
|
+
# +if_missing+ gives the value to use when it does not. Use that only for a
|
|
77
|
+
# field that no live response shows, and say why where it is used.
|
|
78
|
+
def response_integer!(value, field:, provider:, raw:, if_missing: REQUIRED)
|
|
79
|
+
rational = response(value, field: field, provider: provider, raw: raw)
|
|
80
|
+
return if_missing if rational.nil? && !REQUIRED.equal?(if_missing)
|
|
81
|
+
return rational.to_i if rational&.denominator == 1
|
|
82
|
+
|
|
83
|
+
problem = rational.nil? ? "has no #{field}" : "has a #{field} that is not a whole number: #{value.inspect}"
|
|
84
|
+
raise ProviderError.new("the #{provider} response #{problem}", provider: provider, raw: raw)
|
|
85
|
+
end
|
|
86
|
+
|
|
47
87
|
def invalid!(message, provider)
|
|
48
88
|
raise ValidationError.new(message, provider: provider || "einvoice")
|
|
49
89
|
end
|
data/lib/einvoice/tax_split.rb
CHANGED
|
@@ -14,13 +14,12 @@ module Einvoice
|
|
|
14
14
|
:tax_amount, :total_amount)
|
|
15
15
|
|
|
16
16
|
# One line of a TaxBreakdown. +net_amount+ excludes tax, +gross_amount+
|
|
17
|
-
# includes it, and +tax+ is whole TWD.
|
|
18
|
-
# +unit_price+, rounded to 7 decimal places (MIG 表 15-5).
|
|
19
|
-
# derived from the amounts, so +unit price × quantity+ can differ
|
|
20
|
-
# amount by the rounding of the line tax. Each provider note says
|
|
21
|
-
# its API allows.
|
|
22
|
-
TaxBreakdown::Line = Data.define(:tax_type, :net_amount, :tax, :gross_amount, :
|
|
23
|
-
:gross_unit_price)
|
|
17
|
+
# includes it, and +tax+ is whole TWD. +gross_unit_price+ comes from the
|
|
18
|
+
# item's +unit_price+, with tax, rounded to 7 decimal places (MIG 表 15-5).
|
|
19
|
+
# It is not derived from the amounts, so +unit price × quantity+ can differ
|
|
20
|
+
# from the amount by the rounding of the line tax. Each provider note says
|
|
21
|
+
# how much its API allows.
|
|
22
|
+
TaxBreakdown::Line = Data.define(:tax_type, :net_amount, :tax, :gross_amount, :gross_unit_price)
|
|
24
23
|
|
|
25
24
|
# Splits tax over lines by the largest remainder: each taxed line takes the
|
|
26
25
|
# whole part of its share, and the units left go to the lines with the
|
|
@@ -34,49 +33,42 @@ module Einvoice
|
|
|
34
33
|
|
|
35
34
|
module_function
|
|
36
35
|
|
|
37
|
-
# The TaxBreakdown of +items+.
|
|
38
|
-
#
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
invalid!("items[#{index}].tax_type is required when the invoice has mixed tax", provider)
|
|
44
|
-
end
|
|
45
|
-
type
|
|
46
|
-
end
|
|
36
|
+
# The TaxBreakdown of +items+. +types+ is the tax type of each item, in
|
|
37
|
+
# item order (see TaxType.per_item). Input has checked every value: each
|
|
38
|
+
# line has a tax type (an item of a MIXED invoice has its own), and each
|
|
39
|
+
# number is valid. The breakdown only reads the items' numbers as
|
|
40
|
+
# Rationals, because the items keep the caller's values as given.
|
|
41
|
+
def breakdown(items, types:, tax_rate:, price_mode:)
|
|
47
42
|
taxed = types.map { |type| TaxType::TAXED.include?(type) }
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
invalid!("tax_rate must be above 0 when a line is taxable", provider) if taxed.any? && rate.zero?
|
|
43
|
+
# Input has checked the rate: a taxed line has one, above 0. A line that
|
|
44
|
+
# is not taxed has rate 0. An input built with .new skips Input, and may
|
|
45
|
+
# leave the rate out when no line is taxed.
|
|
46
|
+
tax_rate ||= 0r if taxed.none?
|
|
47
|
+
rates = taxed.map { |line_taxed| line_taxed ? tax_rate : 0r }
|
|
54
48
|
inclusive = price_mode == PriceMode::TAX_INCLUSIVE
|
|
55
|
-
amounts = items.
|
|
56
|
-
Number.decimal!(item.amount, field: "items[#{index}].amount", provider: provider)
|
|
57
|
-
end
|
|
49
|
+
amounts = items.map { |item| Number.to_rational(item.amount) }
|
|
58
50
|
split = if inclusive
|
|
59
|
-
split(amounts, taxable: taxed, rate:
|
|
51
|
+
split(amounts, taxable: taxed, rate: tax_rate)
|
|
60
52
|
else
|
|
61
|
-
allocate(amounts, taxable: taxed, total_tax: (taxable_sum(amounts, taxed) *
|
|
53
|
+
allocate(amounts, taxable: taxed, total_tax: (taxable_sum(amounts, taxed) * tax_rate).round)
|
|
62
54
|
end
|
|
63
55
|
|
|
64
56
|
lines = items.each_with_index.map do |item, index|
|
|
65
|
-
price = Number.
|
|
66
|
-
|
|
67
|
-
gross_price = taxed[index] && !inclusive ? price * (1 + rate) : price
|
|
57
|
+
price = Number.to_rational(item.unit_price)
|
|
58
|
+
gross_price = inclusive ? price : price * (1 + rates[index])
|
|
68
59
|
line = split[index]
|
|
69
60
|
TaxBreakdown::Line.new(tax_type: types[index], net_amount: Number.plain(line.amount), tax: line.tax,
|
|
70
61
|
gross_amount: Number.plain(line.amount + line.tax),
|
|
71
|
-
net_unit_price: Number.plain(net_price.round(Number::DECIMAL_PLACES)),
|
|
72
62
|
gross_unit_price: Number.plain(gross_price.round(Number::DECIMAL_PLACES)))
|
|
73
63
|
end
|
|
74
|
-
totals(lines, split
|
|
64
|
+
totals(lines, split)
|
|
75
65
|
end
|
|
76
66
|
|
|
77
|
-
|
|
67
|
+
# The split keeps each amount exact, and the tax is whole TWD, so with
|
|
68
|
+
# tax-included items the total is still the rounded sum of the items.
|
|
69
|
+
def totals(lines, split)
|
|
78
70
|
tax = split.sum(&:tax)
|
|
79
|
-
sales =
|
|
71
|
+
sales = split.sum(0r, &:amount).round
|
|
80
72
|
# Sum the exact split, not the lines: net_amount may be a Float, and 0.35 +
|
|
81
73
|
# 0.15 as Floats rounds to 0, not 1.
|
|
82
74
|
untaxed = lambda do |type|
|
|
@@ -116,9 +108,5 @@ module Einvoice
|
|
|
116
108
|
(total - taxes.sum).times { |k| taxes[order[k]] += 1 }
|
|
117
109
|
taxes
|
|
118
110
|
end
|
|
119
|
-
|
|
120
|
-
def invalid!(message, provider)
|
|
121
|
-
raise ValidationError.new(message, provider: provider || "einvoice")
|
|
122
|
-
end
|
|
123
111
|
end
|
|
124
112
|
end
|
|
@@ -28,9 +28,9 @@ module Einvoice
|
|
|
28
28
|
# +issueData+ for type +I+ (a new invoice). +now+ is the time of this
|
|
29
29
|
# issue: Tradevan requires +transactionDate+ and +transactionTime+.
|
|
30
30
|
def issue(parsed, settings, now)
|
|
31
|
-
types = parsed.
|
|
31
|
+
types = parsed.item_tax_types
|
|
32
32
|
check_tax!(types, parsed.tax_rate)
|
|
33
|
-
breakdown = parsed.tax_breakdown
|
|
33
|
+
breakdown = parsed.tax_breakdown
|
|
34
34
|
check_total!(breakdown)
|
|
35
35
|
sale = sale_identifier(settings, parsed.order_id)
|
|
36
36
|
|
|
@@ -63,7 +63,7 @@ module Einvoice
|
|
|
63
63
|
# +issueData+ for type +A+ (a new allowance). +sale+ and +invoice_date+
|
|
64
64
|
# are the original invoice's (docs, section 4.1). +today+ is a Date.
|
|
65
65
|
def allowance(parsed, settings, sale:, invoice_date:, today:)
|
|
66
|
-
breakdown = parsed.tax_breakdown
|
|
66
|
+
breakdown = parsed.tax_breakdown
|
|
67
67
|
check_tax!(breakdown.lines.map(&:tax_type), parsed.tax_rate)
|
|
68
68
|
amount = parsed.amount
|
|
69
69
|
header(settings).merge(
|
|
@@ -90,7 +90,7 @@ module Einvoice
|
|
|
90
90
|
def issue(input)
|
|
91
91
|
parsed = Input.issue(input, provider: name)
|
|
92
92
|
assert_currency_supported!(parsed.currency)
|
|
93
|
-
tax_type =
|
|
93
|
+
tax_type = parsed.resolved_tax_type
|
|
94
94
|
assert_supports!(Capability::MIXED_TAX) if tax_type == TaxType::MIXED
|
|
95
95
|
data = Payload.issue(parsed, @settings, Taipei.now(@clock))
|
|
96
96
|
Rules.validate!(Rules.issue_errors(data), "issueData") if @validate_payload
|
|
@@ -138,7 +138,7 @@ module Einvoice
|
|
|
138
138
|
# the date is today in Taipei, the +transactionDate+ it sent (inferred).
|
|
139
139
|
def allowance(input)
|
|
140
140
|
parsed = Input.allowance(input, provider: name)
|
|
141
|
-
assert_supports!(Capability::MIXED_TAX) if
|
|
141
|
+
assert_supports!(Capability::MIXED_TAX) if parsed.resolved_tax_type == TaxType::MIXED
|
|
142
142
|
sale, invoice_date = original_invoice(parsed, need_date: true)
|
|
143
143
|
today = Taipei.today(@clock)
|
|
144
144
|
data = Payload.allowance(parsed, @settings, sale: sale, invoice_date: invoice_date, today: today)
|
|
@@ -305,32 +305,19 @@ module Einvoice
|
|
|
305
305
|
)
|
|
306
306
|
end
|
|
307
307
|
|
|
308
|
-
# DEFAULTAPI sends numbers as strings.
|
|
309
|
-
#
|
|
310
|
-
#
|
|
311
|
-
#
|
|
308
|
+
# DEFAULTAPI sends numbers as strings. A missing number is nil, not 0.
|
|
309
|
+
# A value that is not a decimal number, such as "unavailable" or
|
|
310
|
+
# "5abc", is an error: String#to_r would read it as 0 or 5, a number
|
|
311
|
+
# that Tradevan did not send.
|
|
312
312
|
def numeric(row, field)
|
|
313
|
-
|
|
314
|
-
return nil if value.empty?
|
|
315
|
-
|
|
316
|
-
rational = decimal!(value, "detailList #{field}", row)
|
|
317
|
-
rational.denominator == 1 ? rational.to_i : rational.to_f
|
|
313
|
+
Number.response_plain(row[field], field: "detailList #{field}", provider: name, raw: row)
|
|
318
314
|
end
|
|
319
315
|
|
|
320
|
-
# A header total as an Integer. A missing total is 0
|
|
321
|
-
#
|
|
316
|
+
# A header total as an Integer. A missing total is 0: no live response
|
|
317
|
+
# shows if Tradevan can leave one out (4.3). A total with decimals is an
|
|
318
|
+
# error, not cut off.
|
|
322
319
|
def total(result, field)
|
|
323
|
-
|
|
324
|
-
return 0 if value.empty?
|
|
325
|
-
|
|
326
|
-
decimal!(value, field, result).to_i
|
|
327
|
-
end
|
|
328
|
-
|
|
329
|
-
def decimal!(value, field, raw)
|
|
330
|
-
return value.to_r if value.match?(Rules::DECIMAL)
|
|
331
|
-
|
|
332
|
-
raise ProviderError.new("Tradevan returned a #{field} that is not a number: #{value.inspect}",
|
|
333
|
-
provider: name, raw: raw)
|
|
320
|
+
Number.response_integer!(result[field], field: field, provider: name, raw: result, if_missing: 0)
|
|
334
321
|
end
|
|
335
322
|
|
|
336
323
|
def string_keys(hash)
|