Files @ 13c316acf6e1
Branch filter:

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

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

from . import _csv
from .. import strparse

class PaymentImporter(_csv.CSVImporterBase):
    NEEDED_FIELDS = frozenset([
        'Converted Currency',
        'Created (UTC)',
        'Fee',
        'Status',
        'Tax',
    ])
    COPIED_FIELDS = {
        'Card Name': 'payee',
        'Converted Amount': 'amount',
        'Customer Email': 'customer_email',
        'Customer ID': 'customer_id',
        'Description': 'description',
        'Transfer': 'payout_id',
        'id': 'payment_id',
    }
    DATE_FMT = '%Y-%m-%d'

    def _read_row(self, row):
        if (row['Status'] != 'Paid') and (row['Status'] != 'Refunded'):
            return None
        else:
            date_s = strparse.slice_words(row['Created (UTC)'], 0, limit=1)
            return {
                'currency': row['Converted Currency'].upper(),
                'date': strparse.date(date_s, self.DATE_FMT),
                'fee': strparse.currency_decimal(row['Fee']),
                'tax': strparse.currency_decimal(row['Tax']),
            }


class PayoutImporter(_csv.CSVImporterBase):
    DECIMAL_FIELDS = {key: key.lower().replace(' ', '_') for key in [
        'Amount',
        'Payment Count',
        'Payment Gross',
        'Payment Fees',
        'Payment Net',
        'Refund Count',
        'Refund Gross',
        'Refund Fees',
        'Refund Net',
        'Collected Fee Count',
        'Collected Fee Gross',
        'Collected Fee Refund Count',
        'Collected Fee Refund Gross',
        'Adjustment Count',
        'Adjustment Gross',
        'Adjustment Fees',
        'Adjustment Net',
        'Validation Count',
        'Validation Fees',
        'Retried Payout Count',
        'Retried Payout Net',
        'Total Count',
        'Total Gross',
        'Total Fees',
        'Total Net',
    ]}
    NEEDED_FIELDS = frozenset([
        'Currency',
        'Created (UTC)',
        'Status',
        *DECIMAL_FIELDS,
    ])
    COPIED_FIELDS = {
        'Balance Transaction': 'balance_txid',
        'Destination': 'destination_id',
        'Failure Balance Transaction': 'failure_txid',
        'id': 'payout_id',
    }
    DATE_FMT = '%Y-%m-%d %H:%M'
    ENTRY_SEED = {'payee': "Stripe"}

    def _read_row(self, row):
        if row['Status'] != 'paid':
            return None
        retval = {
            self.DECIMAL_FIELDS[key]: strparse.currency_decimal(row[key])
            for key in self.DECIMAL_FIELDS
        }
        retval['currency'] = row['Currency'].upper()
        retval['date'] = strparse.date(row['Created (UTC)'], self.DATE_FMT)
        return retval