Skip to content

Commit

Permalink
refactor(statement): treat credit card transactions as debit entry
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Dornel authored and benjamin-awd committed Nov 22, 2023
1 parent dd0712b commit 79685a7
Show file tree
Hide file tree
Showing 17 changed files with 678 additions and 636 deletions.
2 changes: 1 addition & 1 deletion src/monopoly/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _perform_safety_check(self, statement: Statement) -> bool:
for page in self.document:
lines = page.get_textpage().extractText().split("\n")
decimal_numbers.update(statement.get_decimal_numbers(lines))
total_amount = round(statement.df[StatementFields.AMOUNT].sum(), 2)
total_amount = abs(round(statement.df[StatementFields.AMOUNT].sum(), 2))

result = total_amount in decimal_numbers
if not result:
Expand Down
25 changes: 19 additions & 6 deletions src/monopoly/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import re
from datetime import datetime
from functools import cached_property
from typing import Optional
from typing import Any, Optional

from pandas import DataFrame
from pydantic import field_validator, model_validator
from pydantic.dataclasses import dataclass
from pydantic_core import ArgsKwargs

from monopoly.config import StatementConfig, TransactionConfig
from monopoly.constants import StatementFields
Expand All @@ -15,6 +16,7 @@
logger = logging.getLogger(__name__)


# pylint: disable=bad-classmethod-argument
@dataclass
class Transaction:
"""
Expand Down Expand Up @@ -44,18 +46,30 @@ def prepare_amount_for_float_coercion(cls, amount: str) -> str:
(-1.56 ) -> -1.56
"""
if isinstance(amount, str):
# change cashback to negative transaction
if amount.startswith("(") and amount.endswith(")"):
amount = "-" + amount
return re.sub(r"[,)(\s]", "", amount)
return amount

@model_validator(mode="before") # type: ignore
def treat_parenthesis_enclosure_as_credit(self: ArgsKwargs | Any) -> "ArgsKwargs":
"""
Treat amounts enclosed by parentheses (e.g. cashback) as a credit entry
"""
if self.kwargs:
amount: str = self.kwargs["amount"]
if isinstance(amount, str):
if amount.startswith("(") and amount.endswith(")"):
self.kwargs["suffix"] = "CR"
return self

@model_validator(mode="after")
def convert_credit_amount_to_negative(self) -> "Transaction":
def convert_credit_amount_to_negative(self: "Transaction") -> "Transaction":
"""
Converts transactions with a suffix of "CR" to negative
"""
if self.suffix == "CR":
self.amount = abs(self.amount)

else:
self.amount = -abs(self.amount)
return self

Expand All @@ -77,7 +91,6 @@ class Statement:
@cached_property
def transactions(self) -> list[Transaction]:
transactions: list[Transaction] = []

