Sha256: 32e0c5d13fda0800ef8a1ebd1219208b8cbbeb3b7fb02a6d2bcfa199d43f815e

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

namespace :brisk_bills do
  desc "Create a batch of invoices for the prior month, on all accounts with an uninvoiced balance"
  
  task :create_last_months_invoices => :environment do
    end_of_last_month = Time.utc(*Time.now.to_a).last_month.end_of_month
 
    invoiceable_client_ids = Client.find_invoiceable_clients_at(end_of_last_month)
    
    if invoiceable_client_ids.length > 0
      all_activity_types = ActivityType.find(:all)
      
      Client.find(:all, :conditions => ['id IN (?)', invoiceable_client_ids], :order => 'company_name ASC').each do |client|
        puts "Creating invoice for client \"#{client.company_name}\"..."
        
        inv = Invoice.create!(
           :client => client, 
           :activity_types => all_activity_types,
           :activities => Invoice.recommended_activities_for( client.id, end_of_last_month, all_activity_types )
        )
        
        puts "  Created: id (%d) amount: $%s" % [
          inv.id,
          ('%.2f' % inv.amount).gsub(/(\d)(?=\d{3}+(\.\d*)?$)/, '\1,')
        ]
      end
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
brisk-bills-0.8.2 lib/tasks/create_last_months_invoices.rake
brisk-bills-0.8.1 lib/tasks/create_last_months_invoices.rake