This repository has been archived by the owner on Sep 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate payments, orders & infos to classes
* Create `BaseClass` class + general `Payments` class * Integrate new `Paypal` class * Add classes `Orders` & `Infos` * Add invoice helpers for extracting IDs & dates * Move CSV-related options to class props * Harmonize `invoice2number` nomenclature
- Loading branch information
Showing
7 changed files
with
316 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import json | ||
|
||
from abc import ABCMeta, abstractmethod | ||
from hashlib import md5 | ||
|
||
from pandas import concat, read_csv | ||
|
||
|
||
class BaseClass(metaclass=ABCMeta): | ||
# Props | ||
data = None | ||
identifier = None | ||
|
||
# CSV options | ||
encoding='iso-8859-1' | ||
delimiter=';' | ||
skiprows=None | ||
|
||
|
||
def load_csv(self, csv_files) -> None: | ||
try: | ||
df = concat(map(lambda file: read_csv( | ||
file, | ||
sep=self.delimiter, | ||
encoding=self.encoding, | ||
low_memory=False, | ||
skiprows=self.skiprows | ||
), csv_files)) | ||
|
||
except ValueError: | ||
return [] | ||
|
||
self.load_data(self.process_data(df.to_dict('records'))) | ||
|
||
|
||
def load_json(self, json_files) -> None: | ||
data = [] | ||
|
||
for json_file in json_files: | ||
try: | ||
with open(json_file, 'r') as file: | ||
data.extend(json.load(file)) | ||
|
||
except json.decoder.JSONDecodeError: | ||
raise Exception | ||
|
||
except FileNotFoundError: | ||
pass | ||
|
||
self.load_data(data) | ||
|
||
|
||
def load_data(self, data: list) -> None: | ||
if self.data: | ||
# Permit only unique entries, either by .. | ||
if self.identifier is not None: | ||
# .. (1) using a unique identifier | ||
codes = {item[self.identifier] for item in self.data} | ||
|
||
# Merge only data not already in database | ||
for item in data: | ||
if item[self.identifier] not in codes: | ||
codes.add(item[self.identifier]) | ||
self.data.append(item) | ||
|
||
else: | ||
# (2) .. hashing the whole item | ||
codes = set() | ||
|
||
for item in data: | ||
hash_digest = md5(str(item).encode('utf-8')).hexdigest() | ||
|
||
if hash_digest not in codes: | ||
codes.add(hash_digest) | ||
self.data.append(item) | ||
|
||
# .. otherwise, start from scratch | ||
else: | ||
self.data = data | ||
|
||
|
||
@abstractmethod | ||
def process_data(self, data: list) -> list: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from abc import abstractmethod | ||
from operator import itemgetter | ||
|
||
from .base import BaseClass | ||
|
||
class Payments(BaseClass): | ||
# Props | ||
_blocked_payments = [] | ||
|
||
|
||
def process_data(self, data: list) -> list: | ||
return self.process_payments(data) | ||
|
||
|
||
@abstractmethod | ||
def process_payments(self, data: list) -> tuple: | ||
pass | ||
|
||
|
||
def payments(self): | ||
# Sort payments by date | ||
return sorted(self.data, key=itemgetter('Datum')) | ||
|
||
|
||
def blocked_payments(self): | ||
return sorted(self.blocked_payments, key=itemgetter('Datum')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,56 @@ | ||
# ~*~ coding=utf-8 ~*~ | ||
|
||
# PAYPAL™ | ||
# This module contains functions for processing 'Aktivitäten' | ||
# This module contains a class for processing & working with | ||
# 'Aktivitäten', as exported from PayPal™ | ||
# See https://www.paypal.com/de/smarthelp/article/FAQ1007 | ||
|
||
from .helpers import convert_number, convert_date | ||
from .payments import Payments | ||
|
||
|
||
# Processes 'Download*.CSV' files | ||
def process_payments(data) -> list: | ||
codes = set() | ||
payments = [] | ||
blocked_payments = [] | ||
class Paypal(Payments): | ||
# Props | ||
identifier = 'Transaktion' | ||
|
||
for item in data: | ||
# Skip withdrawals | ||
if item['Brutto'][:1] == '-': | ||
continue | ||
# CSV options | ||
encoding='utf-8' | ||
delimiter=',' | ||
|
||
# Assign identifier | ||
code = item['Transaktionscode'] | ||
|
||
payment = {} | ||
def process_payments(self, data) -> list: | ||
''' | ||
Processes 'Download*.CSV' files | ||
''' | ||
codes = set() | ||
payments = [] | ||
|
||
payment['ID'] = 'nicht zugeordnet' | ||
payment['Transaktion'] = code | ||
payment['Datum'] = convert_date(item['Datum']) | ||
payment['Vorgang'] = 'nicht zugeordnet' | ||
payment['Name'] = item['Name'] | ||
payment['Email'] = item['Absender E-Mail-Adresse'] | ||
payment['Brutto'] = convert_number(item['Brutto']) | ||
payment['Gebühr'] = convert_number(item['Gebühr']) | ||
payment['Netto'] = convert_number(item['Netto']) | ||
payment['Währung'] = item['Währung'] | ||
for item in data: | ||
# Skip withdrawals | ||
if item['Brutto'][:1] == '-': | ||
continue | ||
|
||
if code not in codes: | ||
codes.add(code) | ||
# Assign identifier | ||
code = item['Transaktionscode'] | ||
|
||
# Sort out regular payments | ||
if item['Typ'] == 'Allgemeine Zahlung': | ||
blocked_payments.append(payment) | ||
continue | ||
payment = {} | ||
|
||
payment['ID'] = 'nicht zugeordnet' | ||
payment['Transaktion'] = code | ||
payment['Datum'] = convert_date(item['Datum']) | ||
payment['Vorgang'] = 'nicht zugeordnet' | ||
payment['Name'] = item['Name'] | ||
payment['Email'] = item['Absender E-Mail-Adresse'] | ||
payment['Brutto'] = convert_number(item['Brutto']) | ||
payment['Gebühr'] = convert_number(item['Gebühr']) | ||
payment['Netto'] = convert_number(item['Netto']) | ||
payment['Währung'] = item['Währung'] | ||
|
||
if code not in codes: | ||
codes.add(code) | ||
|
||
# Sort out regular payments | ||
if item['Typ'] == 'Allgemeine Zahlung': | ||
self._blocked_payments.append(payment) | ||
continue | ||
|
||
payments.append(payment) | ||
payments.append(payment) | ||
|
||
return (payments, blocked_payments) | ||
return payments |
Oops, something went wrong.