Skip to content

Commit 6012883

Browse files
committed
add Invoice#shipTo which defaults to buyer but can be disabled by setting to false
1 parent d7eda4a commit 6012883

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

lib/secretariat/invoice.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ module Secretariat
2424
:issue_date,
2525
:service_period_start,
2626
:service_period_end,
27-
:seller,
28-
:buyer,
27+
:seller, # TradeParty
28+
:buyer, # TradeParty
29+
:ship_to, # TradeParty or nil (buyer) or false (no ShipTo)
2930
:buyer_reference,
3031
:line_items,
3132
:currency_code,
@@ -61,6 +62,14 @@ def tax_reason_text
6162
tax_reason || TAX_EXEMPTION_REASONS[tax_category]
6263
end
6364

65+
# ship_to: nil => use buyer (backwards compatibility)
66+
# ship_to: false => ignore
67+
def ship_to_or_buyer
68+
return buyer if ship_to.nil?
69+
70+
ship_to
71+
end
72+
6473
def tax_category_code(tax, version: 2)
6574
if version == 1
6675
return TAX_CATEGORY_CODES_1[tax.tax_category || tax_category] || 'S'
@@ -245,9 +254,9 @@ def to_xml(version: 1, validate: true)
245254
delivery = by_version(version, 'ApplicableSupplyChainTradeDelivery', 'ApplicableHeaderTradeDelivery')
246255

247256
xml['ram'].send(delivery) do
248-
if version == 2
257+
if version == 2 && ship_to_or_buyer
249258
xml['ram'].ShipToTradeParty do
250-
buyer.to_xml(xml, exclude_tax: true, version: version)
259+
ship_to_or_buyer.to_xml(xml, exclude_tax: true, version: version)
251260
end
252261
end
253262
xml['ram'].ActualDeliverySupplyChainEvent do

lib/secretariat/trade_party.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module Secretariat
2323
keyword_init: true,
2424
) do
2525
def to_xml(xml, exclude_tax: false, version: 2)
26-
if id
26+
if id && !exclude_tax
2727
xml['ram'].ID id # BT-46
2828
end
2929
if global_id.present? && global_id_scheme_id.present?

test/invoice_test.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
module Secretariat
66
class InvoiceTest < Minitest::Test
77

8-
def make_eu_invoice(tax_category: :REVERSECHARGE)
8+
def make_eu_invoice(tax_category: :REVERSECHARGE, ship_to: nil)
99
seller = TradeParty.new(
1010
name: 'Depfu inc',
1111
street1: 'Quickbornstr. 46',
@@ -43,6 +43,7 @@ def make_eu_invoice(tax_category: :REVERSECHARGE)
4343
service_period_end: Date.today + 30,
4444
seller: seller,
4545
buyer: buyer,
46+
ship_to: ship_to,
4647
line_items: [line_item],
4748
currency_code: 'USD',
4849
payment_type: :CREDITCARD,
@@ -383,6 +384,28 @@ def test_simple_eu_invoice_v2
383384
puts e.errors
384385
end
385386

387+
def test_simple_eu_invoice_v2_without_ship_to
388+
begin
389+
xml = make_eu_invoice(ship_to: false).to_xml(version: 2)
390+
rescue ValidationError => e
391+
pp e.errors
392+
end
393+
394+
refute_match(/<ram:ShipToTradeParty>/, xml)
395+
396+
v = Validator.new(xml, version: 2)
397+
errors = v.validate_against_schema
398+
if !errors.empty?
399+
puts xml
400+
errors.each do |error|
401+
puts error
402+
end
403+
end
404+
assert_equal [], errors
405+
rescue ValidationError => e
406+
puts e.errors
407+
end
408+
386409
def test_simple_foreign_invoice_v2_taxexpempt
387410
begin
388411
xml = make_foreign_invoice(tax_category: :TAXEXEMPT).to_xml(version: 2)

0 commit comments

Comments
 (0)