-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(export invoices): CSV export (#2248)
## Roadmap Task 👉 https://getlago.canny.io/feature-requests/p/ability-to-export-data-from-the-user-interface ## Context In order to effortlessly download export of invoices by non-technical users, it is required to extend the invoices filtering capabilities. ## Description This change adds the actual CSV export for invoices, also notifies the user through the mailer when the file is ready to download.
- Loading branch information
Showing
9 changed files
with
415 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'csv' | ||
require 'forwardable' | ||
|
||
module DataExports | ||
module Csv | ||
class Invoices < BaseService | ||
extend Forwardable | ||
|
||
def initialize(data_export:, serializer_klass: V1::InvoiceSerializer) | ||
@data_export = data_export | ||
@serializer_klass = serializer_klass | ||
end | ||
|
||
def call | ||
::CSV.generate(headers: true) do |csv| | ||
csv << headers | ||
|
||
query.invoices.find_each do |invoice| | ||
serialized_invoice = serializer_klass | ||
.new(invoice, includes: %i[customer]) | ||
.serialize | ||
|
||
csv << [ | ||
serialized_invoice[:lago_id], | ||
serialized_invoice[:sequential_id], | ||
serialized_invoice[:issuing_date], | ||
serialized_invoice[:customer][:lago_id], | ||
serialized_invoice[:customer][:external_id], | ||
serialized_invoice[:customer][:country], | ||
serialized_invoice[:customer][:tax_identification_number], | ||
serialized_invoice[:number], | ||
serialized_invoice[:total_amount_cents], | ||
serialized_invoice[:currency], | ||
serialized_invoice[:invoice_type], | ||
serialized_invoice[:payment_status], | ||
serialized_invoice[:status], | ||
serialized_invoice[:file_url], | ||
serialized_invoice[:taxes_amount_cents], | ||
serialized_invoice[:credit_notes_amount_cents], | ||
serialized_invoice[:prepaid_credit_amount_cents], | ||
serialized_invoice[:coupons_amount_cents], | ||
serialized_invoice[:payment_due_date], | ||
serialized_invoice[:payment_dispute_lost_at], | ||
serialized_invoice[:payment_overdue] | ||
] | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :data_export, :serializer_klass | ||
|
||
def_delegators :data_export, :organization, :resource_query | ||
|
||
def headers | ||
%w[ | ||
lago_id | ||
sequential_id | ||
issuing_date | ||
customer_lago_id | ||
customer_external_id | ||
customer_country | ||
customer_tax_identification_number | ||
invoince_number | ||
total_amount_cents | ||
currency | ||
invoice_type | ||
payment_status | ||
status | ||
file_url | ||
taxes_amount_cents | ||
credit_notes_amount_cents | ||
prepaid_credit_amount_cents | ||
coupons_amount_cents | ||
payment_due_date | ||
payment_dispute_lost_at | ||
payment_overdue | ||
] | ||
end | ||
|
||
def query | ||
search_term = resource_query["search_term"] | ||
status = resource_query["status"] | ||
filters = resource_query.except("search_term", "status") | ||
|
||
InvoicesQuery.new(organization: organization).call( | ||
search_term:, | ||
status:, | ||
page: nil, | ||
limit: nil, | ||
filters: | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
db/migrate/20240706204557_remove_data_exports_resource_query_null_constraint.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class RemoveDataExportsResourceQueryNullConstraint < ActiveRecord::Migration[7.1] | ||
def change | ||
change_column_null :data_exports, :resource_query, true | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.