Created
January 22, 2025 16:25
-
-
Save victor-tremendous/2423997293a622d026522974a71983d3 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", "Organization ID", "Organization Name", "Schedule", "Credit Limit", "Credit Limit Note", "Last Invoice Created At", "Last Invoice Number", "Last Invoice Amount"] | |
InvoiceAccount | |
.joins(organization: :program) | |
.commercial | |
.active | |
.order("programs.name", "organizations.name") | |
.includes(:invoices) | |
.each do |invoice_account| | |
organization = invoice_account.organization | |
program = organization.program | |
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, | |
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