Skip to content

Commit e320f01

Browse files
committed
Rounding issue when summing tax amount
1 parent b41a072 commit e320f01

2 files changed

Lines changed: 104 additions & 2 deletions

File tree

lib/secretariat/invoice.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,15 @@ def taxes
9393
end
9494

9595
line_items.each do |line_item|
96+
tax_base_amount = (BigDecimal(line_item.net_amount) * line_item.billed_quantity).round(2)
97+
9698
if line_item.tax_percent.nil?
9799
taxes['0'] = Tax.new(tax_percent: BigDecimal(0), tax_category: line_item.tax_category, tax_amount: BigDecimal(0)) if taxes['0'].nil?
98-
taxes['0'].base_amount += BigDecimal(line_item.net_amount) * line_item.billed_quantity
100+
taxes['0'].base_amount += tax_base_amount
99101
else
100102
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?
101103
taxes[line_item.tax_percent].tax_amount += BigDecimal(line_item.tax_amount)
102-
taxes[line_item.tax_percent].base_amount += BigDecimal(line_item.net_amount) * line_item.billed_quantity
104+
taxes[line_item.tax_percent].base_amount += tax_base_amount
103105
end
104106
end
105107

test/invoice_test.rb

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,106 @@ def make_negative_de_invoice
481481
)
482482
end
483483

484+
def make_de_invoice_with_float_quantity
485+
seller = TradeParty.new(
486+
name: 'Depfu inc',
487+
street1: 'Quickbornstr. 46',
488+
city: 'Hamburg',
489+
postal_code: '20253',
490+
country_id: 'DE',
491+
vat_id: 'DE304755032'
492+
)
493+
buyer = TradeParty.new(
494+
name: 'Depfu inc',
495+
person_name: 'Max Mustermann',
496+
street1: 'Quickbornstr. 46',
497+
city: 'Hamburg',
498+
postal_code: '20253',
499+
country_id: 'DE',
500+
vat_id: 'DE304755032'
501+
)
502+
line_item1 = LineItem.new(
503+
name: 'Depfu Starter Plan',
504+
quantity: 78.367,
505+
unit: :PIECE,
506+
gross_amount: BigDecimal('49.6'),
507+
net_amount: BigDecimal('26.6'),
508+
charge_amount: BigDecimal('2084.56'),
509+
discount_amount: BigDecimal('23'),
510+
discount_reason: 'Rabatt',
511+
tax_category: :STANDARDRATE,
512+
tax_percent: '19',
513+
tax_amount: BigDecimal("396.07"),
514+
origin_country_code: 'DE',
515+
currency_code: 'EUR'
516+
)
517+
line_item2 = LineItem.new(
518+
name: 'Depfu Starter Plan',
519+
quantity: 35.207,
520+
unit: :PIECE,
521+
gross_amount: BigDecimal('46.25'),
522+
net_amount: BigDecimal('22.87'),
523+
charge_amount: BigDecimal('805.18'),
524+
discount_amount: BigDecimal('23.38'),
525+
discount_reason: 'Rabatt',
526+
tax_category: :STANDARDRATE,
527+
tax_percent: '19',
528+
tax_amount: BigDecimal("152.98"),
529+
origin_country_code: 'DE',
530+
currency_code: 'EUR'
531+
)
532+
Invoice.new(
533+
id: '12345',
534+
issue_date: Date.today,
535+
service_period_start: Date.today,
536+
service_period_end: Date.today + 30,
537+
seller: seller,
538+
buyer: buyer,
539+
buyer_reference: "112233",
540+
line_items: [line_item1, line_item2],
541+
currency_code: 'USD',
542+
payment_type: :CREDITCARD,
543+
payment_text: 'Kreditkarte',
544+
payment_reference: 'INV 123123123',
545+
payment_iban: 'DE02120300000000202051',
546+
payment_terms_text: "Zahlbar innerhalb von 14 Tagen ohne Abzug",
547+
tax_category: :STANDARDRATE,
548+
tax_amount: BigDecimal('549.05'),
549+
basis_amount: BigDecimal('2889.74'),
550+
grand_total_amount: BigDecimal('3438.79'),
551+
due_amount: BigDecimal('3438.79'),
552+
paid_amount: BigDecimal('0'),
553+
payment_due_date: Date.today + 14,
554+
subject_code: 'REG' # BT-21
555+
)
556+
end
557+
558+
def test_simple_de_invoice_with_decimal_quantity_v2
559+
xml = make_de_invoice_with_float_quantity.to_xml(version: 2)
560+
v = Validator.new(xml, version: 2)
561+
errors = v.validate_against_schema
562+
if !errors.empty?
563+
puts xml
564+
errors.each do |error|
565+
puts error
566+
end
567+
end
568+
assert_equal [], errors
569+
end
570+
571+
def test_simple_de_invoice_with_decimal_quantity_v1
572+
xml = make_de_invoice_with_float_quantity.to_xml(version: 1)
573+
v = Validator.new(xml, version: 1)
574+
errors = v.validate_against_schema
575+
if !errors.empty?
576+
puts xml
577+
errors.each do |error|
578+
puts error
579+
end
580+
end
581+
assert_equal [], errors
582+
end
583+
484584
def test_simple_eu_invoice_v2
485585
begin
486586
xml = make_eu_invoice.to_xml(version: 2)

0 commit comments

Comments
 (0)