Skip to content

Instantly share code, notes, and snippets.

@gtan66
Last active August 29, 2015 14:14
Show Gist options
  • Save gtan66/2726347854ff4e483567 to your computer and use it in GitHub Desktop.
Save gtan66/2726347854ff4e483567 to your computer and use it in GitHub Desktop.
SD manual entries report
def self.export_security_deposit_manual_entries_to_csv
CSV.open("report.csv", "wb") do |csv|
accounts = security_deposit_manual_entries
for acct in accounts do
for sec_deposit in acct.security_deposits do
for journal_entry in sec_deposit.journal_entries.where(entry_item_type: "ManualEntryItem")
csv << [journal_entry.entry_item.created_at, acct.short_code, sec_deposit.location.code,
journal_entry.entry_item.amount.to_f, journal_entry.entry_item.user.try("email"), journal_entry.entry_item.notes]
end
end
end
end
end
def self.security_deposit_manual_entries
Account.includes(security_deposits: { journal_entries: :entry_item }).all.
select { |e|
e.security_deposits.any? { |s|
s.journal_entries.any? {|r|
r.entry_item_type == "ManualEntryItem"
}
}
}
end
@OnFreund
Copy link

OnFreund commented Feb 6, 2015

A SQL version for Aviad to use

SELECT manual_entry_items.amount, email, manual_entry_items.notes
FROM manual_entry_items
INNER JOIN journal_entries ON entry_item_id = manual_entry_items.id AND entry_item_type = 'ManualEntryItem'
INNER JOIN security_deposits ON journal_subject_id = security_deposits.id AND journal_subject_type = 'SecurityDeposit'
INNER JOIN users ON users.id = manual_entry_items.user_id;

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