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', }