for page in self.pages:
lines = self.process_lines(page)
for i, line in enumerate(lines):
Expand Down
172 changes: 86 additions & 86 deletions tests/integration/banks/citibank/raw.csv
Original file line number Diff line number Diff line change
@@ -1,87 +1,87 @@
transaction_date,description,amount
17 OCT,BALANCE PREVIOUS STATEMENT,1089.65
17 OCT,FAST INCOMING PAYMENT,-1089.65
12 OCT,Sandwich Shop @ Goldview SINGAPORE SG,2.36
14 OCT,Public Transport 191582785 SINGAPORE SG,11.80
14 OCT,Grocery Store-Parkside S SINGAPORE SG,6.45
14 OCT,Cafeteria S Singapore SG,16.40
15 OCT,Bagel House SINGAPORE SG,34.24
15 OCT,Grocery Store-Meadowside S SINGAPORE SG,14.90
15 OCT,Great Toast SINGAPORE SG,6.00
16 OCT,Tea Shop Amsterdam NL,4.59
16 OCT,Great Toast SINGAPORE SG,2.20
16 OCT,CAFE DELIGHT - GS SINGAPORE SG,9.80
16 OCT,GROCER MARKET-PARKWAY S SINGAPORE SG,22.30
17 OCT,COFFEE HOUSE@BMLB(MB) SINGAPORE SG,7.20
17 OCT,ITALIAN CUISINE S Singapore SG,16.90
17 OCT,PUBLIC TRANSIT 197215138 SINGAPORE SG,13.09
18 OCT,SUNSET HOTEL DINER SINGAPORE SG,61.20
19 OCT,TOASTY DELIGHT CAFÉ SINGAPORE SG,3.10
19 OCT,GOURMET TREATS S Singapore SG,16.40
19 OCT,ONLINE SHOPPING MALL SINGAPORE SG,37.49
20 OCT,TOASTY DELIGHT CAFÉ SINGAPORE SG,3.10
21 OCT,ID:1234567890 CITI SMS CASH REBATE,-10.0
21 OCT,FROZEN FOODS EXPRESS SINGAPORE SG,12.80
21 OCT,GOURMET TREATS S Singapore SG,16.90
22 OCT,SUSHI SENSE SINGAPORE SG,46.37
22 OCT,BURGER KING & GRILL S SINGAPORE SG,10.00
22 OCT,BURGER KING & GRILL S SINGAPORE SG,8.00
22 OCT,FASHION HOUSE SINGAPORE PTE SINGAPORE SG,29.80
22 OCT,PUBLIC TRANSIT 9876543210 SINGAPORE SG,5.02
23 OCT,FAVEPAYFavePay - Foodie Express Singapore SG,13.50
23 OCT,TOASTY DELIGHT CAFÉ SINGAPORE SG,13.20
24 OCT,GOURMET TREATS S Singapore SG,16.40
24 OCT,DEPARTMENTAL STORE SINGAPORE SG,10.80
25 OCT,FAVEPAYFavePay - Sushi Delights Singapore SG,8.55
25 OCT,TOASTY DELIGHT CAFÉ SINGAPORE SG,2.20
25 OCT,CHILLED GOODS STORE SINGAPORE SG,5.34
25 OCT,NET*DELICIOUS EATS @ URBAN V SINGAPORE SG,11.80
26 OCT,SILVER GYM SINGAPORE SG,210.00
27 OCT,FOOD BAKERY SINGAPORE SG,3.40
27 OCT,BUS/MRT 12345678 SINGAPORE SG,13.24
28 OCT,AWESOME TOAST SINGAPORE SG,3.10
28 OCT,DELUXE MART SINGAPORE SG,16.45
28 OCT,E-MART EXPRESS SINGAPORE SG,69.92
28 OCT,TASTY BITES & CO. Pte. Ltd Singapore SG,21.78
28 OCT,URBAN REFRESHMENTS SINGAPORE SG,9.80
29 OCT,CULINARY DELIGHTS S Singapore SG,18.04
29 OCT,TOASTY CAFE SINGAPORE SG,2.90
29 OCT,Crave & Co. SINGAPORE SG,127.70
31 OCT,FAVEPAYFavePay - Don K Singapore SG,13.49
31 OCT,WWW.PIZZAEXPRESS.SG SINGAPORE SG,18.83
31 OCT,COLD STORAGE SINGAPORE SG,4.60
01 NOV,ID:1234567890 CITI SMS CASH REBATE,-10.0
01 NOV,TEA PURCHASE SEATTLE DE,12.10
02 NOV,TOASTY CAFE SINGAPORE SG,5.80
02 NOV,TOASTY CAFE SINGAPORE SG,3.10
02 NOV,TOASTY CAFE SINGAPORE SG,5.40
02 NOV,Northern Garden Pub SINGAPORE SG,6.50
02 NOV,Urban Drive Shop Singapore SG,1.00
02 NOV,BUS/MRT 1234567 SINGAPORE SG,9.47
03 NOV,TOASTY CAFE SINGAPORE SG,3.10
03 NOV,URBAN EATS @ GOLDEN GATE SINGAPORE SG,11.80
03 NOV,Delicious Delights S Singapore SG,16.40
03 NOV,Tasty Bites SINGAPORE SG,16.90
04 NOV,Currency Exchange Fee SGD 12.10,16.39
04 NOV,Flavors Galore S Singapore SG,0.12
05 NOV,Online Shopper's Paradise SINGAPORE SG,14.90
05 NOV,FAVEPAYFavePay - Gourmet Singapore SG,21.74
06 NOV,VIRTUAL MARKETPLACE SINGAPORE SG,13.50
06 NOV,Delicious Delights S Singapore SG,22.00
07 NOV,Quick Mart - CORNER STREET SINGAPORE SG,6.50
07 NOV,SINGAPORE FOODIE HUB SINGAPORE SG,16.90
07 NOV,Savory Bites S Singapore SG,37.49
08 NOV,Currency Exchange SGD Fee 12.10,3.10
09 NOV,Gourmet Haven S Singapore SG,16.40
09 NOV,Cyber Shopping Zone SINGAPORE SG,2.90
09 NOV,CCY CONVERSION FEE SGD 22.00,0.22
10 NOV,YUMMY EATS EXPRESS SINGAPORE SG,10.40
10 NOV,HEALTHY HARVEST CAFÉ SINGAPORE SG,9.80
11 NOV,DELIGHTFUL MORNING BAKES SINGAPORE SG,2.20
12 NOV,FREEZER FRESH MARKET SINGAPORE SG,6.50
12 NOV,SWIFT AUTO MART SINGAPORE SG,5.50
12 NOV,ARTISAN'S GOURMET STOP SINGAPORE SG,62.90
13 NOV,GREAT TOAST SINGAPORE SG,3.10
14 NOV,Ice Cream Shop SINGAPORE SG,16.39
14 NOV,John's Gastronomia Singapore SG,16.90
14 NOV,GREAT TOAST SINGAPORE SG,3.20
17 OCT,BALANCE PREVIOUS STATEMENT,-1089.65
17 OCT,FAST INCOMING PAYMENT,1089.65
12 OCT,Sandwich Shop @ Goldview SINGAPORE SG,-2.36
14 OCT,Public Transport 191582785 SINGAPORE SG,-11.8
14 OCT,Grocery Store-Parkside S SINGAPORE SG,-6.45
14 OCT,Cafeteria S Singapore SG,-16.4
15 OCT,Bagel House SINGAPORE SG,-34.24
15 OCT,Grocery Store-Meadowside S SINGAPORE SG,-14.9
15 OCT,Great Toast SINGAPORE SG,-6.0
16 OCT,Tea Shop Amsterdam NL,-4.59
16 OCT,Great Toast SINGAPORE SG,-2.2
16 OCT,CAFE DELIGHT - GS SINGAPORE SG,-9.8
16 OCT,GROCER MARKET-PARKWAY S SINGAPORE SG,-22.3
17 OCT,COFFEE HOUSE@BMLB(MB) SINGAPORE SG,-7.2
17 OCT,ITALIAN CUISINE S Singapore SG,-16.9
17 OCT,PUBLIC TRANSIT 197215138 SINGAPORE SG,-13.09
18 OCT,SUNSET HOTEL DINER SINGAPORE SG,-61.2
19 OCT,TOASTY DELIGHT CAFÉ SINGAPORE SG,-3.1
19 OCT,GOURMET TREATS S Singapore SG,-16.4
19 OCT,ONLINE SHOPPING MALL SINGAPORE SG,-37.49
20 OCT,TOASTY DELIGHT CAFÉ SINGAPORE SG,-3.1
21 OCT,ID:1234567890 CITI SMS CASH REBATE,10.0
21 OCT,FROZEN FOODS EXPRESS SINGAPORE SG,-12.8
21 OCT,GOURMET TREATS S Singapore SG,-16.9
22 OCT,SUSHI SENSE SINGAPORE SG,-46.37
22 OCT,BURGER KING & GRILL S SINGAPORE SG,-10.0
22 OCT,BURGER KING & GRILL S SINGAPORE SG,-8.0
22 OCT,FASHION HOUSE SINGAPORE PTE SINGAPORE SG,-29.8
22 OCT,PUBLIC TRANSIT 9876543210 SINGAPORE SG,-5.02
23 OCT,FAVEPAYFavePay - Foodie Express Singapore SG,-13.5
23 OCT,TOASTY DELIGHT CAFÉ SINGAPORE SG,-13.2
24 OCT,GOURMET TREATS S Singapore SG,-16.4
24 OCT,DEPARTMENTAL STORE SINGAPORE SG,-10.8
25 OCT,FAVEPAYFavePay - Sushi Delights Singapore SG,-8.55
25 OCT,TOASTY DELIGHT CAFÉ SINGAPORE SG,-2.2
25 OCT,CHILLED GOODS STORE SINGAPORE SG,-5.34
25 OCT,NET*DELICIOUS EATS @ URBAN V SINGAPORE SG,-11.8
26 OCT,SILVER GYM SINGAPORE SG,-210.0
27 OCT,FOOD BAKERY SINGAPORE SG,-3.4
27 OCT,BUS/MRT 12345678 SINGAPORE SG,-13.24
28 OCT,AWESOME TOAST SINGAPORE SG,-3.1
28 OCT,DELUXE MART SINGAPORE SG,-16.45
28 OCT,E-MART EXPRESS SINGAPORE SG,-69.92
28 OCT,TASTY BITES & CO. Pte. Ltd Singapore SG,-21.78
28 OCT,URBAN REFRESHMENTS SINGAPORE SG,-9.8
29 OCT,CULINARY DELIGHTS S Singapore SG,-18.04
29 OCT,TOASTY CAFE SINGAPORE SG,-2.9
29 OCT,Crave & Co. SINGAPORE SG,-127.7
31 OCT,FAVEPAYFavePay - Don K Singapore SG,-13.49
31 OCT,WWW.PIZZAEXPRESS.SG SINGAPORE SG,-18.83
31 OCT,COLD STORAGE SINGAPORE SG,-4.6
01 NOV,ID:1234567890 CITI SMS CASH REBATE,10.0
01 NOV,TEA PURCHASE SEATTLE DE,-12.1
02 NOV,TOASTY CAFE SINGAPORE SG,-5.8
02 NOV,TOASTY CAFE SINGAPORE SG,-3.1
02 NOV,TOASTY CAFE SINGAPORE SG,-5.4
02 NOV,Northern Garden Pub SINGAPORE SG,-6.5
02 NOV,Urban Drive Shop Singapore SG,-1.0
02 NOV,BUS/MRT 1234567 SINGAPORE SG,-9.47
03 NOV,TOASTY CAFE SINGAPORE SG,-3.1
03 NOV,URBAN EATS @ GOLDEN GATE SINGAPORE SG,-11.8
03 NOV,Delicious Delights S Singapore SG,-16.4
03 NOV,Tasty Bites SINGAPORE SG,-16.9
04 NOV,Currency Exchange Fee SGD 12.10,-16.39
04 NOV,Flavors Galore S Singapore SG,-0.12
05 NOV,Online Shopper's Paradise SINGAPORE SG,-14.9
05 NOV,FAVEPAYFavePay - Gourmet Singapore SG,-21.74
06 NOV,VIRTUAL MARKETPLACE SINGAPORE SG,-13.5
06 NOV,Delicious Delights S Singapore SG,-22.0
07 NOV,Quick Mart - CORNER STREET SINGAPORE SG,-6.5
07 NOV,SINGAPORE FOODIE HUB SINGAPORE SG,-16.9
07 NOV,Savory Bites S Singapore SG,-37.49
08 NOV,Currency Exchange SGD Fee 12.10,-3.1
09 NOV,Gourmet Haven S Singapore SG,-16.4
09 NOV,Cyber Shopping Zone SINGAPORE SG,-2.9
09 NOV,CCY CONVERSION FEE SGD 22.00,-0.22
10 NOV,YUMMY EATS EXPRESS SINGAPORE SG,-10.4
10 NOV,HEALTHY HARVEST CAFÉ SINGAPORE SG,-9.8
11 NOV,DELIGHTFUL MORNING BAKES SINGAPORE SG,-2.2
12 NOV,FREEZER FRESH MARKET SINGAPORE SG,-6.5
12 NOV,SWIFT AUTO MART SINGAPORE SG,-5.5
12 NOV,ARTISAN'S GOURMET STOP SINGAPORE SG,-62.9
13 NOV,GREAT TOAST SINGAPORE SG,-3.1
14 NOV,Ice Cream Shop SINGAPORE SG,-16.39
14 NOV,John's Gastronomia Singapore SG,-16.9
14 NOV,GREAT TOAST SINGAPORE SG,-3.2
Loading

0 comments on commit 79685a7

Please sign in to comment.