Skip to content

Commit 86c1e95

Browse files
committed
Rounding issue when summing tax amount
1 parent 1497344 commit 86c1e95

2 files changed

Lines changed: 103 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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,105 @@ def make_fr_invoice
541541
)
542542
end
543543

544+
def make_de_invoice_with_float_quantity
545+
seller = TradeParty.new(
546+
name: 'Depfu inc',
547+
street1: 'Quickbornstr. 46',
548+
city: 'Hamburg',
549+
postal_code: '20253',
550+
country_id: 'DE',
551+
vat_id: 'DE304755032'
552+
)
553+
buyer = TradeParty.new(
554+
name: 'Depfu inc',
555+
person_name: 'Max Mustermann',
556+
street1: 'Quickbornstr. 46',
557+
city: 'Hamburg',
558+
postal_code: '20253',
559+
country_id: 'DE',
560+
vat_id: 'DE304755032'
561+
)
562+
line_item1 = LineItem.new(
563+
name: 'Depfu Starter Plan',
564+
quantity: 78.367,
565+
unit: :PIECE,
566+
gross_amount: BigDecimal('49.6'),
567+
net_amount: BigDecimal('26.6'),
568+
charge_amount: BigDecimal('2084.56'),
569+
discount_amount: BigDecimal('23'),
570+
discount_reason: 'Rabatt',
571+
tax_category: :STANDARDRATE,
572+
tax_percent: '19',
573+
tax_amount: BigDecimal("396.07"),
574+
origin_country_code: 'DE',
575+
currency_code: 'EUR'
576+
)
577+
line_item2 = LineItem.new(
578+
name: 'Depfu Starter Plan',
579+
quantity: 35.207,
580+
unit: :PIECE,
581+
gross_amount: BigDecimal('46.25'),
582+
net_amount: BigDecimal('22.87'),
583+
charge_amount: BigDecimal('805.18'),
584+
discount_amount: BigDecimal('23.38'),
585+
discount_reason: 'Rabatt',
586+
tax_category: :STANDARDRATE,
587+
tax_percent: '19',
588+
tax_amount: BigDecimal("152.98"),
589+
origin_country_code: 'DE',
590+
currency_code: 'EUR'
591+
)
592+
Invoice.new(
593+
id: '12345',
594+
issue_date: Date.today,
595+
service_period_start: Date.today,
596+
service_period_end: Date.today + 30,
597+
seller: seller,
598+
buyer: buyer,
599+
buyer_reference: "112233",
600+
line_items: [line_item1, line_item2],
601+
currency_code: 'USD',
602+
payment_type: :CREDITCARD,
603+
payment_text: 'Kreditkarte',
604+
payment_reference: 'INV 123123123',
605+
payment_iban: 'DE02120300000000202051',
606+
payment_terms_text: "Zahlbar innerhalb von 14 Tagen ohne Abzug",
607+
tax_category: :STANDARDRATE,
608+
tax_amount: BigDecimal('549.05'),
609+
basis_amount: BigDecimal('2889.74'),
610+
grand_total_amount: BigDecimal('3438.79'),
611+
due_amount: BigDecimal('3438.79'),
612+
paid_amount: BigDecimal('0'),
613+
payment_due_date: Date.today + 14,
614+
subject_code: 'REG' # BT-21
615+
)
616+
end
617+
618+
def test_simple_de_invoice_with_decimal_quantity_v2
619+
xml = make_de_invoice_with_float_quantity.to_xml(version: 2)
620+
v = Validator.new(xml, version: 2)
621+
errors = v.validate_against_schema
622+
if !errors.empty?
623+
puts xml
624+
errors.each do |error|
625+
puts error
626+
end
627+
end
628+
assert_equal [], errors
629+
end
630+
631+
def test_simple_de_invoice_with_decimal_quantity_v1
632+
xml = make_de_invoice_with_float_quantity.to_xml(version: 1)
633+
v = Validator.new(xml, version: 1)
634+
errors = v.validate_against_schema
635+
if !errors.empty?
636+
puts xml
637+
errors.each do |error|
638+
puts error
639+
end
640+
end
641+
assert_equal [], errors
642+
end
544643

545644
def test_simple_eu_invoice_v2
546645
begin

0 commit comments

Comments
 (0)