From bc618d03d5a4a4cb83c6e086b0fc044b8dcd2444 Mon Sep 17 00:00:00 2001 From: ahmedm10 Date: Tue, 20 Oct 2026 18:50:43 +0300 Subject: [PATCH] feat: make the line origin country optional OriginTradeCountry (EN 16931 BT-159) was always written, empty when no origin_country_code was given. It is optional, and a service has no country of origin, so it is now left out when the code is blank. --- lib/secretariat/line_item.rb | 7 +++++-- test/invoice_test.rb | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/secretariat/line_item.rb b/lib/secretariat/line_item.rb index 8f7a683..e10cfce 100644 --- a/lib/secretariat/line_item.rb +++ b/lib/secretariat/line_item.rb @@ -154,8 +154,11 @@ def to_xml(xml, line_item_index, version: 2, validate: true) if (version == 2) xml['ram'].SpecifiedTradeProduct do xml['ram'].Name name - xml['ram'].OriginTradeCountry do - xml['ram'].ID origin_country_code + # BT-159 is optional; it describes where goods were made, which a service doesn't have. + unless origin_country_code.to_s.empty? + xml['ram'].OriginTradeCountry do + xml['ram'].ID origin_country_code + end end end end diff --git a/test/invoice_test.rb b/test/invoice_test.rb index 8af2c76..7148ce8 100644 --- a/test/invoice_test.rb +++ b/test/invoice_test.rb @@ -861,6 +861,20 @@ def test_fr_invoice assert_match(%r{\s*304755032\s*}, xml) end + def test_origin_country_is_written_when_given + xml = make_de_invoice.to_xml(version: 2) + assert_match(%r{\s*DE\s*}, xml) + end + + def test_line_without_origin_country + invoice = make_eu_invoice + invoice.line_items.each { |line_item| line_item.origin_country_code = nil } + xml = invoice.to_xml(version: 2) + + refute_match(/OriginTradeCountry/, xml) + assert_equal [], Validator.new(xml, version: 2).validate_against_schematron + end + def test_invoice_with_quantity_causing_sub_cent_amounts errors = []