Fix admin invoices CSV output

This commit is contained in:
Thiago Youssef 2022-03-14 12:09:13 +02:00
parent f44bdd1930
commit 7d79e6528d
4 changed files with 41 additions and 1 deletions

View file

@ -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

View file

@ -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

3
test/fixtures/files/invoices.csv vendored Normal file
View file

@ -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
1 Number Buyer Due Date Receipt Date Issue Date Total Currency Seller Name
2 2 Best Names 2010-07-06 Unpaid 2010-07-05 16.5 EUR Seller Ltd
3 1 Best Names 2010-07-06 2010-07-05 2010-07-05 16.5 EUR Seller Ltd

View file

@ -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