Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Created November 15, 2010 13:28
Show Gist options
  • Save lukeredpath/700354 to your computer and use it in GitHub Desktop.
Save lukeredpath/700354 to your computer and use it in GitHub Desktop.
Loop through each finance report and run it using my process_finance_report script, creating an invoice in FreeAgent.
#!/usr/bin/env ruby
SECONDS_IN_DAY = (24*60*60)
REPORT_ROOT = File.expand_path("~/Documents/Business/Accounts/iTunes Finance Reports")
# I group US and WW on the same invoice as they are both in USD
INVOICE_GROUPS = [%w{AU}, %w{CA}, %w{GB}, %w{EU}, %w{US WW}, %w{JP}]
one_month_ago = Time.now - (30 * SECONDS_IN_DAY)
# My "fetch_finance_reports" script puts reports in a sub-dir e.g. 2010-09-Aug
Dir.chdir(File.join(REPORT_ROOT, one_month_ago.strftime("%Y-%m-%b"))) do
reports = Dir["*.txt"]
INVOICE_GROUPS.each do |invoice|
puts "* Processing invoices for regions: #{invoice.join(", ")}"
reports_for_invoice = invoice.map { |region| File.basename(reports.grep(/#{region}\.txt/).first) }
system "process_finance_report #{reports_for_invoice.join(" ")}"
end
end
@lukeredpath
Copy link
Author

Removed the ActiveSupport dependency. I realize that the one_month_ago implementation will fail if you run this on the 31st of the month...so, don't do that, right?

@lukeredpath
Copy link
Author

Blogged in more detail here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment