diff --git a/exe/shoplex b/exe/shoplex index 75c841d..b2c600b 100755 --- a/exe/shoplex +++ b/exe/shoplex @@ -2,6 +2,6 @@ require 'shoplex' -puts Shoplex::process ARGF.read +puts Shoplex::process(ARGF.read).csv_out exit 0 diff --git a/exe/shoplex-web b/exe/shoplex-web index 1578407..309c399 100755 --- a/exe/shoplex-web +++ b/exe/shoplex-web @@ -87,9 +87,10 @@ __END__ %h2 Result %h3 Errors and invoices to look out for %div(style="color: red") - Errors here + #{@result.errors.count} errors here + #{@result.errors.inspect} %h3 Lexware file - %a{role: 'button', href:"data:attachment/csv;charset=utf-8,#{URI.encode_www_form_component @result}", target: '_blank', download: 'filename.csv'} + %a{role: 'button', href:"data:attachment/csv;charset=utf-8,#{URI.encode_www_form_component @result.csv_out}", target: '_blank', download: 'filename.csv'} Download file %pre - = @result + = @result.csv_out diff --git a/lib/shoplex.rb b/lib/shoplex.rb index d901a42..5dca39a 100644 --- a/lib/shoplex.rb +++ b/lib/shoplex.rb @@ -15,16 +15,19 @@ module Shoplex class Error < StandardError; end def self.process file_content - shopware_invoices = Shoplex::ShopwareCSVParser.parse(file_content).invoices - bookings = shopware_invoices.map do |invoice| + result = Shoplex::ShopwareCSVParser.parse(file_content) + bookings = result.invoices.map do |invoice| begin Shoplex::ShippingSplitter::apply!(invoice:) Shoplex::InvoiceBookingConverter.convert(invoice:) rescue => e + result.mark_error(maker: self, error: :unknown, obj: [e, invoice]) STDERR.puts e + STDERR.puts e.backtrace end end.compact - return Shoplex::LexwareCSV.create_from(bookings:) + result.csv_out = Shoplex::LexwareCSV.create_from(bookings:) + return result end end diff --git a/lib/shoplex/result.rb b/lib/shoplex/result.rb index 481d18f..15e33e4 100644 --- a/lib/shoplex/result.rb +++ b/lib/shoplex/result.rb @@ -1,7 +1,8 @@ module Shoplex class Result attr_accessor :invoices - attr_accessor :errors + attr_reader :errors + attr_accessor :csv_out def initialize @invoices = [] diff --git a/lib/shoplex/shopware_csv_parser.rb b/lib/shoplex/shopware_csv_parser.rb index 366cc8a..3a4531d 100644 --- a/lib/shoplex/shopware_csv_parser.rb +++ b/lib/shoplex/shopware_csv_parser.rb @@ -14,6 +14,8 @@ def self.parse csv_file_content result.invoices << create_invoice_from(row:) rescue => e result.mark_error(maker: self, error: :creation_failed, obj: [e, row]) + STDERR.puts e + STDERR.puts e.backtrace end else result.mark_error(maker: self, error: :no_invoice_number, obj: row) diff --git a/test/test_shoplex.rb b/test/test_shoplex.rb index 817a3e2..79a4681 100644 --- a/test/test_shoplex.rb +++ b/test/test_shoplex.rb @@ -8,12 +8,12 @@ def test_that_it_has_a_version_number end def test_it_ignores_lines_without_invoice_number - result_file_content = Shoplex::process(File.read('test/files/two_lines_one_invoice_shopware.csv', encoding: Encoding::ISO_8859_1)) + result_file_content = Shoplex::process(File.read('test/files/two_lines_one_invoice_shopware.csv', encoding: Encoding::ISO_8859_1)).csv_out assert_equal 3, result_file_content.lines.count end 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)) + result_file_content = Shoplex::process(File.read('test/files/two_lines_one_invoice_shopware.csv', encoding: Encoding::ISO_8859_1)).csv_out expected = <<~CSV 26.10.2022,6010,6010 90067 LastNameOfBill,59.39,11100,0,EUR 26.10.2022,6010,6010 90067 LastNameOfBill,2.23,0,8300,EUR