Skip to content
Open
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
2 changes: 1 addition & 1 deletion lib/secretariat/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def taxes
else
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?
taxes[line_item.tax_percent].tax_amount += BigDecimal(line_item.tax_amount)
taxes[line_item.tax_percent].base_amount += BigDecimal(line_item.net_amount) * line_item.billed_quantity
taxes[line_item.tax_percent].base_amount += BigDecimal(line_item.charge_amount)
end
end

Expand Down
27 changes: 14 additions & 13 deletions lib/secretariat/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,24 @@ def untaxable?
end

def to_xml(xml, line_item_index, version: 2, validate: true)
net_price = net_amount && BigDecimal(net_amount)
gross_price = gross_amount && BigDecimal(gross_amount)
charge_price = charge_amount && BigDecimal(charge_amount)
xml_net_price = net_amount && BigDecimal(net_amount)
xml_gross_price = gross_amount && BigDecimal(gross_amount)
xml_charge_price = charge_amount && BigDecimal(charge_amount)
xml_billed_quantity = billed_quantity

self.tax_percent ||= BigDecimal(0)

if net_price&.zero?
if xml_net_price&.zero?
self.tax_percent = 0
end

if net_price&.negative?
if xml_net_price&.negative?
# Zugferd doesn't allow negative amounts at the item level.
# Instead, a negative quantity is used.
self.billed_quantity = -billed_quantity
self.gross_amount = gross_price&.abs
self.net_amount = net_price&.abs
self.charge_amount = charge_price&.abs
xml_billed_quantity = -billed_quantity
xml_gross_price= xml_gross_price&.abs
xml_net_price= xml_net_price&.abs
xml_charge_price= xml_charge_price&.abs
end

if validate && !valid?
Expand All @@ -163,7 +164,7 @@ def to_xml(xml, line_item_index, version: 2, validate: true)

xml['ram'].send(agreement) do
xml['ram'].GrossPriceProductTradePrice do
Helpers.currency_element(xml, 'ram', 'ChargeAmount', gross_amount, currency_code, add_currency: version == 1, digits: 4)
Helpers.currency_element(xml, 'ram', 'ChargeAmount', xml_gross_price, currency_code, add_currency: version == 1, digits: 4)
if version == 2 && discount_amount
xml['ram'].BasisQuantity(unitCode: unit_code) do
xml.text(Helpers.format(effective_basis_quantity, digits: 4))
Expand All @@ -187,7 +188,7 @@ def to_xml(xml, line_item_index, version: 2, validate: true)
end
end
xml['ram'].NetPriceProductTradePrice do
Helpers.currency_element(xml, 'ram', 'ChargeAmount', net_amount, currency_code, add_currency: version == 1, digits: 4)
Helpers.currency_element(xml, 'ram', 'ChargeAmount', xml_net_price, currency_code, add_currency: version == 1, digits: 4)
if version == 2
xml['ram'].BasisQuantity(unitCode: unit_code) do
xml.text(Helpers.format(effective_basis_quantity, digits: 4))
Expand All @@ -200,7 +201,7 @@ def to_xml(xml, line_item_index, version: 2, validate: true)

xml['ram'].send(delivery) do
xml['ram'].BilledQuantity(unitCode: unit_code) do
xml.text(Helpers.format(billed_quantity, digits: 4))
xml.text(Helpers.format(xml_billed_quantity, digits: 4))
end
end

Expand Down Expand Up @@ -233,7 +234,7 @@ def to_xml(xml, line_item_index, version: 2, validate: true)

monetary_summation = by_version(version, 'SpecifiedTradeSettlementMonetarySummation', 'SpecifiedTradeSettlementLineMonetarySummation')
xml['ram'].send(monetary_summation) do
Helpers.currency_element(xml, 'ram', 'LineTotalAmount', (billed_quantity.negative? ? -charge_amount : charge_amount), currency_code, add_currency: version == 1)
Helpers.currency_element(xml, 'ram', 'LineTotalAmount', (xml_billed_quantity.negative? ? -xml_charge_price : xml_charge_price), currency_code, add_currency: version == 1)
end
end

Expand Down