Forked from victor-tremendous/commercial_invoicing_data.rb
Last active
May 21, 2025 21:45
-
-
Save magnusvk/5f0661858483c29b558982b0297da52b to your computer and use it in GitHub Desktop.
Pull commercial invoicing data
This file contains hidden or 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
content = CSV.generate do |csv| | |
csv << ["Program ID", "Program Name", "Master Organization ID", "Master Organization Name", "Organization ID", "Organization Name", "Schedule", "Credit Limit", "Credit Limit Note", "Last Invoice Created At", "Last Invoice Number", "Last Invoice Amount"] | |
InvoiceAccount | |
.includes({organization: [:program, :master_organization]}, :invoices) | |
.commercial | |
.active | |
.order("programs.name", "organizations.name") | |
.each do |invoice_account| | |
organization = invoice_account.organization | |
program = organization.program | |
master_organization = organization.master_organization | |
schedule = if invoice_account.commercial_daily? | |
:daily | |
elsif invoice_account.commercial_weekly? | |
:weekly | |
elsif invoice_account.commercial_monthly? | |
:monthly | |
else | |
:on_purchase | |
end | |
credit_limit = Money.from_cents( | |
if program.credit_limit_cents.present? | |
program.credit_limit_cents | |
else | |
200_000_00 | |
end).format(with_currency: false) | |
latest_invoice = invoice_account.invoices.sort { |a, b| b.created_at <=> a.created_at }.first | |
csv << [ | |
program.public_token, | |
program.name, | |
master_organization.public_token, | |
master_organization.name, | |
organization.public_token, | |
organization.name, | |
schedule, | |
credit_limit, | |
program.credit_limit_cents.nil? ? "(Presumed)" : "", | |
latest_invoice&.created_at, | |
latest_invoice&.number, | |
latest_invoice&.amount_money&.format(with_currency: false) | |
] | |
end | |
end | |
export_to_slack(content:, filename: "commercial-invoicing-#{Date.today}.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment