Files
@ 01a86107d7e4
Branch filter:
Location: NPO-Accounting/import2ledger/import2ledger/importers/benevity.py
01a86107d7e4
4.1 KiB
text/x-python
benevity: Add support for latest Benevity format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | import decimal
from . import _csv
from .. import strparse
ZERO_DECIMAL = decimal.Decimal(0)
class _DonationsImporterBase(_csv.CSVImporterBase):
NAME_FIELDS = ['Donor First Name', 'Donor Last Name']
NOT_SHARED = 'Not shared by donor'
@classmethod
def _read_header_row(cls, row):
row_rindex = cls._row_rindex(row, -1)
if row_rindex > 1:
return None
elif row_rindex == 1 and row[0] in cls.HEADER_FIELDS:
return {cls.HEADER_FIELDS[row[0]]: row[1]}
else:
return {}
def _read_row(self, row):
date_s = row.get(self.DATE_FIELD)
if not date_s:
return None
if all(row[key] == self.NOT_SHARED for key in self.NAME_FIELDS):
payee = 'Anonymous'
else:
payee = ' '.join(row[key] for key in self.NAME_FIELDS)
retval = {
'date': strparse.date(date_s, self.DATE_FMT),
'payee': payee,
}
retval.update((retkey, strparse.currency_decimal(row[rowkey]))
for rowkey, retkey in self.DECIMAL_FIELDS.items())
retval['amount'] = retval['donation_amount'] + retval['match_amount']
retval.setdefault('net_amount', retval['amount'] - sum(
fee for key, fee in retval.items() if key.endswith('_fee')
))
return retval
class Donations2018Importer(_DonationsImporterBase):
ENTRY_SEED = {
'ledger template': 'benevity donations ledger entry',
'donation_fee': ZERO_DECIMAL,
'match_fee': ZERO_DECIMAL,
'merchant_fee': ZERO_DECIMAL,
'fee_comment': None,
}
DATE_FIELD = 'Date of Donation'
DATE_FMT = '%Y-%m-%d'
HEADER_FIELDS = {
'Currency': 'currency',
'Disbursement ID': 'disbursement_id',
}
DECIMAL_FIELDS = {
'Donation Amount': 'donation_amount',
'Matched Amount': 'match_amount',
'Total': 'net_amount',
}
NEEDED_FIELDS = frozenset([
DATE_FIELD,
*_DonationsImporterBase.NAME_FIELDS,
*DECIMAL_FIELDS,
])
COPIED_FIELDS = {
'Participating Corporation': 'corporation',
'Project': 'project',
'Comment': 'comment',
'Transaction ID': 'transaction_id',
'Donation Frequency': 'frequency',
}
class Donations2019Importer(_DonationsImporterBase):
ENTRY_SEED = {
'ledger template': 'benevity donations ledger entry',
'fee_comment': None,
}
DATE_FIELD = 'Donation Date'
DATE_FMT = '%Y-%m-%dT%H:%M:%SZ'
HEADER_FIELDS = {
'Disbursement ID': 'disbursement_id',
}
DECIMAL_FIELDS = {
'Donation Amount': 'donation_amount',
'Donation Fee': 'donation_fee',
'Match Amount': 'match_amount',
'Match Fee': 'match_fee',
'Merchant Fee': 'merchant_fee',
'Net total': 'net_amount',
}
NEEDED_FIELDS = frozenset([
DATE_FIELD,
*_DonationsImporterBase.NAME_FIELDS,
*DECIMAL_FIELDS,
])
COPIED_FIELDS = {
'Company': 'corporation',
'Currency': 'currency',
'Project': 'project',
'Comment': 'comment',
'Transaction ID': 'transaction_id',
'Donation Frequency': 'frequency',
}
class Donations2020Importer(_DonationsImporterBase):
ENTRY_SEED = {
'ledger template': 'benevity donations ledger entry',
'match_fee': ZERO_DECIMAL,
}
DATE_FIELD = 'Donation Date'
DATE_FMT = '%Y-%m-%dT%H:%M:%SZ'
HEADER_FIELDS = {
'Disbursement ID': 'disbursement_id',
}
DECIMAL_FIELDS = {
'Total Donation to be Acknowledged': 'donation_amount',
'Cause Support Fee': 'donation_fee',
'Match Amount': 'match_amount',
'Merchant Fee': 'merchant_fee',
}
NEEDED_FIELDS = frozenset([
DATE_FIELD,
*_DonationsImporterBase.NAME_FIELDS,
*DECIMAL_FIELDS,
])
COPIED_FIELDS = {
'Company': 'corporation',
'Currency': 'currency',
'Project': 'project',
'Comment': 'comment',
'Transaction ID': 'transaction_id',
'Donation Frequency': 'frequency',
'Fee Comment': 'fee_comment',
}
|