-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(export-invoice-fees): Export invoice fees to CSV (#2268)
## 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 invoice fees by non-technical users, it is required to add a new CSV exporter and extend the export resource service and mailer. ## Description This change adds a new CSV exporter for invoice fees; also extends the `ExportResourcesService` to handle unknown resource types and invoice_fees. and improve the mailer and its specs to support different kind of exported resources like invoice_fees.
- Loading branch information
Showing
8 changed files
with
380 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'csv' | ||
require 'forwardable' | ||
|
||
module DataExports | ||
module Csv | ||
class InvoiceFees < Invoices | ||
extend Forwardable | ||
|
||
def call | ||
::CSV.generate(headers: true) do |csv| | ||
csv << headers | ||
|
||
query.invoices.find_each do |invoice| | ||
serialized_invoice = serializer_klass | ||
.new(invoice, includes: %i[fees subscriptions]) | ||
.serialize | ||
|
||
subscriptions = serialized_invoice[:subscriptions].index_by do |sub| | ||
sub[:lago_id] | ||
end | ||
|
||
serialized_invoice[:fees].each do |serialized_fee| | ||
subscription_id = serialized_fee[:lago_subscription_id] | ||
serialized_subscription = subscriptions[subscription_id] | ||
|
||
csv << [ | ||
serialized_invoice[:lago_id], | ||
serialized_invoice[:number], | ||
serialized_invoice[:issuing_date], | ||
serialized_fee[:lago_id], | ||
serialized_fee[:item][:type], | ||
serialized_fee[:item][:code], | ||
serialized_fee[:item][:name], | ||
serialized_fee[:item][:invoice_display_name], | ||
serialized_fee[:item][:filter_invoice_display_name], | ||
serialized_fee[:item][:grouped_by], | ||
serialized_subscription&.dig(:external_id), | ||
serialized_subscription&.dig(:plan_code), | ||
serialized_fee[:from_date], | ||
serialized_fee[:to_date], | ||
serialized_fee[:total_amount_cents], | ||
serialized_fee[:total_amount_currency], | ||
serialized_fee[:units], | ||
serialized_fee[:precise_unit_amount], | ||
serialized_fee[:taxes_amount_cents] | ||
] | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def headers | ||
%w[ | ||
invoice_lago_id | ||
invoice_number | ||
invoice_issuing_date | ||
fee_lago_id | ||
fee_item_type | ||
fee_item_code | ||
fee_item_name | ||
fee_item_invoice_display_name | ||
fee_item_filter_invoice_display_name | ||
fee_item_grouped_by | ||
subscription_external_id | ||
subscription_plan_code | ||
fee_from_date | ||
fee_to_date | ||
fee_total_amount_cents | ||
fee_amount_currency | ||
fee_units | ||
fee_precise_unit_amount | ||
fee_taxes_amount_cents | ||
] | ||
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
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
Oops, something went wrong.