Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/secretariat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
=end

require_relative 'secretariat/version'
require_relative 'secretariat/object_extensions'
require_relative 'secretariat/constants'
require_relative 'secretariat/helpers'
require_relative 'secretariat/versioner'
Expand Down
6 changes: 4 additions & 2 deletions lib/secretariat/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
require 'bigdecimal'

module Secretariat
using ObjectExtensions

Invoice = Struct.new("Invoice",
:id,
:issue_date,
Expand Down Expand Up @@ -256,7 +258,7 @@ def to_xml(version: 1, validate: true)
end
trade_settlement = by_version(version, 'ApplicableSupplyChainTradeSettlement', 'ApplicableHeaderTradeSettlement')
xml['ram'].send(trade_settlement) do
if payment_reference && payment_reference != ''
if payment_reference.present?
xml['ram'].PaymentReference payment_reference
end
xml['ram'].InvoiceCurrencyCode currency_code
Expand All @@ -273,7 +275,7 @@ def to_xml(version: 1, validate: true)
xml['ram'].ApplicableTradeTax do
Helpers.currency_element(xml, 'ram', 'CalculatedAmount', tax.tax_amount, currency_code, add_currency: version == 1)
xml['ram'].TypeCode 'VAT'
if tax_reason_text && tax_reason_text != ''
if tax_reason_text.present?
xml['ram'].ExemptionReason tax_reason_text
end
Helpers.currency_element(xml, 'ram', 'BasisAmount', tax.base_amount, currency_code, add_currency: version == 1)
Expand Down
14 changes: 14 additions & 0 deletions lib/secretariat/object_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Secretariat
module ObjectExtensions
refine Object do
# Copied from activesupport/lib/active_support/core_ext/object/blank.rb, line 18
def blank?
respond_to?(:empty?) ? !!empty? : !self
end

def present?
!blank?
end
end
end
end
10 changes: 6 additions & 4 deletions lib/secretariat/trade_party.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
=end

module Secretariat
using ObjectExtensions

TradeParty = Struct.new('TradeParty',
:name, :street1, :street2, :city, :postal_code, :country_id, :vat_id, :global_id, :global_id_scheme_id, :tax_id,
keyword_init: true,
) do
def to_xml(xml, exclude_tax: false, version: 2)
if global_id && global_id != '' && global_id_scheme_id && global_id_scheme_id != ''
if global_id.present? && global_id_scheme_id.present?
xml['ram'].GlobalID(schemeID: global_id_scheme_id) do
xml.text(global_id)
end
Expand All @@ -29,19 +31,19 @@ def to_xml(xml, exclude_tax: false, version: 2)
xml['ram'].PostalTradeAddress do
xml['ram'].PostcodeCode postal_code
xml['ram'].LineOne street1
if street2 && street2 != ''
if street2.present?
xml['ram'].LineTwo street2
end
xml['ram'].CityName city
xml['ram'].CountryID country_id
end
if !exclude_tax && vat_id && vat_id != ''
if !exclude_tax && vat_id.present?
xml['ram'].SpecifiedTaxRegistration do
xml['ram'].ID(schemeID: 'VA') do
xml.text(vat_id)
end
end
elsif tax_id && tax_id != ''
elsif tax_id.present?
xml['ram'].SpecifiedTaxRegistration do
xml['ram'].ID(schemeID: 'FC') do
xml.text(tax_id)
Expand Down
7 changes: 7 additions & 0 deletions test/invoice_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -588,5 +588,12 @@ def test_negative_de_invoice_against_schematron_2
end
assert_equal [], errors
end

def test_invoice_object_extensions
invoice = make_de_invoice
xml = invoice.to_xml(version: 2)

assert_match(/<ram:PaymentReference>#{invoice.payment_reference}<\/ram:PaymentReference>/, xml)
end
end
end