Files @ 6e37753fb313
Branch filter:

Location: NPO-Accounting/import2ledger/import2ledger/importers/benevity.py

Brett Smith
ledger_entry: Support NEWLINE tokens.

Necessary to run on Python 3.7.
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_len = len(row)
        if row_len > 2:
            return None
        elif row_len == 2 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']
        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,
    }
    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',
    }
    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',
    }