From 7d79e6528d3208e3dc8adb7b8f375ee75645393e Mon Sep 17 00:00:00 2001 From: Thiago Youssef Date: Mon, 14 Mar 2022 12:09:13 +0200 Subject: [PATCH] Fix admin invoices CSV output --- app/models/invoice.rb | 27 +++++++++++++++++++++++++ app/services/csv_generator.rb | 2 +- test/fixtures/files/invoices.csv | 3 +++ test/system/admin_area/invoices_test.rb | 10 +++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/files/invoices.csv diff --git a/app/models/invoice.rb b/app/models/invoice.rb index df8b7aff8..d0a1950a5 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -117,6 +117,23 @@ class Invoice < ApplicationRecord e_invoice_sent_at.present? end + def as_csv_row + [ + number, + buyer, + cancelled? ? I18n.t(:cancelled) : due_date, + receipt_date_status, + issue_date, + total, + currency, + seller_name + ] + end + + def self.csv_header + ['Number', 'Buyer', 'Due Date', 'Receipt Date', 'Issue Date', 'Total', 'Currency', 'Seller Name'].freeze + end + def self.create_from_transaction!(transaction) registrar_user = Registrar.find_by(reference_no: transaction.parsed_ref_number) return unless registrar_user @@ -128,6 +145,16 @@ class Invoice < ApplicationRecord private + def receipt_date_status + if paid? + receipt_date + elsif cancelled? + I18n.t(:cancelled) + else + I18n.t(:unpaid) + end + end + def apply_default_buyer_vat_no self.buyer_vat_no = buyer.vat_no end diff --git a/app/services/csv_generator.rb b/app/services/csv_generator.rb index 122a04ff5..a2c1305a3 100644 --- a/app/services/csv_generator.rb +++ b/app/services/csv_generator.rb @@ -12,6 +12,6 @@ class CsvGenerator private def self.custom_csv(class_name) - [Version::DomainVersion, Version::ContactVersion, Domain, Contact, Invoice].include?(class_name) + [Version::DomainVersion, Version::ContactVersion, Domain, Contact, Invoice, Account].include?(class_name) end end diff --git a/test/fixtures/files/invoices.csv b/test/fixtures/files/invoices.csv new file mode 100644 index 000000000..641cfe036 --- /dev/null +++ b/test/fixtures/files/invoices.csv @@ -0,0 +1,3 @@ +Number,Buyer,Due Date,Receipt Date,Issue Date,Total,Currency,Seller Name +2,Best Names,2010-07-06,Unpaid,2010-07-05,16.5,EUR,Seller Ltd +1,Best Names,2010-07-06,2010-07-05,2010-07-05,16.5,EUR,Seller Ltd diff --git a/test/system/admin_area/invoices_test.rb b/test/system/admin_area/invoices_test.rb index 40a50e1c7..a8d0a8a75 100644 --- a/test/system/admin_area/invoices_test.rb +++ b/test/system/admin_area/invoices_test.rb @@ -40,4 +40,14 @@ class AdminAreaInvoicesTest < ApplicationSystemTestCase assert_current_path admin_invoice_path(@invoice) assert_text 'Invoice has been sent' end + + def test_download_invoices_list_as_csv + travel_to Time.zone.parse('2010-07-05 10:30') + + visit admin_invoices_url + click_link('CSV') + + assert_equal "attachment; filename=\"invoices_#{Time.zone.now.to_formatted_s(:number)}.csv\"; filename*=UTF-8''invoices_#{Time.zone.now.to_formatted_s(:number)}.csv", response_headers['Content-Disposition'] + assert_equal file_fixture('invoices.csv').read, page.body + end end