Files @ 13c316acf6e1
Branch filter:

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

Brett Smith
tests: More tests for positive results for last commit.
import pathlib
import re

from . import _csv
from .. import strparse

class IncomeImporter(_csv.CSVImporterBase):
    NEEDED_FIELDS = frozenset([
        'FirstName',
        'LastName',
        'Pledge',
        'Status',
    ])
    COPIED_FIELDS = {
        'Pledge': 'amount',
    }
    ENTRY_SEED = {
        'currency': 'USD',
    }

    def __init__(self, input_file):
        super().__init__(input_file)
        match = re.search(r'(?:\b|_)(\d{4}-\d{2}-\d{2})(?:\b|_)',
                          pathlib.Path(input_file.name).name)
        if match:
            self.entry_seed['date'] = strparse.date(match.group(1), '%Y-%m-%d')

    def _read_row(self, row):
        if row['Status'] != 'Processed':
            return None
        else:
            return {
                'payee': '{0[FirstName]} {0[LastName]}'.format(row),
            }


class PayoutImporter(_csv.CSVImporterBase):
    AMOUNT_KEY = 'Total funds deducted from creator balance'
    PLEDGE_KEY = 'Funds used for pledges to other creators'
    TRANSFER_KEY = 'Funds transferred to you'
    NEEDED_FIELDS = frozenset([
        'Month',
        TRANSFER_KEY,
        PLEDGE_KEY,
        AMOUNT_KEY,
    ])
    ENTRY_SEED = {
        'currency': 'USD',
        'payee': 'Patreon',
    }

    def _read_row(self, row):
        amount = strparse.currency_decimal(row[self.AMOUNT_KEY])
        if not amount:
            return None
        else:
            return {
                'amount': amount,
                'date': strparse.date(row['Month'], '%Y-%m'),
                'pledges_amount': strparse.currency_decimal(row[self.PLEDGE_KEY]),
                'transfer_amount': strparse.currency_decimal(row[self.TRANSFER_KEY]),
            }


class FeeImporterBase(_csv.CSVImporterBase):
    ENTRY_SEED = {
        'currency': 'USD',
        'payee': "Patreon",
    }

    def _read_row(self, row):
        return {
            'amount': row[self.AMOUNT_FIELD],
            'date': strparse.date(row['Month'], '%Y-%m'),
        }


class ServiceFeesImporter(FeeImporterBase):
    AMOUNT_FIELD = 'Patreon Fee'
    NEEDED_FIELDS = frozenset(['Month', AMOUNT_FIELD])


class CardFeesImporter(FeeImporterBase):
    AMOUNT_FIELD = 'Processing Fees'
    NEEDED_FIELDS = frozenset(['Month', AMOUNT_FIELD])


class VATImporter(FeeImporterBase):
    AMOUNT_FIELD = 'Vat Charged'
    NEEDED_FIELDS = frozenset(['Month', AMOUNT_FIELD])
    COPIED_FIELDS = {
        'Country Code': 'country_code',
        'Country Name': 'country_name',
    }