-
Notifications
You must be signed in to change notification settings - Fork 4
Payment processing and CI improvements #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
olf42
wants to merge
19
commits into
develop
Choose a base branch
from
feature/payment_processing
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
90adc4c
added peewee to requirements
olf42 4553b8d
fix Makefile docs command
olf42 f1eff5e
update requirements and add pytest-datadir
olf42 d8b8e8e
add import-bank-statement command
olf42 4bf919d
fix gh actions
olf42 26a91ed
flake8 docs/conf.py
olf42 1d12bd3
flake8 files
olf42 9a606fd
change black-check to style-check
olf42 5941ff3
refactor get_settings_from_file
olf42 1e21a4f
black docs/conf.py
olf42 aaae32c
also run black on the docs dir
olf42 8853cdd
draft of the report command
olf42 a554a5a
flake and black payment and cli
olf42 790d7c1
don't use pytest-datadir
olf42 4de135f
fix gh action to run make style-check
olf42 103d777
add flake8 as dev requirement
olf42 7ee37d1
add print for debugging to failing import test
olf42 09d1f69
add circleci
olf42 4daa49f
Merge branch 'develop' into feature/payment_processing
olf42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,148 @@ | ||
| #!/usr/bin/env python3 | ||
| import arrow | ||
| import csv | ||
| import locale | ||
| import yaml | ||
|
|
||
| from dataclasses import dataclass, asdict | ||
| from decimal import Decimal | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| @dataclass | ||
| class Entry: | ||
| date: arrow.Arrow | ||
| subject: str | ||
| amount: Decimal | ||
| sender: str | ||
| receiver: str | ||
| cid: str = "" | ||
|
|
||
|
|
||
| class PostBankCsvParser: | ||
|
|
||
| expected_header = [ | ||
| "Buchungsdatum", | ||
| "Wertstellung", | ||
| "Umsatzart", | ||
| "Buchungsdetails", | ||
| "Auftraggeber", | ||
| "Empfänger", | ||
| "Betrag (\x80)", | ||
| "Saldo (\x80)", | ||
| ] | ||
|
|
||
| def __init__(self, q): | ||
| self.q = q | ||
|
|
||
| def header_ok(self, header): | ||
| if header != self.expected_header: | ||
| return False | ||
| return True | ||
|
|
||
| def amount_to_decimal(self, value): | ||
| value = value.replace("\x80", "") | ||
| return Decimal.from_float(locale.atof(value)).quantize(Decimal(self.q)) | ||
|
|
||
| def sanitize_subject(self, subject): | ||
| return subject.replace("Referenz NOTPROVIDED", "").replace( | ||
| "Verwendungszweck", "" | ||
| ) | ||
|
|
||
| def get_entry(self, row): | ||
| return Entry( | ||
| date=arrow.get(row[0], "DD.MM.YYYY"), | ||
| sender=row[4], | ||
| receiver=row[5], | ||
| subject=self.sanitize_subject(row[3]), | ||
| amount=self.amount_to_decimal(row[6]), | ||
| ) | ||
|
|
||
|
|
||
| class CsvHeaderError(Exception): | ||
| pass | ||
|
|
||
|
|
||
| class Entries: | ||
| def __init__(self, entries=None): | ||
| if not entries: | ||
| self.entries = [] | ||
| else: | ||
| self.entries = entries | ||
|
|
||
| @classmethod | ||
| def from_csv(cls, csv_file, parser): | ||
| entries = [] | ||
| with open(csv_file, newline="") as csvfile: | ||
| reader = csv.reader(csvfile, delimiter=";", quotechar='"') | ||
| for r, row in enumerate(reader): | ||
| if r == 0: | ||
| if parser.header_ok(row): | ||
| continue | ||
| else: | ||
| raise CsvHeaderError( | ||
| f"expected {parser.expected_header} but got {row}" | ||
| ) | ||
| entries.append(parser.get_entry(row)) | ||
| return cls(entries) | ||
|
|
||
| @classmethod | ||
| def from_yaml(cls, yaml_files): | ||
| if isinstance(yaml_files, Path): | ||
| yaml_files = [yaml_files] | ||
|
|
||
| entries = [] | ||
| for yaml_file in yaml_files: | ||
| with open(yaml_file) as infile: | ||
| data = yaml.load(infile, Loader=yaml.FullLoader) | ||
| for entry in data: | ||
| entries.append(Entry(**entry)) | ||
| return cls(entries) | ||
|
|
||
| def to_yaml(self): | ||
| return yaml.dump(list(map(asdict, self.entries))) | ||
|
|
||
|
|
||
| def import_bank_statement_from_file(bank_statement_file, settings): | ||
| locale.setlocale(locale.LC_ALL, "de_DE.UTF-8") | ||
| e = Entries.from_csv( | ||
| bank_statement_file, PostBankCsvParser(settings.decimal_quantization) | ||
| ) | ||
| outfilename = Path(settings.payments_dir).joinpath( | ||
| f"{arrow.now().isoformat()}.yaml" | ||
| ) | ||
| with open(outfilename, "w") as outfile: | ||
| outfile.write(e.to_yaml()) | ||
| return outfilename | ||
|
|
||
|
|
||
| def report_customer(cid, settings): | ||
| payments = Entries.from_yaml(settings.payments_dir.iterdir()) | ||
| invoices = get_invoices(cid, settings) | ||
| total = Decimal(0) | ||
|
|
||
| print("\nInvoices:") | ||
| for i in invoices: | ||
| print(f"{i['date']}\t-{i['total_gross']}") | ||
| total -= Decimal(i["total_gross"]) | ||
|
|
||
| print("\nPayments:") | ||
| for p in payments.entries: | ||
| if p.cid != cid: | ||
| continue | ||
| print(f"{p.date.format('DD.MM.YYYY')}\t{p.amount}") | ||
| total += p.amount | ||
|
|
||
| total = total.quantize(Decimal(settings.decimal_quantization)) | ||
| print("-".join(["" for i in range(20)])) | ||
| print(f"Balance: {total}") | ||
| print("=".join(["" for i in range(20)])) | ||
|
|
||
|
|
||
| def get_invoices(cid, settings): | ||
| invoices_dir = settings.invoices_dir / cid | ||
| invoices = [] | ||
| for invoice in invoices_dir.glob("*.yaml"): | ||
| with open(invoice) as i_file: | ||
| invoices.append(yaml.safe_load(i_file)) | ||
| return invoices |
This file contains hidden or 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 hidden or 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 @@ | ||
| fixtures |
This file contains hidden or 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,5 @@ | ||
| Buchungsdatum;Wertstellung;Umsatzart;Buchungsdetails;Auftraggeber;Empfänger;Betrag ();Saldo () | ||
| 18.10.2019;18.10.2019;Gutschrift;Referenz NOTPROVIDED1000.2019.10;Martha Muster;Westnetz w.V.;60,21 ;3.381,43 | ||
| 17.10.2019;16.10.2019;Gutschrift;Referenz NOTPROVIDEDKunde 1002;Klaus Karstens;Westnetz w.V.;48,45 ;3.321,22 | ||
| 16.10.2019;16.10.2019;Dauergutschrift;Referenz NOTPROVIDEDVerwendungszweckFrank Nord Internet;Karina Surcu;Westnetz w.V.;96,9 ;3.272,77 | ||
| 15.10.2019;15.10.2019;Gutschrift;Referenz NOTPROVIDED1000.2019.10;Martha Muster;Westnetz w.V.;60,21 ;3.175,87 |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.