donations_from_receipts <- function( receipts, addresses_out = NULL, receipt_type_code = c("IND", "CPCM", "GEN", "PPTY", "OUTS", "NFPC"), exclude_org_name = c("AGGREGATED INDIVIDUAL CONTRIBUTION", "VARIOUS VARIOUS") ) { if (is.character(receipts)) { receipts <- prep_open_dataset_db(receipts) } if (!is.null(addresses_out) && is.character(addresses_out)) { addresses <- out_open_dataset_db(addresses_out) } ret <- receipts |> filter( # Keep individual/party donors; drop record keeping things, like refunds receipt_type_code %in% !!receipt_type_code, !toupper(org_name) %in% !!exclude_org_name ) |> rename(donor_name = org_name) |> # address lookup is the key for matching with the resolved addresses db add_address_lookup(name = "donor_address_raw") |> select( sboe_id, report_id, donor_name, donor_address_raw, amount, profession, employers_name, form_of_payment_desc, everything() ) if (is.null(addresses_out)) return(ret) ret |> left_join( addresses |> select(1:2), by = join_by(donor_address_raw == address_lookup) ) |> rename(donor_address = address_resolved) |> relocate(donor_address, .after = donor_name) |> mutate(donor_address = coalesce(donor_address, !!fixup_po_box_query(donor_address_raw))) } count_receipt_types <- function(receipts) { # # A tibble: 13 × 3 # receipt_type_code receipt_type_desc n # # 1 IND Individual Contribution 7425756 # 2 CPCM Other Political Committee Contribution 78354 # 3 GEN General Contribution 65692 # 4 PPTY Party Contribution 27623 # 5 OUTS Outside Source 16415 # 6 INT Interest Earned 10308 # 7 RFND Refund/Reimbursement to the Committee 7353 # 8 CNRE Contribution to be Reimbursed 3057 # 9 EPPS Exempt Party Price Sale 685 # 10 NFPC Not for Profit Contribution 628 # 11 DON Donation 28 # 12 DEBT Debt Payment 4 # 13 GNS Goods and Services 1 # receipts |> count(receipt_type_code, receipt_type_desc, sort = TRUE) }