Skip to content

Commit

Permalink
use wolfram alphaed formula for shipping cost retaxation
Browse files Browse the repository at this point in the history
  • Loading branch information
fwolfst committed Mar 2, 2023
1 parent ee6c29e commit 354bd22
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 30 deletions.
28 changes: 11 additions & 17 deletions lib/shoplex/shipping_splitter.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
module Shoplex
class ShippingSplitter
def self.apply!(invoice:)
cost_without_shipping = invoice.invoice_amount - invoice.shipping_gross.to_f
return if invoice.shipping_net.to_i == 0 || invoice.shipping_gross.to_i == 0

gross19 = invoice.tax19_amount.to_f / 0.19
gross07 = invoice.tax07_amount.to_f / 0.07
gross19 = gross19.round(2)
gross07 = gross07.round(2)
#https://www.wolframalpha.com/input?i=solve+a+%2B+b+%3D+1+AND+g+-+n+%3D+0.07+*+a+*+n+%2B+0.19+*+b+*+n+for+a

# div/0
ratio19 = case
when gross07 == 0
1
when gross19 == 0
0
else
(gross19 / gross07 ) * gross19 / (gross19 + gross07)
end

shipping_cost_19part = invoice.shipping_gross * ratio19
shipping_cost_07part = invoice.shipping_gross - shipping_cost_19part

ratio_07 = (119 / 12.0) - (25 * invoice.shipping_gross) / (3 * invoice.shipping_net)
ratio_19 = 1.0 - ratio_07

shipping_cost_19part = invoice.shipping_net * ratio_19
shipping_cost_07part = invoice.shipping_net * ratio_07

invoice.tax19_amount = invoice.tax19_amount.to_f + shipping_cost_19part * 0.19
invoice.tax07_amount = invoice.tax07_amount.to_f + shipping_cost_07part * 0.07

invoice.tax19_amount = invoice.tax19_amount.round(2)
invoice.tax07_amount = invoice.tax07_amount.round(2)
end
end
end
5 changes: 4 additions & 1 deletion lib/shoplex/shopware_csv_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def self.parse csv_file_content
def self.convert_tax_numbers!(row:)
row["taxRateSums_7"] = row["taxRateSums_7"].to_f
row["taxRateSums_19"] = row["taxRateSums_19"].to_f
row["invoiceShipping"] = row["invoiceShipping"].to_f
row["invoiceShipping"] = row["invoiceShipping"].to_s.gsub(",",".").to_f
row["invoiceShippingNet"] = row["invoiceShippingNet"].to_s.gsub(",",".").to_f
row["invoiceAmount"] = row["invoiceAmount"].to_s.gsub(",",".").to_f
end

def self.create_invoice_from(row:)
Expand All @@ -48,6 +50,7 @@ def self.create_invoice_from(row:)
tax19_amount: row['taxRateSums_19'],
invoice_amount: row['invoiceAmount'],
shipping_gross: row['invoiceShipping'],
shipping_net: row['invoiceShippingNet'],
lastname: row['billingLastName'],)
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/shoplex/shopware_invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Shoplex
:tax07_amount,
:tax19_amount,
:shipping_gross,
:shipping_net,
keyword_init: true) do
def german?
country == "Deutschland"
Expand Down
19 changes: 11 additions & 8 deletions test/test_shipping_splitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@

class TestShippingSlitter < Minitest::Test
def test_it_adds_19_percent_of_shipping_if_all_articles_are_19_percent
invoice = Shoplex::ShopwareInvoice.new(invoice_amount: 20,
shipping_gross: 10,
tax19_amount: 1.9)
invoice = Shoplex::ShopwareInvoice.new(invoice_amount: 20,
shipping_gross: 4.95,
shipping_net: 4.16,
tax19_amount: 0)

Shoplex::ShippingSplitter.apply!(invoice:)

assert_equal 0.0 , invoice.tax07_amount
assert_equal 1.9 + 1.9, invoice.tax19_amount
assert_equal 0.0 , invoice.tax07_amount.to_f
assert_equal 0.0 + 4.95 - 4.16, invoice.tax19_amount
end

def test_it_adds_07_percent_of_shipping_if_all_articles_are_07_percent
invoice = Shoplex::ShopwareInvoice.new(invoice_amount: 20,
shipping_gross: 10,
tax07_amount: 0.7)
shipping_gross: 10.7,
shipping_net: 10,
tax07_amount: 0.7)

Shoplex::ShippingSplitter.apply!(invoice:)

Expand All @@ -27,7 +29,8 @@ def test_it_adds_07_percent_of_shipping_if_all_articles_are_07_percent

def test_5050_case
invoice = Shoplex::ShopwareInvoice.new(invoice_amount: 40,
shipping_gross: 20,
shipping_gross: 20 + 10 * 0.07 + 10 * 0.19,
shipping_net: 20,
tax07_amount: 0.7,
tax19_amount: 1.9)

Expand Down
6 changes: 3 additions & 3 deletions test/test_shoplex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def test_it_ignores_lines_without_invoice_number
def test_it_does_the_whole_shebang
result_file_content = Shoplex::process(File.read('test/files/two_lines_one_invoice_shopware.csv', encoding: Encoding::ISO_8859_1))
expected = <<~CSV
26.10.2022,6010,6010 90067 LastNameOfBill,59.00,11100,0,EUR
26.10.2022,6010,6010 90067 LastNameOfBill,2.04,0,8310,EUR
26.10.2022,6010,6010 90067 LastNameOfBill,3.72,0,8315,EUR
26.10.2022,6010,6010 90067 LastNameOfBill,59.39,11100,0,EUR
26.10.2022,6010,6010 90067 LastNameOfBill,2.23,0,8310,EUR
26.10.2022,6010,6010 90067 LastNameOfBill,4.04,0,8315,EUR
CSV
assert_equal expected.strip,
result_file_content.strip
Expand Down
2 changes: 1 addition & 1 deletion test/test_shopware_csv_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_it_sets_amount_and_lastname
result = Shoplex::ShopwareCSVParser.parse shopware_csv_file
invoice = result.invoices.first

assert_equal "59,39", invoice.invoice_amount
assert_equal 59.39, invoice.invoice_amount
assert_equal "LastNameOfBill", invoice.lastname
end

Expand Down

0 comments on commit 354bd22

Please sign in to comment.