secretariat 3.3.0 → 3.5.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/README.md +1 -1
- data/lib/secretariat/constants.rb +1 -1
- data/lib/secretariat/invoice.rb +36 -13
- data/lib/secretariat/line_item.rb +19 -13
- data/lib/secretariat/tax.rb +5 -0
- data/lib/secretariat/trade_party.rb +12 -3
- data/lib/secretariat/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 101180b4e01b5fab64b9362975b172d91ecebf1926f22d9128339f8c1599084e
|
4
|
+
data.tar.gz: '0795397ea1963f7f8de510bafc929b5590ff52586b1f96b0a8e3b77ec01b1867'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f21836d1199fd2d1bcf36eb7bda5c2bfcdfc73154e0281200002d5b19bd30ef07c4b5d7b18a400f978637ada150fe7a95efec9623e71267dbbe3cfc7d230d6c9
|
7
|
+
data.tar.gz: d5e574f2a06c3cf7f929b045a58c60867e7986a2f451484aee5ac4cf7185cdae36fc1fd0890a941ef2c330362544043d93a5561a906d09a32d55104c6c10ef83
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@ See tests for examples.
|
|
7
7
|
1. This is an opinionated library optimised for my very specific usecase
|
8
8
|
2. While I did start to add some validations to make sure you can't input absolute garbage into this, I cannot guarantee factual (as in taxation law) correctness of the resulting XML.
|
9
9
|
1. The library, for ZUGFeRD 2.x, currently only supports the EN16931 variant. This is probably what you want as well. PRs welcome.
|
10
|
-
3. This does not contain any code to attach the XML to a PDF file, mainly because I have yet to find a ruby library to do that. For software that does this, take a look at [this python library](https://github.com/akretion/factur-x).
|
10
|
+
3. This does not contain any code to attach the XML to a PDF file, mainly because I have yet to find a ruby library to do that. For software that does this, take a look at [this python library](https://github.com/akretion/factur-x) or [this Java library which also does extended validation](https://mustangproject.org)
|
11
11
|
|
12
12
|
## Contributors
|
13
13
|
|
@@ -66,7 +66,7 @@ module Secretariat
|
|
66
66
|
|
67
67
|
# For the background of vertical and horizontal tax calculation see https://hilfe.pacemo.de/de-form/articles/3489851-rundungsfehler-bei-rechnungen
|
68
68
|
# The idea of introducing an unknown value is that this could be inferred from the given invoice total and line items by probing both variants and selecting the matching one - or reporting a taxation error if neither matches.
|
69
|
-
TAX_CALCULATION_METHODS = %i[HORIZONTAL VERTICAL UNKNOWN].freeze
|
69
|
+
TAX_CALCULATION_METHODS = %i[HORIZONTAL VERTICAL NONE UNKNOWN].freeze
|
70
70
|
|
71
71
|
UNIT_CODES = {
|
72
72
|
:PIECE => "C62",
|
data/lib/secretariat/invoice.rb
CHANGED
@@ -65,11 +65,26 @@ module Secretariat
|
|
65
65
|
|
66
66
|
def taxes
|
67
67
|
taxes = {}
|
68
|
+
# Shortcut for cases where invoices only have one tax and the calculation is off by a cent because of rounding errors
|
69
|
+
# (This can happen if the VAT and the net amount is calculated backwards from a round gross amount)
|
70
|
+
if tax_calculation_method == :NONE
|
71
|
+
tax = Tax.new(tax_percent: BigDecimal(tax_percent || BigDecimal(0)), tax_category: tax_category)
|
72
|
+
tax.base_amount = BigDecimal(basis_amount)
|
73
|
+
tax.tax_amount = BigDecimal(tax_amount || 0)
|
74
|
+
return [tax]
|
75
|
+
end
|
76
|
+
|
68
77
|
line_items.each do |line_item|
|
69
|
-
|
70
|
-
|
71
|
-
|
78
|
+
if line_item.tax_percent.nil?
|
79
|
+
taxes['0'] = Tax.new(tax_percent: BigDecimal(0), tax_category: line_item.tax_category, tax_amount: BigDecimal(0)) if taxes['0'].nil?
|
80
|
+
taxes['0'].base_amount += BigDecimal(line_item.net_amount) * line_item.quantity
|
81
|
+
else
|
82
|
+
taxes[line_item.tax_percent] = Tax.new(tax_percent: BigDecimal(line_item.tax_percent), tax_category: line_item.tax_category) if taxes[line_item.tax_percent].nil?
|
83
|
+
taxes[line_item.tax_percent].tax_amount += BigDecimal(line_item.tax_amount)
|
84
|
+
taxes[line_item.tax_percent].base_amount += BigDecimal(line_item.net_amount) * line_item.quantity
|
85
|
+
end
|
72
86
|
end
|
87
|
+
|
73
88
|
if tax_calculation_method == :VERTICAL
|
74
89
|
taxes.values.map do |tax|
|
75
90
|
tax.tax_amount = (tax.base_amount * tax.tax_percent / 100).round(2)
|
@@ -98,12 +113,19 @@ module Secretariat
|
|
98
113
|
@errors << "Base amount and summed tax base amount deviate: #{basis} / #{summed_tax_base_amount}"
|
99
114
|
return false
|
100
115
|
end
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
116
|
+
if tax_calculation_method == :ITEM_BASED
|
117
|
+
line_items_tax_amount = line_items.sum(&:tax_amount)
|
118
|
+
if tax_amount != line_items_tax_amount
|
119
|
+
@errors << "Tax amount #{tax_amount} and summed up item tax amounts #{line_items_tax_amount} deviate"
|
120
|
+
end
|
121
|
+
elsif tax_calculation_method != :NONE
|
122
|
+
taxes.each do |tax|
|
123
|
+
calc_tax = tax.base_amount * BigDecimal(tax.tax_percent) / BigDecimal(100)
|
124
|
+
calc_tax = calc_tax.round(2)
|
125
|
+
if tax.tax_amount != calc_tax
|
126
|
+
@errors << "Tax amount and calculated tax amount deviate for rate #{tax.tax_percent}: #{tax.tax_amount} / #{calc_tax}"
|
127
|
+
return false
|
128
|
+
end
|
107
129
|
end
|
108
130
|
end
|
109
131
|
grand_total = BigDecimal(grand_total_amount)
|
@@ -150,7 +172,7 @@ module Secretariat
|
|
150
172
|
raise ValidationError.new("Invoice is invalid", errors)
|
151
173
|
end
|
152
174
|
|
153
|
-
builder = Nokogiri::XML::Builder.new do |xml|
|
175
|
+
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
154
176
|
|
155
177
|
root = by_version(version, 'CrossIndustryDocument', 'CrossIndustryInvoice')
|
156
178
|
|
@@ -250,9 +272,10 @@ module Secretariat
|
|
250
272
|
end
|
251
273
|
Helpers.currency_element(xml, 'ram', 'BasisAmount', tax.base_amount, currency_code, add_currency: version == 1)
|
252
274
|
xml['ram'].CategoryCode tax_category_code(tax, version: version)
|
253
|
-
|
254
|
-
|
255
|
-
|
275
|
+
# unless tax.untaxable?
|
276
|
+
percent = by_version(version, 'ApplicablePercent', 'RateApplicablePercent')
|
277
|
+
xml['ram'].send(percent, Helpers.format(tax.tax_percent))
|
278
|
+
# end
|
256
279
|
end
|
257
280
|
end
|
258
281
|
if version == 2 && service_period_start && service_period_end
|
@@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
|
|
14
14
|
limitations under the License.
|
15
15
|
=end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
17
|
require 'bigdecimal'
|
20
18
|
module Secretariat
|
21
19
|
|
@@ -63,13 +61,15 @@ module Secretariat
|
|
63
61
|
return false
|
64
62
|
end
|
65
63
|
end
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
64
|
+
if tax_category != :UNTAXEDSERVICE
|
65
|
+
self.tax_percent ||= BigDecimal(0)
|
66
|
+
calculated_tax = charge_price * BigDecimal(tax_percent) / BigDecimal(100)
|
67
|
+
calculated_tax = calculated_tax.round(2)
|
68
|
+
calculated_tax = -calculated_tax if quantity.negative?
|
69
|
+
if calculated_tax != tax
|
70
|
+
@errors << "Tax and calculated tax deviate: #{tax} / #{calculated_tax}"
|
71
|
+
return false
|
72
|
+
end
|
73
73
|
end
|
74
74
|
return true
|
75
75
|
end
|
@@ -85,11 +85,17 @@ module Secretariat
|
|
85
85
|
TAX_CATEGORY_CODES[tax_category] || 'S'
|
86
86
|
end
|
87
87
|
|
88
|
+
def untaxable?
|
89
|
+
tax_category == :UNTAXEDSERVICE
|
90
|
+
end
|
91
|
+
|
88
92
|
def to_xml(xml, line_item_index, version: 2, validate: true)
|
89
93
|
net_price = net_amount && BigDecimal(net_amount)
|
90
94
|
gross_price = gross_amount && BigDecimal(gross_amount)
|
91
95
|
charge_price = charge_amount && BigDecimal(charge_amount)
|
92
96
|
|
97
|
+
self.tax_percent ||= BigDecimal(0)
|
98
|
+
|
93
99
|
if net_price&.zero?
|
94
100
|
self.tax_percent = 0
|
95
101
|
end
|
@@ -170,10 +176,10 @@ module Secretariat
|
|
170
176
|
xml['ram'].ApplicableTradeTax do
|
171
177
|
xml['ram'].TypeCode 'VAT'
|
172
178
|
xml['ram'].CategoryCode tax_category_code(version: version)
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
179
|
+
unless untaxable?
|
180
|
+
percent = by_version(version, 'ApplicablePercent', 'RateApplicablePercent')
|
181
|
+
xml['ram'].send(percent,Helpers.format(tax_percent))
|
182
|
+
end
|
177
183
|
end
|
178
184
|
monetary_summation = by_version(version, 'SpecifiedTradeSettlementMonetarySummation', 'SpecifiedTradeSettlementLineMonetarySummation')
|
179
185
|
xml['ram'].send(monetary_summation) do
|
data/lib/secretariat/tax.rb
CHANGED
@@ -16,10 +16,15 @@ limitations under the License.
|
|
16
16
|
|
17
17
|
module Secretariat
|
18
18
|
TradeParty = Struct.new('TradeParty',
|
19
|
-
:name, :street1, :street2, :city, :postal_code, :country_id, :vat_id,
|
19
|
+
:name, :street1, :street2, :city, :postal_code, :country_id, :vat_id, :global_id, :global_id_scheme_id, :tax_id,
|
20
20
|
keyword_init: true,
|
21
21
|
) do
|
22
22
|
def to_xml(xml, exclude_tax: false, version: 2)
|
23
|
+
if global_id && global_id != '' && global_id_scheme_id && global_id_scheme_id != ''
|
24
|
+
xml['ram'].GlobalID(schemeID: global_id_scheme_id) do
|
25
|
+
xml.text(global_id)
|
26
|
+
end
|
27
|
+
end
|
23
28
|
xml['ram'].Name name
|
24
29
|
xml['ram'].PostalTradeAddress do
|
25
30
|
xml['ram'].PostcodeCode postal_code
|
@@ -36,9 +41,13 @@ module Secretariat
|
|
36
41
|
xml.text(vat_id)
|
37
42
|
end
|
38
43
|
end
|
44
|
+
elsif tax_id && tax_id != ''
|
45
|
+
xml['ram'].SpecifiedTaxRegistration do
|
46
|
+
xml['ram'].ID(schemeID: 'FC') do
|
47
|
+
xml.text(tax_id)
|
48
|
+
end
|
49
|
+
end
|
39
50
|
end
|
40
51
|
end
|
41
52
|
end
|
42
|
-
|
43
|
-
|
44
53
|
end
|
data/lib/secretariat/version.rb
CHANGED