|
| 1 | +from decimal import Decimal |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | + |
| 6 | +from .Institution import Institution |
| 7 | +from .MappingDatabase import MappingDatabase |
| 8 | +from .read_config import read_config |
| 9 | +from .as_transaction import as_transaction |
| 10 | +from .get_value import get_value |
| 11 | +from .get_beancount_config import get_beancount_config |
| 12 | + |
| 13 | + |
| 14 | +class ChaseSPCard(Institution): |
| 15 | + NAME = "chase_sp_card" # used in cli.py and in tests |
| 16 | + |
| 17 | + def __init__(self, config_file: str): |
| 18 | + # params |
| 19 | + self.config_file = config_file |
| 20 | + # attributes |
| 21 | + self.config = read_config(config_file) |
| 22 | + self.beancount_config = get_beancount_config(self.config) |
| 23 | + # Use basedir of config_file to read mapping database files |
| 24 | + base_dir = Path(config_file).parent |
| 25 | + debit_file = get_value(self.config, "database", "debit_mapping") |
| 26 | + self.debit_file = str(base_dir / debit_file) |
| 27 | + self.debit_db = MappingDatabase(self.debit_file) |
| 28 | + |
| 29 | + def read_transaction(self, file_name: str) -> pd.DataFrame: |
| 30 | + """Read financial transactions into a Pandas DataFrame. |
| 31 | +
|
| 32 | + Parameters |
| 33 | + ---------- |
| 34 | + file_name : str |
| 35 | + Input file name. |
| 36 | +
|
| 37 | + Returns |
| 38 | + ------- |
| 39 | + pd.DataFrame |
| 40 | + A dataframe after pre-processing. |
| 41 | + """ |
| 42 | + converters = { |
| 43 | + "Transaction Date": pd.to_datetime, |
| 44 | + "Post Date": pd.to_datetime, |
| 45 | + "Description": str, |
| 46 | + "Category": str, |
| 47 | + "Type": str, |
| 48 | + "Amount": str, |
| 49 | + "Memo": str, |
| 50 | + } |
| 51 | + df = pd.read_csv(file_name, converters=converters) |
| 52 | + print(f"Found {len(df.index)} transactions in {file_name}") |
| 53 | + |
| 54 | + # Transaction Date,Post Date,Description,Category,Type,Amount,Memo |
| 55 | + # Lowercase names will be keyword arguments later. |
| 56 | + column_names = { |
| 57 | + "Transaction Date": "date", |
| 58 | + "Amount": "amount", |
| 59 | + "Description": "memo", # note this is all lower case |
| 60 | + } |
| 61 | + df.rename(columns=column_names, inplace=True) |
| 62 | + |
| 63 | + df["amount"] = df["amount"].apply(Decimal) |
| 64 | + # Drop positive amounts as they are credit card payments |
| 65 | + df.drop(df.loc[df["amount"] > 0].index, inplace=True) |
| 66 | + # Reverse sign as all transactions are now spending. |
| 67 | + df["amount"] = -df["amount"] |
| 68 | + |
| 69 | + # Reverse row order because the oldest transaction is on the bottom |
| 70 | + # Note: the index column is also reversed |
| 71 | + df = df[::-1] |
| 72 | + |
| 73 | + # print(df.dtypes) # debug |
| 74 | + # print(df) # debug |
| 75 | + return df |
| 76 | + |
| 77 | + def write_bean(self, df: pd.DataFrame, file_name: str) -> None: |
| 78 | + """Write Beancount transactions to file |
| 79 | +
|
| 80 | + Parameters |
| 81 | + ---------- |
| 82 | + df : pd.DataFrame |
| 83 | + Transaction dataframe. |
| 84 | + file_name : str |
| 85 | + Output file name. |
| 86 | +
|
| 87 | + Returns |
| 88 | + ------- |
| 89 | + None |
| 90 | + """ |
| 91 | + try: |
| 92 | + with open(file_name, "w", encoding="utf-8") as f: |
| 93 | + for row in df.index: |
| 94 | + date = df["date"][row] |
| 95 | + amount = df["amount"][row] |
| 96 | + memo = df["memo"][row] |
| 97 | + metadata = { |
| 98 | + "memo": memo, |
| 99 | + } |
| 100 | + |
| 101 | + accounts = self.debit_db.match(memo) |
| 102 | + |
| 103 | + account_metadata = {} |
| 104 | + for x in range(1, len(accounts)): |
| 105 | + account_metadata[f"match{x+1}"] = str(accounts[x]) |
| 106 | + |
| 107 | + output = as_transaction( |
| 108 | + date=date, |
| 109 | + amount=amount, |
| 110 | + metadata=metadata, |
| 111 | + account_metadata=account_metadata, |
| 112 | + **accounts[0], |
| 113 | + **self.beancount_config, |
| 114 | + ) |
| 115 | + # print(output) # debug |
| 116 | + f.write(output) |
| 117 | + print(f"Written {file_name}") |
| 118 | + except IOError as e: |
| 119 | + print(f"Error encountered while writing to: {file_name}") |
| 120 | + print(e) |
| 121 | + |
| 122 | + def convert(self, csv_file: str, bean_file: str): |
| 123 | + """Convert transactions in a CSV file to a Beancount file |
| 124 | +
|
| 125 | + Parameters |
| 126 | + ---------- |
| 127 | + csv_file : str |
| 128 | + Input CSV file name. |
| 129 | +
|
| 130 | + bean_file : str |
| 131 | + Output Beancount file name. |
| 132 | +
|
| 133 | + Returns |
| 134 | + ------- |
| 135 | + None |
| 136 | + """ |
| 137 | + df = self.read_transaction(csv_file) |
| 138 | + self.write_bean(df, bean_file) |
0 commit comments