brightfunds: Add importer for new CSV format.

This commit is contained in:
Brett Smith 2020-11-09 16:40:33 -05:00
parent 347203b5f4
commit c13ddd1274
3 changed files with 86 additions and 27 deletions

View file

@ -1,40 +1,68 @@
import collections
from . import _csv
from . import _xls
from .. import strparse
class DonorReportImporter(_xls.XLSImporterBase):
BOOK_KWARGS = {'encoding_override': 'utf-8'}
class _BrightFundsMixin:
DATE_FMT = '%m/%d/%y'
ENTRY_SEED = {'currency': 'USD'}
NEEDED_FIELDS = frozenset(['Created', 'Amount'])
COPIED_FIELDS = {
NAME_KEYS = ['company_name', 'donor_name', 'on_behalf_of']
DATE_FIELD = 'Date'
DONOR_FIELD = 'Name'
EMAIL_FIELD = 'Email'
TYPE_FIELD = 'Donation Type'
def __init_subclass__(cls):
cls.FIELDS_TO_COPY = {
'Company Name': 'company_name',
'Donor Name': 'donor_name',
'Donor Email': 'donor_email',
'On Behalf Of': 'on_behalf_of',
'Designation': 'designation',
'Fund': 'fund',
'Type': 'type',
cls.DONOR_FIELD: 'donor_name',
cls.EMAIL_FIELD: 'donor_email',
cls.TYPE_FIELD: 'type',
}
cls.NEEDED_FIELDS = frozenset(['Amount', cls.DATE_FIELD, *cls.FIELDS_TO_COPY])
def _cell_is_blank(self, value):
return value == '-' or not value
def _get_value(self, value):
return '' if (value == '-' or not value) else value
def _read_row(self, row):
if any(self._cell_is_blank(row[key]) for key in self.NEEDED_FIELDS):
if not (row['Amount'] and row[self.DATE_FIELD]):
return None
names = [row[key] for key in ['Company Name', 'Donor Name', 'On Behalf Of']
if not self._cell_is_blank(row[key])]
entry_data = {
entry_key: self._get_value(row[row_key])
for row_key, entry_key in self.FIELDS_TO_COPY.items()
}
print(entry_data, row['Amount'])
names = []
for key in self.NAME_KEYS:
name = entry_data[key].strip()
entry_data[key] = name
if name:
names.append(name)
try:
corporation, payee, *_ = names
except ValueError:
corporation = names[0]
payee = corporation
entry_data = {
'amount': '{:.2f}'.format(row['Amount']),
'corporation': corporation,
'date': strparse.date(row['Created'], '%m/%d/%Y'),
'payee': payee,
}
entry_data.update((entry_key, '')
for row_key, entry_key in self.COPIED_FIELDS.items()
if self._cell_is_blank(row[row_key]))
entry_data['amount'] = str(row['Amount'])
entry_data['corporation'] = corporation
entry_data['date'] = strparse.date(row[self.DATE_FIELD], self.DATE_FMT)
entry_data['payee'] = payee
return entry_data
class DonorReportImporter(_BrightFundsMixin, _csv.CSVImporterBase):
pass
class DonorReportXLSImporter(_BrightFundsMixin, _xls.XLSImporterBase):
BOOK_KWARGS = {'encoding_override': 'utf-8'}
DATE_FMT = '%m/%d/%Y'
DATE_FIELD = 'Created'
DONOR_FIELD = 'Donor Name'
EMAIL_FIELD = 'Donor Email'
TYPE_FIELD = 'Type'

View file

@ -0,0 +1,3 @@
Date,Disbursement ID,Name,Fund,Amount,Designation,On Behalf Of,Email,Company Name,Donation Type
10/01/20,123456,Company ,,15,,Employed Person,,Company,MatchingTransaction
09/24/20,123456,Employed Person ,,15,,-,person@example.com,Company,CreditCardTransaction
1 Date Disbursement ID Name Fund Amount Designation On Behalf Of Email Company Name Donation Type
2 10/01/20 123456 Company 15 Employed Person Company MatchingTransaction
3 09/24/20 123456 Employed Person 15 - person@example.com Company CreditCardTransaction

View file

@ -686,7 +686,7 @@
ledger template: benevity donations ledger entry
- source: BrightFunds.xls
importer: brightfunds.DonorReportImporter
importer: brightfunds.DonorReportXLSImporter
expect:
- date: !!python/object/apply:datetime.date [2017, 10, 20]
currency: USD
@ -701,6 +701,34 @@
on_behalf_of: Dakota Smith
type: Matched Donation
- source: BrightFunds.csv
importer: brightfunds.DonorReportImporter
expect:
- date: !!python/object/apply:datetime.date [2020, 10, 01]
currency: USD
amount: !!python/object/apply:decimal.Decimal [15]
payee: Company
corporation: Company
company_name: Company
designation: ""
donor_name: Company
donor_email: ""
fund: ""
on_behalf_of: Employed Person
type: MatchingTransaction
- date: !!python/object/apply:datetime.date [2020, 9, 24]
currency: USD
amount: !!python/object/apply:decimal.Decimal [15]
payee: Employed Person
corporation: Company
company_name: Company
designation: ""
donor_name: Employed Person
donor_email: person@example.com
fund: ""
on_behalf_of: ""
type: CreditCardTransaction
- source: OReillyRoyalties.csv
importer: oreilly.RoyaltiesImporter
expect: