From 354bd22c95728a695ba78af0b12123a8352344df Mon Sep 17 00:00:00 2001 From: Felix Wolfsteller Date: Thu, 2 Mar 2023 20:57:37 +0100 Subject: [PATCH] use wolfram alphaed formula for shipping cost retaxation --- lib/shoplex/shipping_splitter.rb | 28 +++++++++++----------------- lib/shoplex/shopware_csv_parser.rb | 5 ++++- lib/shoplex/shopware_invoice.rb | 1 + test/test_shipping_splitter.rb | 19 +++++++++++-------- test/test_shoplex.rb | 6 +++--- test/test_shopware_csv_parser.rb | 2 +- 6 files changed, 31 insertions(+), 30 deletions(-) diff --git a/lib/shoplex/shipping_splitter.rb b/lib/shoplex/shipping_splitter.rb index f76ef55..15e46fd 100644 --- a/lib/shoplex/shipping_splitter.rb +++ b/lib/shoplex/shipping_splitter.rb @@ -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 diff --git a/lib/shoplex/shopware_csv_parser.rb b/lib/shoplex/shopware_csv_parser.rb index 65541c4..e3adf77 100644 --- a/lib/shoplex/shopware_csv_parser.rb +++ b/lib/shoplex/shopware_csv_parser.rb @@ -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:) @@ -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 diff --git a/lib/shoplex/shopware_invoice.rb b/lib/shoplex/shopware_invoice.rb index 8aab916..c22d870 100644 --- a/lib/shoplex/shopware_invoice.rb +++ b/lib/shoplex/shopware_invoice.rb @@ -9,6 +9,7 @@ module Shoplex :tax07_amount, :tax19_amount, :shipping_gross, + :shipping_net, keyword_init: true) do def german? country == "Deutschland" diff --git a/test/test_shipping_splitter.rb b/test/test_shipping_splitter.rb index d92230d..ed66d5c 100644 --- a/test/test_shipping_splitter.rb +++ b/test/test_shipping_splitter.rb @@ -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:) @@ -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) diff --git a/test/test_shoplex.rb b/test/test_shoplex.rb index 9fd57a5..6a16b41 100644 --- a/test/test_shoplex.rb +++ b/test/test_shoplex.rb @@ -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 diff --git a/test/test_shopware_csv_parser.rb b/test/test_shopware_csv_parser.rb index f5d3f59..8c750a2 100644 --- a/test/test_shopware_csv_parser.rb +++ b/test/test_shopware_csv_parser.rb @@ -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