Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Balance element to InvoiceSumGroup #41

Merged
merged 10 commits into from
Oct 3, 2023
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 .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
gem install nokogiri -v '~> 1.11.3'
gem install savon
gem install webmock
gem install mocha
gem install minitest -v '~> 5.14'
bundle install
rake
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
03.10.2023
* Added Balance element to InvoiceSumGroup [#41] (https://github.com/internetee/e_invoice/pull/41)

26.09.2023
* Updated wsdl test address [#36](https://github.com/internetee/e_invoice/pull/36)

Expand Down
3 changes: 2 additions & 1 deletion estonian_e_invoice.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'savon'
spec.add_development_dependency 'bundler', '>= 2.2.10'
spec.add_development_dependency 'minitest', '~> 5.0'
spec.add_development_dependency 'mocha'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'simplecov'
spec.add_development_dependency 'webmock'
end
end
34 changes: 30 additions & 4 deletions lib/e_invoice/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,48 @@ def build_invoice_details(invoice)

def build_invoice_totals(invoice)
builder.InvoiceSumGroup do
build_invoice_balance(invoice)
builder.InvoiceSum format_decimal(invoice.subtotal, scale: 4)
builder.TotalVATSum format_decimal(invoice.vat_amount)
builder.TotalSum format_decimal(invoice.total)
builder.TotalToPay format_decimal(invoice.payable == false ? 0 : invoice.total)
if invoice.payable == false
builder.TotalToPay format_decimal(0)
else
builder.TotalToPay format_decimal(invoice.total_to_pay || invoice.total)
end
builder.Currency invoice.currency
end
end

def build_invoice_balance(invoice)
builder.Balance do
build_element('BalanceDate', invoice.balance_date)
build_element('BalanceBegin', invoice.balance_begin, :format_decimal)
build_element('Inbound', invoice.inbound, :format_decimal)
build_element('Outbound', invoice.outbound, :format_decimal)
build_element('BalanceEnd', invoice.balance_end, :format_decimal)
end
end

def build_element(name, value, format_method = nil)
return unless value

formatted_value = format_method ? send(format_method, value) : value
builder.__send__(name, formatted_value)
end

def build_invoice_payment_details(invoice)
builder.PaymentInfo do
builder.Currency invoice.currency
builder.PaymentRefId invoice.reference_number
builder.PaymentDescription invoice.number
builder.Payable invoice.payable == false ? 'NO' : 'YES'
builder.PayDueDate invoice.due_date
builder.PaymentTotalSum format_decimal(invoice.payable == false ? 0 : invoice.total)
if invoice.payable == false
builder.PaymentTotalSum format_decimal(0)
else
builder.PaymentTotalSum format_decimal(invoice.total_to_pay || invoice.total)
end
builder.PayerName invoice.payer_name
builder.PaymentId invoice.number
builder.PayToAccount invoice.beneficiary_account_number
Expand Down Expand Up @@ -203,7 +229,7 @@ def build_footer(e_invoice)
end

def format_decimal(decimal, scale: 2)
format("%.#{scale}f", decimal)
format("%.#{scale}f", decimal) if decimal
end
end
end
end
7 changes: 4 additions & 3 deletions lib/e_invoice/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ class Invoice
:date, :recipient_id_code, :reference_number,
:due_date, :payable, :payer_name,
:beneficiary_name, :beneficiary_account_number,
:subtotal, :vat_amount, :total, :currency,
:monthly_invoice
:subtotal, :vat_amount, :total, :total_to_pay, :currency,
:balance_date, :balance_begin, :inbound, :outbound,
:balance_end, :monthly_invoice

alias_method :id, :number

Expand Down Expand Up @@ -35,4 +36,4 @@ def validate_delivery_channels(channels)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/e_invoice/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module EInvoice
VERSION = '0.1.4'.freeze
VERSION = '0.1.5'.freeze
end
132 changes: 131 additions & 1 deletion test/generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ def invoice(payable: @payable)
invoice.subtotal = 100
invoice.vat_amount = 20
invoice.total = 120
invoice.total_to_pay = nil
invoice.delivery_channel = :internet_bank
invoice.balance_date = nil
invoice.balance_begin = nil
invoice.inbound = nil
invoice.outbound = nil
invoice.balance_end = nil
end
end

Expand Down Expand Up @@ -160,6 +166,7 @@ def test_generates_e_invoice_xml
</Extension>
</InvoiceInformation>
<InvoiceSumGroup>
<Balance/>
<InvoiceSum>100.0000</InvoiceSum>
<TotalVATSum>20.00</TotalSum>
<TotalSum>120.00</TotalSum>
Expand Down Expand Up @@ -274,6 +281,7 @@ def test_generates_prepaid_e_invoice_xml
</Extension>
</InvoiceInformation>
<InvoiceSumGroup>
<Balance/>
<InvoiceSum>100.0000</InvoiceSum>
<TotalVATSum>20.00</TotalSum>
<TotalSum>120.00</TotalSum>
Expand Down Expand Up @@ -329,6 +337,128 @@ def test_generates_prepaid_e_invoice_xml
assert_equal expected_xml, actual_xml
end

def test_generates_prepaid_e_invoice_xml_with_balance_inbound
expected_xml = <<~XML
<?xml version="1.0" encoding="UTF-8"?>
<E_Invoice xsi:noNamespaceSchemaLocation="e-invoice_ver1.2.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<Date>2010-07-05</Date>
<FileId>id1234</FileId>
<Version>1.2</Version>
</Header>
<Invoice invoiceId="invoice-1234" regNumber="recipient-1234" sellerRegnumber="john-1234" serviceId="1234">
<InvoiceParties>
<SellerParty>
<Name>John Doe</Name>
<RegNumber>john-1234</RegNumber>
<VATRegNumber>US1234</VATRegNumber>
<ContactData>
<LegalAddress>
<PostalAddress1>seller address line1</PostalAddress1>
<PostalAddress2>seller address line2</PostalAddress2>
<City>seller address city</City>
<PostalCode>12345</PostalCode>
<Country>seller address country</Country>
</LegalAddress>
</ContactData>
</SellerParty>
<BuyerParty>
<Name>Jane Doe</Name>
<RegNumber>1234</RegNumber>
<VATRegNumber>US1234</VATRegNumber>
<ContactData>
<E-mailAddress>info@buyer.test</E-mailAddress>
<LegalAddress>
<PostalAddress1>buyer address line1</PostalAddress1>
<PostalAddress2>buyer address line2</PostalAddress2>
<City>buyer address city</City>
<PostalCode>123456</PostalCode>
<Country>buyer address country</Country>
</LegalAddress>
</ContactData>
<AccountInfo>
<AccountNumber>GB33BUKB20201555555555</AccountNumber>
</AccountInfo>
</BuyerParty>
</InvoiceParties>
<InvoiceInformation>
<Type type="DEB"/>
<DocumentName>ARVE</DocumentName>
<InvoiceNumber>invoice-1234</InvoiceNumber>
<PaymentReferenceNumber>1234</PaymentReferenceNumber>
<InvoiceDate>2010-07-06</InvoiceDate>
<DueDate>2010-07-07</DueDate>
<Extension extensionId="eakChannel">
<InformationContent>INTERNET_BANK</InformationContent>
</Extension>
<Extension extensionId=\"eakStatusAfterImport\">
<InformationContent>SENT</InformationContent>
</Extension>
</InvoiceInformation>
<InvoiceSumGroup>
<Balance>
<Inbound>60.00</Inbound>
</Balance>
<InvoiceSum>100.0000</InvoiceSum>
<TotalVATSum>20.00</TotalSum>
<TotalSum>120.00</TotalSum>
<TotalToPay>60.00</TotalToPay>
<Currency>EUR</Currency>
</InvoiceSumGroup>
<InvoiceItem>
<InvoiceItemGroup>
<ItemEntry>
<Description>acme services</Description>
<ItemDetailInfo>
<ItemUnit>pc</ItemUnit>
<ItemAmount>1.0000</ItemAmount>
<ItemPrice>100.0000</ItemPrice>
</ItemDetailInfo>
<ItemSum>100.0000</ItemSum>
<VAT vatId="TAX">
<VATRate>20.00</VATRate>
<VATSum>20.0000</VATSum>
</VAT>
<ItemTotal>120.0000</ItemTotal>
</ItemEntry>
</InvoiceItemGroup>
<InvoiceItemTotalGroup>
<InvoiceItemTotalAmount>120.0000</InvoiceItemTotalAmount>
<InvoiceItemTotalSum>100.0000</InvoiceItemTotalSum>
<InvoiceItemTotal>120.0000</InvoiceItemTotal>
</InvoiceItemTotalGroup>
</InvoiceItem>
<PaymentInfo>
<Currency>EUR</Currency>
<PaymentRefId>1234</PaymentRefId>
<PaymentDescription>invoice-1234</PaymentDescription>
<Payable>YES</Payable>
<PayDueDate>2010-07-07</PayDueDate>
<PaymentTotalSum>60.00</PaymentTotalSum>
<PayerName>John Smith</PayerName>
<PaymentId>invoice-1234</PaymentId>
<PayToAccount>GB33BUKB20201555555556</PayToAccount>
<PayToName>Acme Ltd</PayToName>
</PaymentInfo>
</Invoice>
<Footer>
<TotalNumberInvoices>1</TotalNumberInvoices>
<TotalAmount>120.00</TotalAmount>
</Footer>
</E_Invoice>
XML

e_invoice = EInvoiceDouble.new(payable: true)
EInvoice::Invoice.any_instance.stubs(:total_to_pay).returns(60)
EInvoice::Invoice.any_instance.stubs(:inbound).returns(60)

actual_xml = Nokogiri::XML(@generator.generate(e_invoice)) { |config| config.noblanks }
.to_xml

expected_xml = Nokogiri::XML(expected_xml) { |config| config.noblanks }.to_xml
assert_equal expected_xml, actual_xml
end

def test_generated_xml_conforms_to_estonian_e_invoice_standard_v1_2
schema = Nokogiri::XML::Schema(File.read('test/xml_schemas/v1.2.xsd'))
xml = @generator.generate(EInvoiceDouble.new)
Expand All @@ -337,4 +467,4 @@ def test_generated_xml_conforms_to_estonian_e_invoice_standard_v1_2
valid = errors.empty?
assert valid, proc { errors.each { |error| puts error } }
end
end
end
3 changes: 2 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'e_invoice'
require 'minitest/autorun'
require 'webmock/minitest'
require 'webmock/minitest'
require 'mocha/minitest'